-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
102 lines (82 loc) · 4.18 KB
/
build.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
import pkg_resources
import subprocess
import os
import sys
import warnings
import argparse
minimum_cosmosis_version = pkg_resources.parse_version("0.0.10")
#minimum_cc_version = pkg_resources.parse_version("5.0.0")
#minimum_cxx_version = pkg_resources.parse_version("5.0.0")
def check_cosmosis(mpi=False):
try:
import cosmosis
try:
cosmosis_version = cosmosis.__version__
except AttributeError:
raise ModuleNotFoundError(f"Installed CosmoSIS version does not meet requirements ({minimum_cosmosis_version}).")
cosmosis_version = pkg_resources.parse_version(cosmosis_version)
if minimum_cosmosis_version > cosmosis_version:
raise ModuleNotFoundError(f"Installed CosmoSIS version ({cosmosis_version}) does not meet requirements ({minimum_cosmosis_version}).")
except ModuleNotFoundError:
env = check_compilers(mpi)
install_cosmosis(env)
import cosmosis
cosmosis_src_dir = os.path.split(os.path.dirname(cosmosis.__file__))[0]
return {"COSMOSIS_SRC_DIR" : cosmosis_src_dir}
def install_cosmosis(env):
cosmosis_source = "git+https://bitbucket.org/tilmantroester/cosmosis.git@kcap#egg=cosmosis-standalone"
subprocess.check_call([sys.executable, "-m", "pip", "install", cosmosis_source], env=env)
def check_compilers(mpi=False):
default_cc = "gcc"
default_cxx = "g++"
default_fc = "gfortran"
default_mpifc = "mpifort" if mpi else ""
env = {"PATH" : os.environ["PATH"] if "PATH" in os.environ else "/usr/bin/",
"CC" : os.environ["CC"] if "CC" in os.environ else default_cc,
"CXX" : os.environ["CXX"] if "CXX" in os.environ else default_cxx,
"FC" : os.environ["FC"] if "FC" in os.environ else default_fc,
"MPIFC" : os.environ["MPIFC"] if "MPIFC" in os.environ else default_mpifc,}
#cc_version = subprocess.check_output("${CC} -dumpversion", shell=True, env=env).decode("utf-8")
#cxx_version = subprocess.check_output("${CXX} -dumpversion", shell=True, env=env).decode("utf-8")
#fc_version = subprocess.check_output("${FC} -dumpversion", shell=True, env=env).decode("utf-8")
#cc_version = pkg_resources.parse_version(cc_version)
#cxx_version = pkg_resources.parse_version(cxx_version)
#if cc_version < minimum_cc_version:
# raise RuntimeError(f"GCC compiler version ({cc_version}) does not meet requirements ({minimum_cc_version}).")
#if cxx_version < minimum_cxx_version:
# raise RuntimeError(f"G++ compiler version ({cxx_version}) does not meet requirements ({minimum_cxx_version}).")
if env["MPIFC"] == "":
print("Compiling CosmoSIS without MPI support. If MPI support is required, set MPIFC.")
return env
def build(mpi=False):
# Check for sufficiently recent CosmoSIS-standalone installation
cosmosis_env = check_cosmosis(mpi)
os.makedirs("build", exist_ok=True)
print("Running cmake.")
subprocess.check_call(["cmake", ".."], cwd="build")
print("Running make.")
subprocess.check_call(["make"], cwd="build")
def clean():
print("Running make csl-clean")
subprocess.check_call(["make", "csl-clean"], cwd="build")
print("Running make cosebis-clean")
subprocess.check_call(["make", "cosebis-clean"], cwd="build")
print("Running make clean")
subprocess.check_call(["make", "clean"], cwd="build")
print("Removing CMakeCache.txt")
os.remove("build/CMakeCache.txt")
def install_requirements(filename="requirements.txt"):
print("Installing requirements.")
with open(filename, "r") as f:
lines = [l.rstrip() for l in f.readlines()]
subprocess.check_call([sys.executable, "-m", "pip", "install"] + lines)
if __name__ == "__main__":
# We're not using distutils/setuptools because we're not installing anything
parser = argparse.ArgumentParser(description="Build the KiDS cosmology pipeline.", add_help=True)
parser.add_argument("--clean", action='store_true', help="Clean up.")
parser.add_argument("--no-mpi", action='store_true', help="Don't try to install with MPI support.")
args = parser.parse_args(sys.argv[1:])
if args.clean:
clean()
else:
build(mpi=not args.no_mpi)