forked from SETI/rms-cspyce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·244 lines (201 loc) · 8.64 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# If you are installing this code via "pip install cspyce", then you should
# never see this file. pip automatically determines whether you need a binary
# distribution or source distribution, and automatically builds it as needed for
# your machine.
#
# If you are doing a build from sources, please read README-developers.md in
# this directory.
from glob import glob
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
from tarfile import TarFile
import tempfile
import time
from zipfile import ZipFile
from setuptools.command.build_ext import build_ext
from setuptools import Command, setup, Extension
try:
import numpy
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
import numpy
IS_LINUX = platform.system() == "Linux"
IS_MACOS = platform.system() == "Darwin"
IS_WINDOWS = platform.system() == "Windows"
assert IS_LINUX or IS_MACOS or IS_WINDOWS
################################################################################
# The following code is responsible for downloading the CSPICE source files
# based on whatever hardware and software it is currently running on.
#
# The contents of the directories
# <download>/cspice/src/cspice
# <download>/include
# are put into the two directories
# cspice/<os>-<arch>/src
# cspice/<os>-<arch>/include
#
# This module should only be called by setup.py.
#
# This code owes a big debt to Dr. Andrew Annex and his file:
# https://github.com/AndrewAnnex/SpiceyPy/blob/main/get_spice.py
# His version is much more ambitious than this and also compiles the files into
# a shared library.
################################################################################
CSPICE_DISTRIBUTIONS = {
# system arch distribution name extension
# -------- ---------- ------------------------- ---------
("Darwin", "x86_64"): ("MacIntel_OSX_AppleC_64bit", "tar.Z"),
("Darwin", "arm64"): ("MacM1_OSX_clang_64bit", "tar.Z"),
("cygwin", "x86_64"): ("PC_Cygwin_GCC_64bit", "tar.Z"),
("FreeBSD", "x86_64"): ("PC_Linux_GCC_64bit", "tar.Z"),
("Linux", "x86_64"): ("PC_Linux_GCC_64bit", "tar.Z"),
("Windows", "x86_64"): ("PC_Windows_VisualC_64bit", "zip"),
# This is just so I can easily test it in a docker image on my Mac
("Linux", "arm64"): ("PC_Linux_GCC_64bit", "tar.Z"),
}
ARCHITECTURE_TRANSLATOR = {"aarch64": "arm64", "AMD64": "x86_64"}
# versions
SPICE_VERSION = "N0067"
class GetCspice:
def __init__(self):
self.host_OS = platform.system()
architecture = platform.machine()
self.architecture = ARCHITECTURE_TRANSLATOR.get(architecture, architecture)
# Check if platform is Unix-like OS or not
self.is_unix = self.host_OS in ("Linux", "Darwin", "FreeBSD")
# Get directory into which we want to put all our files
self.target_directory = os.path.join(
"cspice", self.host_OS + "-" + self.architecture)
def download(self):
if all(Path(os.path.join(self.target_directory, name)).is_dir()
for name in ("src", "include", "lib")):
return self.target_directory
with tempfile.TemporaryDirectory() as tmpdir:
self.download_cspice(destination=tmpdir)
shutil.copytree(os.path.join(tmpdir, "cspice", "src", "cspice"),
os.path.join(self.target_directory, "src"))
shutil.copytree(os.path.join(tmpdir, "cspice", "include"),
os.path.join(self.target_directory, "include"))
shutil.copytree(os.path.join(tmpdir, "cspice", "lib"),
os.path.join(self.target_directory, "lib"))
return self.target_directory
def download_cspice(self, destination):
try:
# Get the remote file path for the Python architecture that
# executes the script.
assert sys.maxsize > 2 ** 32, "Machine must support 64bit"
system = self.host_OS
system = "cygwin" if "CYGWIN" in system else system
distribution, extension = CSPICE_DISTRIBUTIONS[(system, self.architecture)]
except KeyError:
raise RuntimeError("CSpice does not support your system")
cspice_filename = "cspice.{}".format(extension)
url = ("https://naif.jpl.nasa.gov/pub/naif/misc/toolkit_{0}/C/{1}/packages/{2}"
.format(SPICE_VERSION, distribution, cspice_filename))
# Download the file
target_file = os.path.join(destination, cspice_filename)
attempts = 10 # Let's try a maximum of attempts for getting SPICE
while attempts:
try:
if extension == "zip":
# TODO:
# Do we want --connect_timeout? This curl either seems to take
# 3s or several minutes, somewhat randomly.
subprocess.check_call(["curl", url, "-o", target_file])
with ZipFile(target_file, "r") as archive:
archive.extractall(destination)
else:
target_file = target_file[:-2] # remove the .Z
subprocess.check_call("curl {} | gzip -d > {}".format(
url, target_file), shell=True)
with TarFile.open(target_file, "r") as archive:
archive.extractall(destination)
os.unlink(target_file)
break
except (RuntimeError, subprocess.CalledProcessError) as error:
attempts -= 1
if attempts == 0:
raise error
print("Download failed with URLError: {0}, trying again after "
"15 seconds!".format(error))
time.sleep(15)
class GenerateCommand(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Create vectorize.i and cspyce0_info.py from existing swig .i files
from swig.make_vectorize import create_vectorize_header_file
from swig.make_cspyce0_info import make_cspice0_info
create_vectorize_header_file("swig/vectorize.i")
make_cspice0_info("cspyce/cspyce0_info.py")
# Run swig to generate cspyce0.i and cspyce0.py
command = "swig -python -outdir cspyce/. " \
"-o swig/cspyce0_wrap.c swig/cspyce0.i".split(" ")
subprocess.check_call(command)
command = "swig -python -outdir cspyce/. " \
"-o swig/typemap_samples_wrap.c swig/typemap_samples.i".split(" ")
subprocess.check_call(command)
# We cannot run this import until *after* we've created cspyce0.py
from swig.make_cspyce2 import make_cspyce2
make_cspyce2("cspyce/cspyce2.py")
# Some linkers seem to have trouble with 2400 files, so we break it up into
# smaller libraries with a maximum of 250 files apiece.
cspice_directory = GetCspice().download()
def get_c_libraries():
if IS_WINDOWS:
return []
files = sorted(glob(os.path.join(cspice_directory, "src", "*.c")))
splits = 1 if IS_LINUX else 1 + (len(files) // 250)
compiler_flags = ["-w"]
cspice_libraries = [
("cspice_" + str(i + 1), {
"sources": files[i::splits],
"include_dirs": [os.path.join(cspice_directory, "include")],
"cflags": compiler_flags
})
for i in range(splits)]
return cspice_libraries
def get_extensions():
extra_args = {}
if IS_WINDOWS:
cspyce_cflags = ["/DSWIG_PYTHON_CAST_MODE"]
extra_args["libraries"] = ["cspice", "csupport"]
extra_args["library_dirs"] = [os.path.join(cspice_directory, "lib")]
else:
cspyce_cflags = ["-DSWIG_PYTHON_CAST_MODE"]
include_dirs = [os.path.join(cspice_directory, "include"), numpy.get_include()]
cspyce0_module = Extension(
"cspyce._cspyce0",
sources=["swig/cspyce0_wrap.c"],
include_dirs=include_dirs,
extra_compile_args=cspyce_cflags,
**extra_args)
typemap_samples_module = Extension(
"cspyce._typemap_samples",
sources=["swig/typemap_samples_wrap.c"],
include_dirs=include_dirs,
extra_compile_args=cspyce_cflags,
**extra_args
)
return [cspyce0_module, typemap_samples_module]
class MyBuildExt(build_ext):
def initialize_options(self):
build_ext.initialize_options(self)
def do_setup():
setup(
ext_modules=get_extensions(),
libraries=get_c_libraries(),
cmdclass={
"build_ext": MyBuildExt,
"generate": GenerateCommand,
}
)
do_setup()