-
-
Save TruncatedDinoSour/2a38d3f7d18cf2544e0a8a2f1de26bf5 to your computer and use it in GitHub Desktop.
simple python bundler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""a""" | |
def a() -> None: | |
print("this is a.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""b""" | |
def b() -> None: | |
print("this is b.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u can remove empty line or extra whitespace removal, also its probably a nice idea to inherit from the
module
type :u can modify they bundle script to do both of those