#!/usr/bin/env python3 """Zabali slozku webu do ZIPu pro hosting alldev.eu. Pouziti: python3 make_zip.py [--include-sql] - cesty zapisuje s lomitky, i ve Windows, - vynecha .git, node_modules, .DS_Store, __MACOSX a podobne, - symbolicke odkazy nebali (hosting je odmitne) a vypise je, - dumpy *.sql vynecha, pokud neni --include-sql (v koreni webu by byly verejne). """ import os import sys import zipfile SKIP_DIRS = {".git", ".svn", ".hg", "node_modules", "__MACOSX", ".idea", ".vscode", ".cache", ".next", ".nuxt"} SKIP_FILES = {".DS_Store", "Thumbs.db", "desktop.ini"} SQL_EXT = (".sql", ".sql.gz", ".sql.zip") def main(argv): args = [a for a in argv if not a.startswith("--")] include_sql = "--include-sql" in argv if len(args) != 2: print(__doc__.strip()) return 2 src, out = os.path.abspath(args[0]), os.path.abspath(args[1]) if not os.path.isdir(src): print(f"Slozka neexistuje: {src}") return 2 if out.startswith(src + os.sep): print("Vystupni ZIP nesmi byt uvnitr balene slozky.") return 2 skipped, links, count = [], [], 0 with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED, compresslevel=6) as zf: for root, dirs, files in os.walk(src, followlinks=False): rel_root = os.path.relpath(root, src) keep = [] for d in sorted(dirs): full = os.path.join(root, d) if os.path.islink(full): links.append(os.path.join(rel_root, d)) elif d in SKIP_DIRS: skipped.append(os.path.join(rel_root, d) + "/") else: keep.append(d) dirs[:] = keep for f in sorted(files): full = os.path.join(root, f) rel = os.path.normpath(os.path.join(rel_root, f)) if os.path.islink(full): links.append(rel) continue if f in SKIP_FILES or f.startswith("._"): skipped.append(rel) continue if not include_sql and f.lower().endswith(SQL_EXT): skipped.append(rel + " (dump databaze, dej ho zvlast)") continue if not os.path.isfile(full): skipped.append(rel + " (neni bezny soubor)") continue zf.write(full, rel.replace(os.sep, "/")) count += 1 print(f"Vytvoreno: {out}") print(f"Souboru: {count}, velikost ZIPu: {os.path.getsize(out) / 1048576:.1f} MB") if skipped: print(f"Vynechano ({len(skipped)}):") for s in skipped[:40]: print(f" - {s}") if len(skipped) > 40: print(f" ... a dalsich {len(skipped) - 40}") if links: print("POZOR, symbolicke odkazy nebyly zabaleny (hosting je nepodporuje):") for s in links: print(f" - {s}") print("Dalsi krok: python3 check_zip.py " + out) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))