Show More
@@ -0,0 +1,130 b'' | |||
|
1 | #!/usr/bin/env python2 | |
|
2 | from __future__ import absolute_import, print_function | |
|
3 | ||
|
4 | import argparse | |
|
5 | import json | |
|
6 | import os | |
|
7 | import subprocess | |
|
8 | import sys | |
|
9 | ||
|
10 | # Always load hg libraries from the hg we can find on $PATH. | |
|
11 | hglib = json.loads(subprocess.check_output( | |
|
12 | ['hg', 'debuginstall', '-Tjson']))[0]['hgmodules'] | |
|
13 | sys.path.insert(0, os.path.dirname(hglib)) | |
|
14 | ||
|
15 | from mercurial import util | |
|
16 | ||
|
17 | ap = argparse.ArgumentParser() | |
|
18 | ap.add_argument('--paranoid', | |
|
19 | action='store_true', | |
|
20 | help=("Be paranoid about how version numbers compare and " | |
|
21 | "produce something that's more likely to sort " | |
|
22 | "reasonably.")) | |
|
23 | ap.add_argument('--selftest', action='store_true', help='Run self-tests.') | |
|
24 | ap.add_argument('versionfile', help='Path to a valid mercurial __version__.py') | |
|
25 | ||
|
26 | def paranoidver(ver): | |
|
27 | """Given an hg version produce something that distutils can sort. | |
|
28 | ||
|
29 | Some Mac package management systems use distutils code in order to | |
|
30 | figure out upgrades, which makes life difficult. The test case is | |
|
31 | a reduced version of code in the Munki tool used by some large | |
|
32 | organizations to centrally manage OS X packages, which is what | |
|
33 | inspired this kludge. | |
|
34 | ||
|
35 | >>> paranoidver('3.4') | |
|
36 | '3.4.0' | |
|
37 | >>> paranoidver('3.4.2') | |
|
38 | '3.4.2' | |
|
39 | >>> paranoidver('3.0-rc+10') | |
|
40 | '2.9.9999-rc+10' | |
|
41 | >>> paranoidver('4.2+483-5d44d7d4076e') | |
|
42 | '4.2.0+483-5d44d7d4076e' | |
|
43 | >>> paranoidver('4.2.1+598-48d1e1214d8c') | |
|
44 | '4.2.1+598-48d1e1214d8c' | |
|
45 | >>> paranoidver('4.3-rc') | |
|
46 | '4.2.9999-rc' | |
|
47 | >>> paranoidver('4.3') | |
|
48 | '4.3.0' | |
|
49 | >>> from distutils import version | |
|
50 | >>> class LossyPaddedVersion(version.LooseVersion): | |
|
51 | ... '''Subclass version.LooseVersion to compare things like | |
|
52 | ... "10.6" and "10.6.0" as equal''' | |
|
53 | ... def __init__(self, s): | |
|
54 | ... self.parse(s) | |
|
55 | ... | |
|
56 | ... def _pad(self, version_list, max_length): | |
|
57 | ... 'Pad a version list by adding extra 0 components to the end' | |
|
58 | ... # copy the version_list so we don't modify it | |
|
59 | ... cmp_list = list(version_list) | |
|
60 | ... while len(cmp_list) < max_length: | |
|
61 | ... cmp_list.append(0) | |
|
62 | ... return cmp_list | |
|
63 | ... | |
|
64 | ... def __cmp__(self, other): | |
|
65 | ... if isinstance(other, str): | |
|
66 | ... other = MunkiLooseVersion(other) | |
|
67 | ... max_length = max(len(self.version), len(other.version)) | |
|
68 | ... self_cmp_version = self._pad(self.version, max_length) | |
|
69 | ... other_cmp_version = self._pad(other.version, max_length) | |
|
70 | ... return cmp(self_cmp_version, other_cmp_version) | |
|
71 | >>> def testver(older, newer): | |
|
72 | ... o = LossyPaddedVersion(paranoidver(older)) | |
|
73 | ... n = LossyPaddedVersion(paranoidver(newer)) | |
|
74 | ... return o < n | |
|
75 | >>> testver('3.4', '3.5') | |
|
76 | True | |
|
77 | >>> testver('3.4.0', '3.5-rc') | |
|
78 | True | |
|
79 | >>> testver('3.4-rc', '3.5') | |
|
80 | True | |
|
81 | >>> testver('3.4-rc+10-deadbeef', '3.5') | |
|
82 | True | |
|
83 | >>> testver('3.4.2', '3.5-rc') | |
|
84 | True | |
|
85 | >>> testver('3.4.2', '3.5-rc+10-deadbeef') | |
|
86 | True | |
|
87 | >>> testver('4.2+483-5d44d7d4076e', '4.2.1+598-48d1e1214d8c') | |
|
88 | True | |
|
89 | >>> testver('4.3-rc', '4.3') | |
|
90 | True | |
|
91 | >>> testver('4.3', '4.3-rc') | |
|
92 | False | |
|
93 | """ | |
|
94 | major, minor, micro, extra = util.versiontuple(ver, n=4) | |
|
95 | if micro is None: | |
|
96 | micro = 0 | |
|
97 | if extra: | |
|
98 | if extra.startswith('rc'): | |
|
99 | if minor == 0: | |
|
100 | major -= 1 | |
|
101 | minor = 9 | |
|
102 | else: | |
|
103 | minor -= 1 | |
|
104 | micro = 9999 | |
|
105 | extra = '-' + extra | |
|
106 | else: | |
|
107 | extra = '+' + extra | |
|
108 | else: | |
|
109 | extra = '' | |
|
110 | return '%d.%d.%d%s' % (major, minor, micro, extra) | |
|
111 | ||
|
112 | def main(argv): | |
|
113 | opts = ap.parse_args(argv[1:]) | |
|
114 | if opts.selftest: | |
|
115 | import doctest | |
|
116 | doctest.testmod() | |
|
117 | return | |
|
118 | with open(opts.versionfile) as f: | |
|
119 | for l in f: | |
|
120 | if l.startswith('version = '): | |
|
121 | # version number is entire line minus the quotes | |
|
122 | ver = l[len('version = ') + 1:-2] | |
|
123 | break | |
|
124 | if opts.paranoid: | |
|
125 | print(paranoidver(ver)) | |
|
126 | else: | |
|
127 | print(ver) | |
|
128 | ||
|
129 | if __name__ == '__main__': | |
|
130 | main(sys.argv) |
General Comments 0
You need to be logged in to leave comments.
Login now