3 '''A script to buildMoonshot potentially using a schroot for install testing.
6 from contextlib import contextmanager
7 import os, subprocess, exceptions
10 from optparse import OptionParser
11 from shutil import copy
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"
23 class CommandError(exceptions.StandardError):
26 class Schroot(object):
27 '''Represents a schroot used for building moonshot.'''
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',
36 self.end_session = True
39 self.end_session = False
44 run_cmd(('schroot', '-e', '-c', self.name))
45 except CommandError: pass
48 def current_directory(dir):
49 "Change the current directory as a context manager; when the context exits, return."
56 def run_cmd(args, **kwords):
57 rcode = subprocess.call( args, **kwords)
59 raise CommandError(args)
61 def command_output(args) :
62 p = subprocess.Popen(args, stdout=subprocess.PIPE)
63 output = p.communicate()
66 raise CommandError(args)
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 + " -- " \
77 print configure_command
79 run_cmd(configure_command, shell=True)
80 run_cmd(schroot_command + ' make', shell=True)
82 def make_install(package):
83 with current_directory(package):
84 install_command = root_command + " make install"
87 run_cmd(install_command, shell=True)
92 '''Read in the packages file from source_packages
94 try: pf = file("source_packages")
96 print "Error: source_packages file not found"
99 if re.match("^\\s*#", line): return False
100 if "#" in line: raise ValueError(
101 "Source package line contains a comment but not at beginning")
103 return map(lambda(x): x.rstrip(),
104 filter(is_comment, pf.readlines()))
109 opt.add_option('--prefix',
110 dest="prefix", default=prefix,
111 help="Set the prefix under which packages are built and"
113 opt.add_option('-c', '--configure-opt', dest="configure_opts",
115 help="Specify an option to pass to configure")
116 opt.add_option('-r', '--root-cmd', dest="root_command",
117 default=root_command,
118 help="Specify command to gain root for make install")
119 opt.add_option('-s', '--schroot',
121 help="Specify name of schroot to use for build;"
122 "implicitly sets root_command")
123 opt.usage = "%prog [options] [packages]"
124 (options, packages) = opt.parse_args()
125 prefix = options.prefix
126 root_command = options.root_command
127 configure_opts = ['--prefix', prefix,
128 "LDFLAGS='-Wl,-L"+prefix+"/lib -Wl,-L/usr/lib/freeradius"
129 + " -Wl,-R"+prefix+"/lib'",
130 'CPPFLAGS="-I '+prefix+'/include"',
131 '--with-system-libtool', '--with-system-libltdl',
132 "--with-xmltooling="+prefix,
134 if options.configure_opts is not None:
135 configure_opts.extend(options.configure_opts)
138 if options.schroot is not None:
139 our_schroot = Schroot(options.schroot)
140 schroot_command = "schroot -r -c " + our_schroot.name
141 root_command = schroot_command + " -u root"
143 all_packages = read_packages()
144 if len(packages) == 0: packages = all_packages
149 for p in all_packages:
150 if p in packages: build(p)
152 except CommandError as c:
153 print "Error:" + str(c.args)
156 finally: del our_schroot