Skip to content

Commit dbb2721

Browse files
committed
Support building local codecs with numcodecs-wasm-materialize
1 parent aa2a321 commit dbb2721

File tree

3 files changed

+58
-17
lines changed
  • .github/workflows
  • crates/numcodecs-wasm-builder/src
  • py/numcodecs-wasm-materialize/src/numcodecs_wasm_materialize

3 files changed

+58
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ jobs:
296296
rm .python-version
297297
uv python install ${{ matrix.python }}
298298
uv sync && uv pip install .
299-
uv run python3 -m numcodecs_wasm_materialize zfp
299+
uv run python3 -m numcodecs_wasm_materialize zfp --local
300300
301301
- name: Test importing the ZFP WASM codec
302302
run: |

crates/numcodecs-wasm-builder/src/main.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use std::{
55
collections::HashMap,
6-
fs, io,
6+
env, fs, io,
77
path::{Path, PathBuf},
88
process::Command,
99
str::FromStr,
@@ -30,6 +30,10 @@ struct Args {
3030
/// Path to which the wasm file is output
3131
#[arg(long, short)]
3232
output: PathBuf,
33+
34+
/// Compile the local crate instead of the published one
35+
#[arg(long)]
36+
local: bool,
3337
}
3438

3539
fn main() -> io::Result<()> {
@@ -47,8 +51,13 @@ fn main() -> io::Result<()> {
4751
eprintln!("creating {}", target_dir.display());
4852
fs::create_dir_all(&target_dir)?;
4953

50-
let crate_dir =
51-
create_codec_wasm_component_crate(&scratch_dir, &args.crate_, &args.version, &args.codec)?;
54+
let crate_dir = create_codec_wasm_component_crate(
55+
&scratch_dir,
56+
&args.crate_,
57+
&args.version,
58+
&args.codec,
59+
args.local,
60+
)?;
5261
copy_buildenv_to_crate(&crate_dir)?;
5362

5463
let nix_env = NixEnv::new(&crate_dir)?;
@@ -72,6 +81,7 @@ fn create_codec_wasm_component_crate(
7281
crate_: &str,
7382
version: &Version,
7483
codec: &str,
84+
local: bool,
7585
) -> io::Result<PathBuf> {
7686
let crate_dir = scratch_dir.join(format!("{crate_}-wasm-{version}"));
7787
eprintln!("crate_dir={}", crate_dir.display());
@@ -81,6 +91,25 @@ fn create_codec_wasm_component_crate(
8191
}
8292
fs::create_dir_all(&crate_dir)?;
8393

94+
let (numcodecs_wasm_logging_path, numcodecs_wasm_guest_path, numcodecs_my_codec_path) = if local
95+
{
96+
let numcodecs = Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("..");
97+
eprintln!("looking for local workspace in {}", numcodecs.display());
98+
let numcodecs = numcodecs.canonicalize()?;
99+
let numcodecs_wasm_logging = numcodecs.join("crates").join("numcodecs-wasm-logging");
100+
let numcodecs_wasm_guest = numcodecs.join("crates").join("numcodecs-wasm-guest");
101+
let numcodecs_my_codec = numcodecs
102+
.join("codecs")
103+
.join(crate_.strip_prefix("numcodecs-").unwrap_or(crate_));
104+
(
105+
format!(r#" path = "{}","#, numcodecs_wasm_logging.display()),
106+
format!(r#" path = "{}","#, numcodecs_wasm_guest.display()),
107+
format!(r#" path = "{}","#, numcodecs_my_codec.display()),
108+
)
109+
} else {
110+
(String::new(), String::new(), String::new())
111+
};
112+
84113
fs::write(
85114
crate_dir.join("Cargo.toml"),
86115
format!(
@@ -95,9 +124,9 @@ edition = "2024"
95124
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
96125
97126
[dependencies]
98-
numcodecs-wasm-logging = {{ version = "0.2", default-features = false }}
99-
numcodecs-wasm-guest = {{ version = "0.3", default-features = false }}
100-
numcodecs-my-codec = {{ package = "{crate_}", version = "{version}", default-features = false }}
127+
numcodecs-wasm-logging = {{ version = "0.2",{numcodecs_wasm_logging_path} default-features = false }}
128+
numcodecs-wasm-guest = {{ version = "0.3",{numcodecs_wasm_guest_path} default-features = false }}
129+
numcodecs-my-codec = {{ package = "{crate_}", version = "{version}",{numcodecs_my_codec_path} default-features = false }}
101130
"#
102131
),
103132
)?;

py/numcodecs-wasm-materialize/src/numcodecs_wasm_materialize/__main__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import argparse
12
import re
23
import shlex
34
import shutil
45
import subprocess
5-
import sys
66
from pathlib import Path
77

88
import toml
@@ -13,12 +13,23 @@
1313
template_path = repo_path / "py" / "numcodecs-wasm-template"
1414
dist_path = repo_path / "py" / "numcodecs-wasm-materialize" / "dist"
1515

16-
codec_pattern = sys.argv[1]
16+
parser = argparse.ArgumentParser(prog="numcodecs_wasm_materialize")
17+
parser.add_argument(
18+
"codec",
19+
type=re.compile,
20+
help="regex pattern for the codecs to materialize",
21+
)
22+
parser.add_argument(
23+
"--local",
24+
action="store_true",
25+
help="compile the local codec instead of the published one",
26+
)
27+
args = parser.parse_args()
1728

1829
for c in (repo_path / "codecs").iterdir():
1930
codec = c.name
2031

21-
if not codec_pattern or re.match(codec_pattern, codec) is None:
32+
if not args.codec or re.match(args.codec, codec) is None:
2233
continue
2334

2435
codec_crate_path = repo_path / "codecs" / codec
@@ -77,10 +88,11 @@
7788
subprocess.run(
7889
shlex.split(
7990
"cargo run -p numcodecs-wasm-builder --"
80-
f" --crate numcodecs-{templates['crate-suffix']}"
81-
f" --version {templates['crate-version']}"
82-
f" --codec {templates['codec-path']}"
83-
f" --output {staging_path / 'src' / ('numcodecs_wasm_' + templates['package_suffix']) / 'codec.wasm'}"
91+
+ f" --crate numcodecs-{templates['crate-suffix']}"
92+
+ f" --version {templates['crate-version']}"
93+
+ f" --codec {templates['codec-path']}"
94+
+ f" --output {staging_path / 'src' / ('numcodecs_wasm_' + templates['package_suffix']) / 'codec.wasm'}"
95+
+ (" --local" if args.local else "")
8496
),
8597
check=True,
8698
)
@@ -98,16 +110,16 @@
98110
subprocess.run(
99111
shlex.split(
100112
f"uv run python3 {Path(__file__).parent / 'stub.py'} "
101-
f"{'numcodecs_wasm_' + templates['package_suffix']} src"
113+
+ f"{'numcodecs_wasm_' + templates['package_suffix']} src"
102114
),
103115
check=True,
104116
cwd=staging_path,
105117
)
106118
subprocess.run(
107119
shlex.split(
108120
f'uv run python3 -c "from {"numcodecs_wasm_" + templates["package_suffix"]} '
109-
f'import {templates["CodecName"]} as Codec; '
110-
f'assert Codec.codec_id == {templates["codec-id"]!r}, Codec.codec_id"'
121+
+ f"import {templates['CodecName']} as Codec; "
122+
+ f'assert Codec.codec_id == {templates["codec-id"]!r}, Codec.codec_id"'
111123
),
112124
check=True,
113125
cwd=staging_path,

0 commit comments

Comments
 (0)