forked from pygame-community/pygame-geometry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
146 lines (104 loc) · 3.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
143
144
145
146
from pathlib import Path
from setuptools import setup, Extension
import subprocess
import json
import shlex
import sys
import os
extra_compiler_options = []
if sys.platform == "linux" or sys.platform == "linux2":
extra_compiler_options.append("-mavx2")
elif sys.platform == "win32":
extra_compiler_options.append("/arch:AVX2")
elif sys.platform == "darwin":
pass
extensions = [
Extension(
"geometry",
sources=["src_c/geometry.c"],
extra_compile_args=extra_compiler_options,
)
]
def consume_arg(arg: str) -> bool:
if arg in sys.argv:
sys.argv.remove(arg)
return True
return False
def get_geometry_cache_dir() -> Path:
path = Path("./__geometry_cache__")
path.mkdir(exist_ok=True)
return path
def get_times_json_file() -> Path:
path = get_geometry_cache_dir() / "times.json"
path.touch(exist_ok=True)
return path
def update_times_file() -> None:
files = list(Path("./src_c/").glob("**/*.[c|h]"))
data: Dict[str, float] = {str(file): os.path.getmtime(str(file)) for file in files}
with open(get_times_json_file(), "w") as f:
f.truncate(0)
f.write(json.dumps(data, indent=4))
def needs_to_be_rebuilt() -> bool:
files = list(Path("./src_c/").glob("**/*.[c|h]"))
with open(get_times_json_file(), "r+") as f:
file_contents = f.read()
data: Dict[str, float]
if file_contents == "":
data = {}
else:
data = json.loads(file_contents)
if not data:
for file in files:
data[str(file)] = os.path.getmtime(str(file))
f.write(json.dumps(data, indent=4))
return True
result = False
for file in files:
if data[str(file)] != os.path.getmtime(str(file)):
data[str(file)] = os.path.getmtime(str(file))
result = True
with open(get_times_json_file(), "w") as f:
f.truncate(0)
f.write(json.dumps(data, indent=4))
return result
def build() -> None:
if not needs_to_be_rebuilt():
print("latest version of geometry already built")
return
print("building latest version of geometry...")
with open("src_c/geometry.c", "r+") as f:
original_geometry_c = f.read()
f.write(" ")
setup(
name="geometry",
ext_modules=extensions,
)
with open("src_c/geometry.c", "w") as f:
f.truncate(0)
f.write(original_geometry_c)
update_times_file()
if __name__ == "__main__":
for arg in sys.argv:
if arg in ("--format", "install", "--test"):
break
else:
setup(
name="geometry",
ext_modules=extensions,
)
sys.exit(0)
if consume_arg("--format"):
cmd = ["clang-format", "-i"] + [
str(file) for file in Path("./src_c/").glob("**/*.[c|h]")
]
print(shlex.join(cmd))
subprocess.call(cmd)
cmd = ["black", "."]
print(shlex.join(cmd))
subprocess.call(cmd)
run_tests = consume_arg("--test")
build()
if run_tests:
cmd = ["python", "-m", "unittest"]
print(shlex.join(cmd))
subprocess.call(cmd)