-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion.py
More file actions
57 lines (46 loc) · 1.94 KB
/
version.py
File metadata and controls
57 lines (46 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# This script is used in setup.cfg and circleci.
import logging
from subprocess import check_output
LOGGER = logging.getLogger(__name__)
def _run(args):
return check_output(args).decode('utf8').strip()
def is_git_repo():
cmd = ["git", "rev-parse", "--is-inside-work-tree"]
try:
output = _run(cmd)
LOGGER.info(f"is git repo cmd output: {output}")
if output == "true":
return True
except Exception as e:
LOGGER.exception(e)
return False
def get_version():
LOGGER.info("Geting version using git.")
if not is_git_repo():
# In the production docker image git folder doesn't exist.
LOGGER.warning("We are not in a git folder, probably running in production, cannot return version.")
version_returned = 'unknown'
print(f"version=={version_returned}")
return version_returned
describe_cmd = ['git', 'describe', '--tags', '--always']
last_tag = _run(describe_cmd + ['--abbrev=0']) # '1.0.14'
describe = _run(describe_cmd) # '1.0.14-2-gfaa2442' {tag}-{nb_commit_since_tag}-{hash}'
if describe == last_tag:
version_returned = last_tag.replace('v', '', 1)
LOGGER.info("version is the last tag, version==%s", version_returned)
print(f"version=={version_returned}")
return version_returned
branch_name = _run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
if branch_name == 'master':
commits_from_tag = describe[len(last_tag) + 1:].split('-')[0]
version_returned = f"{last_tag.replace('v', '', 1)}.dev{commits_from_tag}"
else:
short_hash = describe[len(last_tag) + 1:].split('-')[1]
version_returned = f"{last_tag.replace('v', '', 1)}.dev0+{short_hash[1:]}"
LOGGER.info("version==%s", version_returned)
print(f"version=={version_returned}")
return version_returned
__version__ = get_version() or "local"
if __name__ == '__main__':
print(__version__)