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