-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
176 lines (148 loc) · 5.38 KB
/
Copy pathversion.py
File metadata and controls
176 lines (148 loc) · 5.38 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This file vendored from Autorelease
import os
import subprocess
try:
from configparser import (
ConfigParser, Error as ConfigParserError, NoSectionError, NoOptionError
)
except ImportError:
# py2
from ConfigParser import (
ConfigParser, Error as ConfigParserError, NoSectionError, NoOptionError
)
try:
from ._installed_version import _installed_version
from ._installed_version import _installed_git_hash
from ._installed_version import _version_setup_depth
except ImportError:
_installed_version = "Unknown"
_installed_git_hash = "Unknown"
_version_setup_depth = -1
def get_git_version():
"""
Return the git hash as a string.
Apparently someone got this from numpy's setup.py. It has since been
modified a few times.
"""
# Return the git revision as a string
# copied from numpy setup.py
def _minimal_ext_cmd(cmd):
# construct minimal environment
env = {}
for k in ['SYSTEMROOT', 'PATH']:
v = os.environ.get(k)
if v is not None:
env[k] = v
# LANGUAGE is used on win32
env['LANGUAGE'] = 'C'
env['LANG'] = 'C'
env['LC_ALL'] = 'C'
with open(os.devnull, 'w') as err_out:
out = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=err_out, # maybe debug later?
env=env).communicate()[0]
return out
try:
git_dir = os.path.dirname(os.path.realpath(__file__))
out = _minimal_ext_cmd(['git', '-C', git_dir, 'rev-parse', 'HEAD'])
GIT_REVISION = out.strip().decode('ascii')
except OSError:
GIT_REVISION = 'Unknown'
return GIT_REVISION
def _seek_parent_dirs_for_file(filename):
rel_directory = None
my_dir = os.path.dirname(os.path.abspath(__file__))
rel_directory_arr = []
while not rel_directory:
expected_dir = os.path.join(*rel_directory_arr) \
if rel_directory_arr else '.'
expected = os.path.join(expected_dir, filename)
if os.path.isfile(os.path.normpath(expected)):
rel_directory = expected_dir
else:
rel_directory_arr.append('..')
if len(rel_directory_arr) > len(my_dir.split(os.sep)):
rel_directory_arr = []
break
return rel_directory
def _find_rel_path_for_file(depth, filename):
rel_directory = None
if depth == 0:
rel_directory = '.'
elif depth >= 1:
rel_directory = os.sep.join(['..'] * depth)
else:
rel_directory = _seek_parent_dirs_for_file(filename)
if rel_directory:
return os.path.normpath(os.path.join(rel_directory, filename))
else:
return None
def get_setup_cfg(directory, filename="setup.cfg"):
"""Load the setup.cfg as a dict-of-dict.
Parameters
----------
directory : str
directory for setup.cfg, relative to cwd; default '.'
filename : str
filename for setup.cfg; default 'setup.cfg'
"""
if isinstance(directory, int):
rel_path = _find_rel_path_for_file(directory, filename)
start_dir = os.path.abspath(os.path.dirname(__file__))
setup_cfg = os.path.normpath(os.path.join(start_dir, rel_path))
else:
setup_cfg = os.path.join(directory, filename)
conf = None
if os.path.exists(setup_cfg):
conf = ConfigParser()
try:
conf.read(setup_cfg)
except ConfigParserError:
conf = None
return conf
def get_setup_value(default_value, directory, option, section='metadata',
filename="setup.cfg"):
"""Get a setup.cfg value or return the provided default.
Parameters
----------
default_value
value to return when setup.cfg is missing or does not define field
directory : str or int
directory for setup.cfg (or search depth if int)
option : str
field name to retrieve from section
section : str
section name to query; default 'metadata'
filename : str
filename for setup.cfg; default 'setup.cfg'
"""
value = default_value
conf = get_setup_cfg(directory, filename)
try:
value = conf.get(section, option)
except (NoSectionError, NoOptionError):
pass # option (or section) not defined in setup.cfg
except AttributeError:
pass # no setup.cfg found (conf is None)
return value
def get_setup_version(default_version, directory, filename="setup.cfg"):
return get_setup_value(default_version, directory=directory,
option='version', section='metadata',
filename=filename)
def get_setup_name(default_name, directory, filename="setup.cfg"):
return get_setup_value(default_name, directory=directory,
option='name', section='metadata',
filename=filename)
short_version = get_setup_version(_installed_version,
directory=_version_setup_depth)
_git_version = get_git_version()
_is_repo = (_git_version != '' and _git_version != "Unknown")
if _is_repo:
git_hash = _git_version
full_version = short_version + "+g" + _git_version[:7]
version = full_version
else:
git_hash = "Unknown"
full_version = short_version + "+g" + _installed_git_hash[:7] + '.install'
version = short_version