comparison setup.py @ 69:cb1ed436642c

Always use version generated by `setup.py`. Better version generation.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 30 Aug 2014 08:35:26 -0700
parents e4a24512b814
children ecee3e8f35e5
comparison
equal deleted inserted replaced
67:563ce5dd02af 69:cb1ed436642c
4 import os 4 import os
5 import os.path 5 import os.path
6 import sys 6 import sys
7 import time 7 import time
8 import subprocess 8 import subprocess
9 from setuptools import setup, find_packages 9 from setuptools import setup, find_packages, Command
10 from setuptools.command.test import test 10 from setuptools.command.test import test
11 11
12 12
13 def read(fname): 13 def read(fname):
14 with open(os.path.join(os.path.dirname(__file__), fname)) as fp: 14 with open(os.path.join(os.path.dirname(__file__), fname)) as fp:
32 import pytest 32 import pytest
33 errno = pytest.main(self.test_args) 33 errno = pytest.main(self.test_args)
34 sys.exit(errno) 34 sys.exit(errno)
35 35
36 36
37 # Figure out the version. 37 class GenerateVersionCommand(Command):
38 # (this is loosely based on what Mercurial does) 38 description = 'generates a version file'
39 version = None 39 user_options = [
40 try: 40 ('force=', 'f', 'force a specific version number')]
41 if os.path.isdir(os.path.join(os.path.dirname(__file__), '.hg')): 41
42 def initialize_options(self):
43 self.force = None
44
45 def finalize_options(self):
46 pass
47
48 def run(self):
49 v = self.force or generate_version()
50 write_version(v)
51 print("Generated version %s" % v)
52 return 0
53
54
55 def generate_version():
56 """ Generate a version file from the source control information.
57 (this is loosely based on what Mercurial does)"""
58 if not os.path.isdir(os.path.join(os.path.dirname(__file__), '.hg')):
59 raise Exception("Can't generate version number: this is not a "
60 "Mercurial repository.")
61
62 try:
63 # Get the version we're currently on. Also see if we have local
64 # changes.
65 cmd = ['hg', 'id', '-i']
66 hgid, err = runcmd(cmd)
67 hgid = hgid.decode('utf8').strip()
68 has_local_changes = hgid.endswith('+')
69 hgid = hgid.rstrip('+')
70
71 # Get the tags on the current version.
42 cmd = ['hg', 'log', '-r', '.', '--template', '{tags}\n'] 72 cmd = ['hg', 'log', '-r', '.', '--template', '{tags}\n']
43 tags, err = runcmd(cmd) 73 tags, err = runcmd(cmd)
44 versions = [t for t in tags.decode('utf8').split() if t[0].isdigit()] 74 versions = [t for t in tags.decode('utf8').split() if t[0].isdigit()]
45 75
46 cmd = ['hg', 'id', '-i'] 76 if versions:
47 hgid, err = runcmd(cmd) 77 # Use the tag found at the current revision.
48 hgid = hgid.decode('utf8').strip() 78 version = versions[-1]
79 else:
80 # Use the latest tag, but add info about how many revisions
81 # there have been since then.
82 cmd = ['hg', 'parents', '--template',
83 '{latesttag}+{latesttagdistance}']
84 version, err = runcmd(cmd)
85 tag, dist = version.decode('utf8').split('+')
86 if dist == '1':
87 # We're on the commit that created the tag in the first place.
88 # Let's just do as if we were on the tag.
89 version = tag
90 else:
91 version = '%s-%s.%s' % (tag, dist, hgid)
49 92
50 if versions: 93 if has_local_changes:
51 # Use tag found at the current revision. Add dirty flag if any. 94 version += time.strftime('+%Y%m%d')
52 version = versions[-1]
53 if hgid.endswith('+'):
54 version += '+'
55 else:
56 # Use latest tag.
57 cmd = ['hg', 'parents', '--template', '{latesttag}+{latesttagdistance}-']
58 version, err = runcmd(cmd)
59 version = version.decode('utf8') + hgid
60 95
61 if version.endswith('+'): 96 return version
62 version += time.strftime('%Y%m%d') 97 except OSError:
63 except OSError: 98 raise Exception("Can't generate version number: Mercurial isn't "
64 # Mercurial isn't installed, or not in the PATH. 99 "installed, or in the PATH.")
65 version = None 100 except Exception as ex:
101 raise Exception("Can't generate version number: %s" % ex)
66 102
67 103
68 if version: 104 def write_version(version):
105 if not version:
106 raise Exception("No version to write!")
107
69 f = open("piecrust/__version__.py", "w") 108 f = open("piecrust/__version__.py", "w")
70 f.write('# this file is autogenerated by setup.py\n') 109 f.write('# this file is autogenerated by setup.py\n')
71 f.write('version = "%s"\n' % version) 110 f.write('APP_VERSION = "%s"\n' % version)
72 f.close() 111 f.close()
73 112
74 113
75 try: 114 # Always generate an up to date version.
76 from piecrust import __version__ 115 version = generate_version()
77 version = __version__.version 116 write_version(version)
78 except ImportError:
79 version = 'unknown'
80 117
81 118
82 setup(name="piecrust", 119 setup(name="piecrust",
83 version=version, 120 version=version,
84 description="A powerful static website generator and lightweight CMS.", 121 description="A powerful static website generator and lightweight CMS.",
117 tests_require=[ 154 tests_require=[
118 'pytest==2.6.1', 155 'pytest==2.6.1',
119 'pytest-mock==0.2.0' 156 'pytest-mock==0.2.0'
120 ], 157 ],
121 cmdclass={ 158 cmdclass={
122 'test': PyTest 159 'test': PyTest,
160 'version' : GenerateVersionCommand
123 }, 161 },
124 classifiers=[ 162 classifiers=[
125 'Development Status :: 3 - Alpha', 163 'Development Status :: 3 - Alpha',
126 'License :: OSI Approved :: Apache Software License', 164 'License :: OSI Approved :: Apache Software License',
127 'Environment :: Console', 165 'Environment :: Console',