Skip to content

Commit

Permalink
cleanup: API improvements (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored Sep 25, 2024
1 parent b5aad15 commit 7e02e98
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 45 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
PYTHON_FILES=lib/src/prelude.py bin/src/invoke.py build.py

build:
./build.py build

install:
./build.py install

format:
uv run ruff format lib/src/prelude.py
uv run ruff format bin/src/invoke.py
uv run ruff format build.py
uv run ruff format $(PYTHON_FILES)

check:
uv run ruff check lib/src/prelude.py
uv run ruff check bin/src/invoke.py
uv run ruff check build.py
uv run ruff check $(PYTHON_FILES)

clean:
rm -rf bin/target lib/target
Expand Down
2 changes: 1 addition & 1 deletion bin/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn find_deps() -> PathBuf {

directories::BaseDirs::new()
.unwrap()
.data_local_dir()
.data_dir()
.join("extism-py")
}

Expand Down
2 changes: 2 additions & 0 deletions bin/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub struct Options {
#[structopt(parse(from_os_str))]
pub input_py: PathBuf,

// #[structopt(parse(from_os_str))]
// pub other_files: Vec<PathBuf>,
#[structopt(short = "o", parse(from_os_str), default_value = "index.wasm")]
pub output: PathBuf,

Expand Down
23 changes: 15 additions & 8 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def bin_dir() -> Path:
if system in ["linux", "darwin"]:
return home / ".local" / "bin"
elif system == "windows":
return Path(os.getenv("APPDATA")) / "Python" / "Scripts"
return Path(os.getenv("USERHOME"))
else:
raise OSError(f"Unsupported OS {system}")

Expand All @@ -42,6 +42,13 @@ def data_dir() -> Path:
raise OSError(f"Unsupported OS {system}")


def exe_file() -> str:
if system == "windows":
return "extism-py.exe"
else:
return "extism-py"


def run_subprocess(command, cwd=None, quiet=False):
try:
logging.info(f"Running command: {' '.join(command)} in {cwd or '.'}")
Expand Down Expand Up @@ -78,7 +85,7 @@ def do_build(args):
check_rust_installed()
run_subprocess(["cargo", "build", "--release"], cwd="./lib", quiet=args.quiet)
run_subprocess(["cargo", "build", "--release"], cwd="./bin", quiet=args.quiet)
shutil.copy2(Path("./bin/target/release/extism-py"), Path("./extism-py"))
shutil.copy2(Path("./bin/target/release") / exe_file(), Path(".") / exe_file())
logging.info("Build completed successfully.")


Expand All @@ -89,16 +96,16 @@ def do_install(args):
bin_dir.mkdir(parents=True, exist_ok=True)
data_dir.mkdir(parents=True, exist_ok=True)

dest_path = bin_dir / "extism-py"
dest_path = bin_dir / exe_file()
logging.info(f"Copying binary to {dest_path}")
shutil.copy2(Path("./bin/target/release/extism-py"), dest_path)
shutil.copy2(Path("./bin/target/release") / exe_file(), dest_path)

logging.info(f"Copying data files to {data_dir}")
shutil.copytree(
Path("./lib/target/wasm32-wasi/wasi-deps/usr"), data_dir, dirs_exist_ok=True
)

logging.info(f"extism-py installed to {bin_dir}")
logging.info(f"{exe_file()} installed to {bin_dir}")
logging.info(f"Data files installed to {data_dir}")
logging.info("Installation completed successfully.")

Expand All @@ -107,15 +114,15 @@ def do_clean(args):
logging.info("Cleaning build artifacts...")
shutil.rmtree("./lib/target", ignore_errors=True)
shutil.rmtree("./bin/target", ignore_errors=True)
if Path("./extism-py").exists():
Path("./extism-py").unlink()
if (Path(".") / exe_file()).exists():
(Path(".") / exe_file()).unlink()
logging.info("Clean completed successfully.")


def get_version():
try:
result = subprocess.run(
["extism-py", "--version"], capture_output=True, text=True, check=True
[exe_file(), "--version"], capture_output=True, text=True, check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError:
Expand Down
4 changes: 2 additions & 2 deletions examples/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def reflect(x: str) -> str:
pass

@extism.import_fn("example", "update_dict")
def update_dict(x: dict) -> dict:
def update_dict(x: extism.JsonObject) -> extism.JsonObject:
pass

@extism.plugin_fn
Expand All @@ -22,5 +22,5 @@ def count_vowels():
if ch in ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u']:
total += 1
extism.log(extism.LogLevel.Info, "Hello!")
extism.output_json(update_dict({"count": total}))
extism.output(update_dict({"count": total}))

Loading

0 comments on commit 7e02e98

Please sign in to comment.