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