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'))
73 './configure'] + configure_opts
74 if len(schroot_command) > 0:
75 configure_command = schroot_command.split(' ') \
77 print ' '.join(configure_command)
79 run_cmd(configure_command, shell=False)
81 try: os.mkdir('doc/api')
83 run_cmd(root_command +' make dist-gzip', shell=True)
84 run_cmd('cp *.tar.gz ' +dist_dir, shell=True)
85 run_cmd(schroot_command + ' make -j3', shell=True)
87 def make_install(package):
88 with current_directory(package):
89 install_command = root_command + " make install"
92 run_cmd(install_command, shell=True)
96 '''Read in the packages file from source_packages
98 try: pf = file("source_packages")
100 print "Error: source_packages file not found"
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")
107 return map(lambda(x): x.rstrip(),
108 filter(is_comment, pf.readlines()))
113 opt.add_option('--prefix',
114 dest="prefix", default=prefix,
115 help="Set the prefix under which packages are built and"
117 opt.add_option('-c', '--configure-opt', dest="configure_opts",
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',
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'
131 opt.add_option('--tar-file',
133 help = 'Tar up resulting distributions in given tar file',
135 opt.usage = "%prog [options] [packages]"
136 (options, packages) = opt.parse_args()
137 prefix = options.prefix
138 root_command = options.root_command
140 configure_opts = ['--prefix', prefix,
141 "LDFLAGS=-Wl,-L"+prefix+"/lib -Wl,-L/usr/lib/freeradius"
142 + " -Wl,-rpath="+prefix+"/lib",
143 'CPPFLAGS=-I '+prefix+'/include',
144 '--with-system-libtool', '--with-system-libltdl',
145 '--enable-tls', '--with-gssapi='+prefix,
146 "--with-xmltooling="+prefix,
148 if options.configure_opts is not None:
149 configure_opts.extend(options.configure_opts)
150 tar_file = options.tar_file
151 if tar_file is not None: dist = True
154 if options.schroot is not None:
155 our_schroot = Schroot(options.schroot)
156 schroot_command_base = "schroot -r -c " + our_schroot.name
157 root_command = schroot_command_base + " -u root --"
158 schroot_command = schroot_command_base + ' --'
160 all_packages = read_packages()
161 if len(packages) == 0: packages = all_packages
166 os.mkdir('distributions')
168 dist_dir = os.path.join(os.getcwd(), 'distributions')
171 for p in all_packages:
172 if p in packages: build(p)
173 if packages[-1] == p : break
175 if tar_file is not None:
176 with current_directory(dist_dir):
177 run_cmd(['tar', '-cf', tar_file,
179 except CommandError as c:
180 print "Error:" + str(c.args)
183 finally: del our_schroot