Skip to content

Instantly share code, notes, and snippets.

@TruncatedDinoSour
Created May 15, 2023 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TruncatedDinoSour/2a38d3f7d18cf2544e0a8a2f1de26bf5 to your computer and use it in GitHub Desktop.
Save TruncatedDinoSour/2a38d3f7d18cf2544e0a8a2f1de26bf5 to your computer and use it in GitHub Desktop.
simple python bundler
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""a"""
def a() -> None:
print("this is a.py")
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""b"""
def b() -> None:
print("this is b.py")
#!/usr/bin/env bash
set -eu
echo 'bundling main.py main.bundle.py'
main() {
cat <<EOF
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""bundled on $(date)"""
def __make_methods_static(cls):
for name, method in cls.__dict__.items():
if callable(method):
setattr(cls, name, staticmethod(method))
return cls
EOF
while IFS= read -r line; do
if echo "$line" | grep -iq 'cat:expand'; then
mod="$(echo "$line" | awk '{print $2}')"
printf '@__make_methods_static\nclass %s:\n' "$mod"
sed 's/^/ /' "$mod.py"
else
echo "$line"
fi
done <main.py | sed '/^\s*$/d;s/\s\+$//'
}
main "$@" &>main.bundle.py
echo 'see main.bundle.py'
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""bundled on Mon 15 May 23:14:16 EEST 2023"""
def __make_methods_static(cls):
for name, method in cls.__dict__.items():
if callable(method):
setattr(cls, name, staticmethod(method))
return cls
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""main"""
from warnings import filterwarnings as filter_warnings
@__make_methods_static
class a:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""a"""
def a() -> None:
print("this is a.py")
@__make_methods_static
class b:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""b"""
def b() -> None:
print("this is b.py")
def main() -> int:
"""entry / main function"""
print("main calling", a.a(), b.b())
return 0
if __name__ == "__main__":
assert main.__annotations__.get("return") is int, "main() should return an integer"
filter_warnings("error", category=Warning)
raise SystemExit(main())
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""main"""
from warnings import filterwarnings as filter_warnings
import a # cat:expand
import b # cat:expand
def main() -> int:
"""entry / main function"""
print("main calling", a.a(), b.b())
return 0
if __name__ == "__main__":
assert main.__annotations__.get("return") is int, "main() should return an integer"
filter_warnings("error", category=Warning)
raise SystemExit(main())
@TruncatedDinoSour
Copy link
Author

u can remove empty line or extra whitespace removal, also its probably a nice idea to inherit from the module type :

from types import ModuleType
class ..(ModuleType): ...

u can modify they bundle script to do both of those

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment