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"
21 class CommandError(exceptions.StandardError):
24 class Schroot(object):
25 '''Represents a schroot used for building moonshot.'''
27 def __init__(self, name):
28 '''Initialize a new schroot option from the named
29 schroot. Unless the named schroot starts with session:, then a
30 new session schroot is created.'''
31 if not name.startswith('session:'):
32 self.name = command_output(('schroot', '-b',
34 self.end_session = True
37 self.end_session = False
42 run_cmd(('schroot', '-e', '-c', self.name))
43 except CommandError: pass
46 def current_directory(dir):
47 "Change the current directory as a context manager; when the context exits, return."
54 def run_cmd(args, **kwords):
55 rcode = subprocess.call( args, **kwords)
57 raise CommandError(args)
59 def command_output(args) :
60 p = subprocess.Popen(args, stdout=subprocess.PIPE)
61 output = p.communicate()
64 raise CommandError(args)
68 with current_directory(package):
69 run_cmd(('autoreconf', '-i', '-f'))
70 configure_command = ' '.join(['./configure'] + configure_opts)
71 print configure_command
73 run_cmd(configure_command, shell=True)
76 def make_install(package):
77 with current_directory(package):
78 install_command = root_command + " make install"
81 run_cmd(install_command, shell=True)
86 '''Read in the packages file from source_packages
88 try: pf = file("source_packages")
90 print "Error: source_packages file not found"
93 if re.match("^\\s*#", line): return False
94 if "#" in line: raise ValueError(
95 "Source package line contains a comment but not at beginning")
97 return map(lambda(x): x.rstrip(),
98 filter(is_comment, pf.readlines()))
103 opt.add_option('--prefix',
104 dest="prefix", default=prefix,
105 help="Set the prefix under which packages are built and"
107 opt.add_option('-c', '--configure-opt', dest="configure_opts",
109 help="Specify an option to pass to configure")
110 opt.add_option('-r', '--root-cmd', dest="root_command",
111 default=root_command,
112 help="Specify command to gain root for make install")
113 opt.add_option('-s', '--schroot',
115 help="Specify name of schroot to use for build;"
116 "implicitly sets root_command")
117 opt.usage = "%prog [options] [packages]"
118 (options, packages) = opt.parse_args()
119 prefix = options.prefix
120 root_command = options.root_command
121 configure_opts = ['--prefix', prefix,
122 "LDFLAGS='-Wl,-L"+prefix+"/lib"
123 + " -Wl,-R"+prefix+"/lib'",
124 'CPPFLAGS="-I '+prefix+'/include"']
125 if options.configure_opts is not None:
126 configure_opts.extend(options.configure_opts)
129 if options.schroot is not None:
130 our_schroot = Schroot(options.schroot)
131 root_command = "schroot -r -c " + our_schroot.name
133 all_packages = read_packages()
134 if len(packages) == 0: packages = all_packages
138 for p in all_packages:
139 if p in packages: build(p)
141 except CommandError as c:
142 print "Error:" + str(c.args)
145 finally: del our_schroot