|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +A quick and dirty script for bisecting nixpkgs to find where we broke |
| 4 | +
|
| 5 | +This is not complete and will probably not be, just keeping for reference |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import List |
| 9 | +import math |
| 10 | +import argparse |
| 11 | +import logging |
| 12 | +import subprocess |
| 13 | +import re |
| 14 | +import os |
| 15 | +import shutil |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + logging.basicConfig(encoding='utf-8', level=logging.DEBUG) |
| 24 | + args = argparser().parse_args() |
| 25 | + |
| 26 | + revlist_path: Path = Path(args.revlist) |
| 27 | + revlist = read_oneline_log(revlist_path) |
| 28 | + logger.info("Found %d revisions, bisection will take approximately %d steps", revlist, math.log2(len(revlist))) |
| 29 | + |
| 30 | + repo_dir = Path(__file__).parent.parent |
| 31 | + logger.info("Changing working directory to repo directory %s", repo_dir) |
| 32 | + os.chdir(repo_dir) |
| 33 | + |
| 34 | + logger.info("Initiating bisection") |
| 35 | + i_bad = 0 |
| 36 | + i_good = len(revlist) - 1 |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +def argparser(): |
| 41 | + parser = argparse.ArgumentParser(prog='nixpkgs-bisect', description='Bisect nixpkgs') |
| 42 | + parser.add_argument( |
| 43 | + "--revlist", |
| 44 | + required=True, |
| 45 | + description=( |
| 46 | + "File containing nixpkgs revision list. Generate this from nixpkgs with this command:\n\n" |
| 47 | + "git log --oneline $KNOWN_GOOD_REV..master --no-abbrev-commit" |
| 48 | + ) |
| 49 | + ) |
| 50 | + return parser |
| 51 | + |
| 52 | + |
| 53 | +def read_oneline_log(revlist_path: Path) -> List[str]: |
| 54 | + with revlist.open() as f: |
| 55 | + return [ |
| 56 | + l.split()[0] |
| 57 | + for l in f |
| 58 | + ] |
| 59 | + |
| 60 | + |
| 61 | +def change_this_repo_nixpkgs_version(flake_url: str): |
| 62 | + flake_file = Path('flake.nix') |
| 63 | + |
| 64 | + logger.info("Setting nixpkgs revision in %s to %s", flake_file, flake_url) |
| 65 | + |
| 66 | + with flakefile.open() as f: |
| 67 | + lines = list(f) |
| 68 | + lines[3] = f'nixpkgs.url = "{flake_url}";\n' |
| 69 | + |
| 70 | + with flakefile.open("w") as f: |
| 71 | + f.write(''.join(lines)) |
| 72 | + |
| 73 | + |
| 74 | +def set_nixpkgs_version(): |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +def try_build() -> bool: |
| 79 | + """ |
| 80 | + Try to nix flake update and build. Returns True on success, False otherwise. |
| 81 | + """ |
| 82 | + |
| 83 | + cmd = "nix flake update && nix build" |
| 84 | + logger.info("Executing: %s", cmd) |
| 85 | + result = subprocess.run(cmd, shell=True) |
| 86 | + return result.returncode == 0 |
| 87 | + |
| 88 | + |
| 89 | +def run_command(cmd: str): |
| 90 | + logger.debug("Executing shell command: %s", cmd) |
| 91 | + result = subprocess.run(cmd, shell=True) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
| 96 | + |
0 commit comments