/usr/bin/python-mkdebian is in python-distutils-extra 2.32-2.
This file is owned by root:root, with mode 0o755.
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | #!/usr/bin/python
# Create/update debian packaging from a Python .egg-info
# Copyright (C) 2009 Canonical Ltd.
# Author: Martin Pitt
# License: GPL v2 or later
from optparse import OptionParser
import subprocess, tempfile, shutil, sys, os, textwrap
sys.path.insert(0, '.')
from DistUtilsExtra import __version__ as pkgversion
def get_egg_info():
'''Get egg information from ./setup.py into a dictionary.'''
if not os.path.isfile('setup.py'):
print >> sys.stderr, 'No setup.py in current directory'
sys.exit(1)
egg = {}
instdir = tempfile.mkdtemp()
try:
result = subprocess.call(['python', 'setup.py', 'install_egg_info', '-d', instdir])
if result != 0:
print >> sys.stderr, 'setup.py install_egg_info failed'
sys.exit(1)
ls = os.listdir(instdir)
if len(ls) != 1 or not os.path.isfile(os.path.join(instdir, ls[0])) or \
not ls[0].endswith('.egg-info'):
print >> sys.stderr, './setup.py install_egg_info did not generate an .egg-info file'
sys.exit(1)
for l in open(os.path.join(instdir, ls[0])):
k, v = l.strip().split(': ', 1)
if k in ['Requires', 'Provides']:
# multi-value fields
egg.setdefault(k, []).append(v)
else:
if k in egg:
print >> sys.stderr, 'WARNING: duplicate field %s in .egg-info' % k
egg[k] = v
return egg
finally:
shutil.rmtree(instdir)
def make_debian(egg, force_control, changelog, additional_dependencies,
distribution, use_old_changelog, force_copyright,
force_rules, prefix):
'''Create/update debian/* from information in egg dictionary'''
if not os.path.isdir('debian'):
os.mkdir('debian')
if not os.path.exists('debian/compat'):
f = open('debian/compat', 'w')
f.write('8\n')
f.close()
prefix_string = ''
if prefix:
prefix_string = '''override_dh_auto_install:
dh_auto_install -- --install-scripts=%s \
--install-data=%s \
--install-lib=%s
override_dh_python2:
dh_python2 %s
''' % (prefix, prefix, prefix, prefix)
if not os.path.exists('debian/rules') or force_rules:
f = open('debian/rules', 'w')
f.write('''#!/usr/bin/make -f
%%:
ifneq ($(shell dh -l | grep -xF translations),)
dh $@ --with python2,translations
else
dh $@ --with python2
endif
%s
''' % prefix_string)
f.close()
os.chmod('debian/rules', 0755)
if not os.path.exists('debian/copyright') or force_copyright:
make_copyright(egg)
if force_control not in ('none', 'deps', 'full'):
print >> sys.stderr, 'ERROR: --force-control takes one of the following options:\n' \
'"none", "deps", or "full"'
sys.exit(0)
if force_control != 'none':
make_control(egg, force_control, additional_dependencies)
make_changelog(egg, changelog, distribution, use_old_changelog)
def make_copyright(egg):
author = egg.get('Author', '')
if 'Author-email' in egg:
author += ' <' + egg['Author-email'] + '>'
f = open('debian/copyright', 'w')
f.write('''Format-Specification: http://svn.debian.org/wsvn/dep/web/deps/dep5.mdwn?op=file&rev=135
Name: %s
Maintainer: %s
''' % (egg['Name'], author))
if 'Home-page' in egg:
print >> f, 'Source:', egg['Home-page']
print >> f, '\nFiles: *'
cgrep = subprocess.Popen("find -type f ! -name 'COPYING*' ! -name LICENSE ! -name '*.pyc' ! -path '*/.*' ! -path './debian/*' ! -name '*.pot' | xargs grep -hi '(c)' | sed 's/^.*([cC])/Copyright: (C)/' | sort -u",
shell=True, stdout=subprocess.PIPE)
out = cgrep.communicate()[0]
assert cgrep.returncode == 0
f.write(out)
if 'License' in egg:
print >> f, 'License:', egg['License']
if 'gpl' in egg['License'].lower():
if '2' in egg['License']:
l = 'GPL-2'
elif '3' in egg['License']:
l = 'GPL-3'
else:
l = 'GPL'
print >> f, ''' The full text of the GPL is distributed in
/usr/share/common-licenses/%s on Debian systems.''' % l
def make_control(egg, force_control, additional_dependencies):
author = egg.get('Author', '')
if 'Author-email' in egg:
author += ' <' + egg['Author-email'] + '>'
# calculate dependencies
print 'Searching packages which provide required Python modules:'
deps = set()
for m in egg.get('Requires', []):
print ' ', m, '...',
p = mod2package(m)
if p:
deps.add(p)
print p
else:
print '[not found]'
# calculate extra build dependencies
bdeps = ''
if subprocess.call('find -name "*.ui" | xargs grep -q \'<widget class="Q\'',
shell=True) == 0:
print 'Package uses KDE *.ui files, adding python-kde4-dev build dependency'
bdeps += ',\n python-kde4-dev'
# prepare tags
control_content = {'Depends': '''${misc:Depends},
${python:Depends}%s''' % ',\n '.join(['',] + list(deps) + additional_dependencies)
}
# add additional fields (even when updating if user chooses them)
if not os.path.exists('debian/control') or force_control == 'full':
control_content.update({
'Source': egg['Name'],
'Build-Depends': '''cdbs (>= 0.4.43),
debhelper (>= 6),
python (>= 2.6.6-3~),
python-distutils-extra (>= 2.10)%s''' % bdeps,
'Maintainer': author,
'Package': egg['Name'],
'Description' : egg.get('Summary', '') + '\n '.join(['',] + list(textwrap.wrap(egg.get('Description', ''), 72)))
})
if not os.path.exists('debian/control'):
f = open('debian/control', 'w')
f.write('''Source: %(Source)s
Section: python
Priority: extra
Build-Depends: %(Build-Depends)s
Maintainer: %(Maintainer)s
Standards-Version: 3.8.3
XS-Python-Version: current
Package: %(Package)s
Architecture: all
XB-Python-Version: ${python:Versions}
Depends: %(Depends)s
Description: %(Description)s
''' % control_content)
f.close()
else:
# update debian/control
in_ = open('debian/control')
out = open('debian/control.new', 'w')
skip_until_new_key = False
try:
for line in in_:
# toggle the switch to off if we encounter a new key not overwritten
if not line.startswith(' '):
skip_until_new_key = False
for controlkey in control_content:
if line.startswith(controlkey):
# skip old values and write our own
out.write('%s: %s\n' % (controlkey, control_content[controlkey]))
skip_until_new_key = True
break
# write current line if not in a overwritten section
if not skip_until_new_key:
out.write(line)
out.close()
in_.close()
except:
os.unlink('debian/control.new')
raise
os.rename('debian/control.new', 'debian/control')
def make_changelog(egg, changelog, distribution, use_old_changelog):
command = ['debchange']
if not distribution:
lsb_release = subprocess.Popen(['lsb_release', '-si'],
stdout=subprocess.PIPE)
distro = lsb_release.communicate()[0].strip()
assert lsb_release.returncode == 0
if distro == 'Debian':
release = 'unstable'
else:
lsb_release = subprocess.Popen(['lsb_release', '-sc'],
stdout=subprocess.PIPE)
release = lsb_release.communicate()[0].strip()
assert lsb_release.returncode == 0
else:
release = distribution
# Use --force-distribution only if distribution specified by user
command.append('--force-distribution')
command.append('-D' + release)
if not os.path.exists('/usr/bin/debchange'):
print >> sys.stderr, 'ERROR: Could not find "debchange".\n' \
'You need to install the "devscripts" package for this.'
sys.exit(0)
if not os.path.exists('debian/changelog'):
if not changelog:
changelog = ['Initial release.']
try:
command.extend(['--create', '--package', egg['Name'],
'-v' + egg['Version'], changelog[0]])
assert subprocess.call(command) == 0
except OSError, e:
print >> sys.stderr, 'ERROR: Could not run "debchange": %s\n' \
'You need to install the "devscripts" package for this.' % str(e)
sys.exit(0)
else:
if not changelog:
changelog = use_old_changelog and [''] or ['New release.']
if not use_old_changelog:
command.append('-v' + egg['Version'])
command.append(changelog[0])
if (subprocess.call(command, stderr=subprocess.PIPE) != 0):
print >> sys.stderr, "Can\'t update changelog."
sys.exit(1)
for message in changelog[1:]:
subprocess.call(['debchange', message])
def mod2package(module):
'''Convert Python module into a package name.
This can return None if the package name cannot be determined.
'''
# first attempt: python-<modulename> (common case)
package = 'python-' + module.split('.')[0].lower()
dpkg = subprocess.Popen(['dpkg', '-L', package], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out = dpkg.communicate()[0]
if dpkg.returncode == 0 and \
('/%s/__init__.py\n' % module.replace('.', '/') in out or \
'/%s.py\n' % module.replace('.', '/') in out):
return package
# try for gi modules
if module.startswith('gi.repository.'):
module_name = module.split('.')[-1]
try:
_module = __import__(module, fromlist=[module_name])
except RuntimeError: # thrown by Gtk without $DISPLAY
print '[cannot import, ignoring]',
return None
pkg = _get_pkg_from_path(_module.__path__)
if pkg:
return pkg
# bigger hammer: dpkg -S for Python packages
# module can be module.submodule.subsubmodule packaged independently
# (e. g. python-desktopcouch and python-desktopcouch-records)
module_name = module.split('.')
while(module_name):
pkg = _get_pkg_from_path('*/%s/__init__.py' % '/'.join(module_name))
if pkg:
return pkg
module_name = module_name[:-1]
# bigger hammer: dpkg -S for compiled extensions
dpkg = subprocess.Popen(['dpkg', '-S', '*/%s.so' % module.replace('.', '/')],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = dpkg.communicate()[0]
pkgs = set()
for line in out.splitlines():
p = line.split(':')[0]
# ignore debug extensions
if p.endswith('-dbg'):
continue
pkgs.add(p)
if len(pkgs) == 1:
return list(pkgs)[0]
elif len(pkgs) > 1:
print '[dpkg -S found more than one extension: %s; ignoring]' % ', '.join(pkgs),
return None
def _get_pkg_from_path(path):
dpkg = subprocess.Popen(['dpkg', '-S', path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = dpkg.communicate()[0]
pkgs = set()
for line in out.splitlines():
pkgs.add(line.split(':')[0])
if len(pkgs) == 1:
package = list(pkgs)[0]
return package
elif len(pkgs) > 1:
py_pkgs = [p for p in pkgs if p.startswith('python-')]
if len(py_pkgs) == 1:
return py_pkgs[0]
else:
print '[dpkg -S found more than one package: %s; ignoring]' % ', '.join(pkgs),
return None
#
# main
#
usage = "usage: %prog [options]"
parser = OptionParser(prog='python_mkdebian', usage=usage, version=pkgversion)
parser.add_option('', '--force-control', dest='force_control', action='store',
help='Force control file behaviour. Can be one of "none" (keep unchanged), "deps" (only update dependencies), or "full" (recreate whole file). By default only dependencies will be updated ("deps").')
parser.add_option('', '--force-copyright', dest='force_copyright', action='store_true',
help='Force the copyright file to be recreated')
parser.add_option('', '--force-rules', dest='force_rules', action='store_true',
help='Force the rules file to be recreated')
parser.add_option('', '--changelog', dest='changelog', action='append',
help='Add string changelog to debian/changelog (can be specified multiple times)')
parser.add_option('', '--dependency', dest='dependencies', action='append',
help='Add additional debian package dependency (can be specified multiple times)')
parser.add_option('-D', '--distribution', dest='distribution', action='store',
help='Specify which Debian/Ubuntu distribution should be used in the changelog')
parser.add_option('', '--no-changelog', dest='use_old_changelog', action='store_true',
help='Don\'t create a new changelog entry')
parser.add_option('', '--prefix', dest='prefix', action='store',
help='Ask to install into a dedicated prefix')
parser.set_defaults(changelog=None, dependencies=[], force_control="deps", distribution=None, use_old_changelog=False, prefix=None)
options, args = parser.parse_args()
# switch stdout to line buffering, for scripts reading our output on the fly
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
egg = get_egg_info()
make_debian(egg, force_control=options.force_control,
changelog=options.changelog,
additional_dependencies=options.dependencies,
distribution=options.distribution,
use_old_changelog=options.use_old_changelog,
force_copyright=options.force_copyright,
force_rules=options.force_rules,
prefix=options.prefix)
|