#!/usr/bin/env python3 # SPDX-FileCopyrightText: Defame1297 and contributors # SPDX-License-Identifier: GPL-3.0-or-later """Assert that a built Collection tarball holds only files meant for consumers. scripts/check_tarball.py dist/mortis-kamino-X.Y.Z.tar.gz galaxy.yml `build_ignore` is a deny list: a new file at the repository root (a tool's cache, an editor's directory, an agent's settings) ships silently. This check is the allow list. A top-level entry that is not in ALLOWED_TOP, or a path under an entry that matches FORBIDDEN, fails the build. When the Collection gains a plugin type or a directory consumers should get, add it to ALLOWED_TOP in the same change. """ import fnmatch import sys import tarfile ALLOWED_TOP = { "COPYING", "FILES.json", "LICENSES", "MANIFEST.json", "NOTICE", "README.md", "REUSE.toml", "changelogs", "docs", "meta", "plugins", "roles", "tests", } # Checked on every member, whatever its top-level directory. FORBIDDEN = [ "*/molecule/*", "*/molecule", "tests/scripts*", "*.pyc", "*__pycache__*", "*.tar.gz", "*.pem", "*.key", "*.vault*", "*vault-pass*", "*/.git/*", "*.env", "*importer_result.json", ] def problems(names): found = [] for name in sorted(names): path = name.rstrip("/") if path.startswith("./"): path = path[2:] if not path: continue top = path.split("/", 1)[0] if top not in ALLOWED_TOP: found.append(f"{path}: top-level entry '{top}' is not allowed in the Collection archive") elif any(fnmatch.fnmatch(path, pat) for pat in FORBIDDEN): found.append(f"{path}: matches a forbidden pattern") return found def main(argv): if len(argv) != 2: sys.stderr.write(__doc__) return 2 with tarfile.open(argv[1]) as tar: names = tar.getnames() bad = problems(names) if bad: sys.stderr.write("Unexpected files in the Collection archive:\n " + "\n ".join(bad) + "\n") return 1 print(f"{argv[1]}: {len(names)} entries, all allowed") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))