1 | #!/usr/bin/python |
---|
2 | # -*- python -*- |
---|
3 | """A simple script for managing updating/building projects""" |
---|
4 | |
---|
5 | from __future__ import with_statement |
---|
6 | from optparse import OptionParser |
---|
7 | import os |
---|
8 | import os.path |
---|
9 | |
---|
10 | class pushd(object): |
---|
11 | def __init__(self, path): |
---|
12 | self.path = path |
---|
13 | |
---|
14 | def __enter__(self): |
---|
15 | self.old = os.getcwd() |
---|
16 | os.chdir(self.path) |
---|
17 | |
---|
18 | def __exit__(self, type, value, traceback): |
---|
19 | os.chdir(self.old) |
---|
20 | |
---|
21 | build_list = {} |
---|
22 | |
---|
23 | class Step: |
---|
24 | """A single step in a given build""" |
---|
25 | def __init__(self, command, category=None): |
---|
26 | self.command = command |
---|
27 | self.category = category |
---|
28 | |
---|
29 | def run(self, options): |
---|
30 | """Run this step if the options tell us to""" |
---|
31 | if self.category is None or getattr(options, self.category): |
---|
32 | if options.dry: |
---|
33 | print self.command |
---|
34 | return 0 |
---|
35 | else: |
---|
36 | return os.system(self.command) |
---|
37 | else: |
---|
38 | return 0 |
---|
39 | |
---|
40 | class Build: |
---|
41 | """A build for a single project, consisting of a list of build steps""" |
---|
42 | def __init__(self, name, path, steps, build_list=build_list): |
---|
43 | self.path = os.path.expandvars(path) |
---|
44 | self.steps = steps |
---|
45 | self.name = name |
---|
46 | build_list[name] = self |
---|
47 | |
---|
48 | def run(self, options): |
---|
49 | """Run the steps associated with this build""" |
---|
50 | if options.dry: |
---|
51 | print "building %s..."%self.name |
---|
52 | with pushd(self.path): |
---|
53 | for i in self.steps: |
---|
54 | status = i.run(options) |
---|
55 | if status != 0: return status |
---|
56 | return 0 |
---|
57 | |
---|
58 | |
---|
59 | class BuildGroup: |
---|
60 | """A group of builds""" |
---|
61 | def __init__(self, name, builds, build_list=build_list): |
---|
62 | self.builds = builds |
---|
63 | self.build_list = build_list |
---|
64 | self.name = name |
---|
65 | build_list[name] = self |
---|
66 | |
---|
67 | def run(self, options): |
---|
68 | """Run the builds associated with this group""" |
---|
69 | if options.dry: |
---|
70 | print "building %s..."%self.name |
---|
71 | for i in self.builds: |
---|
72 | status = self.build_list[i].run(options) |
---|
73 | if status != 0: return status |
---|
74 | return 0 |
---|
75 | |
---|
76 | parser = OptionParser(usage='Usage: %prog [options] build...') |
---|
77 | parser.add_option('-u', '--update', dest='update', action='store_true', |
---|
78 | default=False, |
---|
79 | help='update from SVN (and configure)') |
---|
80 | parser.add_option('-C', '--clean', dest='clean', action='store_true', |
---|
81 | default=False, |
---|
82 | help='clean build first') |
---|
83 | parser.add_option('-c', '--configure', dest='configure', action='store_true', |
---|
84 | default=False, |
---|
85 | help='re-run configure') |
---|
86 | parser.add_option('-f', '--full', dest='full', action='store_true', |
---|
87 | default=False, |
---|
88 | help='full build (clean, update, configure)') |
---|
89 | parser.add_option('-t', '--test', dest='test', action='store_true', |
---|
90 | default=False, |
---|
91 | help='run tests') |
---|
92 | parser.add_option('-d', '--dry', dest='dry', action='store_true', |
---|
93 | default=False, |
---|
94 | help='dry run') |
---|
95 | |
---|
96 | ##### Add new builds/groups here ##### |
---|
97 | Build('moab', '$HOME/svn/moab', [ |
---|
98 | Step('make clean', 'clean'), |
---|
99 | Step('svn up', 'update'), |
---|
100 | Step('autoreconf -fi', 'configure'), |
---|
101 | Step('./configure --prefix=$HOME/moab --with-hdf5=' |
---|
102 | '/usr/local/hdf5serial --enable-shared', 'configure'), |
---|
103 | Step('make'), |
---|
104 | Step('make install'), |
---|
105 | Step('make check', 'test'), |
---|
106 | ]) |
---|
107 | |
---|
108 | Build('cgm', '$HOME/svn/cgm', [ |
---|
109 | Step('make clean', 'clean'), |
---|
110 | Step('svn up', 'update'), |
---|
111 | Step('autoreconf -fi', 'configure'), |
---|
112 | Step('./configure --prefix=$HOME/moab --with-cubit=/cubit/cubit10.2 ' + |
---|
113 | '--enable-shared', 'configure'), |
---|
114 | Step('make'), |
---|
115 | Step('make install'), |
---|
116 | Step('make check', 'test'), |
---|
117 | ]) |
---|
118 | |
---|
119 | Build('lasso', '$HOME/svn/lasso', [ |
---|
120 | Step('make clean', 'clean'), |
---|
121 | Step('svn up', 'update'), |
---|
122 | Step('autoreconf -fi', 'configure'), |
---|
123 | Step('./configure --prefix=$HOME/moab --with-imesh=$HOME/moab ' + |
---|
124 | '--with-igeom=$HOME/moab --enable-shared', 'configure'), |
---|
125 | Step('make'), |
---|
126 | Step('make install'), |
---|
127 | Step('make check', 'test'), |
---|
128 | ]) |
---|
129 | |
---|
130 | Build('meshkit', '$HOME/svn/meshkit', [ |
---|
131 | Step('make clean', 'clean'), |
---|
132 | Step('svn up', 'update'), |
---|
133 | Step('autoreconf -fi', 'update'), |
---|
134 | Step('./configure --prefix=$HOME/moab --with-itaps=$HOME/moab ' + |
---|
135 | '--enable-shared', 'configure'), |
---|
136 | Step('make'), |
---|
137 | Step('make install'), |
---|
138 | Step('make check', 'test'), |
---|
139 | ]) |
---|
140 | |
---|
141 | BuildGroup('itaps', ['cgm', 'moab', 'lasso']) |
---|
142 | ##### Ok, stop adding new builds/groups! ##### |
---|
143 | |
---|
144 | |
---|
145 | (options, args) = parser.parse_args() |
---|
146 | if options.full: |
---|
147 | options.update = options.clean = True |
---|
148 | if options.update: |
---|
149 | options.configure = True |
---|
150 | |
---|
151 | if len(args) == 0: |
---|
152 | print "Nothing to build!" |
---|
153 | exit(0) |
---|
154 | |
---|
155 | for build in args: |
---|
156 | if build not in build_list: |
---|
157 | print "Unknown build '%s'" % build |
---|
158 | exit(1) |
---|
159 | |
---|
160 | for build in args: |
---|
161 | status = build_list[build].run(options) |
---|
162 | if status != 0: |
---|
163 | exit(1) |
---|