Skip to content

Commit

Permalink
Merge pull request #173 from Notenlish/main
Browse files Browse the repository at this point in the history
add ini file loading + ignore file & folders
  • Loading branch information
pmp-p authored Aug 20, 2024
2 parents fff5410 + a09514a commit 1d8cdc6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ dmypy.json

/0.0
/.eric6project
/src
# /src
/dist
/ATTIC
/platform_wasm
Expand Down
37 changes: 36 additions & 1 deletion src/pygbag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from datetime import datetime

from .__init__ import VERSION
from .example_config import EXAMPLE_CONFIG

if "--no_ssl_check" in sys.argv:
import ssl
Expand All @@ -29,6 +30,10 @@

from . import pack
from . import web
from .config_types import Config


from config_to_object import load_config


devmode = "--dev" in sys.argv
Expand Down Expand Up @@ -314,6 +319,8 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN):
help="web site to cache locally [default:%s]" % cdn,
)

parser.add_argument("--ini", default=False, action="store_true", help="Initialize an example pygbag.ini config file")

parser.add_argument(
"--template",
default=DEFAULT_TMPL,
Expand Down Expand Up @@ -407,11 +414,39 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN):
"COLUMNS": args.COLUMNS,
"LINES": args.LINES,
"CONSOLE": args.CONSOLE,
"INI": args.ini,
}

if args.ini:
if not os.path.exists("pygbag.ini"):
with open("pygbag.ini", "w") as f:
f.write(EXAMPLE_CONFIG)
else:
print("ERROR: You already have an pygbag.ini file. Please delete it before attempting to init a fresh config.")
raise SystemExit

if os.path.exists("pygbag.ini"):
config: Config = load_config("pygbag.ini")
ignore_files = config.DEPENDENCIES.ignorefiles
ignore_dirs = config.DEPENDENCIES.ignoredirs
else:
print("WARNING: No pygbag.ini found! See: https://pygame-web.github.io/wiki/pygbag-configuration")
ignore_files = []
ignore_dirs = []

for ignore_arr in [ignore_files, ignore_dirs]:
for ignored in ignore_arr:
if ignored.strip().find(" ") != -1: # has space in folder/file name
print(f"|{ignored}|")
print("ERROR! You cannot use folder/files with spaces in it.")
raise SystemExit # maybe use a custom pygbag exception

pygbag.config = CC

await pack.archive(f"{app_name}.apk", app_folder, build_dir)
print(f"Ignored dirs: {ignore_dirs}")
print(f"Ignored files: {ignore_files}")

await pack.archive(f"{app_name}.apk", app_folder, ignore_dirs, ignore_files, build_dir)

def cache_file(remote_url, suffix):
nonlocal cache_dir
Expand Down
12 changes: 12 additions & 0 deletions src/pygbag/config_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Automatically generated type hinting file for a .ini file
# Generated with config-to-object https://pypi.org/project/config-to-object/1.0.0/
# Run "ini_typefile your_config.ini type_file.py" to create a new type file

from typing import NamedTuple, List, Tuple

class DEPENDENCIES(NamedTuple):
ignoredirs:List[str]
ignorefiles:List[str]

class Config(NamedTuple):
DEPENDENCIES:DEPENDENCIES
6 changes: 6 additions & 0 deletions src/pygbag/example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# An example .ini file for pygbag configs

[DEPENDENCIES]
ignoreDirs = ["Folder1", "Folder2", "Folder3"]
ignoreFiles = ["File1","File2", "File3"]
# run `ini_typefile example.ini config_types.py` to regenerate the type hints for the ini file
4 changes: 4 additions & 0 deletions src/pygbag/example_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EXAMPLE_CONFIG = """[DEPENDENCIES]
ignoreDirs = ["ignoreThisFolder","ignoreThisFolder2","ignoreThisFolder3"]
ignoreFiles = ["File1.ignorethis", "File2.ignorethis", "File3.ignorethis"]
"""
23 changes: 16 additions & 7 deletions src/pygbag/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@
/.github
/.vscode
/.idea
/.venv
/.tox
/.DS_Store
/dist
/build
/venv
/ignore
/static
/ATTIC
""".strip().split(
"\n"
)
""".splitlines()

SKIP_EXT = ["lnk", "pyc", "pyx", "pyd", "pyi", "exe", "bak", "log", "blend", "DS_Store"]


def filter(walked):
global dbg, IGNORE, SKIP_EXT
def filter(walked, ignore_dirs, ignore_files):
global dbg, IGNORE, SKIP_EXT, IGNORE_FILES
IGNORE.extend(ignore_dirs)

IGNORE_FILES = ignore_files

for folder, filenames in walked:
blocking = False

# ignore .* folders
if folder.startswith("."):
continue

for block in IGNORE:
if not block:
continue
Expand All @@ -58,10 +62,15 @@ def filter(walked):
continue

for filename in filenames:
# ignore .* files
if filename.startswith("."):
continue
if filename in [".gitignore"]:
if dbg:
print("REJ 3", folder, filename)
continue
if filename in IGNORE_FILES:
continue

ext = filename.rsplit(".", 1)[-1].lower()
if ext in SKIP_EXT:
Expand Down
4 changes: 2 additions & 2 deletions src/pygbag/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def stream_pack_replay():
print(f"replay packing {len(REPLAY.LIST)=} files complete for {REPLAY.APK}")


async def archive(apkname, target_folder, build_dir=None):
async def archive(apkname, target_folder, ignore_dirs:list[str], ignore_files:list[str], build_dir=None):
global COUNTER, REPLAY

COUNTER = 0
Expand All @@ -90,7 +90,7 @@ async def archive(apkname, target_folder, build_dir=None):

filtered = []
last = ""
for infolder, fullpath in filter(walked):
for infolder, fullpath in filter(walked, ignore_dirs, ignore_files):
if last != infolder:
print(f"Now in {infolder}")
last = infolder
Expand Down

0 comments on commit 1d8cdc6

Please sign in to comment.