/usr/share/pyshared/ubuntutools/misc.py is in ubuntu-dev-tools 0.153.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #
# misc.py - misc functions for the Ubuntu Developer Tools scripts.
#
# Copyright (C) 2008, Jonathan Davies <jpds@ubuntu.com>,
# 2008-2009, Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>,
# 2010, Stefano Rivera <stefanor@ubuntu.com>
# 2011, Evan Broder <evan@ebroder.net>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################
# Modules.
import locale
import os
import sys
import distro_info
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
from ubuntutools.subprocess import Popen, PIPE
_system_distribution_chain = []
def system_distribution_chain():
""" system_distribution_chain() -> [string]
Detect the system's distribution as well as all of its parent
distributions and return them as a list of strings, with the
system distribution first (and the greatest grandparent last). If
the distribution chain can't be determined, print an error message
and return an empty list.
"""
global _system_distribution_chain
if len(_system_distribution_chain) == 0:
try:
p = Popen(('dpkg-vendor', '--query', 'Vendor'),
stdout=PIPE)
_system_distribution_chain.append(p.communicate()[0].strip())
except OSError:
print ('Error: Could not determine what distribution you are '
'running.')
return []
while True:
try:
p = Popen(('dpkg-vendor',
'--vendor', _system_distribution_chain[-1],
'--query', 'Parent'),
stdout=PIPE)
parent = p.communicate()[0].strip()
# Don't check return code, because if a vendor has no
# parent, dpkg-vendor returns 1
if not parent:
break
_system_distribution_chain.append(parent)
except Exception:
print ('Error: Could not determine the parent of the '
'distribution %s' % _system_distribution_chain[-1])
return []
return _system_distribution_chain
def system_distribution():
""" system_distro() -> string
Detect the system's distribution and return it as a string. If the
name of the distribution can't be determined, print an error message
and return None.
"""
return system_distribution_chain()[0]
def host_architecture():
""" host_architecture -> string
Detect the host's architecture and return it as a string. If the
architecture can't be determined, print an error message and return None.
"""
arch = Popen(['dpkg', '--print-architecture'], stdout=PIPE, \
stderr=PIPE).communicate()[0].split()
if not arch or 'not found' in arch[0]:
print 'Error: Not running on a Debian based system; could not ' \
'detect its architecture.'
return None
return arch[0]
def readlist(filename, uniq=True):
""" readlist(filename, uniq) -> list
Read a list of words from the indicated file. If 'uniq' is True, filter
out duplicated words.
"""
if not os.path.isfile(filename):
print 'File "%s" does not exist.' % filename
return False
content = open(filename).read().replace('\n', ' ').replace(',', ' ')
if not content.strip():
print 'File "%s" is empty.' % filename
return False
items = [item for item in content.split() if item]
if uniq:
items = list(set(items))
return items
def split_release_pocket(release, default='Release'):
'''Splits the release and pocket name.
If the argument doesn't contain a pocket name then the 'Release' pocket
is assumed.
Returns the release and pocket name.
'''
pocket = default
if release is None:
raise ValueError('No release name specified')
if '-' in release:
(release, pocket) = release.rsplit('-', 1)
pocket = pocket.capitalize()
if pocket not in ('Release', 'Security', 'Updates', 'Proposed',
'Backports'):
raise PocketDoesNotExistError("Pocket '%s' does not exist." % \
pocket)
return (release, pocket)
def require_utf8():
'''Can be called by programs that only function in UTF-8 locales'''
if locale.getpreferredencoding() != 'UTF-8':
print >> sys.stderr, ("This program only functions in a UTF-8 locale. "
"Aborting.")
sys.exit(1)
_vendor_to_distroinfo = {"Debian": distro_info.DebianDistroInfo,
"Ubuntu": distro_info.UbuntuDistroInfo}
def vendor_to_distroinfo(vendor):
""" vendor_to_distroinfo(string) -> DistroInfo class
Convert a string name of a distribution into a DistroInfo subclass
representing that distribution, or None if the distribution is
unknown.
"""
return _vendor_to_distroinfo.get(vendor)
def codename_to_distribution(codename):
""" codename_to_distribution(string) -> string
Finds a given release codename in your distribution's genaology
(i.e. looking at the current distribution and its parents), or
print an error message and return None if it can't be found
"""
for distro in system_distribution_chain() + ["Ubuntu", "Debian"]:
info = vendor_to_distroinfo(distro)
if not info:
continue
if info().valid(codename):
return distro
|