1616from 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+
1972def 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+
3185setup (
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 ,
0 commit comments