about summary refs log tree commit diff
path: root/maintainers/scripts
diff options
context:
space:
mode:
authorMatthieu Coudron <886074+teto@users.noreply.github.com>2023-08-20 16:49:44 +0200
committerMatthieu Coudron <886074+teto@users.noreply.github.com>2023-10-01 17:30:55 +0200
commit901b21c555fa9820393b31ee54ade8cee915c895 (patch)
tree0f0a2108c89e8ee2dbffcaa74504a1bf305f2160 /maintainers/scripts
parent6013fe8f4198f1c2d6cb5e51a9ebd1400a843448 (diff)
vimPluginsUpdater: init
The nixpkgs documentation mentions how to update out of tree plugins but
one problem is that it requires a nixpkgs clone.
This makes it more convenient.
I've had the need to generate vim plugins and lua overlays for other
projects unrelated to nix and this will make updates easier (aka just
run `nix run nixpkgs#vimPluginsUpdater -- --proc=1` or with the legacy commands:
`nix-shell -p vimPluginsUpdater  --run vim-plugins-updater`.

I added an optional "nixpkgs" argument to command line parser, which is the path
towards a nixpkgs checkout. By default the current folder.

update-luarocks-packages: format with black
Diffstat (limited to 'maintainers/scripts')
-rw-r--r--maintainers/scripts/pluginupdate.py40
-rwxr-xr-xmaintainers/scripts/update-luarocks-packages98
2 files changed, 82 insertions, 56 deletions
diff --git a/maintainers/scripts/pluginupdate.py b/maintainers/scripts/pluginupdate.py
index 6a607eb624803..18b354f0be9c6 100644
--- a/maintainers/scripts/pluginupdate.py
+++ b/maintainers/scripts/pluginupdate.py
@@ -321,8 +321,14 @@ def load_plugins_from_csv(
     return plugins
 
 
-def run_nix_expr(expr):
-    with CleanEnvironment() as nix_path:
+
+def run_nix_expr(expr, nixpkgs: str):
+    '''
+    :param expr nix expression to fetch current plugins
+    :param nixpkgs Path towards a nixpkgs checkout
+    '''
+    # local_pkgs = str(Path(__file__).parent.parent.parent)
+    with CleanEnvironment(nixpkgs) as nix_path:
         cmd = [
             "nix",
             "eval",
@@ -396,9 +402,9 @@ class Editor:
         """CSV spec"""
         print("the update member function should be overriden in subclasses")
 
-    def get_current_plugins(self) -> List[Plugin]:
+    def get_current_plugins(self, nixpkgs) -> List[Plugin]:
         """To fill the cache"""
-        data = run_nix_expr(self.get_plugins)
+        data = run_nix_expr(self.get_plugins, nixpkgs)
         plugins = []
         for name, attr in data.items():
             p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
@@ -414,7 +420,7 @@ class Editor:
         raise NotImplementedError()
 
     def get_update(self, input_file: str, outfile: str, config: FetchConfig):
-        cache: Cache = Cache(self.get_current_plugins(), self.cache_file)
+        cache: Cache = Cache(self.get_current_plugins(self.nixpkgs), self.cache_file)
         _prefetch = functools.partial(prefetch, cache=cache)
 
         def update() -> dict:
@@ -454,6 +460,12 @@ class Editor:
             ),
         )
         common.add_argument(
+            "--nixpkgs",
+            type=str,
+            default=os.getcwd(),
+            help="Adjust log level",
+        )
+        common.add_argument(
             "--input-names",
             "-i",
             dest="input_file",
@@ -541,22 +553,27 @@ class Editor:
         command = args.command or "update"
         log.setLevel(LOG_LEVELS[args.debug])
         log.info("Chose to run command: %s", command)
+        self.nixpkgs = args.nixpkgs
 
-        if not args.no_commit:
-            self.nixpkgs_repo = git.Repo(self.root, search_parent_directories=True)
+        self.nixpkgs_repo = git.Repo(args.nixpkgs, search_parent_directories=True)
 
         getattr(self, command)(args)
 
 
 class CleanEnvironment(object):
+    def __init__(self, nixpkgs):
+        self.local_pkgs = nixpkgs
+
     def __enter__(self) -> str:
-        self.old_environ = os.environ.copy()
+        """
         local_pkgs = str(Path(__file__).parent.parent.parent)
+        """
+        self.old_environ = os.environ.copy()
         self.empty_config = NamedTemporaryFile()
         self.empty_config.write(b"{}")
         self.empty_config.flush()
-        os.environ["NIXPKGS_CONFIG"] = self.empty_config.name
-        return f"localpkgs={local_pkgs}"
+        # os.environ["NIXPKGS_CONFIG"] = self.empty_config.name
+        return f"localpkgs={self.local_pkgs}"
 
     def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
         os.environ.update(self.old_environ)
@@ -758,7 +775,8 @@ def commit(repo: git.Repo, message: str, files: List[Path]) -> None:
 
 
 def update_plugins(editor: Editor, args):
-    """The main entry function of this module. All input arguments are grouped in the `Editor`."""
+    """The main entry function of this module.
+    All input arguments are grouped in the `Editor`."""
 
     log.info("Start updating plugins")
     fetch_config = FetchConfig(args.proc, args.github_token)
diff --git a/maintainers/scripts/update-luarocks-packages b/maintainers/scripts/update-luarocks-packages
index 791cd8a1d89d9..32c2b44260b32 100755
--- a/maintainers/scripts/update-luarocks-packages
+++ b/maintainers/scripts/update-luarocks-packages
@@ -2,11 +2,11 @@
 #!nix-shell update-luarocks-shell.nix -i python3
 
 # format:
-# $ nix run nixpkgs.python3Packages.black -c black update.py
+# $ nix run nixpkgs#python3Packages.black -- update.py
 # type-check:
-# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
+# $ nix run nixpkgs#python3Packages.mypy -- update.py
 # linted:
-# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
+# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
 
 import inspect
 import os
@@ -25,14 +25,14 @@ from pathlib import Path
 log = logging.getLogger()
 log.addHandler(logging.StreamHandler())
 
-ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
+ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent  # type: ignore
 import pluginupdate
 from pluginupdate import update_plugins, FetchConfig, CleanEnvironment
 
-PKG_LIST="maintainers/scripts/luarocks-packages.csv"
-TMP_FILE="$(mktemp)"
-GENERATED_NIXFILE="pkgs/development/lua-modules/generated-packages.nix"
-LUAROCKS_CONFIG="maintainers/scripts/luarocks-config.lua"
+PKG_LIST = "maintainers/scripts/luarocks-packages.csv"
+TMP_FILE = "$(mktemp)"
+GENERATED_NIXFILE = "pkgs/development/lua-modules/generated-packages.nix"
+LUAROCKS_CONFIG = "maintainers/scripts/luarocks-config.lua"
 
 HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
 Regenerate it with:
@@ -40,36 +40,40 @@ nixpkgs$ ./maintainers/scripts/update-luarocks-packages
 
 You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
 */
-""".format(GENERATED_NIXFILE=GENERATED_NIXFILE)
+""".format(
+    GENERATED_NIXFILE=GENERATED_NIXFILE
+)
 
-FOOTER="""
+FOOTER = """
 }
 /* GENERATED - do not edit this file */
 """
 
+
 @dataclass
 class LuaPlugin:
     name: str
-    '''Name of the plugin, as seen on luarocks.org'''
+    """Name of the plugin, as seen on luarocks.org"""
     src: str
-    '''address to the git repository'''
+    """address to the git repository"""
     ref: Optional[str]
-    '''git reference (branch name/tag)'''
+    """git reference (branch name/tag)"""
     version: Optional[str]
-    '''Set it to pin a package '''
+    """Set it to pin a package """
     server: Optional[str]
-    '''luarocks.org registers packages under different manifests.
+    """luarocks.org registers packages under different manifests.
     Its value can be 'http://luarocks.org/dev'
-    '''
+    """
     luaversion: Optional[str]
-    '''Attribue of the lua interpreter if a package is available only for a specific lua version'''
+    """Attribue of the lua interpreter if a package is available only for a specific lua version"""
     maintainers: Optional[str]
-    ''' Optional string listing maintainers separated by spaces'''
+    """ Optional string listing maintainers separated by spaces"""
 
     @property
     def normalized_name(self) -> str:
         return self.name.replace(".", "-")
 
+
 # rename Editor to LangUpdate/ EcosystemUpdater
 class LuaEditor(pluginupdate.Editor):
     def get_current_plugins(self):
@@ -77,11 +81,13 @@ class LuaEditor(pluginupdate.Editor):
 
     def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
         luaPackages = []
-        csvfilename=input_file
+        csvfilename = input_file
         log.info("Loading package descriptions from %s", csvfilename)
 
-        with open(csvfilename, newline='') as csvfile:
-            reader = csv.DictReader(csvfile,)
+        with open(csvfilename, newline="") as csvfile:
+            reader = csv.DictReader(
+                csvfile,
+            )
             for row in reader:
                 # name,server,version,luaversion,maintainers
                 plugin = LuaPlugin(**row)
@@ -91,23 +97,19 @@ class LuaEditor(pluginupdate.Editor):
     def update(self, args):
         update_plugins(self, args)
 
-    def generate_nix(
-        self,
-        results: List[Tuple[LuaPlugin, str]],
-        outfilename: str
-        ):
-
+    def generate_nix(self, results: List[Tuple[LuaPlugin, str]], outfilename: str):
         with tempfile.NamedTemporaryFile("w+") as f:
             f.write(HEADER)
             header2 = textwrap.dedent(
-            # header2 = inspect.cleandoc(
-            """
+                # header2 = inspect.cleandoc(
+                """
                 { self, stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
                 final: prev:
                 {
-            """)
+            """
+            )
             f.write(header2)
-            for (plugin, nix_expr) in results:
+            for plugin, nix_expr in results:
                 f.write(f"{plugin.normalized_name} = {nix_expr}")
             f.write(FOOTER)
             f.flush()
@@ -156,19 +158,20 @@ class LuaEditor(pluginupdate.Editor):
         #         luaPackages.append(plugin)
         pass
 
+
 def generate_pkg_nix(plug: LuaPlugin):
-    '''
+    """
     Generate nix expression for a luarocks package
     Our cache key associates "p.name-p.version" to its rockspec
-    '''
+    """
     log.debug("Generating nix expression for %s", plug.name)
     custom_env = os.environ.copy()
-    custom_env['LUAROCKS_CONFIG'] = LUAROCKS_CONFIG
+    custom_env["LUAROCKS_CONFIG"] = LUAROCKS_CONFIG
 
     # we add --dev else luarocks wont find all the "scm" (=dev) versions of the
     # packages
-	# , "--dev"
-    cmd = [ "luarocks", "nix" ]
+    # , "--dev"
+    cmd = ["luarocks", "nix"]
 
     if plug.maintainers:
         cmd.append(f"--maintainers={plug.maintainers}")
@@ -176,7 +179,10 @@ def generate_pkg_nix(plug: LuaPlugin):
     # if plug.server == "src":
     if plug.src != "":
         if plug.src is None:
-            msg = "src must be set when 'version' is set to \"src\" for package %s" % plug.name
+            msg = (
+                "src must be set when 'version' is set to \"src\" for package %s"
+                % plug.name
+            )
             log.error(msg)
             raise RuntimeError(msg)
         log.debug("Updating from source %s", plug.src)
@@ -185,7 +191,6 @@ def generate_pkg_nix(plug: LuaPlugin):
     else:
         cmd.append(plug.name)
         if plug.version and plug.version != "src":
-
             cmd.append(plug.version)
 
     if plug.server != "src" and plug.server:
@@ -194,23 +199,26 @@ def generate_pkg_nix(plug: LuaPlugin):
     if plug.luaversion:
         cmd.append(f"--lua-version={plug.luaversion}")
 
-    log.debug("running %s", ' '.join(cmd))
+    log.debug("running %s", " ".join(cmd))
 
     output = subprocess.check_output(cmd, env=custom_env, text=True)
     output = "callPackage(" + output.strip() + ") {};\n\n"
     return (plug, output)
 
-def main():
 
-    editor = LuaEditor("lua", ROOT, '',
-        default_in = ROOT.joinpath(PKG_LIST),
-        default_out = ROOT.joinpath(GENERATED_NIXFILE)
-        )
+def main():
+    editor = LuaEditor(
+        "lua",
+        ROOT,
+        "",
+        default_in=ROOT.joinpath(PKG_LIST),
+        default_out=ROOT.joinpath(GENERATED_NIXFILE),
+    )
 
     editor.run()
 
-if __name__ == "__main__":
 
+if __name__ == "__main__":
     main()
 
 #  vim: set ft=python noet fdm=manual fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :