forked from Amulet-Team/Amulet-Map-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
142 lines (118 loc) · 4.2 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
from typing import List
from setuptools import setup, find_packages
from wheel.bdist_wheel import bdist_wheel
from Cython.Build import cythonize
import os
import glob
import shutil
import sys
import numpy
import subprocess
try:
import versioneer
except ImportError:
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import versioneer
# there were issues with other builds carrying over their cache
for d in glob.glob("*.egg-info"):
shutil.rmtree(d)
def load_requirements(path: str) -> List[str]:
requirements = []
with open(path) as f:
for line in f.readlines():
line = line.strip()
if (
not line
or line.startswith("#")
or line.startswith("git+")
or line.startswith("https:")
):
continue
elif line.startswith("-r "):
requirements += load_requirements(line[3:])
else:
requirements.append(line)
return requirements
required_packages = load_requirements("./requirements.txt")
first_party = {
"amulet-core",
"amulet-nbt",
"pymctranslate",
"minecraft-resource-pack",
}
def freeze_requirements(packages: List[str]) -> List[str]:
# Pip install the requirements to find the newest compatible versions
# This makes sure that the source versions are using the same dependencies as the compiled version.
# This also makes sure that the source version is using the newest version of the dependency.
if any("~=" in r and r.split("~=", 1)[0].lower() in first_party for r in packages):
print("pip-install")
try:
# make sure pip is up to date
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
# run pip install
subprocess.run(
[sys.executable, "-m", "pip", "install", *packages, "--upgrade"]
)
# run pip freeze
installed = (
subprocess.check_output(
[sys.executable, "-m", "pip", "freeze"], encoding="utf-8"
)
.strip()
.split("\n")
)
requirements_map = {r.split("==")[0].lower(): r for r in installed}
print(installed, requirements_map)
for index, requirement in enumerate(packages):
if "~=" in requirement:
lib = requirement.split("~=")[0].strip().lower()
if lib in first_party and lib in requirements_map:
packages[index] = requirements_map[lib]
print(f"Modified packages to {packages}")
except Exception as e:
print("Failed to bake versions:", e)
return packages
# build cython extensions
if next(glob.iglob("amulet_map_editor/**/*.pyx", recursive=True), None):
# This throws an error if it does not match any files
ext = cythonize("amulet_map_editor/**/*.pyx")
else:
ext = ()
cmdclass = versioneer.get_cmdclass()
# There might be a better way of doing this
# The extra argument needs to be defined in the sdist
# so that it doesn't error. It doesn't actually use it.
class SDist(cmdclass["sdist"]):
user_options = cmdclass["sdist"].user_options + [
("find-libs=", None, ""),
]
def initialize_options(self):
super().initialize_options()
self.find_libs = None
class BDistWheel(bdist_wheel):
user_options = bdist_wheel.user_options + [
(
"find-libs=",
None,
"Find and fix the newest version of first party libraries. Only used internally.",
),
]
def initialize_options(self):
super().initialize_options()
self.find_libs = None
def finalize_options(self):
if self.find_libs:
self.distribution.install_requires = freeze_requirements(
list(self.distribution.install_requires)
)
super().finalize_options()
cmdclass["sdist"] = SDist
cmdclass["bdist_wheel"] = BDistWheel
setup(
install_requires=required_packages,
packages=find_packages(),
include_package_data=True,
cmdclass=cmdclass,
ext_modules=ext,
include_dirs=[numpy.get_include()],
)