forked from qutip/qutip-qip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·96 lines (85 loc) · 3.44 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
#!/usr/bin/env python
import os
import subprocess
import sys
# Required third-party imports, must be specified in pyproject.toml.
import packaging.version
from setuptools import setup
def process_options():
"""
Determine all runtime options, returning a dictionary of the results. The
keys are:
'rootdir': str
The root directory of the setup. Almost certainly the directory
that this setup.py file is contained in.
'release': bool
Is this a release build (True) or a local development build (False)
"""
options = {}
options['rootdir'] = os.path.dirname(os.path.abspath(__file__))
options = _determine_version(options)
return options
def _determine_version(options):
"""
Adds the 'short_version', 'version' and 'release' options.
Read from the VERSION file to discover the version. This should be a
single line file containing valid Python package public identifier (see PEP
440), for example
4.5.2rc2
5.0.0
5.1.1a1
We do that here rather than in setup.cfg so we can apply the local
versioning number as well.
"""
version_filename = os.path.join(options['rootdir'], 'VERSION')
with open(version_filename, "r") as version_file:
version_string = version_file.read().strip()
version = packaging.version.parse(version_string)
if isinstance(version, packaging.version.LegacyVersion):
raise ValueError("invalid version: " + version_string)
options['short_version'] = str(version.public)
options['release'] = not version.is_devrelease
if not options['release']:
# Put the version string into canonical form, if it wasn't already.
version_string = str(version)
version_string += "+"
try:
git_out = subprocess.run(
('git', 'rev-parse', '--verify', '--short=7', 'HEAD'),
check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
git_hash = git_out.stdout.decode(sys.stdout.encoding).strip()
version_string += git_hash or "nogit"
# CalledProcessError is for if the git command fails for internal
# reasons (e.g. we're not in a git repository), OSError is for if
# something goes wrong when trying to run git (e.g. it's not installed,
# or a permission error).
except (subprocess.CalledProcessError, OSError):
version_string += "nogit"
options['version'] = version_string
return options
def create_version_py_file(options):
"""
Generate and write out the file qutip/version.py, which is used to produce
the '__version__' information for the module. This function will overwrite
an existing file at that location.
"""
filename = os.path.join(
options['rootdir'], 'src', 'qutip_qip', 'version.py')
content = "\n".join([
"# This file is automatically generated by qutip-qip's setup.py.",
f"short_version = '{options['short_version']}'",
f"version = '{options['version']}'",
f"release = {options['release']}",
])
with open(filename, 'w') as file:
print(content, file=file)
if __name__ == "__main__":
options = process_options()
create_version_py_file(options)
# Most of the kwargs to setup are defined in setup.cfg; the only ones we
# keep here are ones that we have done some compile-time processing on.
setup(
version=options['version'],
)