|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from pathlib import Path |
| 3 | +from typing import cast, NotRequired, TypedDict |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | + |
| 9 | +Source = TypedDict( |
| 10 | + "Source", |
| 11 | + { |
| 12 | + "type": str, |
| 13 | + "url": str, |
| 14 | + "reference": str, |
| 15 | + }, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class Package(TypedDict): |
| 20 | + name: str |
| 21 | + version: str |
| 22 | + source: NotRequired[Source] |
| 23 | + dist: Source |
| 24 | + |
| 25 | + |
| 26 | +def clone_git_repo(url: str, rev: str, clone_target_path: Path) -> None: |
| 27 | + subprocess.check_call( |
| 28 | + ["git", "init"], |
| 29 | + cwd=clone_target_path, |
| 30 | + ) |
| 31 | + subprocess.check_call( |
| 32 | + ["git", "fetch", url, rev, "--depth", "1"], |
| 33 | + cwd=clone_target_path, |
| 34 | + ) |
| 35 | + subprocess.check_call( |
| 36 | + ["git", "reset", "--hard", "FETCH_HEAD"], |
| 37 | + cwd=clone_target_path, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def fetch_composer_package(package: Package, clone_target_path: Path) -> None: |
| 42 | + assert ( |
| 43 | + "source" in package and package["source"]["type"] == "git" |
| 44 | + ), f"Package “{package['name']}” does not have source of type “git”." |
| 45 | + |
| 46 | + clone_git_repo( |
| 47 | + url=package["source"]["url"], |
| 48 | + rev=package["source"]["reference"], |
| 49 | + clone_target_path=clone_target_path, |
| 50 | + ) |
| 51 | + |
| 52 | + # Clean up git directory to ensure reproducible output |
| 53 | + shutil.rmtree(clone_target_path / ".git") |
| 54 | + |
| 55 | + |
| 56 | +def make_package( |
| 57 | + package: Package, |
| 58 | + clone_target_path: Path, |
| 59 | +) -> tuple[str, dict[str, Package]]: |
| 60 | + assert ( |
| 61 | + package["source"]["reference"] == package["dist"]["reference"] |
| 62 | + ), f"Package “{package['name']}” has a mismatch between “reference” keys of “dist” and “source” keys." |
| 63 | + |
| 64 | + # While Composer repositories only really require `name`, `version` and `source`/`dist` fields, |
| 65 | + # we will use the original contents of the package’s entry from `composer.lock`, modifying just the sources. |
| 66 | + # Package entries in Composer repositories correspond to `composer.json` files [1] |
| 67 | + # and Composer appears to use them when regenerating the lockfile. |
| 68 | + # If we just used the minimal info, stuff like `autoloading` or `bin` programs would be broken. |
| 69 | + # |
| 70 | + # We cannot use `source` since Composer does not support path sources: |
| 71 | + # "PathDownloader" is a dist type downloader and can not be used to download source |
| 72 | + # |
| 73 | + # [1]: https://getcomposer.org/doc/05-repositories.md#packages> |
| 74 | + |
| 75 | + # Copy the Package so that we do not mutate the original. |
| 76 | + package = cast(Package, dict(package)) |
| 77 | + package.pop("source", None) |
| 78 | + package["dist"] = { |
| 79 | + "type": "path", |
| 80 | + "url": str(clone_target_path / package["name"] / package["version"]), |
| 81 | + "reference": package["dist"]["reference"], |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + package["name"], |
| 86 | + { |
| 87 | + package["version"]: package, |
| 88 | + }, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def main( |
| 93 | + lockfile_path: Path, |
| 94 | + output_path: Path, |
| 95 | +) -> None: |
| 96 | + # We are generating a repository of type Composer |
| 97 | + # https://getcomposer.org/doc/05-repositories.md#composer |
| 98 | + with open(lockfile_path) as lockfile: |
| 99 | + lock = json.load(lockfile) |
| 100 | + repo_path = output_path / "repo" |
| 101 | + |
| 102 | + # We always need to fetch dev dependencies so that `composer update --lock` can update the config. |
| 103 | + packages_to_install = lock["packages"] + lock["packages-dev"] |
| 104 | + |
| 105 | + for package in packages_to_install: |
| 106 | + clone_target_path = repo_path / package["name"] / package["version"] |
| 107 | + clone_target_path.mkdir(parents=True) |
| 108 | + fetch_composer_package(package, clone_target_path) |
| 109 | + |
| 110 | + repo_manifest = { |
| 111 | + "packages": { |
| 112 | + package_name: metadata |
| 113 | + for package_name, metadata in [ |
| 114 | + make_package(package, repo_path) for package in packages_to_install |
| 115 | + ] |
| 116 | + } |
| 117 | + } |
| 118 | + with open(output_path / "packages.json", "w") as repo_manifest_file: |
| 119 | + json.dump( |
| 120 | + repo_manifest, |
| 121 | + repo_manifest_file, |
| 122 | + indent=4, |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + parser = argparse.ArgumentParser( |
| 128 | + description="Generate composer repository for offline fetching" |
| 129 | + ) |
| 130 | + parser.add_argument( |
| 131 | + "lockfile_path", |
| 132 | + help="Path to a composer lockfile", |
| 133 | + ) |
| 134 | + parser.add_argument( |
| 135 | + "output_path", |
| 136 | + help="Output path to store the repository in", |
| 137 | + ) |
| 138 | + |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + main( |
| 142 | + lockfile_path=Path(args.lockfile_path), |
| 143 | + output_path=Path(args.output_path), |
| 144 | + ) |
0 commit comments