Skip to content

Commit

Permalink
more pep723, heuristic wav/mp3 code fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Nov 8, 2023
1 parent ad9b219 commit ae3d761
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 95 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
open_collective: pythonseverywhere
liberapay: pmp-p

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
runs-on: ubuntu-22.04
env:
SDK_VERSION: 3.1.48.4bi
SDK_VERSION: 3.1.49.0bi
SYS_PYTHON: /usr/bin/python3
PACKAGES: emsdk hpy _ctypes pygame
BUILD_STATIC: emsdk _ctypes hpy
Expand Down
4 changes: 2 additions & 2 deletions pygbag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pathlib import Path

__version__ = "0.8.5"
VERSION = "0.8.5"

# hack to test git cdn build without upgrading pygbag
# beware can have side effects when file packager behaviour must change !
Expand All @@ -19,7 +19,7 @@
"""
)
__version__ = "0.0.0"
VERSION = "0.0.0"

# make aio available

Expand Down
27 changes: 15 additions & 12 deletions pygbag/__main__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import sys
import asyncio

asyncrun = asyncio.run

import sys

from .__init__ import __version__
from .__init__ import VERSION

print(f" *pygbag {__version__}*")
print(f" *pygbag {VERSION}*")

from pathlib import Path


async def import_site(sourcefile=None, simulator=False, async_input=None, async_pkg=None):
import sys
import os
from pathlib import Path

if ("--sim" not in sys.argv) and ("--piny" not in sys.argv) and not simulator:
Expand All @@ -27,7 +26,7 @@ async def import_site(sourcefile=None, simulator=False, async_input=None, async_

mod_dir = Path(__file__).parent
support = mod_dir / "support"

cur_dir = os.getcwd()
print(
f"""
- single thread no-os wasm simulator -
Expand All @@ -36,6 +35,7 @@ async def import_site(sourcefile=None, simulator=False, async_input=None, async_
platform support : {support}
{sys.argv=}
{sourcefile=}
{cur_dir=}
"""
)
Expand Down Expand Up @@ -117,14 +117,19 @@ class fake_EventTarget:
async def process(self):
...


def patch_platform_system():
return 'Emscripten'


# et = EventTarget()
class __EMSCRIPTEN__(object):
EventTarget = fake_EventTarget()
ticks = 0

def __init__(self):
import platform

platform.system = patch_platform_system
self.platform = platform

def __getattribute__(self, name):
Expand Down Expand Up @@ -186,11 +191,6 @@ def no_op(cls, *argv, **kw):
class TopLevel_async_handler(aio.toplevel.AsyncInteractiveConsole):
HTML_MARK = '"' * 3 + " # BEGIN -->"

# @classmethod
# async def async_repos(cls):
# abitag = f"cp{sys.version_info.major}{sys.version_info.minor}"
# print(f"{abitag=}")

@classmethod
async def async_imports(cls, callback, *wanted, **kw):
...
Expand Down Expand Up @@ -262,6 +262,9 @@ async def async_get_pkg(cls, want, ex, resume)

print(__name__, "sim repl ready for", sourcefile)

if sys.path[0]!=cur_dir:
sys.path.insert(0,cur_dir)

await shell.runpy(sourcefile)
shell.interactive()

Expand Down
18 changes: 9 additions & 9 deletions pygbag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import shutil
from datetime import datetime

from .__init__ import __version__
from .__init__ import VERSION

import pygbag

Expand All @@ -31,7 +31,7 @@
CACHE_VERSION = CACHE_ROOT / "version.txt"
CACHE_APP = CACHE_ROOT / "web"

cdn_dot = __version__.split(".")
cdn_dot = VERSION.split(".")
cdn_dot.pop()
cdn_version = ".".join(cdn_dot)
del cdn_dot
Expand Down Expand Up @@ -110,7 +110,7 @@ def set_args(program):


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

version_file = app_folder / CACHE_VERSION

Expand All @@ -124,8 +124,8 @@ def cache_check(app_folder, devmode=False):
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 ...")
if cache_ver != VERSION:
print(f"115: cache {cache_ver} mismatch, want {VERSION}, cleaning ...")
clear_cache = True
except:
# something's wrong in cache structure, try clean it up
Expand Down Expand Up @@ -162,7 +162,7 @@ def make_cache_dirs():
make_cache_dirs()

with open(version_file, "w") as file:
file.write(__version__)
file.write(VERSION)

return build_dir, cache_dir

Expand Down Expand Up @@ -235,8 +235,8 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN):

parser.add_argument(
"--version",
default=__version__,
help="override prebuilt version path [default:%s]" % __version__,
default=VERSION,
help="override prebuilt version path [default:%s]" % VERSION,
)

parser.add_argument("--build", action="store_true", help="build only, do not run test server")
Expand Down Expand Up @@ -348,7 +348,7 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN):
"title": (args.title or app_name),
"directory": app_name,
"spdx": "cookiecutter.spdx",
"version": __version__,
"version": VERSION,
"PYBUILD": args.PYBUILD,
}

Expand Down
2 changes: 1 addition & 1 deletion pygbag/filtering.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

dbg = True
dbg = False

# q:what to do with the extreme case $HOME/main.py ?
# or folders > 512MiB total
Expand Down
7 changes: 4 additions & 3 deletions pygbag/optimizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,17 @@ def translated(fn):

if len(tofix):
fixname = Path(f"{fp.parent}/{fp.stem}-pygbag.py")
fixfull = Path(f"{folder}/{fixname}")
opt = Path(f"{folder}/{fixname}")
with open(fname,"r", encoding="utf-8") as source:
data = open(fname,"r").read()
with open(fixfull, "w", encoding="utf-8") as dest:
with open(opt, "w", encoding="utf-8") as dest:
while len(tofix):
bad, good = tofix.pop(0)
warnings.warn(f"potential {bad.upper()} use in {fname}, prefer .{good} !")
data = data.replace(f'.{bad}"',f'.{good}"')
dest.write(data)
yield fixname

yield translated(opt)
continue

if fp.suffix == ".png":
Expand Down
3 changes: 2 additions & 1 deletion pygbag/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ async def pack_files(zf, packlist, zfolders, target_folder):
zpath.insert(0, str(target_folder))
zpath.append(str(asset)[1:])

# print(f"\t{str(asset)[1:]}")
zip_content = target_folder / str(asset)[1:]
print(f"\t{target_folder} : {str(asset)[1:]}")


zpath = list(zfolders)
zpath.append(str(asset)[1:].replace("-pygbag.", "."))
Expand Down
68 changes: 49 additions & 19 deletions pygbag/support/cross/aio/pep0723.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#
# https://peps.python.org/pep-0722/ – Dependency specification for single-file scripts
# https://peps.python.org/pep-0723
# https://peps.python.org/pep-0508/ – Dependency specification for Python Software Packages

# https://setuptools.pypa.io/en/latest/userguide/ext_modules.html
#

import sys
import os
Expand All @@ -20,9 +22,6 @@
from aio.filelike import fopen

import platform

print(platform)

import platform_wasm.todo

PATCHLIST = []
Expand Down Expand Up @@ -136,7 +135,10 @@ def install(pkg_file, sconf=None):
"INSTALLER": b"pygbag",
},
)
if pkg_file not in HISTORY:
HISTORY.append(pkg_file)
importlib.invalidate_caches()
print("142: {pkg_file} installed")
except FileExistsError as ex:
print(f"38: {pkg_file} already installed (or partially)", ex)
except Exception as ex:
Expand Down Expand Up @@ -195,7 +197,10 @@ async def async_repos():
if not aio.cross.simulator:
import platform
print("193:", platform.window.location.href )
if platform.window.location.href.startswith("https://pmp-p.ddns.net/pygbag"):
if platform.window.location.href.startswith("http://localhost:8"):
for idx, repo in enumerate(Config.pkg_repolist):
repo["-CDN-"] = "http://localhost:8000/archives/repo/"
elif platform.window.location.href.startswith("https://pmp-p.ddns.net/pygbag"):
for idx, repo in enumerate(Config.pkg_repolist):
repo["-CDN-"] = "https://pmp-p.ddns.net/archives/repo/"
elif platform.window.location.href.startswith("http://192.168.1.66/pygbag"):
Expand Down Expand Up @@ -283,19 +288,7 @@ async def pip_install(pkg, sconf={}):


async def parse_code(code, env):
global PATCHLIST, async_imports_init, async_repos

# pythonrc is calling aio.pep0723.parse_code not check_list
# so do patching here
patchlevel = platform_wasm.todo.patch()
if patchlevel:
print("264:parse_code() patches loaded :", list(patchlevel.keys()) )
platform_wasm.todo.patch = lambda :None
# and only do that once and for all.
await async_imports_init()
await async_repos()
del async_imports_init, async_repos

global PATCHLIST

maybe_missing = []

Expand Down Expand Up @@ -338,10 +331,23 @@ async def parse_code(code, env):
# parse_code does the patching
# this is not called by pythonrc
async def check_list(code=None, filename=None):
global PATCHLIST
global PATCHLIST, async_imports_init, async_repos
print()
print("-" * 11, "computing required packages", "-" * 10)


# pythonrc is calling aio.pep0723.parse_code not check_list
# so do patching here
patchlevel = platform_wasm.todo.patch()
if patchlevel:
print("264:parse_code() patches loaded :", list(patchlevel.keys()) )
platform_wasm.todo.patch = lambda :None
# and only do that once and for all.
await async_imports_init()
await async_repos()
del async_imports_init, async_repos


# store installed wheel somewhere
env = Path(os.getcwd()) / "build" / "env"
env.mkdir(parents=True, exist_ok=True)
Expand All @@ -353,6 +359,9 @@ async def check_list(code=None, filename=None):
sconf = __import__("sysconfig").get_paths()
sconf["purelib"] = sconf["platlib"] = env.as_posix()

if sconf["platlib"] not in sys.path:
sys.path.append(sconf["platlib"])

# mandatory
importlib.invalidate_caches()

Expand Down Expand Up @@ -401,3 +410,24 @@ async def check_list(code=None, filename=None):

print("-" * 40)
print()

return still_missing


















# aio.pep0723
Loading

0 comments on commit ae3d761

Please sign in to comment.