@@ -1,5 +1,5 @@
11#!/usr/bin/env nix-shell
2−#!nix-shell -p nix-prefetch-git -p python3 nix -i python3
2+#!nix-shell -p nix-prefetch-git -p python3 -p python3Packages.GitPython nix -i python3
33
44# format:
55# $ nix run nixpkgs.python3Packages.black -c black update.py
@@ -27,6 +27,8 @@ from typing import Dict, List, Optional, Tuple, Union, Any, Callable
2727from urllib.parse import urljoin, urlparse
2828from tempfile import NamedTemporaryFile
2929
30+import git
31+
3032ATOM_ENTRY = "{http://www.w3.org/2005/Atom}entry" # " vim gets confused here
3133ATOM_LINK = "{http://www.w3.org/2005/Atom}link" # "
3234ATOM_UPDATED = "{http://www.w3.org/2005/Atom}updated" # "
@@ -74,7 +76,7 @@ def retry(ExceptionToCheck: Any, tries: int = 4, delay: float = 3, backoff: floa
7476
7577
7678class Repo:
77− def __init__(self, owner: str, name: str, alias: str) -> None:
79+ def __init__(self, owner: str, name: str, alias: Optional[str]) -> None:
7880 self.owner = owner
7981 self.name = name
8082 self.alias = alias
@@ -218,12 +220,12 @@ def get_current_plugins() -> List[Plugin]:
218220
219221
220222def prefetch_plugin(
221− user: str, repo_name: str, alias: str, cache: "Cache"
223+ user: str, repo_name: str, alias: Optional[str], cache: "Optional[Cache]" = None
222224) -> Tuple[Plugin, Dict[str, str]]:
223225 repo = Repo(user, repo_name, alias)
224226 commit, date = repo.latest_commit()
225227 has_submodules = repo.has_submodules()
226− cached_plugin = cache[commit]
228+ cached_plugin = cache[commit] if cache else None
227229 if cached_plugin is not None:
228230 cached_plugin.name = alias or repo_name
229231 cached_plugin.date = date
@@ -241,6 +243,11 @@ def prefetch_plugin(
241243 )
242244
243245
246+def fetch_plugin_from_pluginline(plugin_line: str) -> Plugin:
247+ plugin, _ = prefetch_plugin(*parse_plugin_line(plugin_line))
248+ return plugin
249+
250+
244251def print_download_error(plugin: str, ex: Exception):
245252 print(f"{plugin}: {ex}", file=sys.stderr)
246253 ex_traceback = ex.__traceback__
@@ -413,10 +420,14 @@ in lib.fix' (lib.extends overrides packages)
413420 print(f"updated {outfile}")
414421
415422
416−def rewrite_input(input_file: Path, output_file: Path, redirects: dict):
423+def rewrite_input(
424+ input_file: Path, redirects: Dict[str, str] = None, append: Tuple = ()
425+):
417426 with open(input_file, "r") as f:
418427 lines = f.readlines()
419428
429+ lines.extend(append)
430+
420431 if redirects:
421432 lines = [redirects.get(line, line) for line in lines]
422433
@@ -424,32 +435,16 @@ def rewrite_input(input_file: Path, output_file: Path, redirects: dict):
424435 with open(DEPRECATED, "r") as f:
425436 deprecations = json.load(f)
426437 for old, new in redirects.items():
427− old_name = old.split("/")[1].split(" ")[0].strip("\n")
428− new_name = new.split("/")[1].split(" ")[0].strip("\n")
429− if old_name != new_name:
430− deprecations[old_name] = {
431− "new": new_name,
438+ old_plugin = fetch_plugin_from_pluginline(old)
439+ new_plugin = fetch_plugin_from_pluginline(new)
440+ if old_plugin.normalized_name != new_plugin.normalized_name:
441+ deprecations[old_plugin.normalized_name] = {
442+ "new": new_plugin.normalized_name,
432443 "date": cur_date_iso,
433444 }
434445 with open(DEPRECATED, "w") as f:
435446 json.dump(deprecations, f, indent=4, sort_keys=True)
436447
437− print(
438− f"""\
439−Redirects have been detected and {input_file} has been updated. Please take the
440−following steps:
441− 1. Go ahead and commit just the updated expressions as you intended to do:
442− git add {output_file}
443− git commit -m "vimPlugins: Update"
444− 2. Run this script again so these changes will be reflected in the
445− generated expressions:
446− ./update.py
447− 3. Commit {input_file} along with deprecations and generated expressions:
448− git add {output_file} {input_file} {DEPRECATED}
449− git commit -m "vimPlugins: Update redirects"
450− """
451− )
452−
453448 lines = sorted(lines, key=str.casefold)
454449
455450 with open(input_file, "w") as f:
@@ -463,6 +458,13 @@ def parse_args():
463458 f"By default from {DEFAULT_IN} to {DEFAULT_OUT}"
464459 )
465460 )
461+ parser.add_argument(
462+ "--add",
463+ dest="add_plugins",
464+ default=[],
465+ action="append",
466+ help="Plugin to add to vimPlugins from Github in the form owner/repo",
467+ )
466468 parser.add_argument(
467469 "--input-names",
468470 "-i",
@@ -485,30 +487,69 @@ def parse_args():
485487 default=30,
486488 help="Number of concurrent processes to spawn.",
487489 )
488−
489490 return parser.parse_args()
490491
491492
492−def main() -> None:
493+def commit(repo: git.Repo, message: str, files: List[Path]) -> None:
494+ files_staged = repo.index.add([str(f.resolve()) for f in files])
493495
494− args = parse_args()
495− plugin_names = load_plugin_spec(args.input_file)
496− current_plugins = get_current_plugins()
497− cache = Cache(current_plugins)
496+ if files_staged:
497+ print(f'committing to nixpkgs "{message}"')
498+ repo.index.commit(message)
499+ else:
500+ print("no changes in working tree to commit")
498501
499− prefetch_with_cache = functools.partial(prefetch, cache=cache)
500502
501− try:
502− pool = Pool(processes=args.proc)
503− results = pool.map(prefetch_with_cache, plugin_names)
504− finally:
505− cache.store()
503+def get_update(input_file: str, outfile: str, proc: int):
504+ cache: Cache = Cache(get_current_plugins())
505+ _prefetch = functools.partial(prefetch, cache=cache)
506506
507− plugins, redirects = check_results(results)
507+ def update() -> dict:
508+ plugin_names = load_plugin_spec(input_file)
508509
509− generate_nix(plugins, args.outfile)
510+ try:
511+ pool = Pool(processes=proc)
512+ results = pool.map(_prefetch, plugin_names)
513+ finally:
514+ cache.store()
510515
511− rewrite_input(args.input_file, args.outfile, redirects)
516+ plugins, redirects = check_results(results)
517+
518+ generate_nix(plugins, outfile)
519+
520+ return redirects
521+
522+ return update
523+
524+
525+def main():
526+ args = parse_args()
527+ nixpkgs_repo = git.Repo(ROOT, search_parent_directories=True)
528+ update = get_update(args.input_file, args.outfile, args.proc)
529+
530+ redirects = update()
531+ rewrite_input(args.input_file, redirects)
532+ commit(nixpkgs_repo, "vimPlugins: update", [args.outfile])
533+
534+ if redirects:
535+ update()
536+ commit(
537+ nixpkgs_repo,
538+ "vimPlugins: resolve github repository redirects",
539+ [args.outfile, args.input_file, DEPRECATED],
540+ )
541+
542+ for plugin_line in args.add_plugins:
543+ rewrite_input(args.input_file, append=(plugin_line + "\n",))
544+ update()
545+ plugin = fetch_plugin_from_pluginline(plugin_line)
546+ commit(
547+ nixpkgs_repo,
548+ "vimPlugins.{name}: init at {version}".format(
549+ name=plugin.normalized_name, version=plugin.version
550+ ),
551+ [args.outfile, args.input_file],
552+ )
512553
513554
514555if __name__ == "__main__":