Skip to content

Commit 66e2cfc

Browse files
committed
Bump hgdistver to version 0.6 for support for older mercurial versions and other assorted improvements
1 parent 9e0d9dd commit 66e2cfc

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

hgdistver.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import commands
3+
import subprocess
24

35
def version_from_cachefile(cachefile=None):
46
if not cachefile:
@@ -19,24 +21,45 @@ def version_from_cachefile(cachefile=None):
1921
def version_from_hg_id(cachefile=None):
2022
"""stolen logic from mercurials setup.py as well"""
2123
if os.path.isdir('.hg'):
22-
import commands
2324
l = commands.getoutput('hg id -i -t').strip().split()
2425
while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
2526
l.pop()
2627
if len(l) > 1: # tag found
2728
version = l[-1]
2829
if l[0].endswith('+'): # propagate the dirty status to the tag
2930
version += '+'
30-
elif len(l) == 1: #no tag found
31-
cmd = 'hg parents --template "{latesttag}.dev{latesttagdistance}-"'
32-
version = commands.getoutput(cmd) + l[0]
33-
if version[:4] == 'null':
34-
version = '0.0' + version[4:]
31+
return version
32+
33+
def version_from_hg15_parents(cachefile=None):
34+
if os.path.isdir('.hg'):
35+
node = commands.getoutput('hg id -i')
36+
if node == '000000000000+':
37+
return '0.0.dev0-' + node
38+
39+
cmd = 'hg parents --template "{latesttag} {latesttagdistance}"'
40+
out = commands.getoutput(cmd)
41+
try:
42+
tag, dist = out.split()
43+
if tag=='null':
44+
tag = '0.0'
45+
return '%s.dev%s-%s' % (tag, dist, node)
46+
except ValueError:
47+
pass # unpacking failed, old hg
48+
49+
def version_from_hg_log_with_tags(cachefile=None):
50+
if os.path.isdir('.hg'):
51+
node = commands.getoutput('hg id -i')
52+
cmd = r'hg log -r %s:0 --template "{tags}\n"'
53+
cmd = cmd % node.rstrip('+')
54+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
55+
dist = -1 # no revs vs one rev is tricky
56+
57+
for dist, line in enumerate(proc.stdout):
58+
tags = [t for t in line.split() if not t.isalpha()]
59+
if tags:
60+
return '%s.dev%s-%s' % (tags[0], dist, node)
3561

36-
if version.endswith('+'):
37-
import time
38-
version += time.strftime('%Y%m%d')
39-
return version
62+
return '0.0.dev%s-%s' % (dist+1, node)
4063

4164
def _archival_to_version(data):
4265
"""stolen logic from mercurials setup.py"""
@@ -76,6 +99,8 @@ def write_cachefile(path, version):
7699

77100
methods = [
78101
version_from_hg_id,
102+
version_from_hg15_parents,
103+
version_from_hg_log_with_tags,
79104
version_from_archival,
80105
version_from_cachefile,
81106
version_from_sdist_pkginfo,
@@ -87,6 +112,9 @@ def get_version(cachefile=None):
87112
for method in methods:
88113
version = method(cachefile=cachefile)
89114
if version:
115+
if version.endswith('+'):
116+
import time
117+
version += time.strftime('%Y%m%d')
90118
return version
91119
finally:
92120
if cachefile and version:

0 commit comments

Comments
 (0)