Adapt add_module script to new server

This commit is contained in:
2025-07-14 22:51:37 +02:00
parent b093cfc671
commit 894e0149ea

View File

@ -6,21 +6,15 @@ import tempfile
import tarfile import tarfile
from integrity import file_hash from integrity import file_hash
import json import json
import urllib.request
def make_git_archive(repo_path: Path, tag: str, output_path: Path): def download_archive(archive_url: str) -> Path:
try: filename, _ = urllib.request.urlretrieve(archive_url)
subprocess.run( return Path(filename)
["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 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: 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: if file is None:
print("Error: MODULE.bazel file not found in the archive.") print("Error: MODULE.bazel file not found in the archive.")
sys.exit(1) sys.exit(1)
@ -54,33 +48,26 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Add a module to the registry.") 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_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("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("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("-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() args = parser.parse_args()
module_name = args.module_name module_name = args.module_name
module_version = args.module_version module_version = args.module_version
local_git_repo_path = Path(args.local_git_repo_path) archive_url = args.archive_url
git_repo_tag = args.tag or f"v{module_version}" 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 registry_module_path = MODULES_DIR / module_name / module_version
if registry_module_path.exists(): if registry_module_path.exists():
print(f"Error: The module '{module_name}' version '{module_version}' already exists in the registry.") print(f"Error: The module '{module_name}' version '{module_version}' already exists in the registry.")
sys.exit(1) sys.exit(1)
tmp_dir = tempfile.TemporaryDirectory() archive_file = download_archive(archive_url)
archive_file = Path(tmp_dir.name) / f"archive.tar.gz" module_content = extract_module_content(archive_file, archive_prefix)
make_git_archive(local_git_repo_path, git_repo_tag, archive_file)
module_content = extract_module_content(archive_file)
integrity = file_hash(archive_file) integrity = file_hash(archive_file)
partial_hash = integrity[7:17] partial_hash = integrity[7:17]
@ -98,5 +85,8 @@ if __name__ == "__main__":
"integrity": integrity, "integrity": integrity,
} }
if archive_prefix is not None:
source_json["strip_prefix"] = archive_prefix
with open(registry_module_path / "source.json", "wb+") as f: with open(registry_module_path / "source.json", "wb+") as f:
f.write(json.dumps(source_json, indent=2).encode()) f.write(json.dumps(source_json, indent=2).encode())