-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.py
100 lines (72 loc) · 2.95 KB
/
version.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- mode: python; coding: utf-8 -*
# Copyright (c) 2018 Radio Astronomy Software Group
# Licensed under the 2-clause BSD License
from __future__ import absolute_import, division, print_function
import os
import six
import subprocess
import json
pyuvdata_dir = os.path.dirname(os.path.realpath(__file__))
def _get_git_output(args, capture_stderr=False):
"""Get output from Git, ensuring that it is of the ``str`` type,
not bytes."""
argv = ['git', '-C', pyuvdata_dir] + args
if capture_stderr:
data = subprocess.check_output(argv, stderr=subprocess.STDOUT)
else:
data = subprocess.check_output(argv)
data = data.strip()
if six.PY2:
return data
return data.decode('utf8')
def _get_gitinfo_file(git_file=None):
"""Get saved info from GIT_INFO file that was created when installing package"""
if git_file is None:
git_file = os.path.join(pyuvdata_dir, 'GIT_INFO')
with open(git_file) as data_file:
data = [_unicode_to_str(x) for x in json.loads(data_file.read().strip())]
git_origin = data[0]
git_hash = data[1]
git_description = data[2]
git_branch = data[3]
return {'git_origin': git_origin, 'git_hash': git_hash,
'git_description': git_description, 'git_branch': git_branch}
def _unicode_to_str(u):
if six.PY2:
return u.encode('utf8')
return u
def construct_version_info():
version_file = os.path.join(pyuvdata_dir, 'VERSION')
with open(version_file) as f:
version = f.read().strip()
git_origin = ''
git_hash = ''
git_description = ''
git_branch = ''
version_info = {'version': version, 'git_origin': '', 'git_hash': '',
'git_description': '', 'git_branch': ''}
try:
version_info['git_origin'] = _get_git_output(['config', '--get', 'remote.origin.url'], capture_stderr=True)
version_info['git_hash'] = _get_git_output(['rev-parse', 'HEAD'], capture_stderr=True)
version_info['git_description'] = _get_git_output(['describe', '--dirty', '--tag', '--always'])
version_info['git_branch'] = _get_git_output(['rev-parse', '--abbrev-ref', 'HEAD'], capture_stderr=True)
except subprocess.CalledProcessError: # pragma: no cover
try:
# Check if a GIT_INFO file was created when installing package
version_info.update(_get_gitinfo_file())
except (IOError, OSError):
pass
return version_info
version_info = construct_version_info()
version = version_info['version']
git_origin = version_info['git_origin']
git_hash = version_info['git_hash']
git_description = version_info['git_description']
git_branch = version_info['git_branch']
def main():
print('Version = {0}'.format(version))
print('git origin = {0}'.format(git_origin))
print('git branch = {0}'.format(git_branch))
print('git description = {0}'.format(git_description))
if __name__ == '__main__': # pragma: no cover
main()