-
Notifications
You must be signed in to change notification settings - Fork 229
/
dev.py
executable file
·370 lines (317 loc) · 13.4 KB
/
dev.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
"""
A tool to support the maintenance of templates and examples in this repository.
Something like a Makefile but written in Python for easier maintenance.
To list the available commands, run ./dev.py --help.
"""
import argparse
import glob
import tempfile
import typing
import shlex
import subprocess
from pathlib import Path
import json
import sys
import shutil
import os
THIS_DIRECTORY = Path(__file__).parent.absolute()
EXAMPLE_DIRECTORIES = [d for d in (THIS_DIRECTORY / 'examples').iterdir() if d.is_dir()]
TEMPLATE_DIRECTORIES = [
THIS_DIRECTORY / "template",
THIS_DIRECTORY / "template-reactless",
]
# Utilities function
def run_verbose(cmd_args, *args, **kwargs):
kwargs.setdefault("check", True)
cwd = kwargs.get('cwd')
message_suffix = f" [CWD: {Path(cwd).relative_to(THIS_DIRECTORY)}]" if cwd else ''
print(f"$ {shlex.join(cmd_args)}{message_suffix}", flush=True)
subprocess.run(cmd_args, *args, **kwargs)
# Commands
def cmd_all_npm_install(args):
"""Install all node dependencies for all examples"""
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
frontend_dir = next(project_dir.glob("*/frontend/"))
run_verbose(["npm", "install"], cwd=str(frontend_dir))
def cmd_all_npm_build(args):
"""Build javascript code for all examples and templates"""
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
frontend_dir = next(project_dir.glob("*/frontend/"))
run_verbose(["npm", "run", "build"], cwd=str(frontend_dir))
def cmd_e2e_build_images(args):
"""Build docker images for each component e2e tests"""
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
e2e_dir = next(project_dir.glob("**/e2e/"), None)
if e2e_dir and os.listdir(e2e_dir):
# Define the image tag for the docker image
streamlit_version = args.streamlit_version if not args.streamlit_wheel_file else 'custom'
image_tag = (
f"component-template:py-{args.python_version}-st-{streamlit_version}-component-{project_dir.parts[-1]}"
)
# Build the docker image with specified build arguments
docker_args = [
"docker",
"build",
".",
f"--build-arg=PYTHON_VERSION={args.python_version}",
f"--tag={image_tag}",
"--progress=plain",
]
if args.streamlit_wheel_file:
buildcontext_path = THIS_DIRECTORY / "buildcontext"
shutil.rmtree(buildcontext_path, ignore_errors=True)
buildcontext_path.mkdir()
shutil.copy(args.streamlit_wheel_file, buildcontext_path)
docker_args.extend([
f"--target=e2e_whl",
])
else:
docker_args.extend([
f"--build-arg=STREAMLIT_VERSION={args.streamlit_version}",
f"--target=e2e_pip",
])
run_verbose(
docker_args,
env={**os.environ, "DOCKER_BUILDKIT": "1"},
)
def cmd_e2e_run(args):
"""Run e2e tests for all examples and templates in separate docker images"""
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
container_name = project_dir.parts[-1]
image_tag = (
f"component-template:py-{args.python_version}-st-{args.streamlit_version}-component-{container_name}"
)
e2e_dir = next(project_dir.glob("**/e2e/"), None)
if e2e_dir and os.listdir(e2e_dir):
run_verbose([
"docker",
"run",
"--tty",
"--rm",
"--name", container_name,
"--volume", f"{e2e_dir.parent}/:/component/",
image_tag,
"/bin/sh", "-c", # Run a shell command inside the container
"find /component/dist/ -name '*.whl' | xargs -I {} echo '{}[devel]' | xargs pip install && " # Install whl package and dev dependencies
f"pytest -s --browser webkit --browser chromium --browser firefox --reruns 5 --capture=no" # Run pytest
])
def cmd_docker_images_cleanup(args):
"""Cleanup docker images and containers"""
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
container_name = project_dir.parts[-1]
image_name = (
f"component-template:py-{args.python_version}-st-{args.streamlit_version}-component-{container_name}"
)
e2e_dir = next(project_dir.glob("**/e2e/"), None)
if e2e_dir and os.listdir(e2e_dir):
# Remove the associated Docker image
run_verbose(["docker", "rmi", image_name])
def cmd_all_python_build_package(args):
"""Build wheel packages for all examples and templates"""
final_dist_directory = (THIS_DIRECTORY / "dist")
final_dist_directory.mkdir(exist_ok=True)
for project_dir in EXAMPLE_DIRECTORIES + TEMPLATE_DIRECTORIES:
run_verbose([sys.executable, "setup.py", "bdist_wheel", "--universal", "sdist"], cwd=str(project_dir))
wheel_file = next(project_dir.glob("dist/*.whl"))
shutil.copy(wheel_file, final_dist_directory)
def check_deps(template_package_json, current_package_json):
return (
check_deps_section(template_package_json, current_package_json, 'dependencies') +
check_deps_section(template_package_json, current_package_json, 'devDependencies')
)
def check_deps_section(template_package_json, current_package_json, section_name):
current_package_deps = current_package_json.get(section_name, dict())
template_package_deps = template_package_json.get(section_name, dict())
errors = []
for k, v in template_package_deps.items():
if k not in current_package_deps:
errors.append(f'Missing [{k}:{v}] in {section_name!r} section')
continue
current_version = current_package_deps[k]
if current_version != v:
errors.append(f'Invalid version of {k!r}. Expected: {v!r}. Current: {current_version!r}')
return errors
def cmd_example_check_deps(args):
"""Checks that dependencies of examples match the template"""
template_deps = json.loads((THIS_DIRECTORY / "template" / "my_component" / "frontend" / "package.json").read_text())
examples_package_jsons = sorted(next(d.glob("*/frontend/package.json")) for d in EXAMPLE_DIRECTORIES)
exit_code = 0
for examples_package_json in examples_package_jsons:
example_deps = json.loads(examples_package_json.read_text())
errors = check_deps(template_deps, example_deps)
if errors:
print(f"Found error in {examples_package_json.relative_to(THIS_DIRECTORY)!s}")
print("\n".join(errors))
print()
exit_code = 1
if exit_code == 0:
print("No errors")
sys.exit(exit_code)
def cmd_check_test_utils(args):
"""Check that e2e utils files are identical"""
file_list = glob.glob('**/e2e_utils.py', recursive=True)
if file_list:
reference_file = file_list[0]
else:
print("Cannot find e2e_utils.py files")
sys.exit(1)
for file_path in file_list:
run_verbose([
"git",
"--no-pager",
"diff",
"--no-index",
str(reference_file),
str(file_path),
])
class CookiecutterVariant(typing.NamedTuple):
replay_file: Path
repo_directory: Path
COOKIECUTTER_VARIANTS = [
CookiecutterVariant(
replay_file=THIS_DIRECTORY / ".github" / "replay-files" / "template.json",
repo_directory=THIS_DIRECTORY / "template",
),
CookiecutterVariant(
replay_file=THIS_DIRECTORY / ".github" / "replay-files" / "template-reactless.json",
repo_directory=THIS_DIRECTORY / "template-reactless",
),
]
def cmd_check_templates_using_cookiecutter(args):
"""Checks that templates have been generated by cookiecutter and have no unwanted changes."""
if shutil.which("cookiecutter") is None:
raise SystemExit("cookiecutter is not installed")
for cookiecutter_variant in COOKIECUTTER_VARIANTS:
replay_file_content = json.loads(cookiecutter_variant.replay_file.read_text())
with tempfile.TemporaryDirectory() as output_dir:
print(
f"Generating template with replay file: {cookiecutter_variant.replay_file.relative_to(THIS_DIRECTORY)}")
run_verbose(
[
"cookiecutter",
"--replay-file",
str(cookiecutter_variant.replay_file),
"--output-dir",
str(output_dir),
str(THIS_DIRECTORY / "cookiecutter"),
]
)
try:
print(f"Comparing rendered template with local version: {str(cookiecutter_variant.repo_directory)}")
output_template = (
Path(output_dir) / replay_file_content["cookiecutter"]["package_name"]
)
run_verbose(
[
"git",
"--no-pager",
"diff",
"--no-index",
str(output_template),
str(cookiecutter_variant.repo_directory),
]
)
except subprocess.CalledProcessError:
print("The rendered template contains unexpected changes files.")
print("To refresh, do the following:")
print()
render_cmd = ["./dev.py", "templates-update"]
print(f" $ {shlex.join(render_cmd)}")
sys.exit(1)
print("All correct. The template directory does not require refreshing.")
print()
def cmd_update_templates(args):
"""Updates rendered templates using cookiecutter template"""
if shutil.which("cookiecutter") is None:
raise SystemExit("cookiecutter is not installed")
for cookiecutter_variant in COOKIECUTTER_VARIANTS:
replay_file_content = json.loads(cookiecutter_variant.replay_file.read_text())
with tempfile.TemporaryDirectory() as tmp_dir:
output_dir = Path(tmp_dir) / "output-dir"
output_template = (
output_dir / replay_file_content["cookiecutter"]["package_name"]
)
print(f"Generating template with replay file: {cookiecutter_variant.replay_file.relative_to(THIS_DIRECTORY)}")
run_verbose(
[
"cookiecutter",
"--replay-file",
str(cookiecutter_variant.replay_file),
"--output-dir",
str(output_dir),
str(THIS_DIRECTORY / "cookiecutter"),
]
)
print(f"Copying rendered templates to {str(cookiecutter_variant.repo_directory.relative_to(THIS_DIRECTORY))!r}")
shutil.rmtree(cookiecutter_variant.repo_directory, ignore_errors=True)
shutil.copytree(output_template, cookiecutter_variant.repo_directory)
print()
ARG_STREAMLIT_VERSION = ("--streamlit-version", "latest", "Streamlit version for which tests will be run.")
ARG_STREAMLIT_WHEEL_FILE = ("--streamlit-wheel-file", "", "")
ARG_PYTHON_VERSION = ("--python-version", os.environ.get("PYTHON_VERSION", "3.11.4"), "Python version for which tests will be run.")
COMMANDS = {
"all-npm-install": {
"fn": cmd_all_npm_install
},
"all-npm-build": {
"fn": cmd_all_npm_build
},
"all-python-build-package": {
"fn": cmd_all_python_build_package
},
"examples-check-deps": {
"fn": cmd_example_check_deps
},
"templates-check-not-modified": {
"fn": cmd_check_templates_using_cookiecutter
},
"templates-update": {
"fn": cmd_update_templates
},
"e2e-utils-check": {
"fn": cmd_check_test_utils
},
"e2e-build-images": {
"fn": cmd_e2e_build_images,
"arguments": [
ARG_STREAMLIT_VERSION,
ARG_STREAMLIT_WHEEL_FILE,
ARG_PYTHON_VERSION,
]
},
"e2e-run-tests": {
"fn": cmd_e2e_run,
"arguments": [
ARG_STREAMLIT_VERSION,
ARG_PYTHON_VERSION,
]
},
"docker-images-cleanup": {
"fn": cmd_docker_images_cleanup,
"arguments": [
(*ARG_STREAMLIT_VERSION[:2], f"Streamlit version used to create the Docker resources"),
(*ARG_STREAMLIT_WHEEL_FILE[:2], f""),
(*ARG_PYTHON_VERSION[:2], f"Python version used to create the Docker resources")
]
}
}
# Parser
def get_parser():
parser = argparse.ArgumentParser(prog=__file__, description=__doc__)
subparsers = parser.add_subparsers(dest="subcommand", metavar="COMMAND")
subparsers.required = True
for command_name, command_info in COMMANDS.items():
command_fn = command_info['fn']
subparser = subparsers.add_parser(command_name, help=command_fn.__doc__)
for arg_name, arg_default, arg_help in command_info.get('arguments', []):
subparser.add_argument(arg_name, default=arg_default, help=arg_help)
subparser.set_defaults(func=command_fn)
return parser
# Main function
def main():
parser = get_parser()
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()