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