~cjwatson/launchpad-buildd/improve-depwait

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
#! /usr/bin/python -u
# Copyright 2013 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""A script that builds a live file system."""

__metaclass__ = type

from optparse import OptionParser
import os
import re
import subprocess
import sys
import traceback


RETCODE_SUCCESS = 0
RETCODE_FAILURE_INSTALL = 200
RETCODE_FAILURE_BUILD = 201


def get_build_path(build_id, *extra):
    """Generate a path within the build directory.

    :param build_id: the build id to use.
    :param extra: the extra path segments within the build directory.
    :return: the generated path.
    """
    return os.path.join(os.environ["HOME"], "build-" + build_id, *extra)


non_meta_re = re.compile(r'^[a-zA-Z0-9+,./:=@_-]+$')

def shell_escape(arg):
    if non_meta_re.match(arg):
        return arg
    else:
        return "'%s'" % arg.replace("'", "'\\''")


linux32_arches = [
    "armel",
    "armhf",
    "hppa",
    "i386",
    "lpia",
    "mips",
    "mipsel",
    "powerpc",
    "s390",
    "sparc",
    ]
linux64_arches = [
    "alpha",
    "amd64",
    "arm64",
    "hppa64",
    "ia64",
    "ppc64",
    "ppc64el",
    "s390x",
    "sparc64",
    "x32",
    ]


class LiveFSBuilder:
    """Builds a live file system."""

    def __init__(self, options):
        self.options = options
        self.chroot_path = get_build_path(
            self.options.build_id, 'chroot-autobuild')

    def chroot(self, args, echo=False):
        """Run a command in the chroot.

        :param args: the command and arguments to run.
        """
        if self.options.arch in linux32_arches:
            args = ["linux32"] + args
        elif self.options.arch in linux64_arches:
            args = ["linux64"] + args
        if echo:
            print "Running in chroot: %s" % ' '.join(
                "'%s'" % arg for arg in args)
            sys.stdout.flush()
        subprocess.check_call([
            "/usr/bin/sudo", "/usr/sbin/chroot", self.chroot_path] + args)

    def run_build_command(self, args, env=None, echo=False):
        """Run a build command in the chroot.

        This is unpleasant because we need to run it in /build under sudo
        chroot, and there's no way to do this without either a helper
        program in the chroot or unpleasant quoting.  We go for the
        unpleasant quoting.

        :param args: the command and arguments to run.
        :param env: dictionary of additional environment variables to set.
        """
        args = [shell_escape(arg) for arg in args]
        if env:
            args = ["env"] + [
                "%s=%s" % (key, shell_escape(value))
                for key, value in env.items()] + args
        command = "cd /build && %s" % " ".join(args)
        self.chroot(["/bin/sh", "-c", command], echo=echo)

    def install(self):
        self.chroot(["apt-get", "-y", "install", "livecd-rootfs"])
        if self.options.arch == "i386":
            self.chroot([
                "apt-get", "-y", "--no-install-recommends", "install",
                "ltsp-server",
                ])
        if self.options.locale is not None:
            self.chroot([
                "apt-get", "-y", "--install-recommends", "install",
                "ubuntu-defaults-builder",
                ])

    def build(self):
        if self.options.locale is not None:
            self.run_build_command([
                "ubuntu-defaults-image",
                "--locale", self.options.locale,
                "--arch", self.options.arch,
                "--release", self.options.series,
                ])
        else:
            self.run_build_command(["rm", "-rf", "auto"])
            self.run_build_command(["mkdir", "-p", "auto"])
            for lb_script in ("config", "build", "clean"):
                lb_script_path = os.path.join(
                    "/usr/share/livecd-rootfs/live-build/auto", lb_script)
                self.run_build_command(["ln", "-s", lb_script_path, "auto/"])
            self.run_build_command(["lb", "clean", "--purge"])

            base_lb_env = {
                "PROJECT": self.options.project,
                "ARCH": self.options.arch,
                }
            if self.options.subproject is not None:
                base_lb_env["SUBPROJECT"] = self.options.subproject
            if self.options.subarch is not None:
                base_lb_env["SUBARCH"] = self.options.subarch
            lb_env = dict(base_lb_env)
            lb_env["SUITE"] = self.options.series
            if self.options.datestamp is not None:
                lb_env["NOW"] = self.options.datestamp
            if self.options.image_format is not None:
                lb_env["IMAGEFORMAT"] = self.options.image_format
            if self.options.proposed:
                lb_env["PROPOSED"] = "1"
            if self.options.extra_ppas:
                lb_env["EXTRA_PPAS"] = " ".join(self.options.extra_ppas)
            self.run_build_command(["lb", "config"], env=lb_env)
            self.run_build_command(["lb", "build"], env=base_lb_env)


def main():
    parser = OptionParser()
    parser.add_option("--build-id", help="build identifier")
    parser.add_option(
        "--arch", metavar="ARCH", help="build for architecture ARCH")
    parser.add_option(
        "--subarch", metavar="SUBARCH",
        help="build for subarchitecture SUBARCH")
    parser.add_option(
        "--project", metavar="PROJECT", help="build for project PROJECT")
    parser.add_option(
        "--subproject", metavar="SUBPROJECT",
        help="build for subproject SUBPROJECT")
    parser.add_option(
        "--series", metavar="SERIES", help="build for series SERIES")
    parser.add_option("--datestamp", help="date stamp")
    parser.add_option(
        "--image-format", metavar="FORMAT", help="produce an image in FORMAT")
    parser.add_option(
        "--proposed", default=False, action="store_true",
        help="enable use of -proposed pocket")
    parser.add_option(
        "--locale", metavar="LOCALE",
        help="use ubuntu-defaults-image to build an image for LOCALE")
    parser.add_option(
        "--extra-ppa", dest="extra_ppas", default=[], action="append",
        help="use this additional PPA")
    options, _ = parser.parse_args()

    builder = LiveFSBuilder(options)
    try:
        builder.install()
    except Exception:
        traceback.print_exc()
        return RETCODE_FAILURE_INSTALL
    try:
        builder.build()
    except Exception:
        traceback.print_exc()
        return RETCODE_FAILURE_BUILD
    return RETCODE_SUCCESS


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