Skip to content

Commit

Permalink
clean up import system
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Nov 28, 2023
1 parent 164ac00 commit 79f74b6
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 247 deletions.
4 changes: 3 additions & 1 deletion pygbag/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" packager+server for pygbag wasm loader """

" ^(⌒(oo)⌒)^ "

import sys

# some Linux distro are stuck in the past. Better safe than sorry
Expand Down Expand Up @@ -52,7 +54,7 @@ def print_exception(e, out=sys.stderr, **kw):
sys.print_exception = print_exception


def ESC(*argv, flush=False):
def ESC(*argv, flush=True):
for arg in argv:
sys.__stdout__.write(chr(0x1B))
sys.__stdout__.write(arg)
Expand Down
17 changes: 9 additions & 8 deletions pygbag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ def no_op(cls, *argv, **kw):
class TopLevel_async_handler(aio.toplevel.AsyncInteractiveConsole):
HTML_MARK = '"' * 3 + " # BEGIN -->"

@classmethod
async def async_imports(cls, callback, *wanted, **kw):
...
# @classmethod
# async def async_imports(cls, callback, *wanted, **kw):
# ...

@classmethod
def list_imports(cls, code=None, file=None, hint=""):
Expand All @@ -222,7 +222,7 @@ def eval(self, source):
self.buffer.insert(0, "#")
print(f"178: {count} lines queued for async eval")

async def raw_input(self, prompt=">>> "):
async def input_console(self, prompt=">>> "):
if len(self.buffer):
return self.buffer.pop(0)

Expand All @@ -247,11 +247,12 @@ async def async_get_pkg(cls, want, ex, resume)

# start async top level machinery and add a console.
await TopLevel_async_handler.start_toplevel(platform.shell, console=True)
ns = vars(__import__(__name__))

ns["TopLevel_async_handler"] = TopLevel_async_handler
# FIXME: store it elsewhere.
__import__("builtins").TopLevel_async_handler = TopLevel_async_handler
#ns = vars(__import__(__name__))

#ns["TopLevel_async_handler"] = TopLevel_async_handler

aio.toplevel.handler = TopLevel_async_handler

sourcefile = sourcefile or str(sys.argv[-1])

Expand Down
6 changes: 3 additions & 3 deletions pygbag/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

try:
import aioconsole, aiohttp
except:
print(f"""
except Exception as e:
print(e,f"""
pygbag simulator rely on both aioconsole and aiohttp
please use :
{sys.interpreter} -m pip install aioconsole aiohttp
{sys.executable} -m pip install aioconsole aiohttp asyncio_socks_server token_util
""")
raise SystemExit
Expand Down
31 changes: 25 additions & 6 deletions pygbag/support/cross/aio/clock.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import sys
import time
import asyncio

import os
WTF =1

class tui:
# use direct access, it is absolute addressing on raw terminal.
out = sys.__stdout__.write
if 0:
out = sys.__stdout__.write
else:
def out(self, *argv, **kw):
...

try:
LINES = int( os.environ.get('LINES', 200) )
except:
LINES = 200

# save cursor
def __enter__(self):
self.out("\x1b7\x1b[?25l")
self.out("\x1b7","\x1b[?25l")
return self

# restore cursor
def __exit__(self, *tb):
self.out("\x1b8\x1b[?25h")
self.out("\x1b8","\x1b[?25h")

# TODO: limit buffer to LINES
def __call__(self, *a, **kw):
self.out("\x1b[{};{}H{}".format(kw.get("z", 12), kw.get("x", 40), " ".join(a)))
try:
l = ctx()["io"][0]
if len(l)> self.LINES:
return
l.append( [kw.get("z", 1), kw.get("x", 1), " ".join(a)] )
except:
...

#self.out("\x1b[{};{}H{}".format(, " ".join(a)))


def step(x=70, y=0, z=2):
Expand All @@ -31,7 +50,7 @@ def box(t, x, y, z):
for t in lines:
print("│%s│" % t, x=x, z=z)
z += 1
print("└%s┘" % fill, x=x, z=z)
print("└%s┘\n" % fill, x=x, z=z)

with tui() as print:
# draw a clock
Expand Down
8 changes: 5 additions & 3 deletions pygbag/support/cross/aio/pep0723.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Config:
"pygame": "pygame.base",
"pygame-ce": "pygame.base",
"python-i18n" : "i18n",
"pillow" : "PIL",
}


Expand Down Expand Up @@ -259,8 +260,8 @@ async def pip_install(pkg, sysconf={}):
wheel_url = ""

# hack for WASM wheel repo
if pkg in Config.mapping:
pkg = Config.mapping[pkg]
if pkg.lower() in Config.mapping:
pkg = Config.mapping[pkg.lower()]
if pkg in HISTORY:
return
print("228: package renamed to", pkg)
Expand Down Expand Up @@ -408,7 +409,7 @@ async def check_list(code=None, filename=None):
if (env / pkg_final).is_dir():
print("found in env :", pkg)
continue
await pip_install(pkg, sconf)
await pip_install(pkg_final, sconf)

# wasm compilation
if not aio.cross.simulator:
Expand All @@ -422,6 +423,7 @@ async def check_list(code=None, filename=None):
dep = PATCHLIST.pop(0)
print(f"314: patching {dep}")
try:
import platform
platform.patches.pop(dep)()
except Exception as e:
sys.print_exception(e)
Expand Down
108 changes: 11 additions & 97 deletions pygbag/support/cross/aio/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,98 +19,8 @@
if not __UPY__:
import code

def install(pkg_file, sconf=None):
global HISTORY
from installer import install
from installer.destinations import SchemeDictionaryDestination
from installer.sources import WheelFile

# Handler for installation directories and writing into them.
destination = SchemeDictionaryDestination(
sconf or __import_("sysconfig").get_paths(),
interpreter=sys.executable,
script_kind="posix",
)

try:
with WheelFile.open(pkg_file) as source:
install(
source=source,
destination=destination,
# Additional metadata that is generated by the installation tool.
additional_metadata={
"INSTALLER": b"pygbag",
},
)
HISTORY.append(pkg_file)
except FileExistsError:
print(f"47: {pkg_file} already installed")
except Exception as ex:
pdb(f"49: cannot install {pkg_file}")
sys.print_exception(ex)

async def get_repo_pkg(pkg_file, pkg, resume, ex):
global HISTORY

# print("-"*40)
import platform
import json
import sysconfig
import importlib
from pathlib import Path
import aio.pep0723

if not pkg_file in HISTORY:
sconf = sysconfig.get_paths()
# sconf["platlib"] = os.environ.get("HOME","/tmp")
platlib = sconf["platlib"]
Path(platlib).mkdir(exist_ok=True)

if platlib not in sys.path:
sys.path.append(platlib)

try:
aio.toplevel.install(pkg_file, sconf)
except Exception as rx:
pdb(f"75: failed to install {pkg_file}")
sys.print_exception(rx)

# wasm compilation
try:
platform.explore(platlib)
platform.explore(aio.pep0723.sconf["platlib"])
await asyncio.sleep(0)
importlib.invalidate_caches()
# print(f"{pkg_file} installed, preloading", embed.preloading())
except Exception as rx:
pdb(f"failed to preload {pkg_file}")
sys.print_exception(rx)
# let wasm compilation happen
await asyncio.sleep(0)
else:
print(f"90: {pkg_file} already installed")

if pkg in platform.patches:
print("94:", pkg, "requires patching")
await asyncio.sleep(0)
platform.patches.pop(pkg)()
await asyncio.sleep(0)
print("97:", pkg, "patched")

if resume and ex:
try:
if inspect.isawaitable(resume):
print(f"{resume=} is an awaitable")
return resume()
else:
print(f"{resume=} is not awaitable")
resume()
return asyncio.sleep(0)
except Exception as resume_ex:
sys.print_exception(ex, limit=-1)
return None

class AsyncInteractiveConsole(code.InteractiveConsole):

instance = None
console = None
# TODO: use PyConfig interactive flag
Expand Down Expand Up @@ -169,6 +79,7 @@ def runsource(self, source, filename="<stdin>", symbol="single"):
return False

def runcode(self, code):

if embed:
embed.set_ps1()
self.rv = undefined
Expand All @@ -184,11 +95,14 @@ def runcode(self, code):
raise

except ModuleNotFoundError as ex:
print("181: FIXME dependency table for manually built modules")
get_pkg = self.opts.get("get_pkg", self.async_get_pkg)
if get_pkg:
want = str(ex).split("'")[1]
self.shell.coro.append(get_pkg(want, ex, bc))
want = str(ex).split("'")[1]
print("189 : FIXME sync->async->sync import bytecode retry in non interactive")
print(f'await aio.pep0723.pip_install("{want}");import {want}')
async def import_now():
nonlocal want
await aio.pep0723.pip_install(want)
vars(__import__('__main__'))[want] = __import__(want)
self.shell.coro.append( import_now() )

except BaseException as ex:
if self.one_liner:
Expand Down Expand Up @@ -251,7 +165,7 @@ async def interact(self):

try:
try:
self.line = await self.raw_input(prompt)
self.line = await self.input_console(prompt)
if self.line is None:
continue

Expand Down
Loading

0 comments on commit 79f74b6

Please sign in to comment.