stepmold / make_bundle.py
ahkamboh's picture
stepmold Space: run example packs in the browser
66623d7 verified
Raw History Blame Contribute Delete
3.48 kB
"""Build stepmold-bundle.json: the copy of stepmold the Space runs in the browser.
The page fetches this one file, writes its contents into Pyodide's filesystem and imports
stepmold from there. It holds the runtime package (`stepmold/` and `stepmold/backends/`,
the same two packages the wheel ships) and the seven example packs, taken from one
pinned commit of https://gitlab.com/ahkamboh/stepmold.
python3 make_bundle.py # download COMMIT from GitLab
python3 make_bundle.py --from ../stepmold # copy from a local checkout instead
Run it again after changing COMMIT, and commit the new bundle.
"""
import argparse
import io
import json
import os
import sys
import tarfile
import urllib.request
REPO = "https://gitlab.com/ahkamboh/stepmold"
COMMIT = "46f9985f8dc4aae7f92881aa2750a3e57f3d920a"
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stepmold-bundle.json")
TEXT = (".py", ".yaml", ".json", ".jsonl", ".txt", ".md")
def wanted(path):
"""Runtime modules, the example packs, and the license. Nothing else."""
if "__pycache__" in path or not path.endswith(TEXT + ("LICENSE",)):
return False
if path == "LICENSE":
return True
if path.startswith("stepmold/"):
# stepmold.build is the compiler; the wheel leaves it out and so does this.
return path.count("/") == 1 or path.startswith("stepmold/backends/")
return path.startswith("examples/")
def from_gitlab(commit):
url = "%s/-/archive/%s/stepmold-%s.tar.gz" % (REPO, commit, commit)
request = urllib.request.Request(url, headers={"User-Agent": "stepmold-space"})
with urllib.request.urlopen(request, timeout=60) as response:
archive = tarfile.open(fileobj=io.BytesIO(response.read()), mode="r:gz")
files = {}
for member in archive.getmembers():
if not member.isfile():
continue
path = member.name.split("/", 1)[1]
if wanted(path):
files[path] = archive.extractfile(member).read().decode("utf-8")
return files
def from_checkout(root):
files = {}
for folder, _dirs, names in os.walk(root):
for name in names:
full = os.path.join(folder, name)
path = os.path.relpath(full, root).replace(os.sep, "/")
if wanted(path):
with open(full, encoding="utf-8") as handle:
files[path] = handle.read()
return files
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--from", dest="checkout",
help="a local stepmold checkout to copy instead of downloading")
parser.add_argument("--commit", default=COMMIT, help="the commit to download")
args = parser.parse_args(argv)
files = from_checkout(args.checkout) if args.checkout else from_gitlab(args.commit)
if "stepmold/__init__.py" not in files:
sys.exit("no stepmold/__init__.py in the source; is that a stepmold checkout?")
bundle = {
"source": REPO,
"commit": None if args.checkout else args.commit,
"files": dict(sorted(files.items())),
}
with open(OUT, "w", encoding="utf-8") as handle:
json.dump(bundle, handle, ensure_ascii=False, indent=0, sort_keys=False)
handle.write("\n")
size = os.path.getsize(OUT)
print("%s: %d files, %.0f KB" % (os.path.basename(OUT), len(files), size / 1024.0))
if __name__ == "__main__":
main()