Skip to content

Commit

Permalink
Add a clean subcommand to Tools/wasm/wasi.py (pythonGH-114274)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcannon authored and aisk committed Feb 11, 2024
1 parent 3516913 commit 24fa162
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions Tools/wasm/wasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@


CHECKOUT = pathlib.Path(__file__).parent.parent.parent

CROSS_BUILD_DIR = CHECKOUT / "cross-build"
BUILD_DIR = CROSS_BUILD_DIR / "build"
HOST_TRIPLE = "wasm32-wasi"
HOST_DIR = CROSS_BUILD_DIR / HOST_TRIPLE

LOCAL_SETUP = CHECKOUT / "Modules" / "Setup.local"
LOCAL_SETUP_MARKER = "# Generated by Tools/wasm/wasi.py\n".encode("utf-8")


def updated_env(updates={}):
"""Create a new dict representing the environment to use.
Expand Down Expand Up @@ -119,12 +123,11 @@ def build_python_path():
@subdir(BUILD_DIR, clean_ok=True)
def configure_build_python(context, working_dir):
"""Configure the build/host Python."""
local_setup = CHECKOUT / "Modules" / "Setup.local"
if local_setup.exists():
print(f"👍 {local_setup} exists ...")
if LOCAL_SETUP.exists():
print(f"👍 {LOCAL_SETUP} exists ...")
else:
print(f"📝 Touching {local_setup} ...")
local_setup.touch()
print(f"📝 Touching {LOCAL_SETUP} ...")
LOCAL_SETUP.write_bytes(LOCAL_SETUP_MARKER)

configure = [os.path.relpath(CHECKOUT / 'configure', working_dir)]
if context.args:
Expand Down Expand Up @@ -260,6 +263,17 @@ def build_all(context):
for step in steps:
step(context)

def clean_contents(context):
"""Delete all files created by this script."""
if CROSS_BUILD_DIR.exists():
print(f"🧹 Deleting {CROSS_BUILD_DIR} ...")
shutil.rmtree(CROSS_BUILD_DIR)

if LOCAL_SETUP.exists():
with LOCAL_SETUP.open("rb") as file:
if file.read(len(LOCAL_SETUP_MARKER)) == LOCAL_SETUP_MARKER:
print(f"🧹 Deleting generated {LOCAL_SETUP} ...")


def main():
default_host_runner = (f"{shutil.which('wasmtime')} run "
Expand Down Expand Up @@ -290,11 +304,13 @@ def main():
"Python)")
make_host = subcommands.add_parser("make-host",
help="Run `make` for the host/WASI")
clean = subcommands.add_parser("clean", help="Delete files and directories "
"created by this script")
for subcommand in build, configure_build, make_build, configure_host, make_host:
subcommand.add_argument("--quiet", action="store_true", default=False,
dest="quiet",
help="Redirect output from subprocesses to a log file")
for subcommand in build, configure_build, configure_host:
for subcommand in configure_build, configure_host:
subcommand.add_argument("--clean", action="store_true", default=False,
dest="clean",
help="Delete any relevant directories before building")
Expand All @@ -319,7 +335,8 @@ def main():
"make-build-python": make_build_python,
"configure-host": configure_wasi_python,
"make-host": make_wasi_python,
"build": build_all}
"build": build_all,
"clean": clean_contents}
dispatch[context.subcommand](context)


Expand Down

0 comments on commit 24fa162

Please sign in to comment.