Merge pull request #98304 from jtojnar/updateScript-commit3

maintainers/scripts/update.nix: Add support for auto-commiting changes

authored by

Jan Tojnar and committed by
GitHub
74c54720 1b17aea6

+274 -83
+6 -3
doc/stdenv/stdenv.xml
··· 475 475 <programlisting> 476 476 passthru.updateScript = [ ../../update.sh pname "--requested-release=unstable" ]; 477 477 </programlisting> 478 - </para> 479 - <para> 480 - The script will be usually run from the root of the Nixpkgs repository but you should not rely on that. Also note that the update scripts will be run in parallel by default; you should avoid running <command>git commit</command> or any other commands that cannot handle that. 478 + The script will be run with <variable>UPDATE_NIX_ATTR_PATH</variable> environment variable set to the attribute path it is supposed to update. 479 + <note> 480 + <para> 481 + The script will be usually run from the root of the Nixpkgs repository but you should not rely on that. Also note that the update scripts will be run in parallel by default; you should avoid running <command>git commit</command> or any other commands that cannot handle that. 482 + </para> 483 + </note> 481 484 </para> 482 485 <para> 483 486 For information about how to run the updates, execute <command>nix-shell maintainers/scripts/update.nix</command>.
+79 -44
maintainers/scripts/update.nix
··· 4 4 , max-workers ? null 5 5 , include-overlays ? false 6 6 , keep-going ? null 7 + , commit ? null 7 8 }: 8 9 9 10 # TODO: add assert statements ··· 31 32 in 32 33 [x] ++ nubOn f xs; 33 34 34 - packagesWithPath = relativePath: cond: return: pathContent: 35 - let 36 - result = builtins.tryEval pathContent; 35 + /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate. 37 36 38 - dedupResults = lst: nubOn (pkg: pkg.updateScript) (lib.concatLists lst); 39 - in 40 - if result.success then 37 + Type: packagesWithPath :: AttrPath → (AttrPath → derivation → bool) → (AttrSet | List) → List<AttrSet{attrPath :: str; package :: derivation; }> 38 + AttrPath :: [str] 39 + 40 + The packages will be returned as a list of named pairs comprising of: 41 + - attrPath: stringified attribute path (based on `rootPath`) 42 + - package: corresponding derivation 43 + */ 44 + packagesWithPath = rootPath: cond: pkgs: 45 + let 46 + packagesWithPathInner = path: pathContent: 41 47 let 42 - pathContent = result.value; 48 + result = builtins.tryEval pathContent; 49 + 50 + dedupResults = lst: nubOn ({ package, attrPath }: package.updateScript) (lib.concatLists lst); 43 51 in 44 - if lib.isDerivation pathContent then 45 - lib.optional (cond relativePath pathContent) (return relativePath pathContent) 46 - else if lib.isAttrs pathContent then 47 - # If user explicitly points to an attrSet or it is marked for recursion, we recur. 48 - if relativePath == [] || pathContent.recurseForDerivations or false || pathContent.recurseForRelease or false then 49 - dedupResults (lib.mapAttrsToList (name: elem: packagesWithPath (relativePath ++ [name]) cond return elem) pathContent) 50 - else [] 51 - else if lib.isList pathContent then 52 - dedupResults (lib.imap0 (i: elem: packagesWithPath (relativePath ++ [i]) cond return elem) pathContent) 53 - else [] 54 - else []; 52 + if result.success then 53 + let 54 + evaluatedPathContent = result.value; 55 + in 56 + if lib.isDerivation evaluatedPathContent then 57 + lib.optional (cond path evaluatedPathContent) { attrPath = lib.concatStringsSep "." path; package = evaluatedPathContent; } 58 + else if lib.isAttrs evaluatedPathContent then 59 + # If user explicitly points to an attrSet or it is marked for recursion, we recur. 60 + if path == rootPath || evaluatedPathContent.recurseForDerivations or false || evaluatedPathContent.recurseForRelease or false then 61 + dedupResults (lib.mapAttrsToList (name: elem: packagesWithPathInner (path ++ [name]) elem) evaluatedPathContent) 62 + else [] 63 + else if lib.isList evaluatedPathContent then 64 + dedupResults (lib.imap0 (i: elem: packagesWithPathInner (path ++ [i]) elem) evaluatedPathContent) 65 + else [] 66 + else []; 67 + in 68 + packagesWithPathInner rootPath pkgs; 55 69 70 + /* Recursively find all packages (derivations) in `pkgs` matching `cond` predicate. 71 + */ 56 72 packagesWith = packagesWithPath []; 57 73 74 + /* Recursively find all packages in `pkgs` with updateScript by given maintainer. 75 + */ 58 76 packagesWithUpdateScriptAndMaintainer = maintainer': 59 77 let 60 78 maintainer = ··· 63 81 else 64 82 builtins.getAttr maintainer' lib.maintainers; 65 83 in 66 - packagesWith (relativePath: pkg: builtins.hasAttr "updateScript" pkg && 67 - (if builtins.hasAttr "maintainers" pkg.meta 68 - then (if builtins.isList pkg.meta.maintainers 69 - then builtins.elem maintainer pkg.meta.maintainers 70 - else maintainer == pkg.meta.maintainers 71 - ) 72 - else false 73 - ) 74 - ) 75 - (relativePath: pkg: pkg) 76 - pkgs; 84 + packagesWith (path: pkg: builtins.hasAttr "updateScript" pkg && 85 + (if builtins.hasAttr "maintainers" pkg.meta 86 + then (if builtins.isList pkg.meta.maintainers 87 + then builtins.elem maintainer pkg.meta.maintainers 88 + else maintainer == pkg.meta.maintainers 89 + ) 90 + else false 91 + ) 92 + ); 77 93 78 - packagesWithUpdateScript = path: 94 + /* Recursively find all packages under `path` in `pkgs` with updateScript. 95 + */ 96 + packagesWithUpdateScript = path: pkgs: 79 97 let 80 - pathContent = lib.attrByPath (lib.splitString "." path) null pkgs; 98 + prefix = lib.splitString "." path; 99 + pathContent = lib.attrByPath prefix null pkgs; 81 100 in 82 101 if pathContent == null then 83 102 builtins.throw "Attribute path `${path}` does not exists." 84 103 else 85 - packagesWith (relativePath: pkg: builtins.hasAttr "updateScript" pkg) 86 - (relativePath: pkg: pkg) 104 + packagesWithPath prefix (path: pkg: builtins.hasAttr "updateScript" pkg) 87 105 pathContent; 88 106 89 - packageByName = name: 107 + /* Find a package under `path` in `pkgs` and require that it has an updateScript. 108 + */ 109 + packageByName = path: pkgs: 90 110 let 91 - package = lib.attrByPath (lib.splitString "." name) null pkgs; 111 + package = lib.attrByPath (lib.splitString "." path) null pkgs; 92 112 in 93 113 if package == null then 94 - builtins.throw "Package with an attribute name `${name}` does not exists." 114 + builtins.throw "Package with an attribute name `${path}` does not exists." 95 115 else if ! builtins.hasAttr "updateScript" package then 96 - builtins.throw "Package with an attribute name `${name}` does not have a `passthru.updateScript` attribute defined." 116 + builtins.throw "Package with an attribute name `${path}` does not have a `passthru.updateScript` attribute defined." 97 117 else 98 - package; 118 + { attrPath = path; inherit package; }; 99 119 120 + /* List of packages matched based on the CLI arguments. 121 + */ 100 122 packages = 101 123 if package != null then 102 - [ (packageByName package) ] 124 + [ (packageByName package pkgs) ] 103 125 else if maintainer != null then 104 - packagesWithUpdateScriptAndMaintainer maintainer 126 + packagesWithUpdateScriptAndMaintainer maintainer pkgs 105 127 else if path != null then 106 - packagesWithUpdateScript path 128 + packagesWithUpdateScript path pkgs 107 129 else 108 130 builtins.throw "No arguments provided.\n\n${helpText}"; 109 131 ··· 132 154 --argstr keep-going true 133 155 134 156 to continue running when a single update fails. 157 + 158 + You can also make the updater automatically commit on your behalf from updateScripts 159 + that support it by adding 160 + 161 + --argstr commit true 135 162 ''; 136 163 137 - packageData = package: { 164 + /* Transform a matched package into an object for update.py. 165 + */ 166 + packageData = { package, attrPath }: { 138 167 name = package.name; 139 168 pname = lib.getName package; 140 - updateScript = map builtins.toString (lib.toList package.updateScript); 169 + oldVersion = lib.getVersion package; 170 + updateScript = map builtins.toString (lib.toList (package.updateScript.command or package.updateScript)); 171 + supportedFeatures = package.updateScript.supportedFeatures or []; 172 + attrPath = package.updateScript.attrPath or attrPath; 141 173 }; 142 174 175 + /* JSON file with data for update.py. 176 + */ 143 177 packagesJson = pkgs.writeText "packages.json" (builtins.toJSON (map packageData packages)); 144 178 145 179 optionalArgs = 146 180 lib.optional (max-workers != null) "--max-workers=${max-workers}" 147 - ++ lib.optional (keep-going == "true") "--keep-going"; 181 + ++ lib.optional (keep-going == "true") "--keep-going" 182 + ++ lib.optional (commit == "true") "--commit"; 148 183 149 184 args = [ packagesJson ] ++ optionalArgs; 150 185
+178 -35
maintainers/scripts/update.py
··· 1 + from __future__ import annotations 2 + from typing import Dict, Generator, List, Optional, Tuple 1 3 import argparse 2 - import concurrent.futures 4 + import asyncio 5 + import contextlib 3 6 import json 4 7 import os 8 + import re 5 9 import subprocess 6 10 import sys 11 + import tempfile 7 12 8 - updates = {} 13 + class CalledProcessError(Exception): 14 + process: asyncio.subprocess.Process 9 15 10 16 def eprint(*args, **kwargs): 11 17 print(*args, file=sys.stderr, **kwargs) 12 18 13 - def run_update_script(package): 19 + async def check_subprocess(*args, **kwargs): 20 + """ 21 + Emulate check argument of subprocess.run function. 22 + """ 23 + process = await asyncio.create_subprocess_exec(*args, **kwargs) 24 + returncode = await process.wait() 25 + 26 + if returncode != 0: 27 + error = CalledProcessError() 28 + error.process = process 29 + 30 + raise error 31 + 32 + return process 33 + 34 + async def run_update_script(nixpkgs_root: str, merge_lock: asyncio.Lock, temp_dir: Optional[Tuple[str, str]], package: Dict, keep_going: bool): 35 + worktree: Optional[str] = None 36 + 37 + update_script_command = package['updateScript'] 38 + 39 + if temp_dir is not None: 40 + worktree, _branch = temp_dir 41 + 42 + # Update scripts can use $(dirname $0) to get their location but we want to run 43 + # their clones in the git worktree, not in the main nixpkgs repo. 44 + update_script_command = map(lambda arg: re.sub(r'^{0}'.format(re.escape(nixpkgs_root)), worktree, arg), update_script_command) 45 + 14 46 eprint(f" - {package['name']}: UPDATING ...") 15 47 16 - subprocess.run(package['updateScript'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True) 48 + try: 49 + update_process = await check_subprocess('env', f"UPDATE_NIX_ATTR_PATH={package['attrPath']}", *update_script_command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=worktree) 50 + update_info = await update_process.stdout.read() 51 + 52 + await merge_changes(merge_lock, package, update_info, temp_dir) 53 + except KeyboardInterrupt as e: 54 + eprint('Cancelling…') 55 + raise asyncio.exceptions.CancelledError() 56 + except CalledProcessError as e: 57 + eprint(f" - {package['name']}: ERROR") 58 + eprint() 59 + eprint(f"--- SHOWING ERROR LOG FOR {package['name']} ----------------------") 60 + eprint() 61 + stderr = await e.process.stderr.read() 62 + eprint(stderr.decode('utf-8')) 63 + with open(f"{package['pname']}.log", 'wb') as logfile: 64 + logfile.write(stderr) 65 + eprint() 66 + eprint(f"--- SHOWING ERROR LOG FOR {package['name']} ----------------------") 67 + 68 + if not keep_going: 69 + raise asyncio.exceptions.CancelledError() 70 + 71 + @contextlib.contextmanager 72 + def make_worktree() -> Generator[Tuple[str, str], None, None]: 73 + with tempfile.TemporaryDirectory() as wt: 74 + branch_name = f'update-{os.path.basename(wt)}' 75 + target_directory = f'{wt}/nixpkgs' 76 + 77 + subprocess.run(['git', 'worktree', 'add', '-b', branch_name, target_directory]) 78 + yield (target_directory, branch_name) 79 + subprocess.run(['git', 'worktree', 'remove', '--force', target_directory]) 80 + subprocess.run(['git', 'branch', '-D', branch_name]) 81 + 82 + async def commit_changes(name: str, merge_lock: asyncio.Lock, worktree: str, branch: str, changes: List[Dict]) -> None: 83 + for change in changes: 84 + # Git can only handle a single index operation at a time 85 + async with merge_lock: 86 + await check_subprocess('git', 'add', *change['files'], cwd=worktree) 87 + commit_message = '{attrPath}: {oldVersion} → {newVersion}'.format(**change) 88 + await check_subprocess('git', 'commit', '--quiet', '-m', commit_message, cwd=worktree) 89 + await check_subprocess('git', 'cherry-pick', branch) 90 + 91 + async def check_changes(package: Dict, worktree: str, update_info: str): 92 + if 'commit' in package['supportedFeatures']: 93 + changes = json.loads(update_info) 94 + else: 95 + changes = [{}] 96 + 97 + # Try to fill in missing attributes when there is just a single change. 98 + if len(changes) == 1: 99 + # Dynamic data from updater take precedence over static data from passthru.updateScript. 100 + if 'attrPath' not in changes[0]: 101 + # update.nix is always passing attrPath 102 + changes[0]['attrPath'] = package['attrPath'] 103 + 104 + if 'oldVersion' not in changes[0]: 105 + # update.nix is always passing oldVersion 106 + changes[0]['oldVersion'] = package['oldVersion'] 107 + 108 + if 'newVersion' not in changes[0]: 109 + attr_path = changes[0]['attrPath'] 110 + obtain_new_version_process = await check_subprocess('nix-instantiate', '--expr', f'with import ./. {{}}; lib.getVersion {attr_path}', '--eval', '--strict', '--json', stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=worktree) 111 + changes[0]['newVersion'] = json.loads((await obtain_new_version_process.stdout.read()).decode('utf-8')) 112 + 113 + if 'files' not in changes[0]: 114 + changed_files_process = await check_subprocess('git', 'diff', '--name-only', stdout=asyncio.subprocess.PIPE, cwd=worktree) 115 + changed_files = (await changed_files_process.stdout.read()).splitlines() 116 + changes[0]['files'] = changed_files 117 + 118 + if len(changed_files) == 0: 119 + return [] 120 + 121 + return changes 122 + 123 + async def merge_changes(merge_lock: asyncio.Lock, package: Dict, update_info: str, temp_dir: Optional[Tuple[str, str]]) -> None: 124 + if temp_dir is not None: 125 + worktree, branch = temp_dir 126 + changes = await check_changes(package, worktree, update_info) 127 + 128 + if len(changes) > 0: 129 + await commit_changes(package['name'], merge_lock, worktree, branch, changes) 130 + else: 131 + eprint(f" - {package['name']}: DONE, no changes.") 132 + else: 133 + eprint(f" - {package['name']}: DONE.") 134 + 135 + async def updater(nixpkgs_root: str, temp_dir: Optional[Tuple[str, str]], merge_lock: asyncio.Lock, packages_to_update: asyncio.Queue[Optional[Dict]], keep_going: bool, commit: bool): 136 + while True: 137 + package = await packages_to_update.get() 138 + if package is None: 139 + # A sentinel received, we are done. 140 + return 141 + 142 + if not ('commit' in package['supportedFeatures'] or 'attrPath' in package): 143 + temp_dir = None 17 144 145 + await run_update_script(nixpkgs_root, merge_lock, temp_dir, package, keep_going) 18 146 19 - def main(max_workers, keep_going, packages): 20 - with open(sys.argv[1]) as f: 147 + async def start_updates(max_workers: int, keep_going: bool, commit: bool, packages: List[Dict]): 148 + merge_lock = asyncio.Lock() 149 + packages_to_update: asyncio.Queue[Optional[Dict]] = asyncio.Queue() 150 + 151 + with contextlib.ExitStack() as stack: 152 + temp_dirs: List[Optional[Tuple[str, str]]] = [] 153 + 154 + # Do not create more workers than there are packages. 155 + num_workers = min(max_workers, len(packages)) 156 + 157 + nixpkgs_root_process = await check_subprocess('git', 'rev-parse', '--show-toplevel', stdout=asyncio.subprocess.PIPE) 158 + nixpkgs_root = (await nixpkgs_root_process.stdout.read()).decode('utf-8').strip() 159 + 160 + # Set up temporary directories when using auto-commit. 161 + for i in range(num_workers): 162 + temp_dir = stack.enter_context(make_worktree()) if commit else None 163 + temp_dirs.append(temp_dir) 164 + 165 + # Fill up an update queue, 166 + for package in packages: 167 + await packages_to_update.put(package) 168 + 169 + # Add sentinels, one for each worker. 170 + # A workers will terminate when it gets sentinel from the queue. 171 + for i in range(num_workers): 172 + await packages_to_update.put(None) 173 + 174 + # Prepare updater workers for each temp_dir directory. 175 + # At most `num_workers` instances of `run_update_script` will be running at one time. 176 + updaters = asyncio.gather(*[updater(nixpkgs_root, temp_dir, merge_lock, packages_to_update, keep_going, commit) for temp_dir in temp_dirs]) 177 + 178 + try: 179 + # Start updater workers. 180 + await updaters 181 + except asyncio.exceptions.CancelledError as e: 182 + # When one worker is cancelled, cancel the others too. 183 + updaters.cancel() 184 + 185 + def main(max_workers: int, keep_going: bool, commit: bool, packages_path: str) -> None: 186 + with open(packages_path) as f: 21 187 packages = json.load(f) 22 188 23 189 eprint() ··· 31 197 eprint() 32 198 eprint('Running update for:') 33 199 34 - with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) as executor: 35 - for package in packages: 36 - updates[executor.submit(run_update_script, package)] = package 37 - 38 - for future in concurrent.futures.as_completed(updates): 39 - package = updates[future] 40 - 41 - try: 42 - future.result() 43 - eprint(f" - {package['name']}: DONE.") 44 - except subprocess.CalledProcessError as e: 45 - eprint(f" - {package['name']}: ERROR") 46 - eprint() 47 - eprint(f"--- SHOWING ERROR LOG FOR {package['name']} ----------------------") 48 - eprint() 49 - eprint(e.stdout.decode('utf-8')) 50 - with open(f"{package['pname']}.log", 'wb') as f: 51 - f.write(e.stdout) 52 - eprint() 53 - eprint(f"--- SHOWING ERROR LOG FOR {package['name']} ----------------------") 54 - 55 - if not keep_going: 56 - sys.exit(1) 200 + asyncio.run(start_updates(max_workers, keep_going, commit, packages)) 57 201 58 202 eprint() 59 203 eprint('Packages updated!') ··· 65 209 parser = argparse.ArgumentParser(description='Update packages') 66 210 parser.add_argument('--max-workers', '-j', dest='max_workers', type=int, help='Number of updates to run concurrently', nargs='?', default=4) 67 211 parser.add_argument('--keep-going', '-k', dest='keep_going', action='store_true', help='Do not stop after first failure') 212 + parser.add_argument('--commit', '-c', dest='commit', action='store_true', help='Commit the changes') 68 213 parser.add_argument('packages', help='JSON file containing the list of package names and their update scripts') 69 214 70 215 if __name__ == '__main__': 71 216 args = parser.parse_args() 72 217 73 218 try: 74 - main(args.max_workers, args.keep_going, args.packages) 75 - except (KeyboardInterrupt, SystemExit) as e: 76 - for update in updates: 77 - update.cancel() 78 - 79 - sys.exit(e.code if isinstance(e, SystemExit) else 130) 219 + main(args.max_workers, args.keep_going, args.commit, args.packages) 220 + except KeyboardInterrupt as e: 221 + # Let’s cancel outside of the main loop too. 222 + sys.exit(130)
+11 -1
pkgs/common-updater/scripts/update-source-version
··· 11 11 usage() { 12 12 echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]" 13 13 echo " [--version-key=<version-key>] [--system=<system>] [--file=<file-to-update>]" 14 - echo " [--ignore-same-hash]" 14 + echo " [--ignore-same-hash] [--print-changes]" 15 15 } 16 16 17 17 args=() ··· 32 32 ;; 33 33 --ignore-same-hash) 34 34 ignoreSameHash="true" 35 + ;; 36 + --print-changes) 37 + printChanges="true" 35 38 ;; 36 39 --help) 37 40 usage ··· 102 105 103 106 if [[ "$oldVersion" = "$newVersion" ]]; then 104 107 echo "$scriptName: New version same as old version, nothing to do." >&2 108 + if [ -n "$printChanges" ]; then 109 + printf '[]\n' 110 + fi 105 111 exit 0 106 112 fi 107 113 ··· 197 203 198 204 rm -f "$nixFile.bak" 199 205 rm -f "$attr.fetchlog" 206 + 207 + if [ -n "$printChanges" ]; then 208 + printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile" 209 + fi