summaryrefslogtreecommitdiff
path: root/packages/bddeb
blob: eeb64434c3a1103ee072e7df1b5c9b9cef52ff9b (plain)
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
#!/usr/bin/python

import os
import shutil
import sys
import glob

# Use the util functions from cloudinit
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
        sys.argv[0]), os.pardir, os.pardir))
if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")):
    sys.path.insert(0, possible_topdir)

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):
    deb_dir = util.abs_join(root, 'debian')
    os.makedirs(deb_dir)
    
    # Fill in the change log template
    templater.render_to_file(util.abs_join('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(os.pardir, '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('debian', 'control'),
                             util.abs_join(deb_dir, 'control'),
                             params={'requires': requires})
    
    # Just copy the following directly
    for base_fn in ['dirs', 'copyright', 'compat', 'pycompat', 'rules']:
        shutil.copy(util.abs_join('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')
    args = parser.parse_args()

    capture = True
    if args.verbose:
        capture = False

    with util.tempdir() as tdir:

        cmd = [sys.executable,
                 util.abs_join(os.pardir, 'tools', 'read-version')]
        (sysout, _stderr) = util.subp(cmd)
        version = sysout.strip()

        cmd = ['bzr', 'revno']
        (sysout, _stderr) = util.subp(cmd)
        revno = sysout.strip()
        
        cmd = [sys.executable,
                util.abs_join(os.getcwd(), '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)

        cmd = ['tar', '-xvzf', tmp_arch_fn, '-C', tdir]
        util.subp(cmd)
        
        base_name = os.path.basename(arch_fn)[:-len(".tar.gz")]
        shutil.move(util.abs_join(tdir, base_name), 
                    util.abs_join(tdir, 'cloud-init'))

        write_debian_folder(util.abs_join(tdir, 'cloud-init'),
                            version, revno)

        tar_fn = "cloud-init_%s~%s.orig.tar.gz" % (version, revno)
        cmd = ['tar', '-czvf', 
               util.abs_join(tdir, tar_fn), 
               '-C', util.abs_join(tdir, 'cloud-init')]
        cmd.extend(os.listdir(util.abs_join(tdir, 'cloud-init')))
        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)))
        
        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))))
        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('cloud-init_all.deb')
                os.symlink(base_fn, 'cloud-init_all.deb')

    return 0


if __name__ == '__main__':
    sys.exit(main())