fix typo
[moonshot.git] / rpm-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 rpm_packages file
17 packages = []  # Set of packages to build
18 tar_file = None
19
20 class CommandError(exceptions.StandardError):
21     pass
22
23 def is_tarball(name):
24     return re.match('^.*\\.tar\\.gz', name)
25
26 def trim_tarball(t):
27     match = re.match('([^/-]*/)?([^-/]+)', t)
28     return match.group(2)
29
30 @contextmanager
31 def current_directory(dir):
32     "Change the current directory as a context manager; when the context exits, return."
33     cwd = os.getcwd()
34     os.chdir(dir)
35     yield
36     os.chdir(cwd)
37
38
39 def run_cmd(args, **kwords):
40     rcode =  subprocess.call( args, **kwords)
41     if rcode <> 0:
42         raise CommandError(args)
43
44 def command_output(args) :
45     p = subprocess.Popen(args, stdout=subprocess.PIPE)
46     output = p.communicate()
47     output = output[0]
48     if p.returncode != 0:
49         raise CommandError(args)
50     return output.strip()
51
52 def build(package):
53     run_cmd(['rpmbuild', '-ta', package])
54
55
56
57 def read_packages():
58     '''Read in the packages file from rpm_packages
59     '''
60     try: pf = file("rpm_packages")
61     except IOError:
62         print "Error: rpm_packages file not found"
63         exit(1)
64     def is_comment(line):
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")
68         return True
69     return map(lambda(x): x.rstrip(),
70         filter(is_comment, pf.readlines()))
71
72
73 # main program
74 opt = OptionParser()
75 opt.usage = "%prog [options] distributions_dir [packages]"
76 opt.add_option('--tar-file',
77                dest='tar_file',
78                help = 'Tar up resulting packages in given tar file',
79                default = None)
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)
84 if len(args) == 0:
85     print "Distributions directory required"
86     exit(1)
87 dist_dir = args[0]
88 packages = args[1:]
89
90 if len(packages) == 0: packages = read_packages()
91 package_order = {}
92 count = 0
93 tarballs = filter(is_tarball, os.listdir(dist_dir))
94 for t in packages:
95     package_order[trim_tarball(t)] = count
96     count += 1
97     
98
99 os.umask(022)
100
101 try:
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)])
107         for t in tarballs:
108             build(t)
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'])
113
114 except CommandError as c:
115     print "Error:" + str(c.args)
116     exit(1)
117
118
119     
120