add bundle.py to create single-file bbj client

python is able to import modules from a zip file. if the zip file
contains __main__.py it will even run it as a script! this lets us bundle
bbj's frontend together with its sole external dependency (urwid) to
create a single executable file that'll run anywhere with python
installed, no virtualenv needed.

the only downside is that python can't import shared objects (.so) from a
zip file, so urwid can't use its C-accelerated str_util module and has
to fall back to the python version. which is slower, probably.
master
magical 2022-08-10 03:45:03 +00:00
parent ab1550c09c
commit 804438f045
1 changed files with 32 additions and 0 deletions

32
bundle.py 100644
View File

@ -0,0 +1,32 @@
import zipfile
import glob
import os
# TODO: should we include .pyc files?
# TODO: add urwid source into the repo somewhere
files = {
'__main__.py': 'clients/urwid/main.py',
'network.py': 'clients/network_client.py',
'urwid': 'env/lib/python3.8/site-packages/urwid/*.py',
}
with open('bbj_demo', 'wb') as f:
f.write(b"#!/usr/bin/env python3\n")
with zipfile.ZipFile(f, 'w', compression=zipfile.ZIP_DEFLATED) as z:
z.comment = b'BBJ'
for name, source in files.items():
if '*' in source:
dirname = name
for path in sorted(glob.glob(source)):
name = dirname + '/' + os.path.basename(path)
z.write(path, name)
else:
z.write(source, name)
try:
mask = os.umask(0)
os.umask(mask)
except OSError:
mask = 0
os.chmod(z.filename, 0o777&~mask)