97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
import time
|
|
import urllib.parse
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
SERVER = "https://git.annodev.software"
|
|
REPOSITORY = "annodev/anno-base"
|
|
MARKER = ".annobase-version"
|
|
|
|
|
|
def setting(path, key):
|
|
value = json.loads(Path(path).read_text(encoding="utf-8-sig"))
|
|
for part in key.split("."):
|
|
value = value[part]
|
|
text = str(value).strip()
|
|
if not text:
|
|
raise ValueError(f"{path} has an empty {key}")
|
|
return text
|
|
|
|
|
|
def open_url(url, token):
|
|
if urllib.parse.urlparse(url).netloc != urllib.parse.urlparse(SERVER).netloc:
|
|
raise ValueError(f"{url} is not on {SERVER}")
|
|
request = urllib.request.Request(url, headers={"Authorization": "token " + token})
|
|
return urllib.request.urlopen(request, timeout=900)
|
|
|
|
|
|
def release_assets(version, token):
|
|
with open_url(f"{SERVER}/api/v1/repos/{REPOSITORY}/releases/tags/v{version}", token) as response:
|
|
release = json.load(response)
|
|
if release.get("draft"):
|
|
raise ValueError(f"AnnoBase v{version} is a draft release")
|
|
return {asset["name"]: asset["browser_download_url"] for asset in release["assets"]}
|
|
|
|
|
|
def download(url, token, target):
|
|
digest = hashlib.sha256()
|
|
with open_url(url, token) as response, target.open("wb") as stream:
|
|
while chunk := response.read(1024 * 1024):
|
|
stream.write(chunk)
|
|
digest.update(chunk)
|
|
return digest.hexdigest()
|
|
|
|
|
|
def extract(archive, destination):
|
|
with tarfile.open(archive) as tar:
|
|
if hasattr(tarfile, "data_filter"):
|
|
tar.extractall(destination, filter="data")
|
|
else:
|
|
tar.extractall(destination)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--settings", required=True)
|
|
parser.add_argument("--key", default="AnnoBase.Version")
|
|
parser.add_argument("--destination", default=os.path.join(os.environ.get("GITHUB_WORKSPACE", "."), "AnnoBase"))
|
|
args = parser.parse_args()
|
|
token = os.environ.get("ANNOBASE_TOKEN", "").strip()
|
|
if not token:
|
|
sys.exit("ANNOBASE_TOKEN is empty")
|
|
version = setting(args.settings, args.key)
|
|
destination = Path(args.destination)
|
|
marker = destination / MARKER
|
|
if marker.is_file() and marker.read_text().strip() == version:
|
|
print(f"AnnoBase {version} is already in {destination}")
|
|
return
|
|
if destination.exists():
|
|
if not marker.is_file():
|
|
sys.exit(f"{destination} exists and was not made by this step")
|
|
destination.rename(destination.with_name(f"{destination.name}.replaced.{int(time.time())}"))
|
|
assets = release_assets(version, token)
|
|
with tempfile.TemporaryDirectory(prefix="annobase-") as temp:
|
|
temp = Path(temp)
|
|
manifest_path = temp / "manifest.json"
|
|
download(assets["manifest.json"], token, manifest_path)
|
|
manifest = json.loads(manifest_path.read_text())
|
|
if manifest["version"] != version:
|
|
sys.exit(f"The manifest of v{version} names version {manifest['version']}")
|
|
archive = temp / manifest["file"]
|
|
if download(assets[manifest["file"]], token, archive) != manifest["sha256"]:
|
|
sys.exit(f"{manifest['file']} does not match the sha256 of its manifest")
|
|
destination.mkdir(parents=True)
|
|
extract(archive, destination)
|
|
marker.write_text(version + "\n")
|
|
print(f"AnnoBase {version} ({manifest['commit']}) is in {destination}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|