From 36e2674032ccb6c5f610a1b683c9e8c0b5ec80ab Mon Sep 17 00:00:00 2001 From: Nick Yang Date: Fri, 8 Mar 2024 05:10:11 -0600 Subject: [PATCH] Set global `cache_dir_lock` (#4055) Co-authored-by: Ajinkya Udgirkar --- pyproject.toml | 3 ++- src/ansiblelint/__main__.py | 10 ++++++---- src/ansiblelint/schemas/__store__.json | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 21f339b531..e9f726d088 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -248,7 +248,8 @@ ignore = [ "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` "TRY", "PERF203", - "PD011" # We are not using pandas, any .values attributes are unrelated + "PD011", # We are not using pandas, any .values attributes are unrelated + "PLW0603" # global lock file in cache dir ] select = ["ALL"] target-version = "py310" diff --git a/src/ansiblelint/__main__.py b/src/ansiblelint/__main__.py index f50e899a8c..1cb7c3ff01 100755 --- a/src/ansiblelint/__main__.py +++ b/src/ansiblelint/__main__.py @@ -79,7 +79,6 @@ _logger = logging.getLogger(__name__) -cache_dir_lock: None | FileLock = None class LintLogHandler(logging.Handler): @@ -120,8 +119,9 @@ def initialize_logger(level: int = 0) -> None: _logger.debug("Logging initialized to level %s", logging_level) -def initialize_options(arguments: list[str] | None = None) -> None: +def initialize_options(arguments: list[str] | None = None) -> None | FileLock: """Load config options and store them inside options module.""" + cache_dir_lock = None new_options = cli.get_config(arguments or []) new_options.cwd = pathlib.Path.cwd() @@ -145,7 +145,7 @@ def initialize_options(arguments: list[str] | None = None) -> None: options.cache_dir.mkdir(parents=True, exist_ok=True) if not options.offline: # pragma: no cover - cache_dir_lock = FileLock( # pylint: disable=redefined-outer-name + cache_dir_lock = FileLock( f"{options.cache_dir}/.lock", ) try: @@ -160,6 +160,8 @@ def initialize_options(arguments: list[str] | None = None) -> None: if "ANSIBLE_DEVEL_WARNING" not in os.environ: # pragma: no branch os.environ["ANSIBLE_DEVEL_WARNING"] = "false" + return cache_dir_lock + def _do_list(rules: RulesCollection) -> int: # On purpose lazy-imports to avoid pre-loading Ansible @@ -285,7 +287,7 @@ def main(argv: list[str] | None = None) -> int: if argv is None: # pragma: no cover argv = sys.argv - initialize_options(argv[1:]) + cache_dir_lock = initialize_options(argv[1:]) console_options["force_terminal"] = options.colored reconfigure(console_options) diff --git a/src/ansiblelint/schemas/__store__.json b/src/ansiblelint/schemas/__store__.json index d23cd35a46..26e586fe89 100644 --- a/src/ansiblelint/schemas/__store__.json +++ b/src/ansiblelint/schemas/__store__.json @@ -24,7 +24,7 @@ "url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json" }, "meta": { - "etag": "fb58deb0f5f2a3b77ba298764f74bc6e3bd38a761f368a50bb285042f6260354", + "etag": "e04a9e9c210c666c744dfe9ebf1ca1f4e95e71bd16995514b9c3a56186e66c98", "url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json" }, "meta-runtime": {