Skip to content

Commit 712b4f9

Browse files
authored
Merge pull request #2 from cloudflare/ggu/generate_import_tests
Generate import tests and include them in pyodide_bucket.bzl
2 parents 112d536 + c22ad60 commit 712b4f9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/import_tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pathlib import Path
2+
3+
from typing import List
4+
import yaml
5+
6+
def gen(packages: List[str], packages_dir = Path("packages")):
7+
res = {}
8+
for package in packages:
9+
if package == "test": continue
10+
try:
11+
with open(packages_dir / package / "meta.yaml", "r") as file:
12+
imports = set()
13+
meta = yaml.load(file, Loader=yaml.FullLoader)
14+
# add package -> top-level if it exists
15+
if "package" in meta:
16+
if "top-level" in meta["package"]:
17+
imports.update(meta["package"]["top-level"])
18+
# add test -> imports if it exists
19+
if "test" in meta:
20+
if "imports" in meta["test"]:
21+
imports.update(meta["test"]["imports"])
22+
23+
if len(imports) > 0:
24+
res[package] = list(imports)
25+
except FileNotFoundError:
26+
print(f"package {package}'s meta.yaml not found")
27+
return res

packages/script.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import hashlib
1212
from datetime import datetime
1313

14+
import import_tests
15+
1416
def normalize(name):
1517
return re.sub(r"[-_.]+", "-", name).lower()
1618

@@ -27,6 +29,11 @@ def gen_bzl_config(tag, dist):
2729
all_wheels_bytes = (dist / "all_wheels.zip").read_bytes()
2830
all_wheels_hash = hashlib.sha256(all_wheels_bytes).hexdigest()
2931

32+
with open(dist / "pyodide-lock.json", "r") as file:
33+
lock = json.load(file)
34+
packages = [package["name"] for package in lock["packages"].values()]
35+
imports_to_test = import_tests.gen(packages)
36+
3037
with open("pyodide_bucket.bzl", "w") as f:
3138
f.write("# Do not edit this file by hand. See docs/pyodide.md for info on how to generate it.\n")
3239
f.write("# These variables are factored out here because they are being shared by the WORKSPACE files in\n")
@@ -35,7 +42,9 @@ def gen_bzl_config(tag, dist):
3542
f.write("PYODIDE_GITHUB_RELEASE_URL = \"" + github_url + "\"\n")
3643
f.write("PYODIDE_LOCK_SHA256 = \"" + lock_hash + "\"\n")
3744
f.write("PYODIDE_PACKAGES_TAR_ZIP_SHA256 = \"" + zip_hash + "\"\n")
38-
f.write("PYODIDE_ALL_WHEELS_ZIP_SHA256 = \"" + all_wheels_hash + "\"\n")
45+
f.write("PYODIDE_ALL_WHEELS_ZIP_SHA256 = \"" + all_wheels_hash + "\"\n\n")
46+
f.write("# IMPORTANT: when updating this file in git, check the diff to make sure none of the imports below are being removed unexpectedly\n")
47+
f.write("PYODIDE_IMPORTS_TO_TEST = " + json.dumps(imports_to_test, indent=3, sort_keys=True) + "\n")
3948

4049
# creates a package bundle .tar.zip file to be bundled in with edgeworker
4150
# the resulting bundle is written to dist/pyodide_packages.tar.zip

0 commit comments

Comments
 (0)