Skip to content

Fix #2067 - Implement whl packages #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
coverage/
node_modules/
test-results/
cjs/*
!cjs/package.json
core.*
Expand Down
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/zip-CCQFVoSU.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion esm/interpreter/_python.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const registerJSModule = (interpreter, name, value) => {

export const getFormat = (path, url) => {
if (path.endsWith('/*')) {
if (/\.(zip|tar(?:\.gz)?)$/.test(url))
if (/\.(zip|whl|tgz|tar(?:\.gz)?)$/.test(url))
return RegExp.$1;
throw new Error(`Unsupported archive ${url}`);
}
Expand Down
27 changes: 20 additions & 7 deletions esm/interpreter/micropython.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// import fetch from '@webreflection/fetch';
import fetch from '@webreflection/fetch';

import { fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { stdio, buffered } from './_io.js';
import { absoluteURL } from '../utils.js';
import mip from '../python/mip.js';
import zip from '../zip.js';

Expand All @@ -23,14 +25,14 @@ export default {
type,
module: (version = '1.22.0-380') =>
`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${version}/micropython.mjs`,
async engine({ loadMicroPython }, config, url) {
async engine({ loadMicroPython }, config, url, baseURL) {
const { stderr, stdout, get } = stdio({
stderr: buffered(console.error),
stdout: buffered(console.log),
});
url = url.replace(/\.m?js$/, '.wasm');
const interpreter = await get(loadMicroPython({ linebuffer: false, stderr, stdout, url }));
const py_imports = importPackages.bind(interpreter);
const py_imports = importPackages.bind(this, interpreter, baseURL);
loader.set(interpreter, py_imports);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
Expand All @@ -54,6 +56,7 @@ export default {
const extractDir = path.slice(0, -1);
if (extractDir !== './') FS.mkdir(extractDir);
switch (format) {
case 'whl':
case 'zip': {
const blob = new Blob([buffer], { type: 'application/zip' });
return zip().then(async ({ BlobReader, Uint8ArrayWriter, ZipReader }) => {
Expand All @@ -74,6 +77,7 @@ export default {
zipReader.close();
});
}
case 'tgz':
case 'tar.gz': {
const TMP = './_.tar.gz';
writeFile(fs, TMP, buffer);
Expand Down Expand Up @@ -104,9 +108,18 @@ export default {
},
};

async function importPackages(packages) {
const mpyPackageManager = this.pyimport('mip');
for (const mpyPackage of packages)
mpyPackageManager.install(mpyPackage);
async function importPackages(interpreter, baseURL, packages) {
let mip;
for (const mpyPackage of packages) {
if (mpyPackage.endsWith('.whl')) {
const url = absoluteURL(mpyPackage, baseURL);
const buffer = await fetch(url).arrayBuffer();
await this.writeFile(interpreter, './*', buffer, url);
}
else {
if (!mip) mip = interpreter.pyimport('mip');
mip.install(mpyPackage);
}
}
}
/* c8 ignore stop */
2 changes: 1 addition & 1 deletion esm/interpreters.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const interpreter = new Proxy(new Map(), {
const value = config?.js_modules?.[entry];
if (value) base.set(value, baseURL);
}
return engine(module, config, url);
return engine(module, config, url, baseURL);
});
},
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-ASBVzsN+0aNO5ta7X2ltTs6XMVTphS15rcPAI2Nihx0="
"blob": "sha256-mgboWJ8+6oA2F8tVzLDee7ytRfr+VF18ss/TLjBZYgI="
}
}
Binary file added test/whl/arrr-1.0.5-py3-none-any.whl
Binary file not shown.
1 change: 1 addition & 0 deletions test/whl/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages = ["./arrr-1.0.5-py3-none-any.whl"]
18 changes: 18 additions & 0 deletions test/whl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="../../dist/index.js"></script>
</head>
<body>
<script type="micropython" config="./config.toml">
import arrr
print("micropython", dir(arrr))
</script>
<script type="pyodide" config="./config.toml">
import arrr
print("pyodide", dir(arrr))
</script>
</body>
</html>