|
| 1 | +import os |
| 2 | + |
| 3 | +def version_from_cachefile(cachefile=None): |
| 4 | + if not cachefile: |
| 5 | + return |
| 6 | + #replaces 'with open()' from py2.6 |
| 7 | + fd = open(cachefile) |
| 8 | + fd.readline() # remove the comment |
| 9 | + version = None |
| 10 | + try: |
| 11 | + line = fd.readline() |
| 12 | + version_string = line.split(' = ')[1].strip() |
| 13 | + version = version_string[1:-1].decode('string-escape') |
| 14 | + except: # any error means invalid cachefile |
| 15 | + pass |
| 16 | + fd.close() |
| 17 | + return version |
| 18 | + |
| 19 | +def version_from_hg_id(cachefile=None): |
| 20 | + """stolen logic from mercurials setup.py as well""" |
| 21 | + if os.path.isdir('.hg'): |
| 22 | + import commands |
| 23 | + l = commands.getoutput('hg id -i -t').strip().split() |
| 24 | + while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags |
| 25 | + l.pop() |
| 26 | + if len(l) > 1: # tag found |
| 27 | + version = l[-1] |
| 28 | + if l[0].endswith('+'): # propagate the dirty status to the tag |
| 29 | + 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:] |
| 35 | + |
| 36 | + if version.endswith('+'): |
| 37 | + import time |
| 38 | + version += time.strftime('%Y%m%d') |
| 39 | + return version |
| 40 | + |
| 41 | +def _archival_to_version(data): |
| 42 | + """stolen logic from mercurials setup.py""" |
| 43 | + if 'tag' in data: |
| 44 | + return data['tag'] |
| 45 | + elif 'latesttag' in data: |
| 46 | + return '%(latesttag)s.dev%(latesttagdistance)s-%(node).12s' % data |
| 47 | + else: |
| 48 | + return data.get('node', '')[:12] |
| 49 | + |
| 50 | +def _data_from_archival(path): |
| 51 | + import email |
| 52 | + data = email.message_from_file(open(str(path))) |
| 53 | + return dict(data.items()) |
| 54 | + |
| 55 | +def version_from_archival(cachefile=None): |
| 56 | + #XXX: asumes cwd is repo root |
| 57 | + if os.path.exists('.hg_archival.txt'): |
| 58 | + data = _data_from_archival('.hg_archival.txt') |
| 59 | + return _archival_to_version(data) |
| 60 | + |
| 61 | +def version_from_sdist_pkginfo(cachefile=None): |
| 62 | + if cachefile is None and os.path.exists('PKG-INFO'): |
| 63 | + data = _data_from_archival('PKG-INFO') |
| 64 | + version = data.get('Version') |
| 65 | + if version != 'UNKNOWN': |
| 66 | + return version |
| 67 | + |
| 68 | +def write_cachefile(path, version): |
| 69 | + fd = open(path, 'w') |
| 70 | + try: |
| 71 | + fd.write('# this file is autogenerated by hgdistver + setup.py\n') |
| 72 | + fd.write('version = %r' % version) |
| 73 | + finally: |
| 74 | + fd.close() |
| 75 | + |
| 76 | + |
| 77 | +methods = [ |
| 78 | + version_from_hg_id, |
| 79 | + version_from_archival, |
| 80 | + version_from_cachefile, |
| 81 | + version_from_sdist_pkginfo, |
| 82 | +] |
| 83 | + |
| 84 | +def get_version(cachefile=None): |
| 85 | + try: |
| 86 | + version = None |
| 87 | + for method in methods: |
| 88 | + version = method(cachefile=cachefile) |
| 89 | + if version: |
| 90 | + return version |
| 91 | + finally: |
| 92 | + if cachefile and version: |
| 93 | + write_cachefile(cachefile, version) |
0 commit comments