v0.0.2
This commit is contained in:
1 parent
ba300b2448
commit
6e5aa8e4e4
26 files changed
+814
-214
No files matched your search
+57
-25
@@ -1,38 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
EXCLUDED_DIRS = {'.git', '.dev', 'target', 'dist', 'packages', '__pycache__'}
|
||||
EXCLUDED_NAMES = {'.env'}
|
||||
|
||||
def run_git_command(args, repo_path: Path) -> bytes:
|
||||
result = subprocess.run(
|
||||
["git", *args],
|
||||
cwd=repo_path,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=True,
|
||||
)
|
||||
return result.stdout
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 2:
|
||||
print(f'usage: {Path(sys.argv[0]).name} OUTPUT.zip', file=sys.stderr)
|
||||
return 2
|
||||
output = Path(sys.argv[1]).resolve()
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
prefix = f'PostfixAdmin-RS-{project_version()}'
|
||||
with zipfile.ZipFile(output, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive:
|
||||
for path in sorted(ROOT.rglob('*')):
|
||||
if not path.is_file() or path.resolve() == output:
|
||||
def get_files_to_archive(repo_path: Path) -> list[str]:
|
||||
output = run_git_command(
|
||||
["ls-files", "--cached", "--others", "--exclude-standard", "-z"],
|
||||
repo_path,
|
||||
)
|
||||
files = output.decode("utf-8", errors="surrogateescape").split("\0")
|
||||
return [f for f in files if f]
|
||||
|
||||
|
||||
def make_zip(repo_path: Path, output_zip: Path) -> None:
|
||||
files = get_files_to_archive(repo_path)
|
||||
|
||||
output_zip = output_zip.resolve()
|
||||
if output_zip.exists():
|
||||
output_zip.unlink()
|
||||
|
||||
with zipfile.ZipFile(output_zip, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
||||
for rel_path in files:
|
||||
abs_path = repo_path / rel_path
|
||||
|
||||
if not abs_path.exists():
|
||||
continue
|
||||
rel = path.relative_to(ROOT)
|
||||
if any(part in EXCLUDED_DIRS for part in rel.parts) or rel.name in EXCLUDED_NAMES:
|
||||
|
||||
if abs_path.resolve() == output_zip:
|
||||
continue
|
||||
archive.write(path, f'{prefix}/{rel.as_posix()}')
|
||||
return 0
|
||||
|
||||
zf.write(abs_path, arcname=rel_path)
|
||||
|
||||
print(f"Created: {output_zip}")
|
||||
print(f"Added files: {len(files)}")
|
||||
|
||||
|
||||
def project_version() -> str:
|
||||
import tomllib
|
||||
with (ROOT / 'Cargo.toml').open('rb') as handle:
|
||||
return tomllib.load(handle)['package']['version']
|
||||
def main():
|
||||
repo_path = Path.cwd()
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
output_zip = Path(sys.argv[1])
|
||||
else:
|
||||
output_zip = repo_path / f"{repo_path.name}.zip"
|
||||
|
||||
try:
|
||||
run_git_command(["rev-parse", "--show-toplevel"], repo_path)
|
||||
except subprocess.CalledProcessError:
|
||||
print("Error: this directory is not a Git repository.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
make_zip(repo_path, output_zip)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user