22
22
from __future__ import annotations
23
23
24
24
import re
25
+ from functools import cache
25
26
from pathlib import Path
26
27
from typing import TYPE_CHECKING , Any
27
28
34
35
from ansiblelint .errors import MatchError
35
36
36
37
37
- ROLE_NAME_REGEX = r"^[a-z][a-z0-9_]*$"
38
+ ROLE_NAME_REGEX = re . compile ( r"^[a-z][a-z0-9_]*$" )
38
39
39
40
40
41
def _remove_prefix (text : str , prefix : str ) -> str :
41
42
return re .sub (rf"^{ re .escape (prefix )} " , "" , text )
42
43
43
44
45
+ @cache
46
+ def _match_role_name_regex (role_name : str ) -> bool :
47
+ return ROLE_NAME_REGEX .match (role_name ) is not None
48
+
49
+
44
50
class RoleNames (AnsibleLintRule ):
45
51
# Unable to use f-strings due to flake8 bug with AST parsing
46
52
"""Role name {0} does not match ``^[a-z][a-z0-9_]*$`` pattern."""
@@ -52,14 +58,9 @@ class RoleNames(AnsibleLintRule):
52
58
)
53
59
link = "https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_structure.html#roles-directory"
54
60
severity = "HIGH"
55
- done : list [str ] = [] # already noticed roles list
56
61
tags = ["deprecations" , "metadata" ]
57
62
version_added = "v6.8.5"
58
63
59
- def __init__ (self ) -> None :
60
- """Save precompiled regex."""
61
- self ._re = re .compile (ROLE_NAME_REGEX )
62
-
63
64
def matchtask (
64
65
self , task : dict [str , Any ], file : Lintable | None = None
65
66
) -> list [MatchError ]:
@@ -95,15 +96,13 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
95
96
)
96
97
97
98
role_name = _remove_prefix (role_name , "ansible-role-" )
98
- if role_name not in self .done :
99
- self .done .append (role_name )
100
- if role_name and not self ._re .match (role_name ):
101
- result .append (
102
- self .create_matcherror (
103
- filename = file ,
104
- message = self .shortdesc .format (role_name ),
105
- )
99
+ if role_name and not _match_role_name_regex (role_name ):
100
+ result .append (
101
+ self .create_matcherror (
102
+ filename = file ,
103
+ message = self .shortdesc .format (role_name ),
106
104
)
105
+ )
107
106
return result
108
107
109
108
@staticmethod
0 commit comments