Skip to content

Commit 7944135

Browse files
committed
chore: resolve python artifacts during python_wasi init
1 parent a960ec6 commit 7944135

File tree

7 files changed

+51
-24
lines changed

7 files changed

+51
-24
lines changed

libs/common/src/typegraph/runtimes/python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use serde_json::Value;
99

1010
#[cfg_attr(feature = "codegen", derive(JsonSchema))]
1111
#[derive(Serialize, Deserialize, Clone, Debug)]
12-
#[serde(rename_all = "lowercase")]
12+
#[serde(rename_all = "camelCase")]
1313
pub struct ModuleMatData {
14-
pub python_artifact: String,
14+
pub python_artifact: IndexMap<String, Value>,
1515
pub deps: Vec<String>,
1616
pub deps_meta: Option<Vec<IndexMap<String, Value>>>,
1717
}

typegate/src/runtimes/python_wasi/python_wasi.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import * as ast from "graphql/ast";
1414

1515
const logger = getLogger(import.meta);
1616

17+
interface Artifact {
18+
path: string;
19+
hash: string;
20+
size: number;
21+
}
22+
1723
function generateVmIdentifier(mat: Materializer, uuid: string) {
1824
const { mod } = mat.data ?? {};
1925
let identifier = "";
@@ -71,34 +77,40 @@ export class PythonWasiRuntime extends Runtime {
7177
const pyModMat = typegraph.materializers[m.data.mod as number];
7278

7379
// resolve the python module artifacts/files
74-
const { pythonArtifact, _depsMeta } = pyModMat.data;
80+
const { pythonArtifact, depsMeta: depArtifacts } = pyModMat.data;
7581

76-
// const deserialized_deps_meta =
82+
const deps = depArtifacts as Artifact[];
7783

78-
// const repr = await structureRepr(code);
7984
const vmId = generateVmIdentifier(m, uuid);
8085

81-
const modName = basename(pythonArtifact as string);
86+
const artifact = pythonArtifact as Artifact;
87+
const modName = basename(artifact.path);
8288

8389
// TODO: move this logic to postprocess or python runtime
84-
// m.data.name = `${modName}.${m.data.name as string}`;
85-
90+
m.data.name = `${modName}.${m.data.name as string}`;
91+
console.log("*************", m.data.name);
8692
logger.info(`setup vm "${vmId}" for module ${modName}`);
8793
const vm = new PythonVirtualMachine();
8894

8995
// for python modules, imports must be inside a folder above or same directory
90-
const _typegraph = typegate.register.get(typegraphName)!;
91-
const artifact =
92-
_typegraph.tg.tg.meta.artifacts[pythonArtifact as string];
9396
const artifactMeta = {
9497
typegraphName: typegraphName,
9598
relativePath: artifact.path,
9699
hash: artifact.hash,
97100
sizeInBytes: artifact.size,
98101
};
102+
const depMetas = deps.map((dep) => {
103+
return {
104+
typegraphName: typegraphName,
105+
relativePath: dep.path,
106+
hash: dep.hash,
107+
sizeInBytes: dep.size,
108+
};
109+
});
99110

100111
const entryPointFullPath = await typegate.artifactStore.getLocalPath(
101112
artifactMeta,
113+
depMetas,
102114
);
103115
const sourceCode = Deno.readTextFileSync(entryPointFullPath);
104116

typegate/src/typegate/artifacts/mod.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ export abstract class ArtifactStore {
117117
tgName: string,
118118
): Promise<[string, number]> {
119119
const expiresIn = 5 * 60;
120-
const token = await signJWT({ expiresIn }, expiresIn);
121-
const url = new URL(getUploadPath(tgName), origin);
120+
const uuid = crypto.randomUUID();
121+
const token = await signJWT({ uuid }, expiresIn);
122+
const url = new URL(
123+
`${getUploadPath(tgName)}`,
124+
origin,
125+
);
122126
url.searchParams.set("token", token);
123127
return [url.toString(), jwt.getNumericDate(expiresIn)];
124128
}

typegate/tests/runtimes/python_wasi/python_wasi_test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ Meta.test({
342342
await gql`
343343
query {
344344
identityMod(input: {
345-
a: "hello",
346-
b: [1, 2, "three"],
347-
}) {
348-
a
349-
b
350-
}
345+
a: "hello",
346+
b: [1, 2, "three"],
347+
}) {
348+
a
349+
b
350+
}
351351
}
352352
`
353353
.expectData({

typegraph/core/src/conversion/runtimes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ impl MaterializerConverter for PythonMaterializer {
230230
}
231231
Module(module) => {
232232
let data = serde_json::from_value(json!({
233-
"python_artifact": module.file,
233+
"pythonArtifact":json!({
234+
"path": module.file
235+
}),
234236
"deps": module.deps,
235-
"deps_meta": None::<serde_json::Value>,
237+
"depsMeta": None::<serde_json::Value>,
236238
}))
237239
.map_err(|e| e.to_string())?;
238240

typegraph/core/src/utils/postprocess/python_rt.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ impl PostProcessor for PythonProcessor {
1919
if mat.name.as_str() == "pymodule" {
2020
let mut mat_data: ModuleMatData =
2121
object_from_map(std::mem::take(&mut mat.data)).map_err(|e| e.to_string())?;
22-
let path = PathBuf::from(&mat_data.python_artifact);
22+
let path = mat_data.python_artifact.get("path").unwrap();
23+
let path: PathBuf = path.as_str().unwrap().into();
24+
// eprint(&format!("***************** {}", path_value));
2325

2426
if tg.meta.artifacts.contains_key(&path) {
2527
continue;
@@ -35,7 +37,7 @@ impl PostProcessor for PythonProcessor {
3537
Artifact {
3638
hash: module_hash.clone(),
3739
size,
38-
path,
40+
path: path.clone(),
3941
},
4042
);
4143

@@ -55,6 +57,14 @@ impl PostProcessor for PythonProcessor {
5557
dep_artifacts.push(dep_artifact);
5658
tg.deps.push(dep_abs_path);
5759
}
60+
61+
mat_data.python_artifact = map_from_object(Artifact {
62+
hash: module_hash.clone(),
63+
size,
64+
path,
65+
})
66+
.map_err(|e| e.to_string())?;
67+
5868
mat_data.deps_meta = Some(
5969
dep_artifacts
6070
.iter()

typegraph/node/sdk/src/tg_artifact_upload.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export class ArtifactUploader {
129129
const artifactMetas = this.getMetas(this.refArtifacts);
130130

131131
const uploadUrls = await this.fetchUploadUrls(artifactMetas);
132-
133132
const results = await Promise.allSettled(
134133
uploadUrls.map(
135134
async (url, i) => {

0 commit comments

Comments
 (0)