git.iko.soy/typeable/nixpkgs
on master help sign in
vim update.py: allow different in and out files

...also adds an argparse cli parser.

commit3820ff9c8be01aecb5976996ae7d4eb3ef4c4b5c authorMatthieu Coudron <mattator@gmail.com> dateTue 19 Nov 2019, 01:54 (7 years ago) parent01037b3
1 file changed+42−11
pkgs/misc/vim-plugins/update.py+42−11
pkgs/misc/vim-plugins/update.py+42−11
@@ -8,6 +8,7 @@
88# linted:
99# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265 update.py
1010
11+import argparse
1112import functools
1213import json
1314import os
@@ -29,6 +30,8 @@ ATOM_LINK = "{http://www.w3.org/2005/Atom}link"
2930ATOM_UPDATED = "{http://www.w3.org/2005/Atom}updated"
3031
3132ROOT = Path(__file__).parent
33+DEFAULT_IN = ROOT.joinpath("vim-plugin-names")
34+DEFAULT_OUT = ROOT.joinpath("generated.nix")
3235
3336
3437class Repo:
@@ -209,9 +212,9 @@ def check_results(
209212
210213def parse_plugin_line(line: str) -> Tuple[str, str, str]:
211214 try:
212− name, repo = line.split('/')
215+ name, repo = line.split("/")
213216 try:
214− repo, alias = repo.split(' as ')
217+ repo, alias = repo.split(" as ")
215218 return (name, repo, alias.strip())
216219 except ValueError:
217220 # no alias defined
@@ -220,8 +223,7 @@ def parse_plugin_line(line: str) -> Tuple[str, str, str]:
220223 return (None, None, None)
221224
222225
223−def load_plugin_spec() -> List[Tuple[str, str]]:
224− plugin_file = ROOT.joinpath("vim-plugin-names")
226+def load_plugin_spec(plugin_file: str) -> List[Tuple[str, str]]:
225227 plugins = []
226228 with open(plugin_file) as f:
227229 for line in f:
@@ -305,10 +307,10 @@ header = (
305307)
306308
307309
308−def generate_nix(plugins: List[Tuple[str, str, Plugin]]):
310+def generate_nix(plugins: List[Tuple[str, str, Plugin]], outfile: str):
309311 sorted_plugins = sorted(plugins, key=lambda v: v[2].name.lower())
310312
311− with open(ROOT.joinpath("generated.nix"), "w+") as f:
313+ with open(outfile, "w+") as f:
312314 f.write(header)
313315 f.write(
314316 """
@@ -338,15 +340,44 @@ let
338340 }};
339341"""
340342 )
341− f.write("""
343+ f.write(
344+ """
342345});
343346in lib.fix' (lib.extends overrides packages)
344−""")
345− print("updated generated.nix")
347+"""
348+ )
349+ print(f"updated {outfile}")
350+
351+
352+def parse_args():
353+ parser = argparse.ArgumentParser(
354+ description=(
355+ "Updates nix derivations for vim plugins"
356+ f"By default from {DEFAULT_IN} to {DEFAULT_OUT}"
357+ )
358+ )
359+ parser.add_argument(
360+ "--input-names",
361+ "-i",
362+ dest="input_file",
363+ default=DEFAULT_IN,
364+ help="A list of plugins in the form owner/repo",
365+ )
366+ parser.add_argument(
367+ "--out",
368+ "-o",
369+ dest="outfile",
370+ default=DEFAULT_OUT,
371+ help="Filename to save generated nix code",
372+ )
373+
374+ return parser.parse_args()
346375
347376
348377def main() -> None:
349− plugin_names = load_plugin_spec()
378+
379+ args = parse_args()
380+ plugin_names = load_plugin_spec(args.input_file)
350381 current_plugins = get_current_plugins()
351382 cache = Cache(current_plugins)
352383
@@ -362,7 +393,7 @@ def main() -> None:
362393
363394 plugins = check_results(results)
364395
365− generate_nix(plugins)
396+ generate_nix(plugins, args.outfile)
366397
367398
368399if __name__ == "__main__":