Skip to content

Commit 6469c42

Browse files
authored
Merge pull request #510 from nexB/465-use-vcs-for-version
#465 Use setuptools-scm to get version with a git tag
2 parents 1d4ff17 + 755fe78 commit 6469c42

5 files changed

Lines changed: 72 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ docs/_build
5858
/local/
5959
/share/
6060
/tcl/
61+
/.eggs/

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ license_file = NOTICE
33

44
[aliases]
55
pypi_release = clean --all sdist --formats=bztar bdist_wheel register upload
6-
7-
release = clean --all sdist --formats=gztar,bztar,zip bdist_wheel
6+
release = clean --all sdist --formats=bztar,zip bdist_wheel
87

98
[pytest]
109
norecursedirs =

setup.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@
1616
from setuptools import setup
1717

1818

19+
version = '2.0.0rc3'
20+
21+
22+
def get_version(default=version, template='{tag}.{distance}.{commit}{dirty}'):
23+
"""
24+
Return a version collected from git if possible or fall back to an hard-coded
25+
version otherwise.
26+
"""
27+
try:
28+
tag, distance, commit, dirty = get_git_version()
29+
if not distance and not dirty:
30+
# we are from a clean Git tag: use tag
31+
return tag
32+
33+
distance = 'post{}'.format(distance)
34+
if dirty:
35+
time_stamp = get_time_stamp()
36+
dirty = '.dirty.' + get_time_stamp()
37+
else:
38+
dirty = ''
39+
40+
return template.format(**locals())
41+
except:
42+
# no git data: use default version
43+
return default
44+
45+
46+
def get_time_stamp():
47+
"""
48+
Return a numeric UTC time stamp without microseconds.
49+
"""
50+
from datetime import datetime
51+
return (datetime.isoformat(datetime.utcnow()).split('.')[0]
52+
.replace('T', '').replace(':', '').replace('-', ''))
53+
54+
55+
def get_git_version():
56+
"""
57+
Return version parts from Git or raise an exception.
58+
"""
59+
from subprocess import check_output, STDOUT
60+
# this may fail with exceptions
61+
cmd = 'git', 'describe', '--tags', '--long', '--dirty',
62+
version = check_output(cmd, stderr=STDOUT).strip()
63+
dirty = version.endswith('-dirty')
64+
tag, distance, commit = version.split('-')[:3]
65+
# lower tag and strip V prefix in tags
66+
tag = tag.lower().lstrip('v ').strip()
67+
# strip leading g from git describe commit
68+
commit = commit.lstrip('g').strip()
69+
return tag, int(distance), commit, dirty
70+
71+
1972
def read(*names, **kwargs):
2073
return io.open(
2174
join(dirname(__file__), *names),
@@ -28,9 +81,10 @@ def read(*names, **kwargs):
2881
re.sub(':obj:`~?(.*?)`', r'``\1``', read('CHANGELOG.rst'))
2982
)
3083

84+
3185
setup(
3286
name='scancode-toolkit',
33-
version='2.0.0rc3',
87+
version=get_version(),
3488
license='Apache-2.0 with ScanCode acknowledgment and CC0-1.0 and others',
3589
description='ScanCode is a tool to scan code for license, copyright and other interesting facts.',
3690
long_description=long_description,

src/scancode/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
# ScanCode is a free software code scanning tool from nexB Inc. and others.
2323
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
2424

25-
__version__ = '2.0.0rc3'
25+
from __future__ import print_function
26+
from __future__ import absolute_import
2627

2728
from os.path import dirname
2829
from os.path import abspath
@@ -33,6 +34,7 @@
3334

3435
from commoncode import fileutils
3536

37+
3638
scan_src_dir = abspath(dirname(__file__))
3739
src_dir = dirname(scan_src_dir)
3840
root_dir = dirname(src_dir)
@@ -41,3 +43,11 @@
4143

4244
if not exists(scans_cache_dir):
4345
fileutils.create_dir(scans_cache_dir)
46+
47+
48+
from pkg_resources import get_distribution, DistributionNotFound
49+
try:
50+
__version__ = get_distribution('scancode-toolkit').version
51+
except DistributionNotFound:
52+
# package is not installed ??
53+
__version__ = '2.0.0rc3'

thirdparty/base/setuptools.ABOUT

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ download_url: https://pypi.python.org/packages/69/19/b1dff551058ce79d88b1e3688f1
77
home_url: https://pypi.python.org/pypi/setuptools
88
owner: Python Packaging Authority
99

10-
dje_license: psf
11-
license_text_file: PSF.LICENSE
10+
dje_license: psf and mit
11+
license_text_file:
12+
- PSF.LICENSE
13+
- setuptools.LICENSE

0 commit comments

Comments
 (0)