Skip to content

Commit

Permalink
0.6.6 (win32 tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Jan 8, 2023
1 parent 28f3fbd commit 605294e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 33 deletions.
3 changes: 2 additions & 1 deletion pygbag/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" packager+server for pygbag wasm loader """

__version__ = "0.6.2"
#last 0.6.6
__version__ = "0.7.0"


# WaPy=>CPython compat
Expand Down
2 changes: 1 addition & 1 deletion pygbag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def eval(self, source):

sourcefile = sys.argv[-1]

__import__(__name__).__file__ = sourcefile
__import__(__name__).__file__ = str(sourcefile)

if "--piny" in sys.argv:
from . import mutator
Expand Down
67 changes: 48 additions & 19 deletions pygbag/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import asyncio
import sys

# rmtree msg on win32
import warnings

# import os
import argparse

Expand All @@ -23,10 +26,10 @@
devmode = "--dev" in sys.argv

DEFAULT_SCRIPT = "main.py"
CACHE_ROOT = Path("build")
CACHE_PATH = CACHE_ROOT / "web-cache"
CACHE_ROOT = Path("build")
CACHE_PATH = CACHE_ROOT / "web-cache"
CACHE_VERSION = CACHE_ROOT / "version.txt"
CACHE_APP = CACHE_ROOT / "web"
CACHE_APP = CACHE_ROOT / "web"

cdn_dot = __version__.split(".")
cdn_dot.pop()
Expand Down Expand Up @@ -57,8 +60,8 @@
DEFAULT_TMPL = "default.tmpl"



def set_args(program):
global DEFAULT_SCRIPT
import sys
from pathlib import Path

Expand All @@ -70,12 +73,21 @@ def set_args(program):
app_folder = patharg.parent
else:
app_folder = patharg.resolve()
mainscript = DEFAULT_SCRIPT

sys.path.insert(0, str(app_folder))

if patharg.suffix == "pyw":
required.append("79: Error, no .pyw allowed use .py for python script")

script_path = app_folder / mainscript

if not script_path.is_file():
required.append(f"83: Error, no main.py {script_path} found in folder")

if not app_folder.is_dir() or patharg.as_posix().endswith("/pygbag/__main__.py"):
required.append(
"78: Error, Last argument must be a valid app top level directory, or the main python script"
"89: Error, Last argument must be a valid app top level directory, or the main.py python script"
)

if sys.version_info < (3, 8):
Expand All @@ -92,46 +104,61 @@ def set_args(program):
return app_folder, mainscript


def cache_check(app_folder, devmode = False):
def cache_check(app_folder, devmode=False):
global CACHE_PATH, CACHE_APP, __version__

version_file = app_folder / CACHE_VERSION

clear_cache = False

# always clear the cache in devmode, because cache source is local and changes a lot
if devmode :
if devmode:
print("103: DEVMODE: clearing cache")
clear_cache = True
elif version_file.is_file():
try:
with open(version_file, "r") as file:
cache_ver = file.read()
if cache_ver != __version__:
print(f"115: cache {cache_ver} mismatch, want {__version__}, cleaning ...")
print(
f"115: cache {cache_ver} mismatch, want {__version__}, cleaning ..."
)
clear_cache = True
except:
# something's wrong in cache structure, try clean it up
clear_cache = True
else:
clear_cache = True

app_folder.joinpath(CACHE_ROOT).mkdir(exist_ok=True)

cache_root = app_folder.joinpath(CACHE_ROOT)
cache_dir = app_folder / CACHE_PATH
build_dir = app_folder / CACHE_APP
build_dir.mkdir(exist_ok=True)

cache_dir = app_folder / CACHE_PATH
def make_cache_dirs():
nonlocal cache_root, cache_dir

if clear_cache and cache_dir.is_dir():
if shutil.rmtree.avoids_symlink_attacks:
shutil.rmtree(cache_dir.as_posix())
cache_root.mkdir(exist_ok=True)
build_dir.mkdir(exist_ok=True)
cache_dir.mkdir(exist_ok=True)

if clear_cache:
win32 = sys.platform == "win32"
if shutil.rmtree.avoids_symlink_attacks or win32:
if cache_dir.is_dir():
if win32:
warnings.warn(
"clear cache : rmtree is not safe on that system (win32)"
)
shutil.rmtree(cache_dir.as_posix())
else:
print("115: cannot clear cache : rmtree is not safe on that system", file=sys.stderr)
raise SystemEXit(115)
print(
"115: cannot clear cache : rmtree is not safe on that system",
file=sys.stderr,
)
raise SystemExit(115)

# rebuild
cache_dir.mkdir(exist_ok=True)
make_cache_dirs()

with open(version_file, "w") as file:
file.write(__version__)
Expand Down Expand Up @@ -463,7 +490,9 @@ def main():

# sim does not use cache.
if "--sim" in sys.argv:
print(f"To use simulator launch with : {sys.executable} -m pygbag {' '.join(sys.argv[1:])}")
print(
f"To use simulator launch with : {sys.executable} -m pygbag {' '.join(sys.argv[1:])}"
)
return 1
else:
asyncio.run(main_run(app_folder, mainscript))
Expand Down
2 changes: 2 additions & 0 deletions pygbag/mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def transform_source(source: str) -> str:
trail = ll[1:].replace(tag, value)
elif ll.startswith(f"# {tag}"):
trail = ll[2:].replace(tag, value)
elif ll.startswith(f"#!{tag}"):
trail = ll[2:].replace(tag, value)
else:
continue
break
Expand Down
26 changes: 18 additions & 8 deletions pygbag/support/pythonrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,10 @@ def check_code(file_name):
code = code.rsplit(TopLevel_async_handler.HTML_MARK,1)[0]

# do not check site/final/packed code
# preload code must be fully async and no pgzero based
if TopLevel_async_handler.muted:
return True

print("v"*80)


print("^"*80)
if code[0:320].find('#!pgzrun')>=0:
shell.pgzrunning = True

Expand All @@ -686,13 +683,12 @@ def check_code(file_name):
code = code.replace( block, f'{block};await asyncio.sleep(0)')

# fix cwd to match a run of main.py from its folder
__import__('__main__').__file__ = main
__import__('__main__').__file__ = str(main)
cls.HOME = Path(main).parent
os.chdir(cls.HOME)

await cls.preload_code(code, **kw)


if TopLevel_async_handler.instance:
DBG("646: starting shell")
TopLevel_async_handler.instance.start_console(shell)
Expand All @@ -708,7 +704,8 @@ def check_code(file_name):
sys.modules["pgzrun"] = type(__main__)("pgzrun")
import pgzrun
pgzrun.go = lambda: None
await TopLevel_async_handler.async_imports(None, "pygame.base", "pgzero", "pyfxr", **kw)
cb = kw.pop('callback',None)
await TopLevel_async_handler.async_imports(cb, "pygame.base", "pgzero", "pyfxr", **kw)
import pgzero
import pgzero.runner
pgzero.runner.prepare_mod(__main__)
Expand Down Expand Up @@ -1455,8 +1452,21 @@ def patch_matplotlib_pyplot_pause(interval):

matplotlib.pyplot.pause = patch_matplotlib_pyplot_pause

def patch_panda3d_showbase():
import panda3d
import panda3d.core as p3d
from direct.showbase.ShowBase import ShowBase
print("prc patches")

def run(*argv,**env):
print("ShowBase.run patch")

ShowBase.run = run


platform.patches = {
"matplotlib" : patch_matplotlib_pyplot
"matplotlib" : patch_matplotlib_pyplot,
"panda3d" : patch_panda3d_showbase,
}


Expand Down
4 changes: 2 additions & 2 deletions static/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
print("""
Loading {{cookiecutter.title}} from {{cookiecutter.archive}}.apk
Pygbag Version : {{cookiecutter.version}}
Template Version : 0.6.0
Template Version : 0.6.6
Python : {{cookiecutter.PYBUILD}}
CDN URL : {{cookiecutter.cdn}}
Screen : {{cookiecutter.width}}x{{cookiecutter.height}}
Expand Down Expand Up @@ -148,7 +148,7 @@ async def custom_site():

# now that apk is mounted we have access to font cache
# but we need to fill __file__ that is not yet set
__import__(__name__).__file__ = main
__import__(__name__).__file__ = str(main)


# now make a prompt
Expand Down
4 changes: 2 additions & 2 deletions static/noctx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ async def custom_site():
def ui_callback(pkg):
print(f"installing {pkg}")

await shell.interact(main)
await shell.source(main, callback=ui_callback)

# if you don't reach that step
# your main.py has an infinite sync loop somewhere !
print("noctxl.html: ready")


shell.interact()

asyncio.run( custom_site() )

Expand Down

0 comments on commit 605294e

Please sign in to comment.