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 rpm_packages file
17 packages = [] # Set of packages to build
20 class CommandError(exceptions.StandardError):
24 return re.match('^.*\\.tar\\.gz', name)
27 match = re.match('([^/-]*/)?([^-/]+)', t)
31 def current_directory(dir):
32 "Change the current directory as a context manager; when the context exits, return."
39 def run_cmd(args, **kwords):
40 rcode = subprocess.call( args, **kwords)
42 raise CommandError(args)
44 def command_output(args) :
45 p = subprocess.Popen(args, stdout=subprocess.PIPE)
46 output = p.communicate()
49 raise CommandError(args)
53 run_cmd(['rpmbuild', '-ta', package])
58 '''Read in the packages file from rpm_packages
60 try: pf = file("rpm_packages")
62 print "Error: rpm_packages file not found"
65 if re.match("^\\s*#", line): return False
66 if "#" in line: raise ValueError(
67 "Source package line contains a comment but not at beginning")
69 return map(lambda(x): x.rstrip(),
70 filter(is_comment, pf.readlines()))
75 opt.usage = "%prog [options] distributions_dir [packages]"
76 opt.add_option('--tar-file',
78 help = 'Tar up resulting packages in given tar file',
80 (options, args) = opt.parse_args()
81 tar_file = options.tar_file
82 if tar_file is not None:
83 tar_file = os.path.join(os.getcwd(), tar_file)
85 print "Distributions directory required"
90 if len(packages) == 0: packages = read_packages()
93 tarballs = filter(is_tarball, os.listdir(dist_dir))
95 package_order[trim_tarball(t)] = count
102 run_cmd([ 'rm' '-rf',
103 os.path.expanduser("~/rpmbuild")])
104 rum_cmd([ 'rpmdev-setuptree'])
105 with current_directory(dist_dir):
106 tarballs.sort (key = lambda x: package_order[trim_tarball(x)])
109 if tar_file is not None:
110 with current_directory(os.path.expanduser("~/rpmbuild")):
111 run_cmd(['tar', '-cf', tar_file,
112 './RPMS', './SRPMS'])
114 except CommandError as c:
115 print "Error:" + str(c.args)