Truly independent web browser
Meta: Check that cargo-sources.json matches Cargo.lock
Jelle Raaijmakersc0b12ba6a449
diff --git a/Meta/CMake/flatpak/generate-cargo-sources.py b/Meta/CMake/flatpak/generate-cargo-sources.pyindex df25eab5cd5..40557150ace 100644--- a/Meta/CMake/flatpak/generate-cargo-sources.py+++ b/Meta/CMake/flatpak/generate-cargo-sources.py@@ -7,8 +7,10 @@# Generate cargo-sources.json for Flatpak builds from Cargo.lock.# Run this script whenever Rust dependencies change.+import argparseimport jsonimport re+import sysfrom pathlib import Pathfrom typing import Any@@ -16,55 +18,77 @@ from typing import Anyscript_dir = Path(__file__).parentrepo_root = script_dir / ".." / ".." / ".."lock_path = repo_root / "Cargo.lock"+out_path = script_dir / "cargo-sources.json"+++def generate(lock: str) -> str:+ crates = []+ for m in re.finditer(+ r'name = "(.+?)"\nversion = "(.+?)"\nsource = "registry.+?"\nchecksum = "(.+?)"',+ lock,+ ):+ crates.append((m.group(1), m.group(2), m.group(3)))++ sources: list[dict[str, Any]] = [+ {+ "type": "git",+ "url": "https://github.com/corrosion-rs/corrosion.git",+ "tag": "v0.5.1",+ "dest": "corrosion",+ },+ ]++ for name, version, checksum in crates:+ sources.append(+ {+ "type": "archive",+ "archive-type": "tar-gzip",+ "url": f"https://static.crates.io/crates/{name}/{name}-{version}.crate",+ "sha256": checksum,+ "dest": f"cargo/vendor/{name}-{version}",+ }+ )++ checksum_commands = []+ for name, version, checksum in crates:+ d = f"cargo/vendor/{name}-{version}"+ checksum_json = json.dumps({"files": {}, "package": checksum})+ checksum_commands.append(f"echo '{checksum_json}' > {d}/.cargo-checksum.json")++ cargo_config = '[source.crates-io]\\nreplace-with = "vendored-sources"\\n\\n[source.vendored-sources]\\ndirectory = "cargo/vendor"\\n'-lock = lock_path.read_text()--crates = []-for m in re.finditer(- r'name = "(.+?)"\nversion = "(.+?)"\nsource = "registry.+?"\nchecksum = "(.+?)"',- lock,-):- crates.append((m.group(1), m.group(2), m.group(3)))--sources: list[dict[str, Any]] = [- {- "type": "git",- "url": "https://github.com/corrosion-rs/corrosion.git",- "tag": "v0.5.1",- "dest": "corrosion",- },-]--for name, version, checksum in crates:sources.append({- "type": "archive",- "archive-type": "tar-gzip",- "url": f"https://static.crates.io/crates/{name}/{name}-{version}.crate",- "sha256": checksum,- "dest": f"cargo/vendor/{name}-{version}",+ "type": "shell",+ "commands": [+ *checksum_commands,+ "mkdir -p .cargo",+ f"printf '{cargo_config}' > .cargo/config.toml",+ ],})-checksum_commands = []-for name, version, checksum in crates:- d = f"cargo/vendor/{name}-{version}"- checksum_json = json.dumps({"files": {}, "package": checksum})- checksum_commands.append(f"echo '{checksum_json}' > {d}/.cargo-checksum.json")--cargo_config = '[source.crates-io]\\nreplace-with = "vendored-sources"\\n\\n[source.vendored-sources]\\ndirectory = "cargo/vendor"\\n'--sources.append(- {- "type": "shell",- "commands": [- *checksum_commands,- "mkdir -p .cargo",- f"printf '{cargo_config}' > .cargo/config.toml",- ],- }-)+ return json.dumps(sources, indent=4) + "\n"-out_path = script_dir / "cargo-sources.json"-out_path.write_text(json.dumps(sources, indent=4) + "\n")-print(f"Generated {out_path}")++def main():+ parser = argparse.ArgumentParser(description="Generate cargo-sources.json for Flatpak builds from Cargo.lock.")+ parser.add_argument(+ "--check", action="store_true", help="only check that cargo-sources.json matches Cargo.lock, without writing it"+ )+ args = parser.parse_args()++ sources = generate(lock_path.read_text())+ if args.check:+ if not out_path.exists() or out_path.read_text() != sources:+ script = Path(__file__).resolve().relative_to(repo_root.resolve())+ print(f"{out_path.name} does not match Cargo.lock; run {script} to update it.")+ sys.exit(1)+ return++ out_path.write_text(sources)+ print(f"Generated {out_path}")+++if __name__ == "__main__":+ main()diff --git a/Meta/Linters/check_flatpak.py b/Meta/Linters/check_flatpak.pyindex 1c570111b85..7c8284099c9 100755--- a/Meta/Linters/check_flatpak.py+++ b/Meta/Linters/check_flatpak.py@@ -22,6 +22,9 @@ VCPKG_OVERLAYS_PORTS = LADYBIRD_SOURCE_DIR / "Meta" / "CMake" / "vcpkg" / "overlVCPKG_REPO = LADYBIRD_SOURCE_DIR / "Build" / "vcpkg-release"VCPKG_BASELINE_URL = "https://raw.githubusercontent.com/microsoft/vcpkg/{}/versions/baseline.json"FLATPAK_MANIFEST = LADYBIRD_SOURCE_DIR / "Meta" / "CMake" / "flatpak" / "org.ladybird.Ladybird.json"+CARGO_LOCK = LADYBIRD_SOURCE_DIR / "Cargo.lock"+CARGO_SOURCES = LADYBIRD_SOURCE_DIR / "Meta" / "CMake" / "flatpak" / "cargo-sources.json"+GENERATE_CARGO_SOURCES = LADYBIRD_SOURCE_DIR / "Meta" / "CMake" / "flatpak" / "generate-cargo-sources.py"SELF = "Ladybird"# List of build tools that are not provided by the Flatpak SDK and therefore in the manifest@@ -224,19 +227,27 @@ def check_vcpkg_vs_flatpak_versioning(vcpkg_repo: Path):return mismatch_found+def check_cargo_sources_match_cargo_lock():+ return subprocess.run([sys.executable, GENERATE_CARGO_SOURCES, "--check"]).returncode != 0++def main():parser = argparse.ArgumentParser()parser.add_argument("--vcpkg-repo", required=False, type=Path, default=VCPKG_REPO)- parser.add_argument("file_list", type=Path, nargs="*", default=[VCPKG])+ parser.add_argument("file_list", type=Path, nargs="*", default=[VCPKG, CARGO_LOCK])args = parser.parse_args()vcpkg_repo = args.vcpkg_repo.resolve()- file_list = [f.resolve() for f in args.file_list if f.exists()]+ requested = [f.resolve() for f in args.file_list]+ file_list = [f for f in requested if f.exists()]did_fail = Falseif VCPKG in file_list or FLATPAK_MANIFEST in file_list:did_fail = check_vcpkg_vs_flatpak_versioning(vcpkg_repo)+ if any(path in requested for path in (CARGO_LOCK, CARGO_SOURCES, GENERATE_CARGO_SOURCES)):+ did_fail |= check_cargo_sources_match_cargo_lock()+# TODO: Add linting of Flatpak and AppStream manifest# See #5594, bullet point "Missing lint job in CI to ensure Flatpak and AppStream manifests are up to snuff"