-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathbuild-locally.py
132 lines (111 loc) · 4.16 KB
/
build-locally.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
#!/bin/sh
"""exec" "python3" "$0" "$@" #""" # fmt: off # fmt: on
# The line above this comment is a bash / sh / zsh guard
# to stop people from running it with the wrong interpreter
import glob
import os
import platform
import subprocess
import sys
from argparse import ArgumentParser
from subprocess import check_output
def verify_system():
branch_name = check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], text=True).strip()
if branch_name == "main":
raise RuntimeError(
"You should run build-locally from a new branch, not 'main'. "
"Create a new one with:\n\n"
" git checkout -b your-chosen-branch-name\n"
)
def setup_environment(ns):
os.environ["CONFIG"] = ns.config
os.environ["UPLOAD_PACKAGES"] = "False"
os.environ["IS_PR_BUILD"] = "True"
if ns.debug:
os.environ["BUILD_WITH_CONDA_DEBUG"] = "1"
if ns.output_id:
os.environ["BUILD_OUTPUT_ID"] = ns.output_id
if "MINIFORGE_HOME" not in os.environ:
os.environ["MINIFORGE_HOME"] = os.path.join(
os.path.dirname(__file__), "miniforge3"
)
if "OSX_SDK_DIR" not in os.environ:
os.environ["OSX_SDK_DIR"] = os.path.join(os.path.dirname(__file__), "SDKs")
# The default cache location might not be writable using docker on macOS.
if ns.config.startswith("linux") and platform.system() == "Darwin":
os.environ["CONDA_FORGE_DOCKER_RUN_ARGS"] = (
"-e RATTLER_CACHE_DIR=/tmp/rattler_cache"
)
def run_docker_build(ns):
script = ".scripts/run_docker_build.sh"
subprocess.check_call([script])
def run_osx_build(ns):
script = ".scripts/run_osx_build.sh"
subprocess.check_call([script])
def run_win_build(ns):
script = ".scripts/run_win_build.bat"
subprocess.check_call(["cmd", "/D", "/Q", "/C", f"CALL {script}"])
def verify_config(ns):
choices_filter = ns.filter or "*"
valid_configs = {
os.path.basename(f)[:-5]
for f in glob.glob(f".ci_support/{choices_filter}.yaml")
}
if choices_filter != "*":
print(f"filtering for '{choices_filter}.yaml' configs")
print(f"valid configs are {valid_configs}")
if ns.config in valid_configs:
print("Using " + ns.config + " configuration")
return
elif len(valid_configs) == 1:
ns.config = valid_configs.pop()
print("Found " + ns.config + " configuration")
elif ns.config is None:
print("config not selected, please choose from the following:\n")
selections = list(enumerate(sorted(valid_configs), 1))
for i, c in selections:
print(f"{i}. {c}")
try:
s = input("\n> ")
except KeyboardInterrupt:
print("\nno option selected, bye!", file=sys.stderr)
sys.exit(1)
idx = int(s) - 1
ns.config = selections[idx][1]
print(f"selected {ns.config}")
else:
raise ValueError("config " + ns.config + " is not valid")
if ns.config.startswith("osx") and platform.system() == "Darwin":
if "OSX_SDK_DIR" not in os.environ:
raise RuntimeError(
"Need OSX_SDK_DIR env variable set. Run 'export OSX_SDK_DIR=/opt'"
"to download the SDK automatically to '/opt/MacOSX<ver>.sdk'"
)
def main(args=None):
p = ArgumentParser("build-locally")
p.add_argument("config", default=None, nargs="?")
p.add_argument(
"--filter",
default=None,
help="Glob string to filter which build choices are presented in interactive mode.",
)
p.add_argument(
"--debug",
action="store_true",
help="Setup debug environment using `conda debug`",
)
p.add_argument("--output-id", help="If running debug, specify the output to setup.")
ns = p.parse_args(args=args)
verify_system()
verify_config(ns)
setup_environment(ns)
if ns.config.startswith("linux") or (
ns.config.startswith("osx") and platform.system() == "Linux"
):
run_docker_build(ns)
elif ns.config.startswith("osx"):
run_osx_build(ns)
elif ns.config.startswith("win"):
run_win_build(ns)
if __name__ == "__main__":
main()