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