Add --dist option to build script
[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([
73                                       './configure'] + configure_opts)
74         if len(schroot_command) > 0:
75             configure_command = schroot_command + " -- " \
76                 + configure_command
77         print configure_command
78         sys.stdout.flush()
79         run_cmd(configure_command, shell=True)
80         if dist:
81             try: os.mkdir('doc/api')
82             except: pass
83             run_cmd(schroot_command +' make dist-gzip', shell=True)
84             run_cmd('cp *.tar.gz ' +dist_dir, shell=True)
85         run_cmd(schroot_command + ' make', shell=True)
86
87 def make_install(package):
88     with current_directory(package):
89         install_command = root_command + " make install"
90         print install_command
91         sys.stdout.flush()
92         run_cmd(install_command, shell=True)
93         
94
95 def read_packages():
96     '''Read in the packages file from source_packages
97     '''
98     try: pf = file("source_packages")
99     except IOError:
100         print "Error: source_packages file not found"
101         exit(1)
102     def is_comment(line):
103         if re.match("^\\s*#", line): return False
104         if "#" in line: raise ValueError(
105             "Source package line contains a comment but not at beginning")
106         return True
107     return map(lambda(x): x.rstrip(),
108         filter(is_comment, pf.readlines()))
109
110
111 # main program
112 opt = OptionParser()
113 opt.add_option('--prefix',
114                dest="prefix", default=prefix,
115                help="Set the prefix under which packages are built and"
116                + "installed")
117 opt.add_option('-c', '--configure-opt', dest="configure_opts",
118                action="append",
119                help="Specify an option to pass to configure")
120 opt.add_option('-r', '--root-cmd', dest="root_command",
121                default=root_command,
122                help="Specify command to gain root for make install")
123 opt.add_option('-s', '--schroot',
124                dest="schroot",
125                help="Specify name of schroot to use for build;"
126                "implicitly sets root_command")
127 opt.add_option( '--dist', action='store_true',
128                 default=False, dest='dist',
129                 help = 'make dist-gzip in addition to the build'
130                 )
131 opt.usage = "%prog [options] [packages]"
132 (options, packages) = opt.parse_args()
133 prefix = options.prefix
134 root_command = options.root_command
135 dist = options.dist
136 configure_opts = ['--prefix', prefix,
137                   "LDFLAGS='-Wl,-L"+prefix+"/lib -Wl,-L/usr/lib/freeradius"
138                   + " -Wl,-rpath="+prefix+"/lib'",
139                   'CPPFLAGS="-I '+prefix+'/include"',
140                   '--with-system-libtool', '--with-system-libltdl',
141                   '--enable-tls', '--with-gssapi='+prefix,
142                   "--with-xmltooling="+prefix, 
143                   ]
144 if options.configure_opts is not None: 
145     configure_opts.extend(options.configure_opts)
146
147 our_schroot = None
148 if options.schroot is not None:
149     our_schroot = Schroot(options.schroot)
150     schroot_command = "schroot -r -c " + our_schroot.name
151     root_command = schroot_command + " -u root"
152
153 all_packages = read_packages()
154 if len(packages) == 0: packages = all_packages
155
156 os.umask(022)
157 if dist:
158     try:
159         os.mkdir('distributions')
160     except: pass
161     dist_dir = os.path.join(os.getcwd(), 'distributions')
162
163 try:
164     for p in all_packages:
165         if p in packages: build(p)
166         make_install(p)
167 except CommandError as c:
168     print "Error:" + str(c.args)
169     our_schroot = None
170     exit(1)
171 finally: del our_schroot
172
173     
174