33
44import contextlib
55import logging
6- import os
76import re
87import sys
8+ from typing import TYPE_CHECKING
99
1010from ansiblelint .config import options
1111from ansiblelint .constants import ANSIBLE_MOCKED_MODULE , RC
1212
13+ if TYPE_CHECKING :
14+ from pathlib import Path
15+
1316_logger = logging .getLogger (__name__ )
1417
1518
1619def _make_module_stub (module_name : str ) -> None :
20+ if not options .cache_dir :
21+ msg = "Cache directory not set"
22+ raise RuntimeError (msg )
1723 # a.b.c is treated a collection
1824 if re .match (r"^(\w+|\w+\.\w+\.[\.\w]+)$" , module_name ):
1925 parts = module_name .split ("." )
2026 if len (parts ) < 3 :
21- path = f" { options .cache_dir } / modules"
27+ path = options .cache_dir / " modules"
2228 module_file = f"{ options .cache_dir } /modules/{ module_name } .py"
2329 namespace = None
2430 collection = None
2531 else :
2632 namespace = parts [0 ]
2733 collection = parts [1 ]
28- path = f"{ options .cache_dir } /collections/ansible_collections/{ namespace } /{ collection } /plugins/modules/{ '/' .join (parts [2 :- 1 ]) } "
34+ path = (
35+ options .cache_dir
36+ / "collections"
37+ / "ansible_collections"
38+ / namespace
39+ / collection
40+ / "plugins"
41+ / "modules"
42+ / ("/" .join (parts [2 :- 1 ]))
43+ )
2944 module_file = f"{ path } /{ parts [- 1 ]} .py"
30- os . makedirs ( path , exist_ok = True )
45+ path . mkdir ( exist_ok = True , parents = True )
3146 _write_module_stub (
3247 filename = module_file ,
3348 name = module_file ,
@@ -58,17 +73,29 @@ def _write_module_stub(
5873# pylint: disable=too-many-branches
5974def _perform_mockings () -> None :
6075 """Mock modules and roles."""
76+ path : Path
77+ if not options .cache_dir :
78+ msg = "Cache directory not set"
79+ raise RuntimeError (msg )
6180 for role_name in options .mock_roles :
6281 if re .match (r"\w+\.\w+\.\w+$" , role_name ):
6382 namespace , collection , role_dir = role_name .split ("." )
64- path = f"{ options .cache_dir } /collections/ansible_collections/{ namespace } /{ collection } /roles/{ role_dir } /"
83+ path = (
84+ options .cache_dir
85+ / "collections"
86+ / "ansible_collections"
87+ / namespace
88+ / collection
89+ / "roles"
90+ / role_dir
91+ )
6592 else :
66- path = f" { options .cache_dir } / roles/ { role_name } "
93+ path = options .cache_dir / " roles" / role_name
6794 # Avoid error from makedirs if destination is a broken symlink
68- if os . path .islink ( path ) and not os . path .exists (path ): # pragma: no cover
95+ if path .is_symlink ( ) and not path .exists (): # pragma: no cover
6996 _logger .warning ("Removed broken symlink from %s" , path )
70- os .unlink (path )
71- os . makedirs ( path , exist_ok = True )
97+ path .unlink (missing_ok = True )
98+ path . mkdir ( exist_ok = True , parents = True )
7299
73100 if options .mock_modules :
74101 for module_name in options .mock_modules :
@@ -77,11 +104,22 @@ def _perform_mockings() -> None:
77104
78105def _perform_mockings_cleanup () -> None :
79106 """Clean up mocked modules and roles."""
107+ if not options .cache_dir :
108+ msg = "Cache directory not set"
109+ raise RuntimeError (msg )
80110 for role_name in options .mock_roles :
81111 if re .match (r"\w+\.\w+\.\w+$" , role_name ):
82112 namespace , collection , role_dir = role_name .split ("." )
83- path = f"{ options .cache_dir } /collections/ansible_collections/{ namespace } /{ collection } /roles/{ role_dir } /"
113+ path = (
114+ options .cache_dir
115+ / "collections"
116+ / "ansible_collections"
117+ / namespace
118+ / collection
119+ / "roles"
120+ / role_dir
121+ )
84122 else :
85- path = f" { options .cache_dir } / roles/ { role_name } "
123+ path = options .cache_dir / " roles" / role_name
86124 with contextlib .suppress (OSError ):
87- os . rmdir ( path )
125+ path . unlink ( )
0 commit comments