Something went wrong. Try again.
What happens if I push all of nixpkgs
Something went wrong. Try again.
7.1 kB · 235 lines
Python
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236#! /usr/bin/env nix-shell#! nix-shell -i python -p python3.pkgs.joblib python3.pkgs.click python3.pkgs.click-log nix nix-prefetch-git prefetch-yarn-deps prefetch-npm-deps gclient2nix"""electron updater
A script for updating electron source hashes.
It supports the following modes:
| Mode | Description ||------------- | ----------------------------------------------- || `update` | for updating a specific Electron release || `update-all` | for updating all electron releases at once |
The `update` commands requires a `--version` flagto specify the major release to be updated.The `update-all command updates all non-eol major releases.
The `update` and `update-all` commands accept an optional `--commit`flag to automatically commit the changes for you."""import base64import jsonimport loggingimport osimport randomimport reimport subprocessimport sysimport tempfileimport urllib.requestimport clickimport click_log
from datetime import datetimefrom typing import Iterable, Tuplefrom urllib.request import urlopenfrom joblib import Parallel, delayed, Memoryfrom update_util import *
# Relative path to the electron-source info.jsonSOURCE_INFO_JSON = "info.json"
os.chdir(os.path.dirname(__file__))
memory: Memory = Memory("cache", verbose=0)
logger = logging.getLogger(__name__)click_log.basic_config(logger)
def get_gclient_data(rev: str) -> any: output = subprocess.check_output( ["gclient2nix", "generate", f"https://github.com/electron/electron@{rev}", "--root", "src/electron"] )
return json.loads(output)
def get_chromium_file(chromium_tag: str, filepath: str) -> str: return base64.b64decode( urlopen( f"https://chromium.googlesource.com/chromium/src.git/+/{chromium_tag}/{filepath}?format=TEXT" ).read() ).decode("utf-8")
def get_electron_file(electron_tag: str, filepath: str) -> str: return ( urlopen( f"https://raw.githubusercontent.com/electron/electron/{electron_tag}/{filepath}" ) .read() .decode("utf-8") )
@memory.cachedef get_chromium_gn_source(chromium_tag: str) -> dict: gn_pattern = r"'gn_version': 'git_revision:([0-9a-f]{40})'" gn_commit = re.search(gn_pattern, get_chromium_file(chromium_tag, "DEPS")).group(1) gn_prefetch: bytes = subprocess.check_output( [ "nix-prefetch-git", "--quiet", "https://gn.googlesource.com/gn", "--rev", gn_commit, ] ) gn: dict = json.loads(gn_prefetch) return { "gn": { "version": datetime.fromisoformat(gn["date"]).date().isoformat(), "url": gn["url"], "rev": gn["rev"], "hash": gn["hash"], } }
@memory.cachedef get_electron_yarn_hash(electron_tag: str) -> str: print(f"prefetch-yarn-deps", file=sys.stderr) with tempfile.TemporaryDirectory() as tmp_dir: with open(tmp_dir + "/yarn.lock", "w") as f: f.write(get_electron_file(electron_tag, "yarn.lock")) return ( subprocess.check_output(["prefetch-yarn-deps", tmp_dir + "/yarn.lock"]) .decode("utf-8") .strip() )
@memory.cachedef get_chromium_npm_hash(chromium_tag: str) -> str: print(f"prefetch-npm-deps", file=sys.stderr) with tempfile.TemporaryDirectory() as tmp_dir: with open(tmp_dir + "/package-lock.json", "w") as f: f.write(get_chromium_file(chromium_tag, "third_party/node/package-lock.json")) return ( subprocess.check_output( ["prefetch-npm-deps", tmp_dir + "/package-lock.json"] ) .decode("utf-8") .strip() )
def get_update(major_version: str, m: str, gclient_data: any) -> Tuple[str, dict]:
tasks = [] a = lambda: (("electron_yarn_hash", get_electron_yarn_hash(gclient_data["src/electron"]["args"]["tag"]))) tasks.append(delayed(a)()) a = lambda: ( ( "chromium_npm_hash", get_chromium_npm_hash(gclient_data["src"]["args"]["tag"]), ) ) tasks.append(delayed(a)()) random.shuffle(tasks)
task_results = { n[0]: n[1] for n in Parallel(n_jobs=3, require="sharedmem", return_as="generator")(tasks) if n != None }
return ( f"{major_version}", { "deps": gclient_data, **{key: m[key] for key in ["version", "modules", "chrome", "node"]}, "chromium": { "version": m["chrome"], "deps": get_chromium_gn_source(gclient_data["src"]["args"]["tag"]), }, **task_results, }, )
def non_eol_releases(releases: Iterable[int]) -> Iterable[int]: """Returns a list of releases that have not reached end-of-life yet.""" return tuple(filter(lambda x: x in supported_version_range(), releases))
def update_source(version: str, commit: bool) -> None: """Update a given electron-source release
Args: version: The major version number, e.g. '27' commit: Whether the updater should commit the result """ major_version = version
package_name = f"electron-source.electron_{major_version}" print(f"Updating electron-source.electron_{major_version}")
old_info = load_info_json(SOURCE_INFO_JSON) old_version = ( old_info[major_version]["version"] if major_version in old_info else None )
m, rev = get_latest_version(major_version) if old_version == m["version"]: print(f"{package_name} is up-to-date") return
gclient_data = get_gclient_data(rev) new_info = get_update(major_version, m, gclient_data) out = old_info | {new_info[0]: new_info[1]}
save_info_json(SOURCE_INFO_JSON, out)
new_version = new_info[1]["version"] if commit: commit_result(package_name, old_version, new_version, SOURCE_INFO_JSON)
@click.group()def cli() -> None: """A script for updating electron-source hashes""" pass
@cli.command("update", help="Update a single major release")@click.option("-v", "--version", required=True, type=str, help="The major version, e.g. '23'")@click.option("-c", "--commit", is_flag=True, default=False, help="Commit the result")def update(version: str, commit: bool) -> None: update_source(version, commit)
@cli.command("update-all", help="Update all releases at once")@click.option("-c", "--commit", is_flag=True, default=False, help="Commit the result")def update_all(commit: bool) -> None: """Update all eletron-source releases at once
Args: commit: Whether to commit the result """ old_info = load_info_json(SOURCE_INFO_JSON)
filtered_releases = non_eol_releases(tuple(map(lambda x: int(x), old_info.keys())))
for major_version in filtered_releases: update_source(str(major_version), commit)
if __name__ == "__main__": cli()