34-create-package-process (#35)
works on #34 Reviewed-on: #35 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
63
deploy/bundle.py
Normal file
63
deploy/bundle.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from datetime import datetime
|
||||
import fnmatch
|
||||
import os
|
||||
import zipfile
|
||||
|
||||
|
||||
class Packager:
|
||||
def __init__(
|
||||
self,
|
||||
name="chess",
|
||||
include_dirs=None,
|
||||
ignore_patterns=None
|
||||
):
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d%H%M")
|
||||
filename = f"{timestamp}.{name}.zip"
|
||||
self.outfile = os.path.join("./deploy/package", filename)
|
||||
self.include_dirs = include_dirs or []
|
||||
self.ignore_patterns = ignore_patterns or []
|
||||
|
||||
|
||||
def create_package(self):
|
||||
with zipfile.ZipFile(self.outfile, "w", zipfile.ZIP_DEFLATED) as z:
|
||||
for d in self.include_dirs:
|
||||
if not os.path.exists(d):
|
||||
raise FileNotFoundError(f"Missing directory {d}")
|
||||
for root, _, files in os.walk(d):
|
||||
for fname in files:
|
||||
rel_path = os.path.relpath(
|
||||
os.path.join(root, fname),
|
||||
start=os.path.dirname(d)
|
||||
)
|
||||
if self._is_ignored(rel_path):
|
||||
continue
|
||||
z.write(os.path.join(root, fname), rel_path)
|
||||
|
||||
print(f"[+] Package created: {self.outfile}")
|
||||
os.system(f"unzip -l {self.outfile}")
|
||||
|
||||
|
||||
def _is_ignored(self, path):
|
||||
for pat in self.ignore_patterns:
|
||||
if fnmatch.fnmatch(path, pat):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
packager = Packager(
|
||||
name="chess",
|
||||
include_dirs=[
|
||||
"build",
|
||||
"scripts",
|
||||
"binding",
|
||||
],
|
||||
ignore_patterns=[
|
||||
"*.pyc",
|
||||
"__pycache__/*",
|
||||
"*env*",
|
||||
"*config*",
|
||||
]
|
||||
)
|
||||
packager.create_package()
|
||||
Reference in New Issue
Block a user