-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite Cronjob build triggering logic in python; read from influxdb (#…
…275) * uv init scripts * declare my intentions * in scripts uv add influxdb3-python * initial stats from influxdb * point out where arch could be filtered * rewrite triggering logic in python * make recheck * exclude prereleases * automatically install deps * what does github actions use for its python version? * github actions uses python 3.8 * don't evaluate annotations at runtime * document make rule * make trigger-all; make sure env is correct * dependabot for python? * NameError: name 'GITHUB_REPOSITORY' is not defined * stricter bash scripting * logging; refactors; run on actions branch? * /bin/sh is dash on ubuntu * fix typo * better description * why is stderr going missing? * cancel in progress * delete old bash implementation and unused scripts * rename to cronjob_scripts/ * I'm not going to fight against the autoformatter * turns out we use this elsewhere * more fixes after rename * just print to stdout? * don't need this either anymore * take indentation from main * Update cronjob_scripts/checkout_worktree.py Co-authored-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Signed-off-by: David Laban <alsuren@gmail.com> * Update cronjob_scripts/stats.py Co-authored-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Signed-off-by: David Laban <alsuren@gmail.com> * Apply suggestions from code review Co-authored-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Signed-off-by: David Laban <alsuren@gmail.com> * not using this either * code review suggestion * reinstate supported-targets; unit test to make sure it's correct * we're using 3.8 in CI * assert nonempty crate name * fix current arch detection code * nits * note about crates.io db dump * only run python tests on linux * actually don't put python tests in the matrix in the first place * apparently the name is important * cache influxdb client between invocations * bit of loop fusion in get_latest_version() * another walrus suggestion * stop with the blanket exception hiding * another try-catch-throw turned into an assert * typo * don't ask crates.io more than once about the same crate --------- Signed-off-by: David Laban <alsuren@gmail.com> Co-authored-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
- Loading branch information
Showing
28 changed files
with
548 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Scripts | ||
|
||
This folder contains python scripts for use in cargo-quickinstall github actions for triggering builds. | ||
|
||
This code has some quite heavy python dependencies and strong assumptions about running on unix, so we should not use it on the github actions runners that actually do the package building. | ||
|
||
TODO: make a build_scripts/ folder and move all of the package building scripts into there (converting to python as desired). |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import subprocess | ||
|
||
|
||
TARGET_ARCH_TO_BUILD_OS = { | ||
"x86_64-apple-darwin": "macos-latest", | ||
"aarch64-apple-darwin": "macos-latest", | ||
"x86_64-unknown-linux-gnu": "ubuntu-20.04", | ||
"x86_64-unknown-linux-musl": "ubuntu-20.04", | ||
"x86_64-pc-windows-msvc": "windows-latest", | ||
"aarch64-pc-windows-msvc": "windows-latest", | ||
"aarch64-unknown-linux-gnu": "ubuntu-20.04", | ||
"aarch64-unknown-linux-musl": "ubuntu-20.04", | ||
"armv7-unknown-linux-musleabihf": "ubuntu-20.04", | ||
"armv7-unknown-linux-gnueabihf": "ubuntu-20.04", | ||
} | ||
|
||
|
||
def get_build_os(target_arch: str) -> str: | ||
try: | ||
return TARGET_ARCH_TO_BUILD_OS[target_arch] | ||
except KeyError: | ||
raise ValueError(f"Unrecognised target arch: {target_arch}") | ||
|
||
|
||
def get_target_architectures() -> list[str]: | ||
target_arch = os.environ.get("TARGET_ARCH", None) | ||
if target_arch in TARGET_ARCH_TO_BUILD_OS: | ||
return [target_arch] | ||
|
||
if target_arch == "all": | ||
return list(TARGET_ARCH_TO_BUILD_OS.keys()) | ||
|
||
rustc_version_output = subprocess.run( | ||
["rustc", "--version", "--verbose"], capture_output=True, text=True | ||
) | ||
|
||
assert ( | ||
rustc_version_output.returncode == 0 | ||
), f"rustc --version --verbose failed: {rustc_version_output}" | ||
|
||
host_values = [ | ||
line.removeprefix("host: ") | ||
for line in rustc_version_output.stdout.splitlines() | ||
if line.startswith("host: ") | ||
] | ||
assert ( | ||
len(host_values) == 1 | ||
), f"rustc did not tell us its host, or told us multiple: {rustc_version_output}" | ||
|
||
return host_values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
import subprocess | ||
|
||
|
||
def checkout_worktree_for_arch(target_arch: str): | ||
""" | ||
Checkout a git worktree for the given target_arch, in /tmp. | ||
This is required for reading the exclude files. | ||
This is lifted directly from the old trigger-package-build.sh script, and is only expected to | ||
work on linux/macos with dash/bash. | ||
""" | ||
worktree_path = f"/tmp/cargo-quickinstall-{target_arch}" | ||
bash_script = f""" | ||
set -eux | ||
rm -rf {worktree_path} | ||
git worktree remove -f {worktree_path} || true | ||
git branch -D "trigger/{target_arch}" || true | ||
git worktree add --force --force {worktree_path} | ||
cd {worktree_path} | ||
if git fetch origin "trigger/{target_arch}"; then | ||
git checkout "origin/trigger/{target_arch}" -B "trigger/{target_arch}" | ||
elif ! git checkout "trigger/{target_arch}"; then | ||
# New branch with no history. Credit: https://stackoverflow.com/a/13969482 | ||
git checkout --orphan "trigger/{target_arch}" | ||
git rm --cached -r . || true | ||
git commit -m "Initial Commit" --allow-empty | ||
git push origin "trigger/{target_arch}" | ||
fi | ||
""" | ||
subprocess.run(bash_script, shell=True, check=True, text=True) | ||
return worktree_path | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
if len(sys.argv) != 2: | ||
print(f"Usage: {sys.argv[0]} <target_arch>") | ||
sys.exit(1) | ||
|
||
worktree_path = checkout_worktree_for_arch(sys.argv[1]) | ||
print(f"checked out to {worktree_path}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
import functools | ||
from typing import TypedDict | ||
import requests | ||
|
||
import semver | ||
|
||
|
||
class CrateVersionDict(TypedDict): | ||
""" | ||
A returned row from the crates.io index API. | ||
Note that the returned row also includes all of the fields from the jsonschema at | ||
https://doc.rust-lang.org/cargo/reference/registry-index.html#json-schema | ||
but I can't be bothered typing them right now. | ||
""" | ||
|
||
name: str | ||
vers: str | ||
features: dict[str, list[str]] | ||
|
||
|
||
def get_index_url(crate: str): | ||
""" | ||
Packages with 1 character names are placed in a directory named 1. | ||
Packages with 2 character names are placed in a directory named 2. | ||
Packages with 3 character names are placed in the directory 3/{first-character} where {first-character} is the first character of the package name. | ||
All other packages are stored in directories named {first-two}/{second-two} where the top directory is the first two characters of the package name, and the next subdirectory is the third and fourth characters of the package name. For example, cargo would be stored in a file named ca/rg/cargo. | ||
-- https://doc.rust-lang.org/cargo/reference/registry-index.html#index-files | ||
""" | ||
if len(crate) == 0: | ||
raise ValueError("Empty crate name") | ||
if len(crate) == 1: | ||
return f"https://index.crates.io/1/{crate}" | ||
elif len(crate) == 2: | ||
return f"https://index.crates.io/2/{crate}" | ||
elif len(crate) == 3: | ||
return f"https://index.crates.io/3/{crate[0]}/{crate}" | ||
else: | ||
return f"https://index.crates.io/{crate[:2]}/{crate[2:4]}/{crate}" | ||
|
||
|
||
@functools.lru_cache | ||
def get_latest_version(crate: str) -> CrateVersionDict | None: | ||
""" | ||
Calls the crates.io index API to get the latest version of the given crate. | ||
There is no rate limit on this api, so we can call it as much as we like. | ||
""" | ||
url = get_index_url(crate) | ||
|
||
response = requests.get(url) | ||
if response.status_code == 404: | ||
print(f"No crate named {crate}") | ||
return None | ||
response.raise_for_status() | ||
|
||
max_version: CrateVersionDict | None = None | ||
max_parsed_version: semver.VersionInfo | None = None | ||
for line in response.text.splitlines(): | ||
version = json.loads(line) | ||
parsed_version = semver.VersionInfo.parse(version["vers"]) | ||
if version["yanked"] or parsed_version.prerelease: | ||
continue | ||
|
||
if max_parsed_version is None or parsed_version > max_parsed_version: | ||
max_version = version | ||
max_parsed_version = parsed_version | ||
|
||
return max_version | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
if len(sys.argv) != 2: | ||
print(f"Usage: {sys.argv[0]} <crate>") | ||
sys.exit(1) | ||
|
||
version = get_latest_version(sys.argv[1]) | ||
if version is not None: | ||
print(version["vers"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[project] | ||
name = "cronjob-scripts" | ||
version = "0.0.0" | ||
description = "Scripts for use in cargo-quickinstall github actions" | ||
readme = "README.md" | ||
# This is what ubuntu-latest has installed by default | ||
requires-python = ">=3.8" | ||
dependencies = [ | ||
"influxdb3-python>=0.8.0", | ||
"requests>=2.32.3", | ||
"semver>=3.0.2", | ||
] |
Oops, something went wrong.