Run build in schroot too
[moonshot.git] / builder
1 #!/usr/bin/python
2
3 '''A script to buildMoonshot  potentially using a schroot for install testing.
4 '''
5
6 from contextlib import contextmanager
7 import os, subprocess, exceptions
8 import re
9 import sys
10 from optparse import OptionParser
11 from shutil import copy
12
13
14
15 # These variables can be overridden by options. If packages is not
16 # set, then it is read from the source_packages file
17 packages = []  # Set of packages to build
18 prefix = "/usr/local/moonshot"
19 root_command = "fakeroot"
20
21 schroot_command = ""
22
23 class CommandError(exceptions.StandardError):
24     pass
25
26 class Schroot(object):
27     '''Represents a schroot used for building moonshot.'''
28
29     def __init__(self, name):
30         '''Initialize a new schroot option from the named
31         schroot. Unless the named schroot starts with session:, then a
32         new session schroot is created.'''
33         if not name.startswith('session:'):
34             self.name = command_output(('schroot', '-b',
35                                         '-c', name))
36             self.end_session = True
37         else:
38             self.name = name
39             self.end_session = False
40
41     def __del__(self):
42         if self.end_session:
43             try:
44                 run_cmd(('schroot', '-e', '-c', self.name))
45             except CommandError: pass
46
47 @contextmanager
48 def current_directory(dir):
49     "Change the current directory as a context manager; when the context exits, return."
50     cwd = os.getcwd()
51     os.chdir(dir)
52     yield
53     os.chdir(cwd)
54
55
56 def run_cmd(args, **kwords):
57     rcode =  subprocess.call( args, **kwords)
58     if rcode <> 0:
59         raise CommandError(args)
60
61 def command_output(args) :
62     p = subprocess.Popen(args, stdout=subprocess.PIPE)
63     output = p.communicate()
64     output = output[0]
65     if p.returncode != 0:
66         raise CommandError(args)
67     return output.strip()
68
69 def build(package):
70     with current_directory(package):
71         run_cmd(('autoreconf', '-i', '-f'))
72         configure_command = ' '.join([schroot_command,
73                                       './configure'] + configure_opts)
74         print configure_command
75         sys.stdout.flush()
76         run_cmd(configure_command, shell=True)
77         run_cmd(schroot_command + ' make', shell=true)
78
79 def make_install(package):
80     with current_directory(package):
81         install_command = root_command + " make install"
82         print install_command
83         sys.stdout.flush()
84         run_cmd(install_command, shell=True)
85         
86
87
88 def read_packages():
89     '''Read in the packages file from source_packages
90     '''
91     try: pf = file("source_packages")
92     except IOError:
93         print "Error: source_packages file not found"
94         exit(1)
95     def is_comment(line):
96         if re.match("^\\s*#", line): return False
97         if "#" in line: raise ValueError(
98             "Source package line contains a comment but not at beginning")
99         return True
100     return map(lambda(x): x.rstrip(),
101         filter(is_comment, pf.readlines()))
102
103
104 # main program
105 opt = OptionParser()
106 opt.add_option('--prefix',
107                dest="prefix", default=prefix,
108                help="Set the prefix under which packages are built and"
109                + "installed")
110 opt.add_option('-c', '--configure-opt', dest="configure_opts",
111                action="append",
112                help="Specify an option to pass to configure")
113 opt.add_option('-r', '--root-cmd', dest="root_command",
114                default=root_command,
115                help="Specify command to gain root for make install")
116 opt.add_option('-s', '--schroot',
117                dest="schroot",
118                help="Specify name of schroot to use for build;"
119                "implicitly sets root_command")
120 opt.usage = "%prog [options] [packages]"
121 (options, packages) = opt.parse_args()
122 prefix = options.prefix
123 root_command = options.root_command
124 configure_opts = ['--prefix', prefix,
125                   "LDFLAGS='-Wl,-L"+prefix+"/lib"
126                   + " -Wl,-R"+prefix+"/lib'",
127                   'CPPFLAGS="-I '+prefix+'/include"']
128 if options.configure_opts is not None: 
129     configure_opts.extend(options.configure_opts)
130
131 our_schroot = None
132 if options.schroot is not None:
133     our_schroot = Schroot(options.schroot)
134     schroot_command = "schroot -r -c " + our_schroot.name
135     root_command = schroot_command + " -u root"
136
137 all_packages = read_packages()
138 if len(packages) == 0: packages = all_packages
139
140
141 try:
142     for p in all_packages:
143         if p in packages: build(p)
144         make_install(p)
145 except CommandError as c:
146     print "Error:" + str(c.args)
147     our_schroot = None
148     exit(1)
149 finally: del our_schroot
150
151     
152