1 |
#!/usr/bin/python |
#!/usr/bin/python |
2 |
|
|
3 |
|
# A lot of the code comes from ftpadmin, see |
4 |
|
# http://git.gnome.org/browse/sysadmin-bin/tree/ftpadmin |
5 |
|
# Written by Olav Vitters |
6 |
|
|
7 |
|
# basic modules: |
8 |
import os |
import os |
9 |
import os.path |
import os.path |
10 |
import sys |
import sys |
11 |
import re |
import re |
12 |
import subprocess |
import subprocess |
13 |
import urllib2 |
|
14 |
import urlparse |
# command line parsing, error handling: |
15 |
import argparse |
import argparse |
16 |
import errno |
import errno |
17 |
|
|
18 |
|
# overwriting files by moving them (safer): |
19 |
import tempfile |
import tempfile |
20 |
import shutil |
import shutil |
21 |
|
|
22 |
|
# version comparison: |
23 |
|
import rpm |
24 |
|
|
25 |
|
# opening tarballs: |
26 |
|
import tarfile |
27 |
|
import gzip |
28 |
|
import bz2 |
29 |
|
import lzma # pyliblzma |
30 |
|
|
31 |
|
# getting links from HTML document: |
32 |
from sgmllib import SGMLParser |
from sgmllib import SGMLParser |
33 |
|
import urllib2 |
34 |
|
import urlparse |
35 |
|
|
36 |
MEDIA="Core Release Source" |
MEDIA="Core Release Source" |
37 |
URL="http://download.gnome.org/sources/" |
URL="http://download.gnome.org/sources/" |
38 |
PKGROOT='~/pkgs' |
PKGROOT='~/pkgs' |
39 |
|
|
40 |
|
re_majmin = re.compile(r'^([0-9]+\.[0-9]+).*') |
41 |
re_version = re.compile(r'([-.]|\d+|[^-.\d]+)') |
re_version = re.compile(r'([-.]|\d+|[^-.\d]+)') |
42 |
|
|
43 |
def version_cmp(a, b): |
def version_cmp(a, b): |
47 |
-1 if a < b |
-1 if a < b |
48 |
0 if a == b |
0 if a == b |
49 |
1 if a > b |
1 if a > b |
50 |
|
""" |
51 |
|
|
52 |
Logic from Bugzilla::Install::Util::vers_cmp""" |
return rpm.labelCompare(('1', a, '1'), ('1', b, '1')) |
|
A = re_version.findall(a.lstrip('0')) |
|
|
B = re_version.findall(b.lstrip('0')) |
|
|
|
|
|
while A and B: |
|
|
a = A.pop(0) |
|
|
b = B.pop(0) |
|
|
|
|
|
if a == b: |
|
|
continue |
|
|
elif a == '-': |
|
|
return -1 |
|
|
elif b == '-': |
|
|
return 1 |
|
|
elif a == '.': |
|
|
return -1 |
|
|
elif b == '.': |
|
|
return 1 |
|
|
elif a.isdigit() and b.isdigit(): |
|
|
c = cmp(a, b) if (a.startswith('0') or b.startswith('0')) else cmp(int(a, 10), int(b, 10)) |
|
|
if c: |
|
|
return c |
|
|
else: |
|
|
c = cmp(a.upper(), b.upper()) |
|
|
if c: |
|
|
return c |
|
|
|
|
|
return cmp(len(A), len(B)) |
|
53 |
|
|
54 |
def get_latest_version(versions, max_version=None): |
def get_latest_version(versions, max_version=None): |
55 |
"""Gets the latest version number |
"""Gets the latest version number |
63 |
latest = version |
latest = version |
64 |
return latest |
return latest |
65 |
|
|
66 |
|
def judge_version_increase(version_old, version_new): |
67 |
|
"""Judge quality of version increase: |
68 |
|
|
69 |
|
Returns a tuple containing judgement and message |
70 |
|
|
71 |
|
Judgement: |
72 |
|
Less than 0: Error |
73 |
|
0 to 4: Better not |
74 |
|
5+: Ok""" |
75 |
|
versions = (version_old, version_new) |
76 |
|
|
77 |
|
print " => ".join(versions) |
78 |
|
|
79 |
|
# First do a basic version comparison to ensure version_new is actually newer |
80 |
|
compare = version_cmp(version_new, version_old) |
81 |
|
|
82 |
|
if compare == 0: |
83 |
|
return (-2, "Already at version %s!" % (version_old)) |
84 |
|
|
85 |
|
if compare != 1: |
86 |
|
return (-3, "Version %s is older than current version %s!" % (version_new, version_old)) |
87 |
|
|
88 |
|
# Version is newer, but we don't want to see if it follows the GNOME versioning scheme |
89 |
|
majmins = [re_majmin.sub(r'\1', ver) for ver in versions if re_majmin.match(ver) is not None] |
90 |
|
|
91 |
|
if len(majmins) == 1: |
92 |
|
return (-1, "Version number scheme changes: %s" % (", ".join(versions))) |
93 |
|
|
94 |
|
if len(majmins) == 0: |
95 |
|
return (0, "Unsupported version numbers: %s" % (", ".join(versions))) |
96 |
|
|
97 |
|
# Follows GNOME versioning scheme |
98 |
|
# Meaning: x.y.z |
99 |
|
# x = major |
100 |
|
# y = minor : even if stable |
101 |
|
# z = micro |
102 |
|
|
103 |
|
# Major+minor the same? Then go ahead and upgrade! |
104 |
|
if majmins[0] == majmins[1]: |
105 |
|
# Majmin of both versions are the same, looks good! |
106 |
|
return (10, None) |
107 |
|
|
108 |
|
# More detailed analysis needed, so figure out the numbers |
109 |
|
majmin_nrs = [map(long, ver.split('.')) for ver in majmins] |
110 |
|
|
111 |
|
# Check/ensure major version number is the same |
112 |
|
if majmin_nrs[0][0] != majmin_nrs[1][0]: |
113 |
|
return (1, "Major version number increase") |
114 |
|
|
115 |
|
# Minor indicates stable/unstable |
116 |
|
devstate = (majmin_nrs[0][1] % 2 == 0, majmin_nrs[1][1] % 2 == 0) |
117 |
|
|
118 |
|
# Upgrading to unstable is weird |
119 |
|
if not devstate[1]: |
120 |
|
if devstate[0]: |
121 |
|
return (1, "Stable to unstable increase") |
122 |
|
|
123 |
|
return (4, "Unstable to unstable version increase") |
124 |
|
|
125 |
|
# Unstable => stable is always ok |
126 |
|
if not devstate[0]: |
127 |
|
return (5, "Unstable to stable") |
128 |
|
|
129 |
|
# Can only be increase of minors from one stable to the next |
130 |
|
return (6, "Stable version increase") |
131 |
|
|
132 |
def line_input (file): |
def line_input (file): |
133 |
for line in file: |
for line in file: |
134 |
if line[-1] == '\n': |
if line[-1] == '\n': |
170 |
if href: |
if href: |
171 |
self.urls.extend(href) |
self.urls.extend(href) |
172 |
|
|
173 |
|
class XzTarFile(tarfile.TarFile): |
174 |
|
|
175 |
|
OPEN_METH = tarfile.TarFile.OPEN_METH.copy() |
176 |
|
OPEN_METH["xz"] = "xzopen" |
177 |
|
|
178 |
|
@classmethod |
179 |
|
def xzopen(cls, name, mode="r", fileobj=None, **kwargs): |
180 |
|
"""Open gzip compressed tar archive name for reading or writing. |
181 |
|
Appending is not allowed. |
182 |
|
""" |
183 |
|
if len(mode) > 1 or mode not in "rw": |
184 |
|
raise ValueError("mode must be 'r' or 'w'") |
185 |
|
|
186 |
|
if fileobj is not None: |
187 |
|
fileobj = _LMZAProxy(fileobj, mode) |
188 |
|
else: |
189 |
|
fileobj = lzma.LZMAFile(name, mode) |
190 |
|
|
191 |
|
try: |
192 |
|
# lzma doesn't immediately return an error |
193 |
|
# try and read a bit of data to determine if it is a valid xz file |
194 |
|
fileobj.read(_LZMAProxy.blocksize) |
195 |
|
fileobj.seek(0) |
196 |
|
t = cls.taropen(name, mode, fileobj, **kwargs) |
197 |
|
except IOError: |
198 |
|
raise tarfile.ReadError("not a xz file") |
199 |
|
except lzma.error: |
200 |
|
raise tarfile.ReadError("not a xz file") |
201 |
|
t._extfileobj = False |
202 |
|
return t |
203 |
|
|
204 |
|
if not hasattr(tarfile.TarFile, 'xzopen'): |
205 |
|
tarfile.open = XzTarFile.open |
206 |
|
|
207 |
class SpecFile(object): |
class SpecFile(object): |
208 |
re_update_version = re.compile(r'^(?P<pre>Version:\s*)(?P<version>.+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE) |
re_update_version = re.compile(r'^(?P<pre>Version:\s*)(?P<version>.+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE) |
209 |
re_update_release = re.compile(r'^(?P<pre>Release:\s*)(?P<release>%mkrel \d+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE) |
re_update_release = re.compile(r'^(?P<pre>Release:\s*)(?P<release>%mkrel \d+)(?P<post>\s*)$', re.MULTILINE + re.IGNORECASE) |
220 |
"""Update specfile (increase version)""" |
"""Update specfile (increase version)""" |
221 |
cur_version = self.version |
cur_version = self.version |
222 |
|
|
223 |
compare = version_cmp(version, cur_version) |
(judgement, msg) = judge_version_increase(cur_version, version) |
224 |
|
|
225 |
if compare == 0: |
if judgement < 0: |
226 |
print >>sys.stderr, "ERROR: Already at version %s!" % (cur_version) |
print >>sys.stderr, "ERROR: %s!" % (msg) |
227 |
return False |
return False |
228 |
|
|
229 |
if compare != 1: |
if judgement < 5: |
230 |
print >>sys.stderr, "ERROR: Version %s is older than current version %s!" % (version, cur_version) |
print "WARNING: %s!" % (msg) |
231 |
return False |
return False |
232 |
|
|
233 |
# XXX - os.path.join is hackish |
# XXX - os.path.join is hackish |
256 |
write_file(self.path, data) |
write_file(self.path, data) |
257 |
|
|
258 |
|
|
259 |
# Check RPM also agrees that version number has increased |
# Verify that RPM also agrees that version number has changed |
260 |
if self.version != version: |
if self.version != version: |
261 |
print "ERROR: Increased version to %s, but RPM doesn't agree!?!" % version |
print "ERROR: Increased version to %s, but RPM doesn't agree!?!" % version |
262 |
return False |
return False |
393 |
@property |
@property |
394 |
def svn_author(self): |
def svn_author(self): |
395 |
if not hasattr(self, '_svn_author'): |
if not hasattr(self, '_svn_author'): |
396 |
p = subprocess.Popen(['svn', 'log', '-q', "--", self.path], stdout=subprocess.PIPE, close_fds=True) |
try: |
397 |
contents = p.stdout.read().strip("\n").splitlines() |
contents = subprocess.check_output(['svn', 'log', '-q', "--", self.path], close_fds=True).strip("\n").splitlines() |
398 |
ecode = p.wait() |
|
|
if ecode == 0: |
|
399 |
for line in contents: |
for line in contents: |
400 |
if ' | ' not in line: |
if ' | ' not in line: |
401 |
continue |
continue |
403 |
fields = line.split(' | ') |
fields = line.split(' | ') |
404 |
if len(fields) >= 3: |
if len(fields) >= 3: |
405 |
self._svn_author = fields[1] |
self._svn_author = fields[1] |
406 |
|
except subprocess.CalledProcessError: |
407 |
|
pass |
408 |
|
|
409 |
if not hasattr(self, '_svn_author'): |
if not hasattr(self, '_svn_author'): |
410 |
return None |
return None |
431 |
def get_downstream_names(): |
def get_downstream_names(): |
432 |
re_file = re.compile(r'^(?P<module>.*?)[_-](?:(?P<oldversion>([0-9]+[\.])*[0-9]+)-)?(?P<version>([0-9]+[\.\-])*[0-9]+)\.(?P<format>(?:tar\.|diff\.)?[a-z][a-z0-9]*)$') |
re_file = re.compile(r'^(?P<module>.*?)[_-](?:(?P<oldversion>([0-9]+[\.])*[0-9]+)-)?(?P<version>([0-9]+[\.\-])*[0-9]+)\.(?P<format>(?:tar\.|diff\.)?[a-z][a-z0-9]*)$') |
433 |
|
|
434 |
p = subprocess.Popen(['urpmf', '--files', '.', "--media", MEDIA], stdout=subprocess.PIPE, close_fds=True) |
contents = subprocess.check_output(['urpmf', '--files', '.', "--media", MEDIA], close_fds=True).strip("\n").splitlines() |
|
contents = p.stdout.read().strip("\n").splitlines() |
|
|
ecode = p.wait() |
|
|
if ecode != 0: |
|
|
sys.exit(1) |
|
435 |
|
|
436 |
FILES = {} |
FILES = {} |
437 |
TARBALLS = {} |
TARBALLS = {} |
484 |
|
|
485 |
matches = upstream & set(downstream.keys()) |
matches = upstream & set(downstream.keys()) |
486 |
for module in matches: |
for module in matches: |
487 |
print "\n".join(downstream[module]) |
print "\n".join(sorted(downstream[module])) |
488 |
|
|
489 |
def cmd_patches(options, parser): |
def cmd_patches(options, parser): |
490 |
upstream = get_upstream_names() |
upstream = get_upstream_names() |
514 |
p.add_dep3() |
p.add_dep3() |
515 |
|
|
516 |
def cmd_package_new_version(options, parser): |
def cmd_package_new_version(options, parser): |
517 |
package = options.package |
# Determine the package name |
518 |
|
if options.upstream: |
519 |
|
downstream, downstream_files = get_downstream_names() |
520 |
|
|
521 |
|
if options.package not in downstream: |
522 |
|
print >>sys.stderr, "ERROR: No packages for upstream name: %s" % options.package |
523 |
|
sys.exit(1) |
524 |
|
|
525 |
|
if len(downstream[options.package]) != 1: |
526 |
|
# XXX - Make it more intelligent |
527 |
|
print >>sys.stderr, "ERROR: Multiple packages found for %s: %s" % (options.package, ", ".join(downstream[options.package])) |
528 |
|
sys.exit(1) |
529 |
|
|
530 |
|
package = list(downstream[options.package])[0] |
531 |
|
else: |
532 |
|
package = options.package |
533 |
|
|
534 |
|
# Directories packages are located in |
535 |
root = os.path.expanduser(PKGROOT) |
root = os.path.expanduser(PKGROOT) |
536 |
cwd = os.path.join(root, package) |
cwd = os.path.join(root, package) |
537 |
|
|
538 |
|
# Checkout package to ensure the checkout reflects the latest changes |
539 |
try: |
try: |
540 |
subprocess.check_call(['mgarepo', 'co', package], cwd=root) |
subprocess.check_call(['mgarepo', 'co', package], cwd=root) |
541 |
except subprocess.CalledProcessError: |
except subprocess.CalledProcessError: |
542 |
sys.exit(1) |
sys.exit(1) |
543 |
|
|
544 |
|
# SpecFile class handles the actual version+release change |
545 |
s = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package)) |
s = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package)) |
546 |
print "%s => %s" % (s.version, options.version) |
print "%s => %s" % (s.version, options.version) |
547 |
if not s.update(options.version): |
if not s.update(options.version): |
548 |
sys.exit(1) |
sys.exit(1) |
549 |
|
|
550 |
|
# We can even checkin and submit :-) |
551 |
if options.submit: |
if options.submit: |
552 |
try: |
try: |
553 |
# checkin changes |
# checkin changes |
554 |
subprocess.check_call(['mgarepo', 'ci', '-m', 'new version'], cwd=cwd) |
subprocess.check_call(['mgarepo', 'ci', '-m', 'new version %s' % options.version], cwd=cwd) |
555 |
# and submit |
# and submit |
556 |
subprocess.check_call(['mgarepo', 'submit'], cwd=cwd) |
subprocess.check_call(['mgarepo', 'submit'], cwd=cwd) |
557 |
except subprocess.CalledProcessError: |
except subprocess.CalledProcessError: |
592 |
subparser = subparsers.add_parser('increase', help='Increase version number') |
subparser = subparsers.add_parser('increase', help='Increase version number') |
593 |
subparser.add_argument("package", help="Package name") |
subparser.add_argument("package", help="Package name") |
594 |
subparser.add_argument("version", help="Version number") |
subparser.add_argument("version", help="Version number") |
595 |
|
subparser.add_argument("-u", "--upstream", action="store_true", dest="upstream", |
596 |
|
help="Package name reflects the upstream name") |
597 |
subparser.add_argument("-s", "--submit", action="store_true", dest="submit", |
subparser.add_argument("-s", "--submit", action="store_true", dest="submit", |
598 |
help="Commit changes and submit") |
help="Commit changes and submit") |
599 |
subparser.set_defaults( |
subparser.set_defaults( |
600 |
func=cmd_package_new_version, submit=False |
func=cmd_package_new_version, submit=False, upstream=False |
601 |
) |
) |
602 |
|
|
603 |
if len(sys.argv) == 1: |
if len(sys.argv) == 1: |