-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobs
executable file
·416 lines (365 loc) · 13.3 KB
/
obs
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/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
DEFAULT_VARIANT = 'factory'
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)
if srcl.startswith('http://') or srcl.startswith('https://'):
srcl = os.path.basename(srcl)
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):
if VERBOSE:
print(" ".join(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, revision=""):
rc = 0
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')
rc = do_call(['git', 'archive', '--format=tar', '--prefix=%s/' % (prefix),
'-o', tarfile, revision or 'HEAD'])
if gitinfo:
gitinfo_file = make_gitinfo(tmpdir, prefix)
cwd = os.getcwd()
do("cd %s" % (tmpdir), os.chdir, tmpdir)
rc = rc | do_call(['tar', '-rf', tarfile, gitinfo_file])
do("cd %s" % (cwd), os.chdir, cwd)
zipcmd.append(tarfile)
rc = rc | 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']:
comp = 'tbz2'
elif ext in ['.gz', '.tgz']:
comp = 'tgz'
else:
raise IOError("Unsupported compression: " + ext)
cl = ['hg', 'archive', '-t', comp]
if revision:
cl += ['-r', revision]
cl += [where]
rc = rc | do_call(cl)
else:
raise IOError("not a git or mercurial repository: " + where)
return rc
class Commands:
def __init__(self, args):
self.args = args
if self.args.cmd not in ('install',) and len(args.args) > 0:
self.variant = VARIANTS[args.args[0]]
else:
self.variant = VARIANTS[DEFAULT_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):
if VERBOSE:
for k, v in sorted(VARIANTS.items(), key=lambda v: v[0]):
print '[%s]' % (k)
for kk, vv in v.iteritems():
print "%15s = %s" % (kk, vv)
print
else:
print '\n'.join(sorted(VARIANTS.keys()))
def archive(self):
self._setup_projname()
d = os.path.join(self.buildservice, self.projname)
self._checkdir(d)
return mkarchive(d, self.projname,
gitinfo=self.args.gitinfo, revision=self.args.revision)
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)]
rc = do_call(cl)
if rc == 0 and self.args.checkin:
cl = self.cmd.split()
cl += ['ci', '-m', self.args.msg or '"new version"']
rc = do_call(cl)
return rc
def run(self):
self.clean()
self.archive()
self._cd_prj()
return self.build()
def test(self):
self.args.tests = True
self.clean()
self.archive()
self._cd_prj()
return 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 install(self):
args = self.args.args
if len(args) == 0:
print("Expected <package> or <repo>/<platform> [<package> ...]")
return 1
elif len(args) == 1:
return do_call(['sudo', 'zypper', 'in', args[0]])
repo, platform = args[0].rsplit('/', 1)
packages = args[1:]
bu = re.compile("^baseurl=(.*)$")
import glob
urls = []
for fn in glob.glob('/etc/zypp/repos.d/*.repo'):
for line in (bu.match(l.strip()) for l in open(fn)):
if line:
urls.append(line.group(1))
repoobs = '%s/%s' % (repo, platform)
repourl = '/%s/%s/' % (repo.replace(':', ':/'), platform)
for u in urls:
if u.find(repourl) >= 0 or u.find(repoobs) >= 0:
return do_call(['sudo', 'zypper', 'in', '-r', u] + packages)
if do_call(['sudo', 'zypper', 'ar', '-c', 'obs://'+repoobs, repo]) != 0:
return 1
if do_call(['sudo', 'zypper', 'ref', repo]) != 0:
return 1
return do_call(['sudo', 'zypper', 'in', '-r', repo] + packages)
def fuzzy_get(items, s):
"""
Finds s in items using a fuzzy
matching algorithm:
1. if exact match, return value
2. if unique prefix, return value
3. if unique prefix substring, return value
"""
if s in items:
return s
import re
def fuzzy_match(rx):
matcher = re.compile(rx, re.I)
matches = [m for m in items if matcher.match(m)]
if len(matches) == 1:
return matches[0]
return None
# prefix match
m = fuzzy_match(s + '.*')
if m:
return m
# substring match
m = fuzzy_match('.*'.join(s) + '.*')
if m:
return m
return None
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('-r', '--revision', dest='revision',
help="Archive this revision instead of HEAD/tip")
parser.add_argument('cmd', metavar='cmd', type=str, default="",
help='|'.join(ls_commands()))
parser.add_argument('args', metavar='args', nargs='*', help='command arguments')
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()
return 0
cmd = fuzzy_get(ls_commands(), args.cmd)
if cmd is not None:
return getattr(commands, cmd)()
else:
print >>sys.stderr, "Unknown command ", args.cmd
parser.print_help()
return 1
except Exception, e:
if VERBOSE:
import traceback
traceback.print_exc()
print >>sys.stderr, "Error:", e
return 1
if __name__ == "__main__":
rc = main() or 0
sys.exit(rc)
# vim:et:ts=4: