forked from metatensor/metatensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
153 lines (124 loc) · 5.15 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
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
import os
import subprocess
import sys
import uuid
import packaging.version
from setuptools import setup
from setuptools.command.bdist_egg import bdist_egg
from setuptools.command.sdist import sdist
ROOT = os.path.realpath(os.path.dirname(__file__))
METATENSOR_CORE = os.path.join(ROOT, "python", "metatensor-core")
METATENSOR_OPERATIONS = os.path.join(ROOT, "python", "metatensor-operations")
METATENSOR_TORCH = os.path.join(ROOT, "python", "metatensor-torch")
METATENSOR_LEARN = os.path.join(ROOT, "python", "metatensor-learn")
METATENSOR_VERSION = "0.2.0"
class bdist_egg_disabled(bdist_egg):
"""Disabled version of bdist_egg
Prevents setup.py install performing setuptools' default easy_install,
which it should never ever do.
"""
def run(self):
sys.exit(
"Aborting implicit building of eggs.\nUse `pip install .` or "
"`python -m build --wheel . && pip install dist/metatensor-*.whl` "
"to install from source."
)
class sdist_git_version(sdist):
"""
Create a sdist with an additional generated file containing the extra
version from git.
"""
def run(self):
with open("n_commits_since_last_tag", "w") as fd:
fd.write(str(n_commits_since_last_tag()))
# run original sdist
super().run()
os.unlink("n_commits_since_last_tag")
def n_commits_since_last_tag():
"""
If git is available and we are building from a checkout, get the number of commits
since the last tag. Otherwise, this always returns 0.
"""
script = os.path.join(ROOT, "scripts", "n-commits-since-last-tag.py")
if not os.path.exists(script):
return 0
TAG_PREFIX = "metatensor-python-v"
output = subprocess.run(
[sys.executable, script, TAG_PREFIX],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
encoding="utf8",
)
if output.stderr:
print(output.stderr, file=sys.stderr)
return 0
else:
return int(output.stdout)
def create_version_number(version):
version = packaging.version.parse(version)
if os.path.exists("n_commits_since_last_tag"):
# we are building from a sdist, without git available, but the git
# version was recorded in the `n_commits_since_last_tag` file
with open("n_commits_since_last_tag") as fd:
n_commits = int(fd.read().strip())
else:
n_commits = n_commits_since_last_tag()
if n_commits != 0:
# if we have commits since the last tag, this mean we are in a pre-release of
# the next version. So we increase either the minor version number or the
# release candidate number (if we are closing up on a release)
if version.pre is not None:
assert version.pre[0] == "rc"
pre = ("rc", version.pre[1] + 1)
release = version.release
else:
major, minor, patch = version.release
release = (major, minor + 1, 0)
pre = None
# this is using a private API which is intended to become public soon:
# https://github.com/pypa/packaging/pull/698. In the mean time we'll
# use this
version._version = version._version._replace(release=release)
version._version = version._version._replace(pre=pre)
version._version = version._version._replace(dev=("dev", n_commits))
return str(version)
if __name__ == "__main__":
install_requires = []
extras_require = {}
# when packaging a sdist for release, we should never use local dependencies
METATENSOR_NO_LOCAL_DEPS = os.environ.get("METATENSOR_NO_LOCAL_DEPS", "0") == "1"
if not METATENSOR_NO_LOCAL_DEPS and os.path.exists(METATENSOR_CORE):
# we are building from a git checkout
assert os.path.exists(METATENSOR_OPERATIONS)
assert os.path.exists(METATENSOR_TORCH)
assert os.path.exists(METATENSOR_LEARN)
# add a random uuid to the file url to prevent pip from using a cached
# wheel for metatensor-core, and force it to re-build from scratch
uuid = uuid.uuid4()
install_requires.append(
f"metatensor-core @ file://{METATENSOR_CORE}?{uuid}",
)
install_requires.append(
f"metatensor-operations @ file://{METATENSOR_OPERATIONS}?{uuid}",
)
install_requires.append(
f"metatensor-learn @ file://{METATENSOR_LEARN}?{uuid}",
)
extras_require["torch"] = f"metatensor-torch @ file://{METATENSOR_TORCH}?{uuid}"
else:
# we are building from a sdist/installing from a wheel
install_requires.append("metatensor-core")
install_requires.append("metatensor-operations")
install_requires.append("metatensor-learn")
extras_require["torch"] = "metatensor-torch"
setup(
version=create_version_number(METATENSOR_VERSION),
author=", ".join(open(os.path.join(ROOT, "AUTHORS")).read().splitlines()),
install_requires=install_requires,
extras_require=extras_require,
cmdclass={
"bdist_egg": bdist_egg if "bdist_egg" in sys.argv else bdist_egg_disabled,
"sdist": sdist_git_version,
},
)