|
|
|
@@ -10,7 +10,7 @@ import urllib.parse
|
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
|
|
DEFAULT_REGISTRY_URL = "https://git.thyme-labs.com/api/packages/khodo/generic"
|
|
|
|
|
DEFAULT_VERSION = "latest"
|
|
|
|
|
REGISTRY_SLOT = "latest"
|
|
|
|
|
PAGE_LIMIT = 50
|
|
|
|
|
|
|
|
|
|
def read_config(path):
|
|
|
|
@@ -30,7 +30,10 @@ def read_config(path):
|
|
|
|
|
registry_url = artifact.get("registry_url")
|
|
|
|
|
if not isinstance(registry_url, str) or not registry_url:
|
|
|
|
|
registry_url = DEFAULT_REGISTRY_URL
|
|
|
|
|
return name, registry_url
|
|
|
|
|
targets = artifact.get("targets")
|
|
|
|
|
if not isinstance(targets, list) or not targets or any(not isinstance(target, str) or target not in data.get("tooling", {}).get("targets", {}) for target in targets):
|
|
|
|
|
raise SystemExit(f"{path}: package.artifact.targets must name configured build targets")
|
|
|
|
|
return name, registry_url, targets
|
|
|
|
|
|
|
|
|
|
def parse_registry_url(registry_url):
|
|
|
|
|
parsed = urllib.parse.urlparse(registry_url.rstrip("/"))
|
|
|
|
@@ -66,11 +69,11 @@ def version_delete_url(parsed, prefix, owner, package_name, version):
|
|
|
|
|
path = f"{prefix}/api/packages/{quote_path(owner, 'generic', package_name, version)}"
|
|
|
|
|
return build_url(parsed, path)
|
|
|
|
|
|
|
|
|
|
def file_upload_url(registry_url, package_name, version, path):
|
|
|
|
|
def file_upload_url(registry_url, package_name, path):
|
|
|
|
|
return "/".join((
|
|
|
|
|
registry_url.rstrip("/"),
|
|
|
|
|
urllib.parse.quote(package_name, safe=""),
|
|
|
|
|
urllib.parse.quote(version, safe=""),
|
|
|
|
|
REGISTRY_SLOT,
|
|
|
|
|
urllib.parse.quote(path.name, safe=""),
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
@@ -129,23 +132,18 @@ def delete_version(parsed, prefix, owner, package_name, version, auth):
|
|
|
|
|
raise SystemExit(f"DELETE {url} returned HTTP {response.status}")
|
|
|
|
|
print(f"Deleted package version: {package_name}/{version}")
|
|
|
|
|
|
|
|
|
|
def discover_artifacts(dist_dir, package_name, version):
|
|
|
|
|
archives = sorted(dist_dir.glob(f"{package_name}-{version}-*.tar.gz"))
|
|
|
|
|
def discover_artifacts(dist_dir, package_name, targets):
|
|
|
|
|
artifacts = []
|
|
|
|
|
for archive in archives:
|
|
|
|
|
artifacts.append(archive)
|
|
|
|
|
checksum = archive.with_name(archive.name + ".sha256")
|
|
|
|
|
if checksum.is_file():
|
|
|
|
|
artifacts.append(checksum)
|
|
|
|
|
if not artifacts:
|
|
|
|
|
raise SystemExit(f"no artifacts found matching {dist_dir}/{package_name}-{version}-*.tar.gz")
|
|
|
|
|
for target in sorted(set(targets)):
|
|
|
|
|
archive = dist_dir / f"{package_name}-{target}.tar.gz"
|
|
|
|
|
artifacts.extend((archive, archive.with_name(archive.name + ".sha256")))
|
|
|
|
|
missing = [path for path in artifacts if not path.is_file()]
|
|
|
|
|
if missing:
|
|
|
|
|
raise SystemExit("missing artifact files: " + ", ".join(str(path) for path in missing))
|
|
|
|
|
return artifacts
|
|
|
|
|
|
|
|
|
|
def upload_file(registry_url, package_name, version, path, auth):
|
|
|
|
|
url = file_upload_url(registry_url, package_name, version, path)
|
|
|
|
|
def upload_file(registry_url, package_name, path, auth):
|
|
|
|
|
url = file_upload_url(registry_url, package_name, path)
|
|
|
|
|
headers = {
|
|
|
|
|
"Content-Length": str(path.stat().st_size),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
@@ -155,34 +153,33 @@ def upload_file(registry_url, package_name, version, path, auth):
|
|
|
|
|
with response:
|
|
|
|
|
if response.status != 201:
|
|
|
|
|
raise SystemExit(f"PUT {url} returned HTTP {response.status}")
|
|
|
|
|
print(f"Uploaded package file: {package_name}/{version}/{path.name}")
|
|
|
|
|
print(f"Uploaded package file: {package_name}/{path.name}")
|
|
|
|
|
|
|
|
|
|
def parse_args(argv):
|
|
|
|
|
parser = argparse.ArgumentParser(description="Replace Product Vamp's rolling Gitea generic package.")
|
|
|
|
|
parser.add_argument("--config", type=pathlib.Path, default=pathlib.Path("modulus.json"))
|
|
|
|
|
parser.add_argument("--dist-dir", type=pathlib.Path, default=pathlib.Path("build/packages"))
|
|
|
|
|
parser.add_argument("--version", default=DEFAULT_VERSION)
|
|
|
|
|
parser.add_argument("--user", default=None)
|
|
|
|
|
parser.add_argument("--token", default=None)
|
|
|
|
|
return parser.parse_args(argv)
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
args = parse_args(argv)
|
|
|
|
|
package_name, registry_url = read_config(args.config)
|
|
|
|
|
package_name, registry_url, targets = read_config(args.config)
|
|
|
|
|
user = args.user or os_environ("PACKAGE_REGISTRY_USER")
|
|
|
|
|
token = args.token or os_environ("PACKAGE_REGISTRY_TOKEN")
|
|
|
|
|
auth = authorization(user, token)
|
|
|
|
|
parsed, prefix, owner = parse_registry_url(registry_url)
|
|
|
|
|
artifacts = discover_artifacts(args.dist_dir, package_name, args.version)
|
|
|
|
|
artifacts = discover_artifacts(args.dist_dir, package_name, targets)
|
|
|
|
|
existing_versions = list_versions(parsed, prefix, owner, package_name, auth)
|
|
|
|
|
# Gitea rejects duplicate file names within a generic package version, so
|
|
|
|
|
# replacing the rolling release requires deleting the old version first.
|
|
|
|
|
if args.version in existing_versions:
|
|
|
|
|
delete_version(parsed, prefix, owner, package_name, args.version, auth)
|
|
|
|
|
if REGISTRY_SLOT in existing_versions:
|
|
|
|
|
delete_version(parsed, prefix, owner, package_name, REGISTRY_SLOT, auth)
|
|
|
|
|
for artifact in artifacts:
|
|
|
|
|
upload_file(registry_url, package_name, args.version, artifact, auth)
|
|
|
|
|
upload_file(registry_url, package_name, artifact, auth)
|
|
|
|
|
for version in existing_versions:
|
|
|
|
|
if version != args.version:
|
|
|
|
|
if version != REGISTRY_SLOT:
|
|
|
|
|
delete_version(parsed, prefix, owner, package_name, version, auth)
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|