Skip to content

Commit d46324d

Browse files
committed
Fix #2067 - Implement whl packages
1 parent f48f1c0 commit d46324d

11 files changed

+46
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
coverage/
33
node_modules/
4+
test-results/
45
cjs/*
56
!cjs/package.json
67
core.*

docs/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CCQFVoSU.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/interpreter/_python.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const registerJSModule = (interpreter, name, value) => {
1717

1818
export const getFormat = (path, url) => {
1919
if (path.endsWith('/*')) {
20-
if (/\.(zip|tar(?:\.gz)?)$/.test(url))
20+
if (/\.(zip|whl|tgz|tar(?:\.gz)?)$/.test(url))
2121
return RegExp.$1;
2222
throw new Error(`Unsupported archive ${url}`);
2323
}

esm/interpreter/micropython.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// import fetch from '@webreflection/fetch';
1+
import fetch from '@webreflection/fetch';
2+
23
import { fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
34
import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './_python.js';
45
import { stdio, buffered } from './_io.js';
6+
import { absoluteURL } from '../utils.js';
57
import mip from '../python/mip.js';
68
import zip from '../zip.js';
79

@@ -23,14 +25,14 @@ export default {
2325
type,
2426
module: (version = '1.22.0-380') =>
2527
`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${version}/micropython.mjs`,
26-
async engine({ loadMicroPython }, config, url) {
28+
async engine({ loadMicroPython }, config, url, baseURL) {
2729
const { stderr, stdout, get } = stdio({
2830
stderr: buffered(console.error),
2931
stdout: buffered(console.log),
3032
});
3133
url = url.replace(/\.m?js$/, '.wasm');
3234
const interpreter = await get(loadMicroPython({ linebuffer: false, stderr, stdout, url }));
33-
const py_imports = importPackages.bind(interpreter);
35+
const py_imports = importPackages.bind(this, interpreter, baseURL);
3436
loader.set(interpreter, py_imports);
3537
if (config.files) await fetchFiles(this, interpreter, config.files);
3638
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
@@ -54,6 +56,7 @@ export default {
5456
const extractDir = path.slice(0, -1);
5557
if (extractDir !== './') FS.mkdir(extractDir);
5658
switch (format) {
59+
case 'whl':
5760
case 'zip': {
5861
const blob = new Blob([buffer], { type: 'application/zip' });
5962
return zip().then(async ({ BlobReader, Uint8ArrayWriter, ZipReader }) => {
@@ -74,6 +77,7 @@ export default {
7477
zipReader.close();
7578
});
7679
}
80+
case 'tgz':
7781
case 'tar.gz': {
7882
const TMP = './_.tar.gz';
7983
writeFile(fs, TMP, buffer);
@@ -104,9 +108,18 @@ export default {
104108
},
105109
};
106110

107-
async function importPackages(packages) {
108-
const mpyPackageManager = this.pyimport('mip');
109-
for (const mpyPackage of packages)
110-
mpyPackageManager.install(mpyPackage);
111+
async function importPackages(interpreter, baseURL, packages) {
112+
let mip;
113+
for (const mpyPackage of packages) {
114+
if (mpyPackage.endsWith('.whl')) {
115+
const url = absoluteURL(mpyPackage, baseURL);
116+
const buffer = await fetch(url).arrayBuffer();
117+
await this.writeFile(interpreter, './*', buffer, url);
118+
}
119+
else {
120+
if (!mip) mip = interpreter.pyimport('mip');
121+
mip.install(mpyPackage);
122+
}
123+
}
111124
}
112125
/* c8 ignore stop */

esm/interpreters.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const interpreter = new Proxy(new Map(), {
4343
const value = config?.js_modules?.[entry];
4444
if (value) base.set(value, baseURL);
4545
}
46-
return engine(module, config, url);
46+
return engine(module, config, url, baseURL);
4747
});
4848
},
4949
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@
9090
"to-json-callback": "^0.1.1"
9191
},
9292
"worker": {
93-
"blob": "sha256-ASBVzsN+0aNO5ta7X2ltTs6XMVTphS15rcPAI2Nihx0="
93+
"blob": "sha256-mgboWJ8+6oA2F8tVzLDee7ytRfr+VF18ss/TLjBZYgI="
9494
}
9595
}

test/whl/arrr-1.0.5-py3-none-any.whl

6.82 KB
Binary file not shown.

test/whl/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages = ["./arrr-1.0.5-py3-none-any.whl"]

test/whl/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="module" src="../../dist/index.js"></script>
7+
</head>
8+
<body>
9+
<script type="micropython" config="./config.toml">
10+
import arrr
11+
print("micropython", dir(arrr))
12+
</script>
13+
<script type="pyodide" config="./config.toml">
14+
import arrr
15+
print("pyodide", dir(arrr))
16+
</script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)