forked from krig/obs-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobs
More file actions
executable file
·331 lines (287 loc) · 10.7 KB
/
obs
File metadata and controls
executable file
·331 lines (287 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
#
# helper utility for using the open build service
# to build test packages from a source repository
# makes many assumptions
import os
import re
import sys
import subprocess
import argparse
import tempfile
import atexit
import shutil
from ConfigParser import RawConfigParser
VARIANTS = {}
VERBOSE = False
DRYRUN = False
variant_defaults = {
"cmd": "osc",
"arch": "x86_64",
"bsbase": "$HOME/build-service",
"buildroot": "/srv/buildroot",
"root": "%(buildroot)s/%(repo)s",
"buildservice": "%(bsbase)s/obs/%(branch)s",
"github_fork_pfx": "krig:"
}
def resolve_archive_name(projdir, projname):
specfilename = os.path.join(projdir, "%s.spec" % (projname))
if os.path.isfile(specfilename):
ver_re = re.compile(r"Version:\s*(.+)")
src_re = re.compile(r"Source[0]?:\s*(.+)")
lines = open(specfilename).readlines()
verl = [ver_re.match(l).group(1).strip() for l in lines if ver_re.match(l)]
srcl = [src_re.match(l).group(1).strip() for l in lines if src_re.match(l)]
if verl and srcl:
verl = verl[0]
srcl = srcl[0]
srcl = srcl.replace("%{name}", projname)
srcl = srcl.replace("%{version}", verl)
return srcl
else:
return "%s.tar.bz2" % (projname)
def init_variants(obsbranch):
parser = RawConfigParser()
parser.read([os.path.expanduser('~/.obs.conf'),
os.path.expanduser('~/.config/obs-scripts/obs.conf'),
os.path.expanduser('~/.local/config/obs.conf'),
os.path.expanduser('~/bin/obs.conf')])
for section in parser.sections():
variant = {}
variant.update(variant_defaults)
if parser.has_section("defaults"):
variant.update(parser.items("defaults"))
variant.update(parser.items(section))
if obsbranch:
variant["branch"] = "home:%s:branches:%s" % \
(obs_username(variant["cmd"]), variant["branch"])
for k, v in variant.iteritems():
variant[k] = os.path.expandvars(v % variant)
VARIANTS[section] = variant
def do(name, cmd, *args):
if VERBOSE:
print name
if not DRYRUN:
return cmd(*args)
return True
def do_call(cmd):
return do(" ".join(cmd), subprocess.call, cmd)
def bsname(bdir, drop_pfx = None):
here = os.path.basename(os.getcwd())
if drop_pfx and here.startswith(drop_pfx):
here = here.replace(drop_pfx, "")
out = "pacemaker"
if here == "glue":
out = "cluster-glue"
elif here == "crmsh-dev":
out = "crmsh"
elif os.path.isdir(os.path.join(bdir, here)):
out = here
else:
out = here
#return "pacemaker"
if VERBOSE:
print "bsname:", out
return out
def get_stdout(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
outp = p.communicate()[0]
return outp.strip()
# perhaps load from/save to disk
whois_cache = {}
def obs_username(cmd):
try:
id = whois_cache[cmd]
except:
whois = get_stdout("%s whois" % cmd)
id = whois[0:whois.index(':')]
whois_cache[cmd] = id
return id
def pipe_to(cmd, filename):
def piper():
p2 = open(filename, 'w')
p1 = subprocess.Popen(cmd, stdout=p2)
p1.communicate()
p2.close()
do(" ".join(cmd) + " > " + filename, piper)
def make_gitinfo(tmpdir, prefix):
if VERBOSE:
print "mkdir %s" % (os.path.join(tmpdir, prefix))
if not DRYRUN:
os.mkdir(os.path.join(tmpdir, prefix))
filename = os.path.join(tmpdir, prefix, '.git_info')
pipe_to(['git', 'describe', '--tags', '--always'],
filename)
return os.path.join(prefix, '.git_info')
def mkarchive(projdir, projname, gitinfo=False):
archivename = resolve_archive_name(projdir, projname)
where = os.path.join(projdir, archivename)
prefix, ext = os.path.splitext(archivename)
if prefix.lower().endswith('.tar'):
prefix = os.path.splitext(prefix)[0]
if os.path.isdir('.git'):
if ext in ['.bz2', '.tbz']:
zipcmd = ['bzip2']
elif ext in ['.xz']:
zipcmd = ['xz']
elif ext in ['.gz', '.tgz']:
zipcmd = ['gzip']
else:
raise IOError("Unsupported compression: " + ext)
tmpdir = '<tmpdir>'
if not DRYRUN:
tmpdir = tempfile.mkdtemp()
atexit.register(lambda: shutil.rmtree(tmpdir, ignore_errors=True))
tarfile = os.path.join(tmpdir, prefix + '.tar')
do_call(['git', 'archive', '--format=tar', '--prefix=%s/' % (prefix),
'-o', tarfile, 'HEAD'])
if gitinfo:
gitinfo_file = make_gitinfo(tmpdir, prefix)
cwd = os.getcwd()
do("cd %s" % (tmpdir), os.chdir, tmpdir)
do_call(['tar', '-rf', tarfile, gitinfo_file])
do("cd %s" % (cwd), os.chdir, cwd)
zipcmd.append(tarfile)
do_call(zipcmd)
if VERBOSE:
print "cp %s %s" % (tarfile + ext, where)
if not DRYRUN:
shutil.copyfile(tarfile + ext, where)
elif os.path.isdir('.hg'):
if ext in ['.bz2', '.tbz']:
do_call(['hg', 'archive', '-t', 'tbz2', where])
elif ext in ['.gz', '.tgz']:
do_call(['hg', 'archive', '-t', 'tgz', where])
else:
raise IOError("Unsupported compression: " + ext)
else:
raise IOError("not a git or mercurial repository: " + where)
class Commands:
def __init__(self, args):
self.args = args
self.variant = VARIANTS[args.variant]
for k, v in self.variant.iteritems():
setattr(self, k, v)
if args.projname:
self.projname = args.projname
def _setup_projname(self):
if not hasattr(self, 'projname'):
self._checkdir(self.buildservice)
self.projname = bsname(self.buildservice, self.github_fork_pfx)
def _checkdir(self, d):
if not os.path.isdir(d):
raise IOError("Not a directory: " + d)
def variants(self):
for k, v in VARIANTS.iteritems():
print '[%s]' % (k)
for kk, vv in v.iteritems():
print "%15s = %s" % (kk, vv)
print
def archive(self):
self._setup_projname()
d = os.path.join(self.buildservice, self.projname)
self._checkdir(d)
mkarchive(d, self.projname, gitinfo=self.args.gitinfo)
def _cd_prj(self):
self._setup_projname()
path = os.path.join(self.buildservice, self.projname)
self._checkdir(path)
if VERBOSE:
print "cd", path
if not DRYRUN:
os.chdir(path)
def build(self):
self._setup_projname()
self._create_root()
cl = self.cmd.split()
cl += ['build', '-d', '--no-verify', '--release=1']
if self.args.localpkg:
cl += ['--local-package']
if self.args.offline:
cl += ['--offline']
if self.args.tests:
cl += ['--define', "with_regression_tests 1"]
cl += ['--root='+self.root]
cl += [self.repo, self.arch]
cl += ["%s.spec" % (self.projname)]
if do_call(cl) == 0 and self.args.checkin:
cl = self.cmd.split()
cl += ['ci', '-m', self.args.msg or '"new version"']
do_call(cl)
def run(self):
self.clean()
self.archive()
self._cd_prj()
self.build()
def test(self):
self.args.tests = True
self.clean()
self.archive()
self._cd_prj()
self.build()
def help(self):
pass
def _create_root(self):
if not os.path.isdir(self.root):
do_call(['sudo', 'mkdir', '-p', self.root])
def clean(self):
if self.args.tests:
fn = '%s/tmp/.crmsh_regression_tests_ran' % (self.root)
if os.path.isfile(fn):
do_call(['sudo', 'rm', fn])
def main():
def ls_commands():
return [x for x in sorted(dir(Commands)) if not x.startswith('_')]
parser = argparse.ArgumentParser(
description="Helper utility for the open build service",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-t', '--tests', dest='tests', action='store_true',
help="Enable regression tests")
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help="Verbose output")
parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
help='only print result of command')
parser.add_argument('--offline', dest='offline', action='store_true',
help='Start with cached prjconf and packages without contacting the api server')
parser.add_argument('-d', '--dir', metavar='dir', type=str, default=".",
help='source directory')
parser.add_argument('-g', '--add-git-info', dest='gitinfo', action='store_true',
help='Create a .git_info file and add it to the generated archive')
parser.add_argument('--local-package', dest='localpkg', action='store_true',
help='Pass flag --local-package to osc')
parser.add_argument('-p', '--projname', dest='projname',
help='Override project name')
parser.add_argument('-b', '--branch', dest='obsbranch', action='store_true',
help="Work on the OBS branch (home:USERNAME:branches)")
parser.add_argument('-c', '--checkin', dest='checkin', action='store_true',
help="On successful build, checkin to the OBS server")
parser.add_argument('-m', '--message', dest='msg',
help="Use this message for checkin to the OBS server")
parser.add_argument('cmd', metavar='cmd', type=str, default="",
help='|'.join(ls_commands()))
parser.add_argument('variant', metavar='variant', nargs='?', type=str, default="factory",
help='build variant')
args = parser.parse_args()
global VERBOSE
VERBOSE = args.verbose or args.dryrun
global DRYRUN
DRYRUN = args.dryrun
init_variants(args.obsbranch)
try:
commands = Commands(args)
if args.cmd == "help":
parser.print_help()
elif args.cmd in ls_commands():
getattr(commands, args.cmd)()
else:
print >>sys.stderr, "Unknown command ", args.cmd
parser.print_help()
except Exception, e:
if VERBOSE:
import traceback
traceback.print_exc()
print >>sys.stderr, "Error:", e
sys.exit(1)
if __name__ == "__main__":
main()
# vim:et:ts=4: