Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions extended/publish/central_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
Usage:
central_bundle.py stage --from <repo> [--from <repo>...] --out <dir> [--project <owner/repo>]
[--dev-suffix=<-dev.sha>] [--exclude-group <group>...]
central_bundle.py sign <dir> [--key <id>] [--checksums-only]
[--skip-target <target>...]
central_bundle.py sign <dir> [--key <id>] [--checksums-only] [--all-checksums]
central_bundle.py validate <dir> [--version <v> | --dev=<-dev.sha>] [--require <group:artifact>...]
[--verify-signatures | --unsigned]
central_bundle.py zip <dir> <bundle.zip>
Expand Down Expand Up @@ -35,13 +36,21 @@
out and listed, so the root offers only the targets the bundle carries and a consumer
on another target is told the module does not support it instead of failing on a
missing file. A real dependency on a missing fork component is left for validate.
With --skip-target (repeatable: tvosArm64, js, ...) every component that is a target
of a root (its Gradle module's component names the root, and its artifact is
<root>-<target> in lower case) is left out before that, so the roots lose those
variants the same way: a release carries the targets its consumers use, not every
target the build configured. wasm-js is its own target, never taken for js.
With --dev-suffix every staged version V becomes V<suffix>, everywhere it is named:
directories, file names, each POM's own version and the versions of the fork's
artifacts it depends on, and in each Gradle module the component, its variants'
available-at targets, file names and urls, and dependency versions. A reference left
at V would send a consumer of the dev build back to the release.
sign writes .md5, .sha1, .sha256 and .sha512 beside every file, and an ASCII-armoured
detached signature (.asc) made with gpg, which must hold the secret key already.
sign writes .md5 and .sha1 beside every file, which is what Central requires (it takes
.sha256 and .sha512 but does not ask for them, and every file counts against the
namespace's file limit), and an ASCII-armoured detached signature (.asc) made with
gpg, which must hold the secret key already. --all-checksums writes .sha256 and
.sha512 as well.
GPG_PASSPHRASE, when set, is the key's passphrase. --checksums-only writes the
checksums and no signatures, for a repository that does not ask for them.
validate checks <dir> the way Central's validation does, and a little more:
Expand All @@ -53,7 +62,8 @@
and scm with url and connection;
- every component that is not a bare POM has its main file, a sources jar and a
javadoc jar;
- every file has .asc, .md5, .sha1, .sha256 and .sha512, and the checksums match;
- every file has .asc, .md5 and .sha1, and every checksum beside it (.sha256 and
.sha512 too, when there) matches;
- every org.thisisthepy dependency a POM names, and every org.thisisthepy variant a
Gradle module points at, is in the bundle at that version, so nothing on Central
sends a consumer to a coordinate nobody published;
Expand Down Expand Up @@ -89,6 +99,8 @@
POM_NS = "http://maven.apache.org/POM/4.0.0"
XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
CHECKSUMS = {"md5": hashlib.md5, "sha1": hashlib.sha1, "sha256": hashlib.sha256, "sha512": hashlib.sha512}
# What Central requires beside every file; the others in CHECKSUMS are optional there.
REQUIRED_CHECKSUMS = ("md5", "sha1")
SIDE_FILES = tuple("." + ext for ext in CHECKSUMS) + (".asc",)
SKIPPED = ("maven-metadata", "_remote.repositories", ".lastUpdated")
VERSION_RULE = re.compile(r"^\d+(\.\d+)*(-[A-Za-z0-9.]+)?-ext\.\d+$")
Expand Down Expand Up @@ -127,7 +139,7 @@ def primary_files(directory):

# --- stage -------------------------------------------------------------------------------

def stage(sources, out, project, dev_suffix=None, excluded=()):
def stage(sources, out, project, dev_suffix=None, excluded=(), skip_targets=()):
os.makedirs(out, exist_ok=True)
conflicts = []
roots = []
Expand Down Expand Up @@ -157,6 +169,8 @@ def stage(sources, out, project, dev_suffix=None, excluded=()):
for line in roots:
print(f"FAIL {line}")
sys.exit(f"error: {len(roots)} Gradle module(s) differ between hosts; only the first repository may publish them")
if skip_targets:
drop_targets(out, skip_targets)
prune_unbuilt(out)
if dev_suffix:
rename_versions(out, dev_suffix)
Expand All @@ -177,6 +191,34 @@ def stage(sources, out, project, dev_suffix=None, excluded=()):
print(f"staged {sum(1 for _ in components(out))} component(s) in {out}, {len(added)} empty jar(s) added")


def target_of(group, artifact, version, directory):
"""The target a component publishes for its root (iosarm64, wasm-js), or None for a root."""
module_path = os.path.join(directory, f"{artifact}-{version}.module")
if not os.path.isfile(module_path):
return None
with open(module_path) as f:
component = json.load(f).get("component", {})
root = component.get("module", "")
if "url" not in component or component.get("group") != group or not artifact.startswith(root + "-"):
return None
return artifact[len(root) + 1:]


def drop_targets(out, skip_targets):
skipped = {target.lower() for target in skip_targets}
dropped = []
for group, artifact, version, directory in list(components(out)):
if target_of(group, artifact, version, directory) in skipped:
shutil.rmtree(directory)
parent = os.path.dirname(directory)
if not os.listdir(parent):
os.rmdir(parent)
dropped.append(f"{group}:{artifact}:{version}")
for line in sorted(dropped):
print(f"skip {line}")
print(f"left out {len(dropped)} component(s) for {', '.join(sorted(skipped))}")


def prune_unbuilt(out):
pruned = {}
for group, artifact, version, directory in list(components(out)):
Expand Down Expand Up @@ -372,7 +414,7 @@ def empty_jar(path, classifier, coordinate):

# --- sign --------------------------------------------------------------------------------

def sign(out, key, checksums_only=False):
def sign(out, key, checksums_only=False, all_checksums=False):
passphrase = os.environ.get("GPG_PASSPHRASE")
count = 0
for _, _, _, directory in components(out):
Expand All @@ -381,6 +423,8 @@ def sign(out, key, checksums_only=False):
with open(path, "rb") as f:
data = f.read()
for ext, algorithm in CHECKSUMS.items():
if not all_checksums and ext not in REQUIRED_CHECKSUMS:
continue
with open(f"{path}.{ext}", "w") as f:
f.write(algorithm(data).hexdigest())
count += 1
Expand Down Expand Up @@ -466,7 +510,8 @@ def validate(out, version, required, verify, dev=None, unsigned=False):
for ext, algorithm in CHECKSUMS.items():
side = f"{path}.{ext}"
if not os.path.isfile(side):
errors.append(f"{where}: {name} has no .{ext}")
if ext in REQUIRED_CHECKSUMS:
errors.append(f"{where}: {name} has no .{ext}")
continue
with open(side) as f:
content = f.read().split()
Expand Down Expand Up @@ -637,10 +682,12 @@ def main():
p.add_argument("--project", default="thisisthepy/compose-multiplatform-core-extended")
p.add_argument("--dev-suffix")
p.add_argument("--exclude-group", action="append", default=[])
p.add_argument("--skip-target", dest="skip_targets", action="append", default=[])
p = commands.add_parser("sign")
p.add_argument("dir")
p.add_argument("--key")
p.add_argument("--checksums-only", action="store_true")
p.add_argument("--all-checksums", action="store_true")
p = commands.add_parser("validate")
p.add_argument("dir")
p.add_argument("--version")
Expand All @@ -656,9 +703,10 @@ def main():
p.add_argument("url")
args = parser.parse_args()
if args.command == "stage":
stage([os.path.expanduser(s) for s in args.sources], args.out, args.project, args.dev_suffix, set(args.exclude_group))
stage([os.path.expanduser(s) for s in args.sources], args.out, args.project, args.dev_suffix, set(args.exclude_group),
args.skip_targets)
elif args.command == "sign":
sign(args.dir, args.key, args.checksums_only)
sign(args.dir, args.key, args.checksums_only, args.all_checksums)
elif args.command == "validate":
if args.dev and not DEV_SUFFIX.match(args.dev):
sys.exit(f"error: a dev suffix is -dev.<commit>[.<commit>], not '{args.dev}'")
Expand Down
47 changes: 45 additions & 2 deletions extended/publish/tests/central-bundle.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,18 @@ expect_fail "a required coordinate is enforced" "required, and not in the bundle

out="$(build noasc)"; rm "$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.jar.asc"
expect_fail "a missing signature fails" "has no .asc" "$out"
out="$(build nosha512)"; rm "$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.pom.sha512"
expect_fail "a missing sha512 fails" "has no .sha512" "$out"
out="$(build nosha1)"; rm "$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.pom.sha1"
expect_fail "a missing sha1 fails" "has no .sha1" "$out"
# Central requires .md5 and .sha1 and only takes .sha256 and .sha512; every file counts against
# the namespace's file limit, so sign writes the two it requires unless asked for all four.
out="$(build twosums)"; d="$out/org/thisisthepy/compose/ui/ui/$V"
[[ -f "$d/ui-$V.pom.md5" && -f "$d/ui-$V.pom.sha1" && ! -e "$d/ui-$V.pom.sha256" && ! -e "$d/ui-$V.pom.sha512" ]] &&
pass "sign writes .md5 and .sha1 and no .sha256 or .sha512" || fail "sign wrote other checksums than .md5 and .sha1"
python3 -I "$tool" sign "$out" --all-checksums > /dev/null
[[ -f "$d/ui-$V.pom.sha256" && -f "$d/ui-$V.pom.sha512" ]] &&
pass "sign --all-checksums writes .sha256 and .sha512 as well" || fail "sign --all-checksums"
echo 0 > "$d/ui-$V.jar.sha256"
expect_fail "a .sha256 that is there and wrong fails" "sha256 does not match" "$out"
out="$(build badmd5)"; echo 0 > "$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.jar.md5"
expect_fail "a wrong checksum fails" "md5 does not match" "$out"
out="$(build tampered)"; echo x >> "$out/org/thisisthepy/compose/ui/ui-linuxx64/$V/ui-linuxx64-$V.klib"
Expand Down Expand Up @@ -191,6 +201,39 @@ fi
grep -q "linuxX64ApiElements-published" "$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.module" &&
pass "a variant on a staged target is kept" || fail "a staged target's variant was pruned"

# --skip-target: a target's components are left out and the root loses its variant for it,
# so a release carries only the targets its consumers use. wasm-js is not js.
repo="$work/skip-repo"; out="$work/skip-out"; rm -rf "$repo" "$out"
make_repo "$repo"
for t in js wasm-js; do
d="$repo/org/thisisthepy/compose/ui/ui-$t/$V"; mkdir -p "$d"
pom "$d/ui-$t-$V.pom" org.thisisthepy.compose.ui "ui-$t" "$V" klib
echo klib > "$d/ui-$t-$V.klib"
cat > "$d/ui-$t-$V.module" <<JSON
{"formatVersion":"1.1","component":{"url":"../../ui/$V/ui-$V.module","group":"org.thisisthepy.compose.ui","module":"ui","version":"$V"},
"variants":[{"name":"${t}ApiElements-published","files":[{"name":"ui-$t-$V.klib","url":"ui-$t-$V.klib"}]}]}
JSON
done
python3 -I - "$repo/org/thisisthepy/compose/ui/ui/$V/ui-$V.module" "$V" <<'PY2'
import json, sys
path, v = sys.argv[1:]
m = json.load(open(path))
for t, name in (("js", "jsApiElements-published"), ("wasm-js", "wasmJsApiElements-published")):
m["variants"].append({"name": name, "available-at": {"url": f"../../ui-{t}/{v}/ui-{t}-{v}.module", "group": "org.thisisthepy.compose.ui", "module": f"ui-{t}", "version": v}})
json.dump(m, open(path, "w"))
PY2
python3 -I "$tool" stage --from "$repo" --out "$out" --skip-target js --skip-target tvosArm64 > "$work/log"
python3 -I "$tool" sign "$out" > /dev/null
root="$out/org/thisisthepy/compose/ui/ui/$V/ui-$V.module"
if [[ ! -e "$out/org/thisisthepy/compose/ui/ui-js" && -d "$out/org/thisisthepy/compose/ui/ui-wasm-js" ]] &&
grep -q "skip org.thisisthepy.compose.ui:ui-js:$V" "$work/log" && ! grep -q '"jsApiElements-published"' "$root" &&
grep -q wasmJsApiElements-published "$root" && grep -q linuxX64ApiElements-published "$root" &&
python3 -I "$tool" validate "$out" --verify-signatures > "$work/vlog"; then
pass "a skipped target's components and root variant are left out, wasm-js and linuxX64 kept"
else
fail "skip-target"; cat "$work/log" "$work/vlog" >&2
fi

# A dev build: every version, and every reference to one, gets the suffix.
S=-dev.0123abc
repo="$work/dev-repo"; out="$work/dev-out"; rm -rf "$repo" "$out"
Expand Down
Loading