forked from openSUSE/obs-service-set_version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_version
executable file
·478 lines (416 loc) · 17.4 KB
/
set_version
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/python
# -*- coding: utf-8 -*-
# A simple script to update version number in spec, dsc or arch linux files
#
# (C) 2010 by Adrian Schröter <[email protected]>
# (C) 2015 by Thomas Bechtold <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.
from __future__ import print_function
import argparse
import glob
import os
import re
import shutil
import sys
import tarfile
import zipfile
import codecs
import logging
try:
from packaging.version import LegacyVersion, Version, parse
except ImportError:
HAS_PACKAGING = False
import warnings
warnings.warn("install 'packaging' to improve python package versions",
RuntimeWarning)
else:
HAS_PACKAGING = True
if os.environ.get('DEBUG_SET_VERSION') == "1":
logging.getLogger().setLevel(logging.DEBUG)
outdir = None
suffixes = ('.obscpio', '.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2',
'.tar.xz', '.tar.zst', '.zip')
suffixes_re = "|".join(map(lambda x: re.escape(x), suffixes))
def _get_local_files():
""" sorted local file list by modification time (newest first)"""
files = glob.glob('*')
files.sort(key=lambda x: os.stat(os.path.join(os.getcwd(), x)).st_mtime,
reverse=True)
return files
class VersionDetector(object):
def __init__(self, regex=None, file_list=(), basename='',
versionfile=None):
self.regex = regex
self.file_list = file_list
self.basename = basename
self.versionfile = versionfile
def autodetect(self):
logging.debug("Starting version autodetect")
logging.debug("-- Starting version detection via specified file")
version = self._get_version_via_versionfile()
if not version:
logging.debug("--- Could not find version via specified file")
logging.debug("-- Starting version detection via obsinfo")
version = self._get_version_via_obsinfo()
if not version:
logging.debug("--- Could not find version via obsinfo")
logging.debug("-- Starting version detection via archive dirname")
version = self._get_version_via_archive_dirname()
if not version:
logging.debug("--- Could not find version via archive dirname")
logging.debug("-- Starting version detection via filename")
version = self._get_version_via_filename()
if not version:
logging.debug("--- Could not find version via filename")
logging.debug("-- Starting version detection via debian changelog")
version = self.get_version_via_debian_changelog("debian.changelog")
if not version:
logging.debug("--- Could not find version via debian changelog")
return version
def _get_version_via_filename(self):
""" detect version based on file names"""
logging.debug("detecting version via files")
for f in self.file_list:
logging.debug(" - checking file %s", f)
if self.regex:
logging.debug(" - using regex: %r", self.regex)
regex = self.regex
else:
regex = r"^%s.*[-_]([\d].*)(?:%s)$" % (
re.escape(self.basename),
suffixes_re)
m = re.match(regex, f)
if m:
return m.group(1)
# Nothing found
return None
def _get_version_via_versionfile(self):
""" detect version based on custom file contents"""
logging.debug("detecting version via custom file")
if not self.versionfile:
logging.debug("Custom file name not set")
return None
logging.debug(" - checking file '%s'", self.versionfile)
if self.regex:
regex = self.regex
else:
regex = r"^[Vv]ersion:\s+([\d].*)(?:)\s?$"
logging.debug(" - using regex: %r", regex)
if not os.path.exists(self.versionfile):
logging.debug(" - file: %s does not exist", self.versionfile)
raise OSError(os.errno.ENOENT, os.strerror(os.errno.ENOENT),
self.versionfile)
with codecs.open(self.versionfile, 'r', 'utf8') as fp:
for line in fp:
m = re.match(regex, line)
if m:
return m.group(1)
return None
def __get_version(self, str_list):
if self.regex:
regex = self.regex
else:
regex = r"%s.*[-_]([\d][^\/]*).*" % self.basename
for s in str_list:
m = re.match(regex, s)
if m:
return m.group(1)
# Nothing found
return None
def _get_version_via_archive_dirname(self):
""" detect version based tar'd directory name"""
for f in filter(lambda x: x.endswith(suffixes), self.file_list):
logging.debug("Checking path: '%s'.", f)
if not os.path.isfile(f):
logging.debug("Skipping path: '%s' is not a regular file.", f)
continue
# handle tarfiles
if tarfile.is_tarfile(f):
with tarfile.open(f) as tf:
v = self.__get_version(tf.getnames())
if v:
return v
# handle zipfiles
if zipfile.is_zipfile(f):
try:
with zipfile.ZipFile(f, 'r') as zf:
v = self.__get_version(zf.namelist())
if v:
return v
# is_zipfile has often false positives and module is
# crashing on processing
except OSError:
pass
except IOError:
pass
# Nothing found
return None
def _get_version_via_obsinfo(self):
for fname in filter(lambda x: x.startswith(self.basename) and
x.endswith(".obsinfo"), self.file_list):
if os.path.exists(fname):
with codecs.open(fname, 'r', 'utf8') as fp:
for line in fp:
if line.startswith("version: "):
string = line[9:]
string = string.rstrip()
return string
# Nothing found
return None
@staticmethod
def get_version_via_debian_changelog(filename):
# from http://anonscm.debian.org/cgit/pkg-python-debian/\
# python-debian.git/tree/lib/debian/changelog.py
topline = re.compile(r'^(\w%(name_chars)s*) \(([^\(\) \t]+)\)'
r'((\s+%(name_chars)s+)+)\;'
% {'name_chars': '[-+0-9a-z.]'},
re.IGNORECASE)
if os.path.exists(filename):
with codecs.open(filename, 'r', 'utf8') as f:
firstline = f.readline()
topmatch = topline.match(firstline)
if topmatch:
return topmatch.group(2)
# Nothing found
return None
@staticmethod
def _get_version_via_debian_dsc(filename):
version = re.compile(r'^Version:([ \t\f\v]*)[^%\n\r]*', re.IGNORECASE)
if os.path.exists(filename):
with codecs.open(filename, 'r', 'utf8') as f:
for line in f:
versionmatch = version.match(line)
if versionmatch:
return versionmatch.group(0)
# Nothing found
return None
class PackageTypeDetector(object):
# pylint: disable=too-few-public-methods
@staticmethod
def _get_package_type(files):
pt_found = False
for f in filter(lambda x: x.endswith(suffixes), files):
pt_found = PackageTypeDetector._is_python(f)
if pt_found:
return "python"
# no package type found
return None
@staticmethod
def _is_python(f):
names = []
if not os.path.isfile(f):
logging.debug("Skipping path: '%s' is not a regular file.", f)
return False
if tarfile.is_tarfile(f):
with tarfile.open(f) as tf:
names = tf.getnames()
if zipfile.is_zipfile(f):
try:
with zipfile.ZipFile(f, 'r') as zf:
names = zf.namelist()
except OSError:
# is_zipfile has often false positives and module is
# crashing on processing
pass
for n in map(lambda x: os.path.normpath(x), names):
if n.endswith("egg-info/PKG-INFO"):
return True
return False
def _replace_define(filename, def_name, def_value, add_if_missing=True):
# first, modify a copy of filename and then move it
with codecs.open(filename, 'r+', 'utf8') as f:
contents = f.read()
f.seek(0)
contents_new, subs = re.subn(
r'^%define {def_name}(\s*)[^%].*'.format(
def_name=def_name),
r'%define {def_name}\g<1>{def_value}'.format(
def_name=def_name, def_value=def_value),
contents, flags=re.MULTILINE)
if subs == 0 and add_if_missing:
# seems there was no define. add new one before 'Name:'
contents_new, subs = re.subn(
r'^(Name:.*)$',
r'%define {def_name} {def_value}\n\n\g<1>'.format(
def_name=def_name, def_value=def_value),
contents, flags=re.MULTILINE)
f.truncate()
f.write(contents_new)
def _replace_spec_setup(filename, version_define):
# first, modify a copy of filename and then move it
with codecs.open(filename, 'r+', 'utf8') as f:
contents = f.read()
f.seek(0)
# %setup without "-n" uses implicit "-n" as "%{name}-%{version}"
contents_new, subs = re.subn(
r'^%setup\s*((?:-q)?)?\s*$',
r'%setup \1 -n %{{name}}-%{{{version_define}}}'.format(
version_define=version_define),
contents, flags=re.MULTILINE)
if subs == 0:
# keep inline macros for rpm
contents_new, subs = re.subn(
r'^%setup(.*)%{version}(.*)$',
r'%setup\g<1>%{{{version_define}}}\g<2>'.format(
version_define=version_define),
contents, flags=re.MULTILINE)
if subs > 0:
f.truncate()
f.write(contents_new)
def _replace_tag(filename, tag, string):
# first, modify a copy of filename and then move it
with codecs.open(filename, 'r+', 'utf8') as f:
contents = f.read()
f.seek(0)
if filename.endswith("PKGBUILD") or filename.endswith("build.collax"):
contents_new, subs = re.subn(
r"^{tag}=.*".format(tag=tag),
r"{tag}={string}".format(tag=tag, string=string), contents,
flags=re.MULTILINE)
else:
# keep inline macros for rpm
contents_new, subs = re.subn(
r'^{tag}:([ \t\f\v]*)[^%\n\r]*'.format(tag=tag),
r'{tag}:\g<1>{string}'.format(
tag=tag, string=string),
contents, flags=re.MULTILINE)
if subs > 0:
f.truncate()
f.write(contents_new)
def _replace_debian_changelog_version(fname, version_new):
# first, modify a copy of filename and then move it
# get current version
version_current = VersionDetector.get_version_via_debian_changelog(fname)
with codecs.open(fname, 'r+', 'utf8') as f:
content_lines = f.readlines()
f.seek(0)
content_lines[0] = content_lines[0].replace(
version_current, version_new, 1)
f.truncate()
f.writelines(content_lines)
def _version_python_pip2rpm(version_pip):
"""generate a rpm compatible version from a python pip version"""
version_rpm = version_pip
if not HAS_PACKAGING:
return version_rpm
v = parse(version_pip)
if isinstance(v, Version):
if v.is_prerelease:
v_rpm = v.public
# we need to add the 'x' in front of alpha/beta release because
# in the python world, "1.1a10" > "1.1.dev10"
# but in the rpm world, "1.1~a10" < "1.1~dev10"
v_rpm = v_rpm.replace('a', '~xalpha')
v_rpm = v_rpm.replace('b', '~xbeta')
v_rpm = v_rpm.replace('rc', '~xrc')
v_rpm = v_rpm.replace('.dev', '~dev')
version_rpm = v_rpm
elif isinstance(v, LegacyVersion):
# TODO(toabctl): handle setuptools style legacy version
pass
return version_rpm
def _version_detect(args, files_local):
vdetect = VersionDetector(args['regex'], files_local, args["basename"],
args["fromfile"])
ver = vdetect.autodetect()
logging.debug("Found version '%s'", ver)
return ver
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Open Build Service source service "set_version".'
'Used to update build description files with a '
'detected or given version number.')
parser.add_argument('--outdir', required=True,
help='output directory for modified sources')
parser.add_argument('--version',
help='use given version string, do not detect it '
'from source files')
parser.add_argument('--basename', default="",
help='detect version based on the file name with '
'a given prefix')
parser.add_argument('--file', action='append',
help='modify only this build description. '
'maybe used multiple times.')
parser.add_argument('--debug', default=False,
help='Enable more verbose output.')
parser.add_argument('--regex',
help='regex to be used by autodetect')
parser.add_argument('--fromfile',
help='detect version based on the '
'file contents and regex')
args = vars(parser.parse_args())
version = args['version']
outdir = args['outdir']
if not outdir:
print("no outdir specified")
sys.exit(-1)
if args['debug']:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug("Running in debug mode")
files_local = _get_local_files()
if not version:
try:
version = _version_detect(args, files_local)
except Exception as e:
print("Detection failed with error: \"", e, "\".")
sys.exit(-1)
if not version:
print("unable to detect the version")
sys.exit(-1)
# if no files explicitly specified process whole directory
files = args['file'] or files_local
# do version convertion if needed
pack_type = PackageTypeDetector._get_package_type(files)
version_converted = None
if pack_type == "python":
version_converted = _version_python_pip2rpm(version)
# handle rpm specs
for f in filter(lambda x: x.endswith(".spec"), files):
filename = outdir + "/" + f
shutil.copyfile(f, filename)
_replace_define(filename, "version_unconverted", version,
add_if_missing=False)
if version_converted and version_converted != version:
_replace_define(filename, "version_unconverted", version)
_replace_tag(filename, 'Version', version_converted)
_replace_spec_setup(filename, "version_unconverted")
else:
_replace_tag(filename, 'Version', version)
_replace_tag(filename, 'Release', "0")
# handle debian packages
# append -0 only for non-native packages, otherwise native packages
# will be half-converted to non-native and break dpkg-buildpackage
for f in filter(lambda x: x.endswith(".dsc"), files):
filename = outdir + "/" + f
shutil.copyfile(f, filename)
if "-" in VersionDetector._get_version_via_debian_dsc(filename):
_replace_tag(filename, 'Version', version + "-0")
else:
_replace_tag(filename, 'Version', version)
for f in filter(lambda x: x.endswith(("debian.changelog")), files):
filename = outdir + "/" + f
shutil.copyfile(f, filename)
if "-" in VersionDetector.get_version_via_debian_changelog(filename):
_replace_debian_changelog_version(filename, version + "-0")
else:
_replace_debian_changelog_version(filename, version)
# handle build.collax recipes
for f in filter(lambda x: x.endswith(("build.collax")), files):
filename = outdir + "/" + f
shutil.copyfile(f, filename)
_replace_tag(filename, "version", version)
_replace_tag(filename, "build", "0")
# handle arch linux PKGBUILD files
# TODO: Handle the md5sums generation!
for f in filter(lambda x: x.endswith(("PKGBUILD")), files):
filename = outdir + "/" + f
shutil.copyfile(f, filename)
_replace_tag(filename, "md5sums", "('SKIP')")
_replace_tag(filename, "sha256sums", "('SKIP')")
_replace_tag(filename, "pkgver", version)
_replace_tag(filename, "pkgrel", "0")