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/" |
46 |
-1 if a < b |
-1 if a < b |
47 |
0 if a == b |
0 if a == b |
48 |
1 if a > b |
1 if a > b |
49 |
|
""" |
50 |
|
|
51 |
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)) |
|
52 |
|
|
53 |
def get_latest_version(versions, max_version=None): |
def get_latest_version(versions, max_version=None): |
54 |
"""Gets the latest version number |
"""Gets the latest version number |
62 |
latest = version |
latest = version |
63 |
return latest |
return latest |
64 |
|
|
65 |
|
def judge_version_increase(version_old, version_new): |
66 |
|
"""Judge quality of version increase: |
67 |
|
|
68 |
|
Returns a tuple containing judgement and message |
69 |
|
|
70 |
|
Judgement: |
71 |
|
Less than 0: Error |
72 |
|
0 to 4: Better not |
73 |
|
5+: Ok""" |
74 |
|
versions = (version_old, version_new) |
75 |
|
|
76 |
|
print " => ".join(versions) |
77 |
|
|
78 |
|
# First do a basic version comparison to ensure version_new is actually newer |
79 |
|
compare = version_cmp(version_new, version_old) |
80 |
|
|
81 |
|
if compare == 0: |
82 |
|
return (-2, "Already at version %s!" % (version_old)) |
83 |
|
|
84 |
|
if compare != 1: |
85 |
|
return (-3, "Version %s is older than current version %s!" % (version_new, version_old)) |
86 |
|
|
87 |
|
# Version is newer, but we don't want to see if it follows the GNOME versioning scheme |
88 |
|
majmins = [re_majmin.sub(r'\1', ver) for ver in versions if re_majmin.match(ver) is not None] |
89 |
|
|
90 |
|
if len(majmins) == 1: |
91 |
|
return (-1, "Version number scheme changes: %s" % (", ".join(versions))) |
92 |
|
|
93 |
|
if len(majmins) == 0: |
94 |
|
return (0, "Unsupported version numbers: %s" % (", ".join(versions))) |
95 |
|
|
96 |
|
# Follows GNOME versioning scheme |
97 |
|
# Meaning: x.y.z |
98 |
|
# x = major |
99 |
|
# y = minor : even if stable |
100 |
|
# z = micro |
101 |
|
|
102 |
|
# Major+minor the same? Then go ahead and upgrade! |
103 |
|
if majmins[0] == majmins[1]: |
104 |
|
# Majmin of both versions are the same, looks good! |
105 |
|
return (10, None) |
106 |
|
|
107 |
|
# More detailed analysis needed, so figure out the numbers |
108 |
|
majmin_nrs = [map(long, ver.split('.')) for ver in majmins] |
109 |
|
|
110 |
|
# Check/ensure major version number is the same |
111 |
|
if majmin_nrs[0][0] != majmin_nrs[1][0]: |
112 |
|
return (1, "Major version number increase") |
113 |
|
|
114 |
|
# Minor indicates stable/unstable |
115 |
|
devstate = (majmin_nrs[0][1] % 2 == 0, majmin_nrs[1][1] % 2 == 0) |
116 |
|
|
117 |
|
# Upgrading to unstable is weird |
118 |
|
if not devstate[1]: |
119 |
|
if devstate[0]: |
120 |
|
return (1, "Stable to unstable increase") |
121 |
|
|
122 |
|
return (4, "Unstable to unstable version increase") |
123 |
|
|
124 |
|
# Unstable => stable is always ok |
125 |
|
if not devstate[0]: |
126 |
|
return (5, "Unstable to stable") |
127 |
|
|
128 |
|
# Can only be increase of minors from one stable to the next |
129 |
|
return (6, "Stable version increase") |
130 |
|
|
131 |
def line_input (file): |
def line_input (file): |
132 |
for line in file: |
for line in file: |
133 |
if line[-1] == '\n': |
if line[-1] == '\n': |
169 |
if href: |
if href: |
170 |
self.urls.extend(href) |
self.urls.extend(href) |
171 |
|
|
172 |
|
class XzTarFile(tarfile.TarFile): |
173 |
|
|
174 |
|
OPEN_METH = tarfile.TarFile.OPEN_METH.copy() |
175 |
|
OPEN_METH["xz"] = "xzopen" |
176 |
|
|
177 |
|
@classmethod |
178 |
|
def xzopen(cls, name, mode="r", fileobj=None, **kwargs): |
179 |
|
"""Open gzip compressed tar archive name for reading or writing. |
180 |
|
Appending is not allowed. |
181 |
|
""" |
182 |
|
if len(mode) > 1 or mode not in "rw": |
183 |
|
raise ValueError("mode must be 'r' or 'w'") |
184 |
|
|
185 |
|
if fileobj is not None: |
186 |
|
fileobj = _LMZAProxy(fileobj, mode) |
187 |
|
else: |
188 |
|
fileobj = lzma.LZMAFile(name, mode) |
189 |
|
|
190 |
|
try: |
191 |
|
# lzma doesn't immediately return an error |
192 |
|
# try and read a bit of data to determine if it is a valid xz file |
193 |
|
fileobj.read(_LZMAProxy.blocksize) |
194 |
|
fileobj.seek(0) |
195 |
|
t = cls.taropen(name, mode, fileobj, **kwargs) |
196 |
|
except IOError: |
197 |
|
raise tarfile.ReadError("not a xz file") |
198 |
|
except lzma.error: |
199 |
|
raise tarfile.ReadError("not a xz file") |
200 |
|
t._extfileobj = False |
201 |
|
return t |
202 |
|
|
203 |
|
if not hasattr(tarfile.TarFile, 'xzopen'): |
204 |
|
tarfile.open = XzTarFile.open |
205 |
|
|
206 |
class SpecFile(object): |
class SpecFile(object): |
207 |
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) |
208 |
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) |
214 |
@property |
@property |
215 |
def version(self): |
def version(self): |
216 |
return subprocess.check_output(["rpm", "--specfile", self.path, "--queryformat", "%{VERSION}\n"]).splitlines()[0] |
return subprocess.check_output(["rpm", "--specfile", self.path, "--queryformat", "%{VERSION}\n"]).splitlines()[0] |
217 |
|
|
218 |
def update(self, version): |
def update(self, version): |
219 |
"""Update specfile (increase version)""" |
"""Update specfile (increase version)""" |
220 |
cur_version = self.version |
cur_version = self.version |
221 |
|
|
222 |
if version_cmp(version, cur_version) != 1: |
(judgement, msg) = judge_version_increase(cur_version, version) |
223 |
print >>sys.stderr, "ERROR: Version %s is older than current version %s!" % (version, cur_version) |
|
224 |
|
if judgement < 0: |
225 |
|
print >>sys.stderr, "ERROR: %s!" % (msg) |
226 |
|
return False |
227 |
|
|
228 |
|
if judgement < 5: |
229 |
|
print >>sys.stderr, "WARNING: %s!" % (msg) |
230 |
|
return False |
231 |
|
|
232 |
|
# XXX - os.path.join is hackish |
233 |
|
if subprocess.check_output(["svn", "diff", os.path.join(self.path, '..')]) != '': |
234 |
|
print >>sys.stderr, "ERROR: Package has uncommitted changes!" |
235 |
return False |
return False |
236 |
|
|
237 |
with open(self.path, "rw") as f: |
with open(self.path, "rw") as f: |
238 |
data = f.read() |
data = f.read() |
239 |
|
|
240 |
if data.count("%mkrel") != 1: |
if data.count("%mkrel") != 1: |
241 |
print "WARNING: Multiple %mkrel found; don't know what to do!" |
print >>sys.stderr, "ERROR: Multiple %mkrel found; don't know what to do!" |
242 |
return False |
return False |
243 |
|
|
244 |
data, nr = self.re_update_version.subn(r'\g<pre>%s\g<post>' % version, data, 1) |
data, nr = self.re_update_version.subn(r'\g<pre>%s\g<post>' % version, data, 1) |
245 |
if nr != 1: |
if nr != 1: |
246 |
print "WARNING: Could not increase version!" |
print >>sys.stderr, "ERROR: Could not increase version!" |
247 |
return False |
return False |
248 |
|
|
249 |
data, nr = self.re_update_release.subn(r'\g<pre>%mkrel 1\g<post>', data, 1) |
data, nr = self.re_update_release.subn(r'\g<pre>%mkrel 1\g<post>', data, 1) |
250 |
if nr != 1: |
if nr != 1: |
251 |
print "WARNING: Could not reset release!" |
print >>sys.stderr, "ERROR: Could not reset release!" |
252 |
return False |
return False |
253 |
|
|
254 |
# Overwrite file with new version number |
# Overwrite file with new version number |
255 |
write_file(self.path, data) |
write_file(self.path, data) |
256 |
|
|
257 |
|
|
258 |
# Check RPM also agrees that version number has increased |
# Verify that RPM also agrees that version number has changed |
259 |
if self.version != version: |
if self.version != version: |
260 |
print "ERROR: Increased version to %s, but RPM doesn't agree!?!" % version |
print "ERROR: Increased version to %s, but RPM doesn't agree!?!" % version |
261 |
return False |
return False |
392 |
@property |
@property |
393 |
def svn_author(self): |
def svn_author(self): |
394 |
if not hasattr(self, '_svn_author'): |
if not hasattr(self, '_svn_author'): |
395 |
p = subprocess.Popen(['svn', 'log', '-q', "--", self.path], stdout=subprocess.PIPE, close_fds=True) |
try: |
396 |
contents = p.stdout.read().strip("\n").splitlines() |
contents = subprocess.check_output(['svn', 'log', '-q', "--", self.path], close_fds=True).strip("\n").splitlines() |
397 |
ecode = p.wait() |
|
|
if ecode == 0: |
|
398 |
for line in contents: |
for line in contents: |
399 |
if ' | ' not in line: |
if ' | ' not in line: |
400 |
continue |
continue |
402 |
fields = line.split(' | ') |
fields = line.split(' | ') |
403 |
if len(fields) >= 3: |
if len(fields) >= 3: |
404 |
self._svn_author = fields[1] |
self._svn_author = fields[1] |
405 |
|
except subprocess.CalledProcessError: |
406 |
|
pass |
407 |
|
|
408 |
if not hasattr(self, '_svn_author'): |
if not hasattr(self, '_svn_author'): |
409 |
return None |
return None |
430 |
def get_downstream_names(): |
def get_downstream_names(): |
431 |
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]*)$') |
432 |
|
|
433 |
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) |
|
434 |
|
|
435 |
FILES = {} |
FILES = {} |
436 |
TARBALLS = {} |
TARBALLS = {} |
483 |
|
|
484 |
matches = upstream & set(downstream.keys()) |
matches = upstream & set(downstream.keys()) |
485 |
for module in matches: |
for module in matches: |
486 |
print "\n".join(downstream[module]) |
print "\n".join(sorted(downstream[module])) |
487 |
|
|
488 |
def cmd_patches(options, parser): |
def cmd_patches(options, parser): |
489 |
upstream = get_upstream_names() |
upstream = get_upstream_names() |
513 |
p.add_dep3() |
p.add_dep3() |
514 |
|
|
515 |
def cmd_package_new_version(options, parser): |
def cmd_package_new_version(options, parser): |
516 |
cwd = os.path.expanduser(PKGROOT) |
# Determine the package name |
517 |
package = options.package |
if options.upstream: |
518 |
|
downstream, downstream_files = get_downstream_names() |
519 |
|
|
520 |
|
if options.package not in downstream: |
521 |
|
print >>sys.stderr, "ERROR: No packages for upstream name: %s" % options.package |
522 |
|
sys.exit(1) |
523 |
|
|
524 |
|
if len(downstream[options.package]) != 1: |
525 |
|
# XXX - Make it more intelligent |
526 |
|
print >>sys.stderr, "ERROR: Multiple packages found for %s: %s" % (options.package, ", ".join(downstream[options.package])) |
527 |
|
sys.exit(1) |
528 |
|
|
529 |
|
package = list(downstream[options.package])[0] |
530 |
|
else: |
531 |
|
package = options.package |
532 |
|
|
533 |
|
# Directories packages are located in |
534 |
|
root = os.path.expanduser(PKGROOT) |
535 |
|
cwd = os.path.join(root, package) |
536 |
|
|
537 |
|
# Checkout package to ensure the checkout reflects the latest changes |
538 |
|
try: |
539 |
|
subprocess.check_call(['mgarepo', 'co', package], cwd=root) |
540 |
|
except subprocess.CalledProcessError: |
541 |
|
sys.exit(1) |
542 |
|
|
543 |
subprocess.call(['mgarepo', 'co', package], cwd=cwd) |
# SpecFile class handles the actual version+release change |
544 |
s = SpecFile(os.path.join(cwd, package, "SPECS", "%s.spec" % package)) |
s = SpecFile(os.path.join(cwd, "SPECS", "%s.spec" % package)) |
545 |
print s.version |
print "%s => %s" % (s.version, options.version) |
546 |
if not s.update(options.version): |
if not s.update(options.version): |
547 |
sys.exit(1) |
sys.exit(1) |
548 |
|
|
549 |
|
# We can even checkin and submit :-) |
550 |
|
if options.submit: |
551 |
|
try: |
552 |
|
# checkin changes |
553 |
|
subprocess.check_call(['mgarepo', 'ci', '-m', 'new version %s' % options.version], cwd=cwd) |
554 |
|
# and submit |
555 |
|
subprocess.check_call(['mgarepo', 'submit'], cwd=cwd) |
556 |
|
except subprocess.CalledProcessError: |
557 |
|
sys.exit(1) |
558 |
|
|
559 |
|
|
560 |
def main(): |
def main(): |
561 |
description = """Mageia GNOME commands.""" |
description = """Mageia GNOME commands.""" |
591 |
subparser = subparsers.add_parser('increase', help='Increase version number') |
subparser = subparsers.add_parser('increase', help='Increase version number') |
592 |
subparser.add_argument("package", help="Package name") |
subparser.add_argument("package", help="Package name") |
593 |
subparser.add_argument("version", help="Version number") |
subparser.add_argument("version", help="Version number") |
594 |
|
subparser.add_argument("-u", "--upstream", action="store_true", dest="upstream", |
595 |
|
help="Package name reflects the upstream name") |
596 |
|
subparser.add_argument("-s", "--submit", action="store_true", dest="submit", |
597 |
|
help="Commit changes and submit") |
598 |
subparser.set_defaults( |
subparser.set_defaults( |
599 |
func=cmd_package_new_version, path=False |
func=cmd_package_new_version, submit=False, upstream=False |
600 |
) |
) |
601 |
|
|
602 |
if len(sys.argv) == 1: |
if len(sys.argv) == 1: |