|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Package a QGIS plugin directory for upload to the official QGIS plugin repository.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import re |
| 9 | +import zipfile |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +EXCLUDE_PATTERNS = [ |
| 13 | + r"^ui_.*\.py$", |
| 14 | + r"^resources_rc\.py$", |
| 15 | + r"^.*_rc\.py$", |
| 16 | + r"^.*\.pyc$", |
| 17 | + r"^.*\.pyo$", |
| 18 | + r"^.*\.bak$", |
| 19 | + r"^.*~$", |
| 20 | + r"^\..*\.swp$", |
| 21 | + r"^.*\.orig$", |
| 22 | + r"^package_plugin\.py$", |
| 23 | +] |
| 24 | + |
| 25 | +EXCLUDE_DIRS = { |
| 26 | + "__pycache__", |
| 27 | + "__MACOSX", |
| 28 | + ".git", |
| 29 | + ".svn", |
| 30 | + ".hg", |
| 31 | + ".github", |
| 32 | + ".idea", |
| 33 | + ".vscode", |
| 34 | + ".pytest_cache", |
| 35 | + ".mypy_cache", |
| 36 | + ".tox", |
| 37 | + ".eggs", |
| 38 | + "build", |
| 39 | + "dist", |
| 40 | + "node_modules", |
| 41 | + "scripts", |
| 42 | + "help", |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def should_exclude_file(filename: str) -> bool: |
| 47 | + return any(re.match(pattern, filename) for pattern in EXCLUDE_PATTERNS) |
| 48 | + |
| 49 | + |
| 50 | +def should_exclude_dir(dirname: str) -> bool: |
| 51 | + return dirname.startswith(".") or dirname in EXCLUDE_DIRS or dirname.endswith(".egg-info") |
| 52 | + |
| 53 | + |
| 54 | +def get_version_from_metadata(plugin_dir: Path) -> str: |
| 55 | + metadata_file = plugin_dir / "metadata.txt" |
| 56 | + if metadata_file.exists(): |
| 57 | + with metadata_file.open("r", encoding="utf-8") as f: |
| 58 | + for line in f: |
| 59 | + if line.startswith("version="): |
| 60 | + return line.split("=", 1)[1].strip() |
| 61 | + return "unknown" |
| 62 | + |
| 63 | + |
| 64 | +def package_plugin(source_dir: Path, output_path: Path | None, target_name: str) -> Path: |
| 65 | + if not source_dir.exists(): |
| 66 | + raise FileNotFoundError(f"Source directory not found: {source_dir}") |
| 67 | + if not source_dir.is_dir(): |
| 68 | + raise ValueError(f"Source path is not a directory: {source_dir}") |
| 69 | + |
| 70 | + version = get_version_from_metadata(source_dir) |
| 71 | + if output_path is None: |
| 72 | + output_path = source_dir.parent / f"{target_name}-{version}.zip" |
| 73 | + |
| 74 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 75 | + if output_path.exists(): |
| 76 | + output_path.unlink() |
| 77 | + |
| 78 | + print(f"Packaging plugin from: {source_dir}") |
| 79 | + print(f"Output zip file: {output_path}") |
| 80 | + print(f"Root folder name in zip: {target_name}") |
| 81 | + print(f"Plugin version: {version}") |
| 82 | + |
| 83 | + files_added = 0 |
| 84 | + files_excluded = 0 |
| 85 | + with zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 86 | + for root, dirs, files in os.walk(source_dir): |
| 87 | + dirs[:] = [d for d in dirs if not should_exclude_dir(d)] |
| 88 | + for file in files: |
| 89 | + file_path = Path(root) / file |
| 90 | + if should_exclude_file(file) or file.startswith("."): |
| 91 | + files_excluded += 1 |
| 92 | + continue |
| 93 | + rel_path = file_path.relative_to(source_dir) |
| 94 | + archive_name = Path(target_name) / rel_path |
| 95 | + zipf.write(file_path, archive_name) |
| 96 | + files_added += 1 |
| 97 | + |
| 98 | + print(f"Package created successfully: {output_path}") |
| 99 | + print(f"Files added: {files_added}") |
| 100 | + print(f"Files excluded: {files_excluded}") |
| 101 | + return output_path |
| 102 | + |
| 103 | + |
| 104 | +def main() -> int: |
| 105 | + parser = argparse.ArgumentParser(description=__doc__) |
| 106 | + parser.add_argument("--output", "-o", type=Path, default=None, help="Output path for the zip file") |
| 107 | + parser.add_argument("--source", "-s", type=Path, default=Path("."), help="Source plugin directory") |
| 108 | + parser.add_argument("--name", "-n", default=None, help="Target plugin folder name in the zip") |
| 109 | + args = parser.parse_args() |
| 110 | + |
| 111 | + source_dir = args.source.resolve() |
| 112 | + target_name = args.name or source_dir.name |
| 113 | + package_plugin(source_dir, args.output, target_name) |
| 114 | + return 0 |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + raise SystemExit(main()) |
0 commit comments