18
18
19
19
20
20
if TYPE_CHECKING :
21
+ from collections .abc import Sequence
21
22
from pathlib import Path
22
23
from types import TracebackType
23
24
25
+ from .collection import Collection
24
26
from .config import Config
25
27
from .output import Output
26
28
29
+
27
30
from typing import Any
28
31
29
32
@@ -156,50 +159,58 @@ def subprocess_run( # pylint: disable=too-many-positional-arguments
156
159
)
157
160
158
161
159
- def oxford_join (words : list [str ]) -> str :
162
+ def oxford_join (words : Sequence [str | Path ]) -> str :
160
163
"""Join a list of words with commas and an oxford comma.
161
164
162
165
Args:
163
166
words: A list of words to join
164
167
Returns:
165
168
A string of words joined with commas and an oxford comma
166
169
"""
167
- words . sort ( )
168
- if not words :
170
+ _words = sorted ([ str ( word ) for word in words ] )
171
+ if not _words :
169
172
return ""
170
- if len (words ) == 1 :
171
- return words [0 ]
172
- if len (words ) == 2 : # noqa: PLR2004
173
- return " and " .join (words )
174
- return ", " .join (words [:- 1 ]) + ", and " + words [- 1 ]
173
+ if len (_words ) == 1 :
174
+ return _words [0 ]
175
+ if len (_words ) == 2 : # noqa: PLR2004
176
+ return " and " .join (_words )
177
+ return ", " .join (_words [:- 1 ]) + ", and " + _words [- 1 ]
175
178
176
179
177
- def opt_deps_to_files (collection_path : Path , dep_str : str ) -> list [Path ]:
180
+ def opt_deps_to_files (collection : Collection , output : Output ) -> list [Path ]:
178
181
"""Convert a string of optional dependencies to a list of files.
179
182
180
183
Args:
181
- collection_path : The path to the collection
182
- dep_str: A string of optional dependencies
184
+ collection : The collection object
185
+ output: The output object
183
186
Returns:
184
- A list of files
187
+ A list of paths
185
188
"""
186
- deps = dep_str .split ("," )
189
+ if not collection .opt_deps :
190
+ msg = "No optional dependencies specified."
191
+ output .debug (msg )
192
+ return []
193
+
194
+ deps = collection .opt_deps .split ("," )
187
195
files = []
188
196
for dep in deps :
189
197
_dep = dep .strip ()
190
- variant1 = collection_path / f"{ _dep } -requirements.txt"
198
+ variant1 = collection . path / f"{ _dep } -requirements.txt"
191
199
if variant1 .exists ():
192
200
files .append (variant1 )
193
201
continue
194
- variant2 = collection_path / f"requirements-{ _dep } .txt"
202
+ variant2 = collection . path / f"requirements-{ _dep } .txt"
195
203
if variant2 .exists ():
196
204
files .append (variant2 )
197
205
continue
198
206
msg = (
199
207
f"Failed to find optional dependency file for '{ _dep } '."
200
208
f" Checked for '{ variant1 .name } ' and '{ variant2 .name } '. Skipping."
201
209
)
202
- logger .error (msg )
210
+ output .error (msg )
211
+ count = len (files )
212
+ msg = f"Found { count } optional dependency file{ 's' * (count > 1 )} . { oxford_join (files )} "
213
+ output .debug (msg )
203
214
return files
204
215
205
216
@@ -277,7 +288,11 @@ def collect_manifests( # noqa: C901
277
288
return sort_dict (collections )
278
289
279
290
280
- def builder_introspect (config : Config , output : Output ) -> None :
291
+ def builder_introspect (
292
+ config : Config ,
293
+ output : Output ,
294
+ opt_dep_paths : list [Path ] | None = None ,
295
+ ) -> None :
281
296
"""Introspect a collection.
282
297
283
298
Use the sys executable to run builder, since it is a direct dependency
@@ -286,29 +301,20 @@ def builder_introspect(config: Config, output: Output) -> None:
286
301
Args:
287
302
config: The configuration object.
288
303
output: The output object.
304
+ opt_dep_paths: A list of optional dependency paths.
289
305
"""
290
306
command = (
291
307
f"{ sys .executable } -m ansible_builder introspect { config .site_pkg_path } "
292
308
f" --write-pip { config .discovered_python_reqs } "
293
309
f" --write-bindep { config .discovered_bindep_reqs } "
294
310
" --sanitize"
295
311
)
296
- if (
297
- hasattr (config .args , "collection_specifier" )
298
- and hasattr (config , "collection" )
299
- and config .collection .opt_deps
300
- and config .collection .path
301
- ):
302
- dep_paths = opt_deps_to_files (
303
- collection_path = config .collection .path ,
304
- dep_str = config .collection .opt_deps ,
305
- )
306
- for dep_path in dep_paths :
307
- command += f" --user-pip { dep_path } "
312
+ for opt_dep_path in opt_dep_paths or []:
313
+ command += f" --user-pip { opt_dep_path } "
308
314
msg = f"Writing discovered python requirements to: { config .discovered_python_reqs } "
309
- logger .debug (msg )
315
+ output .debug (msg )
310
316
msg = f"Writing discovered system requirements to: { config .discovered_bindep_reqs } "
311
- logger .debug (msg )
317
+ output .debug (msg )
312
318
work = "Persisting requirements to file system"
313
319
try :
314
320
subprocess_run (
0 commit comments