|
| 1 | +""" |
| 2 | +Methods for requesting information from a remote Git repository. |
| 3 | +""" |
| 4 | + |
| 5 | +import math |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | + |
| 9 | +import giturlparse |
| 10 | + |
| 11 | +DEFAULT_TIMEOUT = 10 |
| 12 | + |
| 13 | + |
| 14 | +class Remote: |
| 15 | + """Represents a remote Git repository""" |
| 16 | + |
| 17 | + url: str |
| 18 | + |
| 19 | + _impl: "_RemoteImpl | None" = None |
| 20 | + |
| 21 | + def __init__(self, url: str): |
| 22 | + self.url = url |
| 23 | + |
| 24 | + p = giturlparse.parse(url) |
| 25 | + |
| 26 | + match p.platform: |
| 27 | + case "github": |
| 28 | + self._impl = _GitHubRemote(p) |
| 29 | + |
| 30 | + @property |
| 31 | + def firmware_download_url(self) -> str: |
| 32 | + """URL of a page where users can download firmware builds""" |
| 33 | + if self._impl: |
| 34 | + return self._impl.firmware_download_url |
| 35 | + |
| 36 | + raise NotImplementedError(f"Cannot get download URL for {self.url}") |
| 37 | + |
| 38 | + def repo_exists(self) -> bool: |
| 39 | + """Get whether the remote URL points to a valid repo""" |
| 40 | + |
| 41 | + # Git will return a non-zero status code if it can't access the given URL. |
| 42 | + status = subprocess.call( |
| 43 | + ["git", "ls-remote", self.url], |
| 44 | + stdout=subprocess.DEVNULL, |
| 45 | + stderr=subprocess.DEVNULL, |
| 46 | + ) |
| 47 | + return status == 0 |
| 48 | + |
| 49 | + def revision_exists(self, revision: str) -> bool: |
| 50 | + """Get whether the remote repo contains a commit with a given revision""" |
| 51 | + |
| 52 | + # If the given revision is a tag or branch, then ls-remote can find it. |
| 53 | + # The output will be empty if the revision isn't found. |
| 54 | + if subprocess.check_output(["git", "ls-remote", self.url, revision]): |
| 55 | + return True |
| 56 | + |
| 57 | + # If the given revision is a (possibly abbreviated) commit hash, then |
| 58 | + # check if it can be fetched from the remote repo without actually |
| 59 | + # fetching it. (This works for commit hashes and tags, but not branches.) |
| 60 | + status = subprocess.call( |
| 61 | + [ |
| 62 | + "git", |
| 63 | + "fetch", |
| 64 | + self.url, |
| 65 | + revision, |
| 66 | + "--negotiate-only", |
| 67 | + "--negotiation-tip", |
| 68 | + revision, |
| 69 | + ], |
| 70 | + stdout=subprocess.DEVNULL, |
| 71 | + stderr=subprocess.DEVNULL, |
| 72 | + ) |
| 73 | + return status == 0 |
| 74 | + |
| 75 | + def get_tags(self) -> list[str]: |
| 76 | + """ |
| 77 | + Get a list of tags from the remote repo. |
| 78 | +
|
| 79 | + Tags are sorted in descending order by version. |
| 80 | + """ |
| 81 | + lines = subprocess.check_output( |
| 82 | + ["git", "ls-remote", "--tags", "--refs", self.url], text=True |
| 83 | + ).splitlines() |
| 84 | + |
| 85 | + # ls-remote output is "<hash> refs/tags/<tag>" for each tag. |
| 86 | + # Return only the text after "refs/tags/". |
| 87 | + tags = (line.split()[-1].removeprefix("refs/tags/") for line in lines) |
| 88 | + |
| 89 | + return sorted( |
| 90 | + tags, |
| 91 | + key=_TaggedVersion, |
| 92 | + reverse=True, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +class _RemoteImpl: |
| 97 | + """Implementation for platform-specific accessors""" |
| 98 | + |
| 99 | + @property |
| 100 | + def firmware_download_url(self) -> str: |
| 101 | + """URL of a page where users can download firmware builds""" |
| 102 | + raise NotImplementedError() |
| 103 | + |
| 104 | + |
| 105 | +class _GitHubRemote(_RemoteImpl): |
| 106 | + """Implementation for GitHub""" |
| 107 | + |
| 108 | + def __init__(self, parsed: giturlparse.GitUrlParsed): |
| 109 | + self._parsed = parsed |
| 110 | + |
| 111 | + @property |
| 112 | + def owner(self) -> str: |
| 113 | + """Username of the repo's owner""" |
| 114 | + return self._parsed.owner |
| 115 | + |
| 116 | + @property |
| 117 | + def repo(self) -> str: |
| 118 | + """Name of the repo""" |
| 119 | + return self._parsed.repo |
| 120 | + |
| 121 | + @property |
| 122 | + def firmware_download_url(self) -> str: |
| 123 | + return ( |
| 124 | + f"https://github.com/{self.owner}/{self.repo}/actions/workflows/build.yml" |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +class _TaggedVersion: |
| 129 | + major: int | None = None |
| 130 | + minor: int | None = None |
| 131 | + patch: int | None = None |
| 132 | + |
| 133 | + def __init__(self, tag: str): |
| 134 | + self.tag = tag |
| 135 | + |
| 136 | + if m := re.match(r"v(\d+)(?:\.(\d+))?(?:\.(\d+))?", self.tag): |
| 137 | + self.major = _try_int(m.group(1)) |
| 138 | + self.minor = _try_int(m.group(2)) |
| 139 | + self.patch = _try_int(m.group(3)) |
| 140 | + |
| 141 | + def __lt__(self, other: _TaggedVersion): |
| 142 | + return self._sort_key < other._sort_key |
| 143 | + |
| 144 | + @property |
| 145 | + def _sort_key(self): |
| 146 | + return ( |
| 147 | + _int_or_inf(self.major), |
| 148 | + _int_or_inf(self.minor), |
| 149 | + _int_or_inf(self.patch), |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def _try_int(val: str | None): |
| 154 | + return None if val is None else int(val) |
| 155 | + |
| 156 | + |
| 157 | +def _int_or_inf(val: int | None): |
| 158 | + return math.inf if val is None else val |
0 commit comments