-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsetup.py
executable file
·108 lines (99 loc) · 2.9 KB
/
setup.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
101
102
103
104
105
106
107
108
#!/usr/bin/env python
"""Installation script."""
# import imp # inline
# import importlib # inline
import shlex as _sh
import os
import setuptools as _stp
import subprocess
import sys
def retrieve_git_info():
"""Return commit hash of HEAD, or "release", or None if failure.
If the git command fails, then return None.
If HEAD has tag with prefix "vM" where M is an integer, then
return 'release'.
Tags with such names are regarded as version or release tags.
Otherwise, return the commit hash as str.
"""
# Is Git installed?
cmd = _sh.split('''
git --version
''')
try:
subprocess.call(
cmd,
stdout=subprocess.PIPE)
except OSError:
return None
# Decide whether this is a release
cmd = _sh.split('''
git describe
--tags
--candidates=0
HEAD
''')
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p.wait()
if p.returncode == 0:
tag = p.stdout.read().decode('utf-8')
if len(tag) >= 2 and tag.startswith('v'):
try:
int(tag[1])
return 'release'
except ValueError:
pass
# Otherwise, return commit hash
cmd = _sh.split('''
git log
-1
--format=%H
''')
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE)
p.wait()
sha1 = p.stdout.read().decode('utf-8')
return sha1
def run_setup():
"""Get version from git, then install."""
# load long description from README.rst
# If .git directory is present,
# create commit_hash.txt accordingly
# to indicate version information
if os.path.exists('.git'):
# Provide commit hash or
# empty file to indicate release
sha1 = retrieve_git_info()
if sha1 is None:
sha1 = 'unknown-commit'
elif sha1 == 'release':
sha1 = ''
commit_hash_header = (
'# DO NOT EDIT! '
'This file was automatically generated by '
'setup.py of polytope')
with open('polytope/commit_hash.txt', 'w') as f:
f.write(commit_hash_header + '\n')
f.write(sha1 + '\n')
# Import polytope/version.py
# without importing polytope
if sys.version_info.major == 2:
import imp
version = imp.load_module(
'version',
*imp.find_module('version', ['polytope']))
else:
import importlib.util
spec = importlib.util.spec_from_file_location(
'version', 'polytope/version.py')
version = importlib.util.module_from_spec(spec)
sys.modules['version'] = version
spec.loader.exec_module(version)
polytope_version = version.version
_stp.setup(
version=polytope_version)
if __name__ == '__main__':
run_setup()