diff --git a/tools/add_module.py b/tools/add_module.py index c0ef016..82feb7e 100644 --- a/tools/add_module.py +++ b/tools/add_module.py @@ -6,21 +6,15 @@ import tempfile import tarfile from integrity import file_hash import json +import urllib.request -def make_git_archive(repo_path: Path, tag: str, output_path: Path): - try: - subprocess.run( - ["git", "archive", "--format=tar.gz", "--output", str(output_path.absolute()), tag], - cwd=repo_path, - check=True - ) - except subprocess.CalledProcessError as e: - print(f"Error creating git archive: {e}") - sys.exit(1) +def download_archive(archive_url: str) -> Path: + filename, _ = urllib.request.urlretrieve(archive_url) + return Path(filename) -def extract_module_content(archive_path: Path) -> str: +def extract_module_content(archive_path: Path, prefix: str | None) -> str: with tarfile.open(archive_path, "r:gz") as tar: - file = tar.extractfile("MODULE.bazel") + file = tar.extractfile(("" if str is None else (prefix + "/")) + "MODULE.bazel") if file is None: print("Error: MODULE.bazel file not found in the archive.") sys.exit(1) @@ -54,33 +48,26 @@ if __name__ == "__main__": parser = argparse.ArgumentParser(description="Add a module to the registry.") parser.add_argument("module_name", type=str, help="Name of the module to add") parser.add_argument("module_version", type=str, help="Version of the module to add") - parser.add_argument("local_git_repo_path", type=str, help="Path to the local git repository") + parser.add_argument("archive_url", type=str, help="Path to the archive URL") parser.add_argument("remote_blob_path", type=str, help="SCP-compatible path to the remote blob storage folder") - parser.add_argument("-t", "--tag", type=str, default=None, help="Git tag for the module version") parser.add_argument("-p", "--scp_port", type=int, default=22, help="Port for SCP connection (default: 22)") + parser.add_argument("--prefix", type=str, help="Archive prefix") args = parser.parse_args() module_name = args.module_name module_version = args.module_version - local_git_repo_path = Path(args.local_git_repo_path) - git_repo_tag = args.tag or f"v{module_version}" + archive_url = args.archive_url + archive_prefix = args.prefix - if not local_git_repo_path.exists(): - print(f"Error: The specified local git repository path '{local_git_repo_path}' does not exist.") - sys.exit(1) - registry_module_path = MODULES_DIR / module_name / module_version if registry_module_path.exists(): print(f"Error: The module '{module_name}' version '{module_version}' already exists in the registry.") sys.exit(1) - tmp_dir = tempfile.TemporaryDirectory() + archive_file = download_archive(archive_url) - archive_file = Path(tmp_dir.name) / f"archive.tar.gz" - make_git_archive(local_git_repo_path, git_repo_tag, archive_file) - - module_content = extract_module_content(archive_file) + module_content = extract_module_content(archive_file, archive_prefix) integrity = file_hash(archive_file) partial_hash = integrity[7:17] @@ -98,5 +85,8 @@ if __name__ == "__main__": "integrity": integrity, } + if archive_prefix is not None: + source_json["strip_prefix"] = archive_prefix + with open(registry_module_path / "source.json", "wb+") as f: f.write(json.dumps(source_json, indent=2).encode())