Commit 0e2bd2da authored by Romain Casati's avatar Romain Casati
Browse files

Basthon kernel's Python part is now loaded like Wasm packages.

parent fa3b4331
all:
MINIFIED=src/basthon.min.js src/basthon_goodies.min.js
FILE_PACKAGER = extern/emscripten/tools/file_packager.py
BASTHON_DATA = src/basthon-py/build/basthon.data
BASTHON_JS = src/basthon-py/build/basthon.js
BASTHON_MODULE = src/basthon-py/basthon
BASTHON_VFS_DEST = /lib/python3.8/site-packages/basthon/
MINIFIED=src/basthon-js/basthon.min.js src/basthon-js/basthon_goodies.min.js
minify: $(MINIFIED)
# this is a workaround for the buggy sources= parameter in map file
......@@ -8,7 +14,19 @@ minify: $(MINIFIED)
%.min.js: %.js
cd $(dir $<) ; uglifyjs.terser $(notdir $<) --source-map url=$(notdir $@).map --compress --mangle --output $(notdir $@) ; cd -
build: minify
$(BASTHON_DATA) $(BASTHON_JS) &:
mkdir -p $(dir $@)
python3 $(FILE_PACKAGER) $(BASTHON_DATA) \
--js-output=$(BASTHON_JS) \
--preload $(BASTHON_MODULE)"@"$(BASTHON_VFS_DEST) \
--lz4 \
--use-preload-plugins \
--export-name="pyodide._module" \
--exclude "*__pycache__*"
wasm-package: $(BASTHON_DATA) $(BASTHON_JS)
build: minify wasm-package
cd setup/ && make build
publish:
......@@ -16,7 +34,7 @@ publish:
clean:
cd setup/ && make clean
rm -f $(MINIFIED)
rm -f $(MINIFIED) $(BASTHON_DATA) $(BASTHON_JS)
install:
pip3 install setup/dist/basthon-kernel-*.tar.gz
......@@ -35,4 +53,4 @@ devel-www-publish: www
test: www
bash -c "set -m ; python3 -m http.server --directory www/output/ --bind localhost 8888 & sleep 1 ; firefox localhost:8888 ; fg %1"
.PHONY: all build publish clean install www www-publish test minify devel-www-publish
.PHONY: all build wasm-package publish clean install www www-publish test minify devel-www-publish
emscripten
\ No newline at end of file
Please, create here a link to ̀pyodide/emsdk/emsdk/fastcomp/emscripten/`.
build:
for d in $$(find ../src/ -mindepth 1 -maxdepth 1 -type d); do cd $$d ; make build ; cd - ; done
for d in $$(find ../src/modules/ -mindepth 1 -maxdepth 1 -type d); do cd $$d ; make build ; cd - ; done
python3 setup.py sdist bdist_wheel
publish:
python3 -m twine upload dist/*
clean:
for d in $$(find ../src/ -mindepth 1 -maxdepth 1 -type d); do cd $$d ; make clean ; cd - ; done
for d in $$(find ../src/modules/ -mindepth 1 -maxdepth 1 -type d); do cd $$d ; make clean ; cd - ; done
rm -rf basthon_kernel.egg-info/ basthon-kernel/data/ build/ dist/
.PHONY: publish build clean
......@@ -15,9 +15,6 @@ def main():
parser.add_argument('--install', help='Install Basthon kernel in current directory',
action="store_true")
parser.add_argument('--update', help='Update Basthon kernel scripts',
action="store_true")
args = parser.parse_args()
if args.install:
......@@ -26,18 +23,13 @@ def main():
data_path = os.path.join(os.path.dirname(__file__), 'data')
for path in os.listdir(data_path):
f = os.path.join(data_path, path)
if os.path.isfile(f):
shutil.copyfile(f, path)
if args.update:
print('Update Basthon kernel scripts to version {}'.format(implementation))
data_path = os.path.join(os.path.dirname(__file__), 'data')
for path in os.listdir(data_path):
shutil.copyfile(os.path.join(data_path, path), path)
for root, dirs, files in os.walk(data_path):
for f in files:
f = os.path.join(root, f)
shutil.copyfile(f, os.path.relpath(f, data_path))
for d in dirs:
d = os.path.join(root, d)
os.makedirs(os.path.relpath(d, data_path), exist_ok=True)
if __name__ == "__main__":
......
......@@ -5,7 +5,10 @@ import shutil
import sys
this_dir = os.getcwd()
src_dir = os.path.join(os.path.dirname(this_dir), "src")
root_dir = os.path.dirname(this_dir)
src_dest = ((os.path.join(root_dir, "src", "basthon-js"), '.'),
(os.path.join(root_dir, "src", "basthon-py", "build"), 'basthon-py'),
(os.path.join(root_dir, "src", "modules"), 'modules'))
with open("README.md") as f:
LONG_DESCRIPTION = f.read()
......@@ -21,16 +24,14 @@ if command == "sdist":
# copy files from basthon into data_dir
content = []
for root, dirs, files in os.walk(src_dir):
if root == src_dir:
content += [os.path.join(root, f)
for f in files
if os.path.splitext(f)[1] in ('.js', '.map')]
else:
content += [os.path.join(root, f)
for f in files if os.path.splitext(f)[1] == '.whl']
for fname in content:
dest = os.path.join(data_dir, os.path.basename(fname))
for src, dest in src_dest:
for root, dirs, files in os.walk(src):
exts = ('.js', '.map', '.data') if root == src else ('.whl',)
content += [(os.path.join(root, f), dest)
for f in files if os.path.splitext(f)[1] in exts]
for fname, dest in content:
dest = os.path.join(data_dir, dest, os.path.basename(fname))
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copyfile(fname, dest)
......@@ -79,7 +80,7 @@ setup(
keywords='Python browser',
package_data={
'basthon-kernel': ['data/*.*']
'basthon-kernel': ['data/*.*', 'data/*/*.*']
},
......
......@@ -32,7 +32,7 @@
that.loaded = false;
/**
* Get the URL of the current script (usefull for serving basthon.py.js)
* Get the URL of the current script (usefull for serving basthon-py)
*/
that.urlScript = (function () {
if( module != null ) {
......@@ -48,9 +48,14 @@
*/
that.basthonRoot = that.dirname(that.urlScript);
/**
* Get the URL of Basthon modules dir.
*/
that.basthonModulesRoot = that.basthonRoot + "/modules";
/**
* A separate namespace for packages (module) managment.
* (defined here since we need basthonRoot)
* (defined here since we need basthonModulesRoot)
*/
that.packages = (function () {
let pkg = {};
......@@ -68,27 +73,27 @@
*/
pkg.internal = {
"turtle": {
path: that.basthonRoot + "/turtle-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/turtle-0.0.1-py3-none-any.whl",
},
"requests": {
path: that.basthonRoot + "/requests-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/requests-0.0.1-py3-none-any.whl",
},
"proj4py": {
path: that.basthonRoot + "/proj4py-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/proj4py-0.0.1-py3-none-any.whl",
deps: ["pkg_resources"],
},
"folium": {
path: "folium", // loaded from PyPi
},
"graphviz": {
path: that.basthonRoot + "/graphviz-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/graphviz-0.0.1-py3-none-any.whl",
deps: ["pkg_resources"],
},
"IPython": {
path: that.basthonRoot + "/IPython-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/IPython-0.0.1-py3-none-any.whl",
},
"p5": {
path: that.basthonRoot + "/p5-0.0.1-py3-none-any.whl",
path: that.basthonModulesRoot + "/p5-0.0.1-py3-none-any.whl",
deps: ["pkg_resources"],
},
};
......@@ -154,7 +159,7 @@
if( packages.length === 0 ) { return; }
const packagesPath = packages.map(p => pkg.internal[p].path);
await pkg.loadPyodide('micropip');
await pyodide.globals.Basthon.micropip_install(packagesPath);
await pyodide.globals.basthon.Basthon.micropip_install(packagesPath);
if(callback) {
callback(packages);
}
......@@ -239,8 +244,8 @@
const sys = pyodide.pyimport("sys");
that.pythonVersion = sys.version;
that.packages.init();
// reading basthon.py.js from same folder than current script
return pyodide.runPythonAsync(`import pyodide ; pyodide.eval_code(pyodide.open_url('${that.basthonRoot}/basthon.py.js').getvalue(), globals())`);
return pyodide.loadPackage(that.basthonRoot + "/basthon-py/basthon.js").then(
() => pyodide.runPythonAsync("import basthon"));
};
/**
......@@ -334,7 +339,7 @@
*/
that.findImports = function (code) {
if( !that.loaded ) { return ; }
let imports = pyodide.globals.Basthon.find_imports(code);
let imports = pyodide.globals.basthon.Basthon.find_imports(code);
// manually update internal packages dependencies
for( const i of imports ) {
imports = imports.concat(
......@@ -356,7 +361,7 @@
*/
that.eval = function (code, data=null) {
if( !that.loaded ) { return ; }
return pyodide.globals.Basthon.eval(code, data);
return pyodide.globals.basthon.Basthon.eval(code, data);
};
/**
......@@ -376,7 +381,7 @@
"folium", "pandas", "PIL"];
for( const p of toLoad )
if( hacked.includes(p) )
pyodide.globals.Basthon[`hack_${p}`]();
pyodide.globals.basthon.Basthon[`hack_${p}`]();
};
/**
......@@ -400,7 +405,7 @@
}
// loading dependencies then running
await that.loadDependencies(code, that.hackPackagesCallback);
return await pyodide.globals.Basthon.shell_eval(code, outCallback, errCallback, data);
return await pyodide.globals.basthon.Basthon.shell_eval(code, outCallback, errCallback, data);
};
/**
......@@ -447,7 +452,7 @@
* Get the current execution count.
*/
that.executionCount = function () {
return pyodide.globals.Basthon.execution_count;
return pyodide.globals.basthon.Basthon.execution_count;
};
/**
......@@ -455,7 +460,7 @@
*/
that.restart = function () {
if( !that.loaded ) { return ; }
return pyodide.globals.Basthon.restart();
return pyodide.globals.basthon.Basthon.restart();
};
/**
......@@ -471,7 +476,7 @@
*/
that.putModule = async function (filename, content) {
await that.loadDependencies(content, that.hackPackagesCallback);
pyodide.globals.Basthon.put_module(filename, content);
pyodide.globals.basthon.Basthon.put_module(filename, content);
};
/**
......
build/
\ No newline at end of file
......@@ -425,8 +425,9 @@ pyodide.Basthon = Basthon
# hacking Pyodide bugy input function
import builtins
_default_input = __builtins__.input
_default_input = builtins.input
def _hacked_input(prompt=None):
......@@ -445,7 +446,7 @@ for a in dir(_default_input):
pass
# replacing
__builtins__.input = _hacked_input
builtins.input = _hacked_input
# hacking help function
......
import pyodide
import basthon
__all__ = ["display", "display_image", "HTML"]
display = pyodide.Basthon.basthon_internal.display
display = basthon.Basthon.basthon_internal.display
display_image = pyodide.Basthon.basthon_internal.display_image
display_image = basthon.Basthon.basthon_internal.display_image
def HTML(html):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment