#!/usr/bin/env python3 import glob import os import shutil import sys def find_root(): # expected path is in /packages/ top_dir = os.environ.get("CLOUD_INIT_TOP_D", None) if top_dir is None: top_dir = os.path.dirname( os.path.dirname(os.path.abspath(sys.argv[0]))) if os.path.isfile(os.path.join(top_dir, 'setup.py')): return os.path.abspath(top_dir) raise OSError(("Unable to determine where your cloud-init topdir is." " set CLOUD_INIT_TOP_D?")) # Use the util functions from cloudinit sys.path.insert(0, find_root()) from cloudinit import templater from cloudinit import util import argparse # Package names that will showup in requires to what we can actually # use in our debian 'control' file, this is a translation of the 'requires' # file pypi package name to a debian/ubuntu package name. STD_NAMED_PACKAGES = [ 'configobj', 'jinja2', 'jsonpatch', 'oauthlib', 'prettytable', 'requests', 'six', 'httpretty', 'mock', 'nose', 'setuptools', 'flake8', 'hacking', 'unittest2', ] NONSTD_NAMED_PACKAGES = { 'argparse': ('python-argparse', None), 'contextlib2': ('python-contextlib2', None), 'cheetah': ('python-cheetah', None), 'pyserial': ('python-serial', 'python3-serial'), 'pyyaml': ('python-yaml', 'python3-yaml'), 'six': ('python-six', 'python3-six'), 'pep8': ('pep8', 'python3-pep8'), 'pyflakes': ('pyflakes', 'pyflakes'), } DEBUILD_ARGS = ["-S", "-d"] def write_debian_folder(root, version, revno, pkgmap, pyver="3", append_requires=[]): deb_dir = util.abs_join(root, 'debian') os.makedirs(deb_dir) # Fill in the change log template templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'changelog.in'), util.abs_join(deb_dir, 'changelog'), params={ 'version': version, 'revision': revno, }) # Write out the control file template cmd = [util.abs_join(find_root(), 'tools', 'read-dependencies')] (stdout, _stderr) = util.subp(cmd) pypi_pkgs = [p.lower().strip() for p in stdout.splitlines()] (stdout, _stderr) = util.subp(cmd + ['test-requirements.txt']) pypi_test_pkgs = [p.lower().strip() for p in stdout.splitlines()] # Map to known packages requires = append_requires test_requires = [] lists = ((pypi_pkgs, requires), (pypi_test_pkgs, test_requires)) for pypilist, target in lists: for p in pypilist: if p not in pkgmap: raise RuntimeError(("Do not know how to translate pypi " "dependency %r to a known package") % (p)) elif pkgmap[p]: target.append(pkgmap[p]) if pyver == "3": python = "python3" else: python = "python" templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'control.in'), util.abs_join(deb_dir, 'control'), params={'requires': ','.join(requires), 'test_requires': ','.join(test_requires), 'python': python}) templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'rules.in'), util.abs_join(deb_dir, 'rules'), params={'python': python, 'pyver': pyver}) # Just copy any other files directly (including .in) pdeb_d = util.abs_join(find_root(), 'packages', 'debian') for f in [os.path.join(pdeb_d, f) for f in os.listdir(pdeb_d)]: if os.path.isfile(f): shutil.copy(f, util.abs_join(deb_dir, os.path.basename(f))) def main(): parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", dest="verbose", help=("run verbosely" " (default: %(default)s)"), default=False, action='store_true') parser.add_argument("--cloud-utils", dest="cloud_utils", help=("depend on cloud-utils package" " (default: %(default)s)"), default=False, action='store_true') parser.add_argument("--python2", dest="python2", help=("build debs for python2 rather than python3"), default=False, action='store_true') parser.add_argument("--init-system", dest="init_system", help=("build deb with INIT_SYSTEM=xxx" " (default: %(default)s"), default=os.environ.get("INIT_SYSTEM", "upstart,systemd")) for ent in DEBUILD_ARGS: parser.add_argument(ent, dest="debuild_args", action='append_const', const=ent, help=("pass through '%s' to debuild" % ent), default=[]) parser.add_argument("--sign", default=False, action='store_true', help="sign result. do not pass -us -uc to debuild") parser.add_argument("--signuser", default=False, action='store', help="user to sign, see man dpkg-genchanges") args = parser.parse_args() if not args.sign: args.debuild_args.extend(['-us', '-uc']) if args.signuser: args.debuild_args.extend(['-e%s' % args.signuser]) os.environ['INIT_SYSTEM'] = args.init_system capture = True if args.verbose: capture = False pkgmap = {} for p in NONSTD_NAMED_PACKAGES: pkgmap[p] = NONSTD_NAMED_PACKAGES[p][int(not args.python2)] for p in STD_NAMED_PACKAGES: if args.python2: pkgmap[p] = "python-" + p pyver = "2" else: pkgmap[p] = "python3-" + p pyver = "3" with util.tempdir() as tdir: cmd = [util.abs_join(find_root(), 'tools', 'read-version')] (sysout, _stderr) = util.subp(cmd) version = sysout.strip() cmd = ['bzr', 'revno'] (sysout, _stderr) = util.subp(cmd) revno = sysout.strip() # This is really only a temporary archive # since we will extract it then add in the debian # folder, then re-archive it for debian happiness print("Creating a temporary tarball using the 'make-tarball' helper") cmd = [util.abs_join(find_root(), 'tools', 'make-tarball')] (sysout, _stderr) = util.subp(cmd) arch_fn = sysout.strip() tmp_arch_fn = util.abs_join(tdir, os.path.basename(arch_fn)) shutil.move(arch_fn, tmp_arch_fn) print("Extracting temporary tarball %r" % (tmp_arch_fn)) cmd = ['tar', '-xvzf', tmp_arch_fn, '-C', tdir] util.subp(cmd, capture=capture) extracted_name = tmp_arch_fn[:-len('.tar.gz')] os.remove(tmp_arch_fn) xdir = util.abs_join(tdir, 'cloud-init') shutil.move(extracted_name, xdir) print("Creating a debian/ folder in %r" % (xdir)) if args.cloud_utils: append_requires=['cloud-utils | cloud-guest-utils'] else: append_requires=[] write_debian_folder(xdir, version, revno, pkgmap, pyver=pyver, append_requires=append_requires) # The naming here seems to follow some debian standard # so it will whine if it is changed... tar_fn = "cloud-init_%s~bzr%s.orig.tar.gz" % (version, revno) print("Archiving the adjusted source into %r" % (util.abs_join(tdir, tar_fn))) cmd = ['tar', '-czvf', util.abs_join(tdir, tar_fn), '-C', xdir] cmd.extend(os.listdir(xdir)) util.subp(cmd, capture=capture) # Copy it locally for reference shutil.copy(util.abs_join(tdir, tar_fn), util.abs_join(os.getcwd(), tar_fn)) print("Copied that archive to %r for local usage (if desired)." % (util.abs_join(os.getcwd(), tar_fn))) print("Running 'debuild %s' in %r" % (' '.join(args.debuild_args), xdir)) with util.chdir(xdir): cmd = ['debuild', '--preserve-envvar', 'INIT_SYSTEM'] if args.debuild_args: cmd.extend(args.debuild_args) util.subp(cmd, capture=capture) link_fn = os.path.join(os.getcwd(), 'cloud-init_all.deb') link_dsc = os.path.join(os.getcwd(), 'cloud-init.dsc') for base_fn in os.listdir(os.path.join(tdir)): full_fn = os.path.join(tdir, base_fn) if not os.path.isfile(full_fn): continue shutil.move(full_fn, base_fn) print("Wrote %r" % (base_fn)) if base_fn.endswith('_all.deb'): # Add in the local link util.del_file(link_fn) os.symlink(base_fn, link_fn) print("Linked %r to %r" % (base_fn, os.path.basename(link_fn))) if base_fn.endswith('.dsc'): util.del_file(link_dsc) os.symlink(base_fn, link_dsc) print("Linked %r to %r" % (base_fn, os.path.basename(link_dsc))) return 0 if __name__ == '__main__': sys.exit(main())