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