Skip to content

Commit 9e0d9dd

Browse files
committed
Make experimental branch use hgdistver with a cache file for creating version numbers. I thank Ronny for this.
1 parent 8cbc330 commit 9e0d9dd

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

bpython/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# THE SOFTWARE.
2222

2323

24-
__version__ = 'dev'
24+
# __version__ = 'dev'
2525

2626

2727
def embed(locals_=None, args=['-i', '-q'], banner=None):

bpython/args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import code
99
from optparse import OptionParser, OptionGroup
1010

11-
from bpython import __version__
11+
from bpython.meta import version
1212
from bpython.config import loadini, Struct, migrate_rc
1313

1414

@@ -81,7 +81,7 @@ def parse(args, extras=None, ignore_stdin=False):
8181
os.execv(sys.executable, [sys.executable] + args)
8282

8383
if options.version:
84-
print 'bpython version', __version__,
84+
print 'bpython version', version,
8585
print 'on top of Python', sys.version.split()[0]
8686
print ('(C) 2008-2010 Bob Farrell, Andreas Stuehrk et al. '
8787
'See AUTHORS for detail.')

hgdistver.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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)

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import platform
88
import re
99
import sys
10+
11+
from hgdistver import get_version
12+
1013
try:
1114
from setuptools import setup
1215
using_setuptools = True
@@ -19,8 +22,6 @@
1922
except ImportError:
2023
from distutils.command.build_py import build_py
2124

22-
from bpython import __version__
23-
2425

2526
if platform.system() == 'FreeBSD':
2627
man_dir = 'man'
@@ -29,7 +30,7 @@
2930

3031
setup(
3132
name="bpython",
32-
version = __version__,
33+
version = get_version('bpython/meta.py'),
3334
author = "Bob Farrell, Andreas Stuehrk et al.",
3435
author_email = "robertanthonyfarrell@gmail.com",
3536
description = "Fancy Interface to the Python Interpreter",

0 commit comments

Comments
 (0)