Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-laplante committed May 12, 2024
1 parent fe8a06a commit 23f7fcc
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 153 deletions.
137 changes: 0 additions & 137 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,143 +42,6 @@ const Inner: React.FC = () => {

console.warn("LOADING SQLITE");

await pyodide.loadPackage("sqlite3");
console.warn("LOADED!");

pyodide.unpackArchive(data, "zip", {
extractDir: "bb"
});
pyodide.runPython(`
import os.path
import sys
from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location, spec_from_loader
import _frozen_importlib
import _frozen_importlib_external
class MyMetaFinder(MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
print(f"importing {fullname}")
if path is None or path == "":
path = sys.path
if "." in fullname:
*parents, name = fullname.split(".")
else:
name = fullname
for entry in path:
if os.path.isdir(os.path.join(entry, name)):
# this module has child modules
filename = os.path.join(entry, name, "__init__.py")
submodule_locations = [os.path.join(entry, name)]
else:
filename = os.path.join(entry, name + ".py")
submodule_locations = None
if not os.path.exists(filename):
continue
print(">> " + filename)
return spec_from_file_location(fullname, filename, loader=MyLoader(filename),
submodule_search_locations=submodule_locations)
# we don't know how to import this
return None
class MyLoader(Loader):
def __init__(self, filename):
self.filename = filename
def create_module(self, spec):
# Use default module creation semantics
return None
def exec_module(self, module):
print("FILE NAME: " + self.filename)
if self.filename.endswith("fcntl/__init__.py"):
data = """
from unittest.mock import Mock
fcntl = Mock()
"""
else:
with open(self.filename) as f:
data = f.read()
try:
exec(data, vars(module))
except Exception as e:
print(e)
raise e
class BuiltinImporterShim(_frozen_importlib.BuiltinImporter):
@classmethod
def find_spec(cls, fullname, path=None, target=None):
ret = super().find_spec(fullname, path, target)
if ret:
print(f"shim handling: {ret}")
return ret
class FrozenImporterShim:
_original_importer = None
@classmethod
def set_original_importer(cls, imp):
cls._original_importer = imp
@classmethod
def find_spec(cls, fullname, path=None, target=None):
if fullname == "fcntl":
return spec_from_loader("fcntl", MyLoader("fcntl/__init__.py"))
ret = cls._original_importer.find_spec(fullname, path, target)
if ret:
print(f"frozen shim handling: {ret}")
return ret
print(sys.meta_path)
js_finder = sys.meta_path.pop()
path_finder = sys.meta_path.pop()
frozen_finder = sys.meta_path.pop()
builtin = sys.meta_path.pop()
#sys.meta_path.append(MyMetaFinder())
sys.meta_path.append(path_finder)
i = FrozenImporterShim()
i.set_original_importer(frozen_finder)
sys.meta_path.append(i)
sys.meta_path.append(BuiltinImporterShim())
print(sys.meta_path)
`)
const file = pyodide.FS.readdir("./bb");
console.log(file);

pyodide.runPython(`
import sys
sys.path.insert(0, "./bb/bitbake-2.8.0/lib/")
from bb.data_smart import DataSmart
`)

const DataSmart = pyodide.globals.get('DataSmart');
const d = DataSmart();

d.setVar("A", "B");
d.setVar("A:test", "C");
d.setVar("OVERRIDES", "test");
d.setVarFlag("A", "p", "OK");

console.log(d.getVar("A"));

DataSmart.destroy();
setRan(true);

} else {
console.warn(`data = ${!!data}, p = ${!!pyodide}`);
Expand Down
3 changes: 3 additions & 0 deletions src/components/JQueryTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const JQueryTerminal: React.ForwardRefExoticComponent<React.PropsWithoutR
terminalObjectRef.current?.freeze(true);
terminalObjectRef.current?.set_prompt("");
},
setPrompt: (prompt) => {
terminalObjectRef.current?.set_prompt(prompt);
},
setInterpreter: (interpreter?: TypeOrArray<JQueryTerminal.Interpreter>) => {
if (terminalObjectRef.current) {
terminalObjectRef.current.set_interpreter(interpreter);
Expand Down
24 changes: 8 additions & 16 deletions src/components/PlaygroundTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,16 @@ export const PlaygroundTerminal: React.FC = () => {
}, [])

useEffect(() => {
if (state.pyodideStatus === "idle") {
terminalRef.current.echo(`Pyodide: ${state.pyodideStatus}`);
terminalRef.current.echo(`Downloading bitbake: ${progress(state.bitbakeProgress, 80)}%`);
if (state.pyodideStatus !== "done unpacking") {
terminalRef.current.setPrompt(
`Downloading bitbake: ${progress(state.bitbakeProgress, 80)}%\nPyodide: ${state.pyodideStatus}`
)
} else {
terminalRef.current.setPrompt(
`Done unpacking BitBake`
)
}

terminalRef.current.update(-1, `Pyodide: ${state.pyodideStatus}`);
terminalRef.current.update(-2, `Downloading bitbake: ${progress(state.bitbakeProgress, 80)}%`);
}, [state]);
//
// useEffect(() => {
// if (done && pyodide) {
// terminalRef.current.echo("Unpacking BitBake...");
// pyodide.unpackArchive(data, "zip", {
// extractDir: "bb"
// });
// terminalRef.current.echo("Done!");
// }
// }, [data, done, pyodide]);

const interpreter = (command, term) => {

Expand Down
150 changes: 150 additions & 0 deletions src/hooks/useEnvironmentSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {usePyodide} from "./usePyodide";
import {useEffect} from "react";
import {useSWRProgress} from "./useSWRProgress";



const initialEnvironmentState = {
pyodideStatus: "idle",
bitbakeProgress: 0
Expand Down Expand Up @@ -33,5 +35,153 @@ export const useEnvironmentSetup = () => {
dispatch({type: "bitbakeProgressChanged", bitbakeProgress: progress});
}, [progress, dispatch]);

useEffect(() => {
if (pyodide && data) {
dispatch({type: "pyodideStatusChanged", pyodideStatus: "Loading sqlite3"});
const f = async() => {
await pyodide.loadPackage("sqlite3");
dispatch({type: "pyodideStatusChanged", pyodideStatus: "done sqlite3"});
dispatch({type: "pyodideStatusChanged", pyodideStatus: "Unpacking..."});
pyodide.unpackArchive(data, "zip", {
extractDir: "bb"
});
dispatch({type: "pyodideStatusChanged", pyodideStatus: "done unpacking"});

pyodide.runPython(`
import os.path
import sys
from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location, spec_from_loader
import _frozen_importlib
import _frozen_importlib_external
class MyMetaFinder(MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
print(f"importing {fullname}")
if path is None or path == "":
path = sys.path
if "." in fullname:
*parents, name = fullname.split(".")
else:
name = fullname
for entry in path:
if os.path.isdir(os.path.join(entry, name)):
# this module has child modules
filename = os.path.join(entry, name, "__init__.py")
submodule_locations = [os.path.join(entry, name)]
else:
filename = os.path.join(entry, name + ".py")
submodule_locations = None
if not os.path.exists(filename):
continue
print(">> " + filename)
return spec_from_file_location(fullname, filename, loader=MyLoader(filename),
submodule_search_locations=submodule_locations)
# we don't know how to import this
return None
class MyLoader(Loader):
def __init__(self, filename):
self.filename = filename
def create_module(self, spec):
# Use default module creation semantics
return None
def exec_module(self, module):
print("FILE NAME: " + self.filename)
if self.filename.endswith("fcntl/__init__.py"):
data = """
from unittest.mock import Mock
fcntl = Mock()
"""
else:
with open(self.filename) as f:
data = f.read()
try:
exec(data, vars(module))
except Exception as e:
print(e)
raise e
class BuiltinImporterShim(_frozen_importlib.BuiltinImporter):
@classmethod
def find_spec(cls, fullname, path=None, target=None):
ret = super().find_spec(fullname, path, target)
if ret:
print(f"shim handling: {ret}")
return ret
class FrozenImporterShim:
_original_importer = None
@classmethod
def set_original_importer(cls, imp):
cls._original_importer = imp
@classmethod
def find_spec(cls, fullname, path=None, target=None):
if fullname == "fcntl":
return spec_from_loader("fcntl", MyLoader("fcntl/__init__.py"))
ret = cls._original_importer.find_spec(fullname, path, target)
if ret:
print(f"frozen shim handling: {ret}")
return ret
print(sys.meta_path)
js_finder = sys.meta_path.pop()
path_finder = sys.meta_path.pop()
frozen_finder = sys.meta_path.pop()
builtin = sys.meta_path.pop()
#sys.meta_path.append(MyMetaFinder())
sys.meta_path.append(path_finder)
i = FrozenImporterShim()
i.set_original_importer(frozen_finder)
sys.meta_path.append(i)
sys.meta_path.append(BuiltinImporterShim())
print(sys.meta_path)
`)
const file = pyodide.FS.readdir("./bb");
console.log(file);

pyodide.runPython(`
import sys
sys.path.insert(0, "./bb/bitbake-2.8.0/lib/")
from bb.data_smart import DataSmart
`)

const DataSmart = pyodide.globals.get('DataSmart');
const d = DataSmart();

d.setVar("A", "B");
d.setVar("A:test", "C");
d.setVar("OVERRIDES", "test");
d.setVarFlag("A", "p", "OK");

console.log(d.getVar("A"));

DataSmart.destroy();
}

f();
}
}, [data, dispatch, pyodide]);

return {state};
};

0 comments on commit 23f7fcc

Please sign in to comment.