#!/usr/bin/python import os import shutil import sys import glob def find_root(): look_where = [ os.path.join(os.getcwd(), 'setup.py'), os.path.join(os.pardir, 'setup.py'), ] for fn in look_where: if os.path.isfile(fn): return os.path.dirname(os.path.abspath(fn)) raise ImportError(("Unable to determine where your cloud-init 'setup.py'" " file is located at!")) # 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 PKG_MP = { 'tempita': 'python-tempita', 'boto': 'python-boto', 'configobj': 'python-configobj', 'oauth': 'python-oauth', 'yaml': 'python-yaml', 'prettytable': 'python-prettytable', 'argparse': 'python-argparse', } def write_debian_folder(root, version, revno, init_sys): 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'), util.abs_join(deb_dir, 'changelog'), params={ 'version': version, 'revision': revno, }) # Write out the control file template cmd = [sys.executable, util.abs_join(find_root(), 'tools', 'read-dependencies')] (stdout, _stderr) = util.subp(cmd) # Map to known packages pkgs = [p.lower().strip() for p in stdout.splitlines()] requires = [] for p in pkgs: tgt_pkg = None for name in PKG_MP.keys(): if p.find(name) != -1: tgt_pkg = PKG_MP.get(name) break if not tgt_pkg: raise RuntimeError(("Do not know how to translate %s to " " a known package") % (p)) else: requires.append(tgt_pkg) templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'control'), util.abs_join(deb_dir, 'control'), params={'requires': requires}) templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'rules'), util.abs_join(deb_dir, 'rules'), params={'init_sys': init_sys}) # Just copy the following directly for base_fn in ['dirs', 'copyright', 'compat', 'pycompat']: shutil.copy(util.abs_join(find_root(), 'packages', 'debian', base_fn), util.abs_join(deb_dir, base_fn)) def main(): parser = argparse.ArgumentParser() parser.add_argument("-n", "--no-sign", dest="sign", help=("attempt to sign " "the package (default: %(default)s)"), default=True, action='store_false') parser.add_argument("-v", "--verbose", dest="verbose", help=("run verbosely" " (default: %(default)s)"), default=False, action='store_true') parser.add_argument("-b", "--boot", dest="boot", help="select boot type (default: %(default)s)", metavar="TYPE", default='upstart', choices=('upstart', 'upstart-local')) args = parser.parse_args() capture = True if args.verbose: capture = False 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)) xdir = util.abs_join(tdir, 'cloud-init') os.makedirs(xdir) cmd = ['tar', '-xvzf', tmp_arch_fn, '-C', xdir] util.subp(cmd, capture=capture) print("Creating a debian/ folder in %r" % (xdir))) write_debian_folder(xdir, version, revno, args.boot) # The naming here seems to follow some debian standard # so it will whine if it is changed... tar_fn = "cloud-init_%s~%s.orig.tar.gz" % (version, revno) print("Archiving that new folder into %r" % (tar_fn)) cmd = ['tar', '-czvf', util.abs_join(tdir, tar_fn), '-C', xdir] cmd.extend(os.listdir(xdir)) util.subp(cmd, capture=capture) shutil.copy(util.abs_join(tdir, tar_fn), tar_fn) print("Wrote out archive %r" % (util.abs_join(tar_fn))) print("Running 'debuild' in %r" % (xdir)) with util.chdir(util.abs_join(tdir, 'cloud-init')): cmd = ['debuild'] if not args.sign: cmd.extend(['-us', '-uc']) util.subp(cmd, capture=capture) globs = [] globs.extend(glob.glob("%s/*.deb" % (os.path.join(tdir)))) link_fn = os.path.join(os.getcwd(), 'cloud-init_all.deb') for fn in globs: base_fn = os.path.basename(fn) shutil.move(fn, base_fn) print("Wrote out debian package %r" % (base_fn)) if 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, link_fn)) return 0 if __name__ == '__main__': sys.exit(main())