forked from emscripten-forge/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
240 lines (199 loc) · 7.03 KB
/
builder.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
from boa.core.monkeypatch import *
from boa.core.run_build import run_build
from boa.pyapi import py_build
import subprocess
import os
import json
import warnings
import tempfile
import rich
from collections import OrderedDict
import functools
import tempfile
import shutil
import empack
import glob
from testing.package_testing import test_package as test_package_impl
from empack.file_patterns import pkg_file_filter_from_yaml
from contextlib import contextmanager
from mamba.utils import init_api_context
import libmambapy as api
from pathlib import Path
RECIPES_SUBDIR_MAPPING = OrderedDict(
[("recipes", ""), ("recipes_emscripten", "emscripten-32")]
)
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_PATH = os.path.join(THIS_DIR, "empack_config.yaml")
PKG_FILE_FILTER = pkg_file_filter_from_yaml(CONFIG_PATH)
CONDA_PREFIX = os.environ.get("CONDA_PREFIX")
if CONDA_PREFIX is None:
raise RuntimeError(
"environment varialbe `CONDA_PREFIX` is not set but needed to run this script"
)
CONDA_BLD_DIR = os.path.join(CONDA_PREFIX, "conda-bld")
Path(CONDA_BLD_DIR).mkdir(exist_ok=True)
from typing import List, Optional
import typer
import rich
app = typer.Typer(pretty_exceptions_show_locals=False)
build_app = typer.Typer()
app.add_typer(build_app, name="build")
@contextmanager
def restore_cwd():
base_work_dir = os.getcwd()
yield
os.chdir(base_work_dir)
def find_files_with_changes(old, new):
cmd = ["git", "diff", "--name-only", old, new]
result = subprocess.run(
cmd,
shell=False,
check=True,
capture_output=True,
)
output_str = result.stdout.decode()
error_str = result.stderr.decode()
if len(error_str):
print(error_str)
files_with_changes = output_str.splitlines()
# print(files_with_changes)
return files_with_changes
def find_recipes_with_changes(old, new):
files_with_changes = find_files_with_changes(old=old, new=new)
recipes_with_changes = {k: set() for k in RECIPES_SUBDIR_MAPPING.keys()}
# print("recipes_with_changes", recipes_with_changes)
for subdir in RECIPES_SUBDIR_MAPPING.keys():
for file_with_change in files_with_changes:
if file_with_change.startswith(f"recipes/{subdir}/"):
# print(file_with_change)
file_with_change = file_with_change[len(f"recipes/{subdir}/") :]
file_with_change = os.path.normpath(file_with_change)
recipe = file_with_change.split(os.sep)[0]
recipes_with_changes[subdir].add(recipe)
for subdir in RECIPES_SUBDIR_MAPPING.keys():
recipes_with_changes[subdir] = sorted(list(recipes_with_changes[subdir]))
return recipes_with_changes
def test_package(recipe, work_dir):
# recipe_dir = os.path.join(recipes_dir, recipe_name)
print(f"Test recipe: {recipe} in work_dir: {work_dir}")
test_package_impl(recipe=recipe, work_dir=work_dir, conda_bld_dir=CONDA_BLD_DIR)
def cleanup():
do_not_delete = ["noarch", "linux-64", "emscripten-32", "icons"]
do_not_delete = [os.path.join(CONDA_BLD_DIR, d) for d in do_not_delete]
for dirname in glob.iglob(os.path.join(CONDA_BLD_DIR, "**"), recursive=False):
if os.path.isdir(dirname):
if dirname not in do_not_delete and "_" in dirname:
shutil.rmtree(dirname)
def post_build_callback(
recipe, target_platform, sorted_outputs, final_names, skip_tests, work_dir
):
if target_platform == "emscripten-32" and (not skip_tests):
with restore_cwd():
test_package(recipe=recipe, work_dir=work_dir)
cleanup()
def boa_build(
work_dir,
platform,
target=None,
recipe_dir=None,
skip_tests=False,
skip_existing=False,
):
target_platform = None
if platform:
target_platform = platform
str_skip_existing = "default"
if skip_existing:
str_skip_existing = "yes"
cb = functools.partial(
post_build_callback, skip_tests=skip_tests, work_dir=work_dir
)
py_build(
target=target,
recipe_dir=recipe_dir,
target_platform=target_platform,
skip_existing=str_skip_existing,
post_build_callback=cb,
)
os.chdir(work_dir)
@build_app.command()
def directory(
recipes_dir,
emscripten_32: Optional[bool] = typer.Option(False),
skip_tests: Optional[bool] = typer.Option(False),
skip_existing: Optional[bool] = typer.Option(False),
):
work_dir = os.getcwd()
platform = ""
if emscripten_32:
platform = "emscripten-32"
boa_build(
work_dir=work_dir,
target=recipes_dir,
recipe_dir=None,
platform=platform,
skip_tests=skip_tests,
skip_existing=skip_existing,
)
@build_app.command()
def explicit(
recipe_dir,
emscripten_32: Optional[bool] = typer.Option(False),
skip_tests: Optional[bool] = typer.Option(False),
skip_existing: Optional[bool] = typer.Option(False),
):
work_dir = os.getcwd()
assert os.path.isdir(recipe_dir), f"{recipe_dir} is not a dir"
platform = ""
if emscripten_32:
print("WITH EM")
platform = "emscripten-32"
boa_build(
work_dir=work_dir,
target=recipe_dir,
platform=platform,
skip_tests=skip_tests,
skip_existing=skip_existing,
)
@build_app.command()
def changed(
root_dir,
old,
new,
dryrun: Optional[bool] = typer.Option(False),
skip_tests: Optional[bool] = typer.Option(False),
skip_existing: Optional[bool] = typer.Option(False),
):
work_dir = os.getcwd()
recipes_dir = os.path.join(root_dir, "recipes")
recipes_with_changes_per_subdir = find_recipes_with_changes(old=old, new=new)
rich.pretty.pprint(recipes_with_changes_per_subdir)
for subdir, recipe_with_changes in recipes_with_changes_per_subdir.items():
# create a temp dir and copy all changed recipes
# to that dir (because Then we can let boa do the
# topological sorting)
with tempfile.TemporaryDirectory() as tmp_folder_root:
tmp_recipes_root_str = os.path.join(
tmp_folder_root, "recipes", "recipes_per_platform"
)
os.makedirs(tmp_folder_root, exist_ok=True)
for recipe_with_change in recipe_with_changes:
recipe_dir = os.path.join(recipes_dir, subdir, recipe_with_change)
# diff can shown deleted recipe as changed
if os.path.isdir(recipe_dir):
tmp_recipe_dir = os.path.join(
tmp_recipes_root_str, recipe_with_change
)
# os.mkdir(tmp_recipe_dir)
shutil.copytree(recipe_dir, tmp_recipe_dir)
print([x[0] for x in os.walk(tmp_recipes_root_str)])
boa_build(
work_dir=work_dir,
target=tmp_recipes_root_str,
recipe_dir=None,
platform=RECIPES_SUBDIR_MAPPING[subdir],
skip_tests=skip_tests,
skip_existing=skip_existing,
)
if __name__ == "__main__":
app()