From cd27333687758447b1b27fd179d141f395cb1016 Mon Sep 17 00:00:00 2001 From: Dmitriy Kholkin Date: Thu, 6 Oct 2022 00:42:47 +0300 Subject: [PATCH] scripts for generate zen kernel patch --- scripts/find-latest-zfs-kernel.py | 21 +++++ scripts/gen-patch-zen.sh | 32 ++++++++ scripts/update-zen.py | 129 ++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100755 scripts/find-latest-zfs-kernel.py create mode 100755 scripts/gen-patch-zen.sh create mode 100755 scripts/update-zen.py diff --git a/scripts/find-latest-zfs-kernel.py b/scripts/find-latest-zfs-kernel.py new file mode 100755 index 0000000..42482fd --- /dev/null +++ b/scripts/find-latest-zfs-kernel.py @@ -0,0 +1,21 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i python3 -p python3 + +import sys +import re + +if __name__ == "__main__": + file = sys.argv[1] + results = [] + with open(file, "r") as lines: + for line in lines: + if "kernel.kernelOlder" in line: + results.append(line) + line = results[-2].strip() + pattern = re.compile(r"\d\.\d{1,2}") + version = pattern.search(line).group(0) + major, minor = version.split('.') + if int(minor) > 1: + minor = int(minor) - 1 + version = f"{major}.{minor}" + print(version) \ No newline at end of file diff --git a/scripts/gen-patch-zen.sh b/scripts/gen-patch-zen.sh new file mode 100755 index 0000000..b96f3e2 --- /dev/null +++ b/scripts/gen-patch-zen.sh @@ -0,0 +1,32 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i bash -p jq python3 +set -eu -o pipefail + +kCONFIG="$(pwd)" +kNIXPKGS=$(nix flake archive --json --dry-run nixpkgs | jq -r '.path') +kMAX_VER=`python $kCONFIG/scripts/find-latest-zfs-kernel.py $kNIXPKGS/pkgs/os-specific/linux/zfs/default.nix` + +echo $kMAX_VER +read -p "> Press any key to continue...1" -n 1 -r + +mkdir -p /tmp/nixpkgs/pkgs/os-specific/linux/kernel/ +mkdir -p ./patches + +cp $kNIXPKGS/pkgs/os-specific/linux/kernel/zen-kernels.nix /tmp/nixpkgs/pkgs/os-specific/linux/kernel/zen-kernels.nix +cd /tmp/nixpkgs +git init +git add -A +git commit -m "temp" + +read -p "> Press any key to continue...2" -n 1 -r + +python $kCONFIG/scripts/update-zen.py zen $kMAX_VER /tmp/nixpkgs/pkgs/os-specific/linux/kernel/zen-kernels.nix + +read -p "> Press any key to continue...3" -n 1 -r + +git add -A +git diff --cached > $kCONFIG/patches/zen-kernels.patch +cd $kCONFIG +rm -rf /tmp/nixpkgs + +echo "Complete!" \ No newline at end of file diff --git a/scripts/update-zen.py b/scripts/update-zen.py new file mode 100755 index 0000000..8f69810 --- /dev/null +++ b/scripts/update-zen.py @@ -0,0 +1,129 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i python3 -p python3 nix nix-prefetch-git + +import fileinput +import json +import os +import sys +import re +import subprocess + +from datetime import datetime +from urllib.request import urlopen, Request + + +def panic(exc): + raise Exception(exc) + + +DIR = os.path.dirname(os.path.abspath(__file__)) +HEADERS = {'Accept': 'application/vnd.github.v3+json'} + + +def github_api_request(endpoint): + base_url = 'https://api.github.com/' + request = Request(base_url + endpoint, headers=HEADERS) + with urlopen(request) as http_response: + return json.loads(http_response.read().decode('utf-8')) + + +def get_commit_date(repo, sha): + url = f'https://api.github.com/repos/{repo}/commits/{sha}' + request = Request(url, headers=HEADERS) + with urlopen(request) as http_response: + commit = json.loads(http_response.read().decode()) + date = commit['commit']['committer']['date'].rstrip('Z') + date = datetime.fromisoformat(date).date().isoformat() + return 'unstable-' + date + + +def nix_prefetch_git(url, rev): + """Prefetches the requested Git revision (incl. submodules) of the given repository URL.""" + print(f'nix-prefetch-git {url} {rev}') + out = subprocess.check_output([ + 'nix-prefetch-git', '--quiet', + '--url', url, + '--rev', rev, + '--fetch-submodules']) + return json.loads(out)['sha256'] + + +def nix_prefetch_url(url, unpack=False): + """Prefetches the content of the given URL.""" + print(f'nix-prefetch-url {url}') + options = ['--type', 'sha256'] + if unpack: + options += ['--unpack'] + out = subprocess.check_output(['nix-prefetch-url'] + options + [url]) + return out.decode('utf-8').rstrip() + + +def update_file(relpath, variant, version, suffix, sha256): + file_path = os.path.join(DIR, relpath) + with fileinput.FileInput(file_path, inplace=True) as f: + for line in f: + result = line + result = re.sub( + fr'^ version = ".+"; #{variant}', + f' version = "{version}"; #{variant}', + result) + result = re.sub( + fr'^ suffix = ".+"; #{variant}', + f' suffix = "{suffix}"; #{variant}', + result) + result = re.sub( + fr'^ sha256 = ".+"; #{variant}', + f' sha256 = "{sha256}"; #{variant}', + result) + print(result, end='') + + +def read_file(relpath, variant): + file_path = os.path.join(DIR, relpath) + re_version = re.compile(fr'^\s*version = "(.+)"; #{variant}') + re_suffix = re.compile(fr'^\s*suffix = "(.+)"; #{variant}') + version = None + suffix = None + with fileinput.FileInput(file_path, mode='r') as f: + for line in f: + version_match = re_version.match(line) + if version_match: + version = version_match.group(1) + continue + + suffix_match = re_suffix.match(line) + if suffix_match: + suffix = suffix_match.group(1) + continue + + if version and suffix: + break + return version, suffix + + +if __name__ == "__main__": + if len(sys.argv) == 1: + panic("Update variant expected") + variant = sys.argv[1] + if variant not in ("zen", "lqx"): + panic(f"Unexepected variant instead of 'zen' or 'lqx': {sys.argv[1]}") + version = sys.argv[2] + zen_file = sys.argv[3] + pattern = re.compile(fr"v({version}\.?\d*)-({variant}\d+)") + isFinded = False + page = 1 + while not isFinded: + zen_tags = github_api_request(f'repos/zen-kernel/zen-kernel/releases?page={page}&per_page=50') + for tag in zen_tags: + zen_match = pattern.match(tag['tag_name']) + if zen_match: + zen_tag = zen_match.group(0) + zen_version = zen_match.group(1) + zen_suffix = zen_match.group(2) + isFinded = True + break + page += 1 + old_version, old_suffix = read_file(zen_file, variant) + if old_version != zen_version or old_suffix != zen_suffix: + zen_hash = nix_prefetch_git('https://github.com/zen-kernel/zen-kernel.git', zen_tag) + update_file(zen_file, variant, zen_version, zen_suffix, zen_hash)