Unverified Commit 24c0b632 authored by Gaétan Lepage's avatar Gaétan Lepage Committed by GitHub
Browse files

luarocks-packages-updater: errors don't crash script (#366629)

parents 2b4f91db 32a0baeb
Loading
Loading
Loading
Loading
+59 −27
Original line number Diff line number Diff line
@@ -21,8 +21,28 @@ from pathlib import Path
import pluginupdate
from pluginupdate import FetchConfig, update_plugins


class ColoredFormatter(logging.Formatter):
    # Define color codes
    COLORS = {
        "DEBUG": "\033[94m",  # Blue
        "INFO": "\033[92m",  # Green
        "WARNING": "\033[93m",  # Yellow
        "ERROR": "\033[91m",  # Red
        "CRITICAL": "\033[95m",  # Magenta
    }
    RESET = "\033[0m"

    def format(self, record):
        log_color = self.COLORS.get(record.levelname, self.RESET)
        record.msg = f"{log_color}{record.msg}{self.RESET}"
        return super().format(record)


handler = logging.StreamHandler()
handler.setFormatter(ColoredFormatter("%(levelname)s: %(message)s"))
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.handlers = [handler]

ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent  # type: ignore

@@ -142,7 +162,15 @@ class LuaEditor(pluginupdate.Editor):
            finally:
                pass

            self.generate_nix(results, output_file)
            successful_results = [(plug, nix_expr) for plug, nix_expr, error in results if nix_expr is not None]
            errors = [(plug, error) for plug, nix_expr, error in results if error is not None]

            self.generate_nix(successful_results, output_file)

            if errors:
                log.error("The following plugins failed to update:")
                for plug, error in errors:
                    log.error("%s: %s", plug.name, error)

            redirects = {}
            return redirects
@@ -171,6 +199,7 @@ def generate_pkg_nix(plug: LuaPlugin):
    """
    log.debug("Generating nix expression for %s", plug.name)

    try:
        cmd = ["luarocks", "nix"]

        if plug.maintainers:
@@ -203,7 +232,10 @@ def generate_pkg_nix(plug: LuaPlugin):

        output = subprocess.check_output(cmd, text=True)
        output = "callPackage(" + output.strip() + ") {};\n\n"
    return (plug, output)
        return (plug, output, None)
    except subprocess.CalledProcessError as e:
        log.error("Failed to generate nix expression for %s: %s", plug.name, e)
        return (plug, None, str(e))


def main():