33
44
55command = 'git describe --tags --long --dirty'
6+ fmt = '{tag}.{commitcount}+{gitsha}'
67
78
89def validate_version_format (dist , attr , value ):
@@ -15,11 +16,29 @@ def validate_version_format(dist, attr, value):
1516 dist .metadata .version = version
1617
1718
18- def format_version (version , fmt ):
19+ def format_version (version , fmt = fmt ):
1920 parts = version .split ('-' )
2021 assert len (parts ) in (3 , 4 )
2122 dirty = len (parts ) == 4
2223 tag , count , sha = parts [:3 ]
2324 if count == '0' and not dirty :
2425 return tag
2526 return fmt .format (tag = tag , commitcount = count , gitsha = sha .lstrip ('g' ))
27+
28+
29+ if __name__ == "__main__" :
30+ # determine version from git
31+ git_version = check_output (command .split ()).decode ('utf-8' ).strip ()
32+ git_version = format_version (version = git_version )
33+
34+ # monkey-patch `setuptools.setup` to inject the git version
35+ import setuptools
36+ original_setup = setuptools .setup
37+
38+ def setup (version = None , * args , ** kw ):
39+ return original_setup (version = git_version , * args , ** kw )
40+
41+ setuptools .setup = setup
42+
43+ # import the packages's setup module
44+ import setup
0 commit comments