1
+ import logging
1
2
import os
2
3
import pathlib
3
4
import re
24
25
)
25
26
from ansiblelint .loaders import yaml_from_file
26
27
28
+ _logger = logging .getLogger (__name__ )
29
+
27
30
28
31
def check_ansible_presence (exit_on_error : bool = False ) -> Tuple [str , str ]:
29
32
"""Assures we stop execution if Ansible is missing or outdated.
@@ -85,7 +88,7 @@ def _get_ver_err() -> Tuple[str, str]:
85
88
86
89
ver , err = _get_ver_err ()
87
90
if exit_on_error and err :
88
- print (err , file = sys . stderr )
91
+ _logger . error (err )
89
92
sys .exit (ANSIBLE_MISSING_RC )
90
93
return ver , err
91
94
@@ -104,7 +107,7 @@ def prepare_environment() -> None:
104
107
"requirements.yml" ,
105
108
]
106
109
107
- print ("Running %s" % " " .join (cmd ), file = sys . stderr )
110
+ _logger . info ("Running %s" , " " .join (cmd ))
108
111
run = subprocess .run (
109
112
cmd ,
110
113
universal_newlines = True ,
@@ -113,7 +116,7 @@ def prepare_environment() -> None:
113
116
stderr = subprocess .STDOUT ,
114
117
)
115
118
if run .returncode != 0 :
116
- print (run .stdout , file = sys . stderr )
119
+ _logger . error (run .stdout )
117
120
sys .exit (run .returncode )
118
121
119
122
# Run galaxy collection install works on v2 requirements.yml
@@ -129,7 +132,7 @@ def prepare_environment() -> None:
129
132
"requirements.yml" ,
130
133
]
131
134
132
- print ("Running %s" % " " .join (cmd ), file = sys . stderr )
135
+ _logger . info ("Running %s" , " " .join (cmd ))
133
136
run = subprocess .run (
134
137
cmd ,
135
138
universal_newlines = True ,
@@ -138,7 +141,7 @@ def prepare_environment() -> None:
138
141
stderr = subprocess .STDOUT ,
139
142
)
140
143
if run .returncode != 0 :
141
- print (run .stdout , file = sys .stderr )
144
+ _logger . error (run .stdout , file = sys .stderr )
142
145
sys .exit (run .returncode )
143
146
144
147
_install_galaxy_role ()
@@ -162,9 +165,9 @@ def _install_galaxy_role() -> None:
162
165
role_name = re .sub (r'^{0}' .format (re .escape ('ansible-role-' )), '' , role_name )
163
166
fqrn = f"{ role_namespace } .{ role_name } "
164
167
if not re .match (r"[a-z0-9][a-z0-9_]+\.[a-z][a-z0-9_]+$" , fqrn ):
165
- print (
166
- f """\
167
- Computed fully qualified role name of { fqrn } is not valid.
168
+ _logger . error (
169
+ """\
170
+ Computed fully qualified role name of %s is not valid.
168
171
Please edit meta/main.yml and assure we can correctly determine full role name:
169
172
170
173
galaxy_info:
@@ -174,7 +177,7 @@ def _install_galaxy_role() -> None:
174
177
Namespace: https://galaxy.ansible.com/docs/contributing/namespaces.html#galaxy-namespace-limitations
175
178
Role: https://galaxy.ansible.com/docs/contributing/creating_role.html#role-names
176
179
""" ,
177
- file = sys . stderr ,
180
+ fqrn ,
178
181
)
179
182
sys .exit (INVALID_PREREQUISITES_RC )
180
183
p = pathlib .Path (f"{ options .project_dir } /.cache/roles" )
@@ -184,9 +187,9 @@ def _install_galaxy_role() -> None:
184
187
# it appears that is_dir() reports true instead, so we rely on exits().
185
188
if not link_path .exists ():
186
189
link_path .symlink_to (pathlib .Path ("../.." , target_is_directory = True ))
187
- print (
188
- f "Using { link_path } symlink to current repository in order to enable Ansible to find the role using its expected full name." ,
189
- file = sys . stderr ,
190
+ _logger . info (
191
+ "Using %s symlink to current repository in order to enable Ansible to find the role using its expected full name." ,
192
+ link_path ,
190
193
)
191
194
192
195
@@ -223,10 +226,7 @@ def _make_module_stub(module_name: str) -> None:
223
226
collection = collection ,
224
227
)
225
228
elif "." in module_name :
226
- print (
227
- "Config error: %s is not a valid module name." % module_name ,
228
- file = sys .stderr ,
229
- )
229
+ _logger .error ("Config error: %s is not a valid module name." , module_name )
230
230
sys .exit (INVALID_CONFIG_RC )
231
231
else :
232
232
os .makedirs (f"{ options .project_dir } /.cache/modules" , exist_ok = True )
@@ -257,7 +257,7 @@ def _update_env(varname: str, value: List[str], default: str = "") -> None:
257
257
value_str = ":" .join (value )
258
258
if value_str != os .environ .get (varname , "" ):
259
259
os .environ [varname ] = value_str
260
- print ("Added %s=%s" % ( varname , value_str ), file = sys . stderr )
260
+ _logger . info ("Added %s=%s" , varname , value_str )
261
261
262
262
263
263
def _perform_mockings () -> None :
0 commit comments