116
116
from ..module_utils .sap_launchpad_software_center_download_runner import *
117
117
from ..module_utils .sap_id_sso import sap_sso_login
118
118
119
+ def _check_similar_files (download_path , filename ):
120
+ """
121
+ Checks for similar files in the download path based on the given filename.
122
+
123
+ Args:
124
+ download_path (str): The path where files are downloaded.
125
+ filename (str): The filename to check for similar files.
126
+
127
+ Returns:
128
+ bool: True if similar files exist, False otherwise.
129
+ filename_similar_names: A list of similar filenames if they exist, empty list otherwise.
130
+ """
131
+ # pattern = download_path + '/**/' + os.path.splitext(filename)[0]
132
+ filename_base = os .path .splitext (filename )[0 ]
133
+ filename_pattern = os .path .join (download_path , "**" , filename_base )
134
+ filename_similar = glob .glob (filename_pattern , recursive = True )
135
+
136
+ if filename_similar :
137
+ filename_similar_names = [os .path .basename (f ) for f in filename_similar ]
138
+ return True , filename_similar_names
139
+ else :
140
+ return False , []
141
+
119
142
120
143
def run_module ():
121
144
@@ -192,16 +215,9 @@ def run_module():
192
215
result ['msg' ] = f"File already exists: { filename } "
193
216
module .exit_json (** result )
194
217
else :
195
- # Exact file not found, search with pattern
196
- # pattern = download_path + '/**/' + os.path.splitext(filename)[0] + '*' # old pattern
197
- filename_base = os .path .splitext (filename )[0 ]
198
- filename_ext = os .path .splitext (filename )[1 ]
199
- filename_pattern = os .path .join (download_path , "**" , filename_base + "*" + filename_ext )
200
- filename_similar = glob .glob (filename_pattern , recursive = True )
201
-
202
- # Skip if similar files were found and search_alternatives was not set.
203
- if filename_similar and not (query and search_alternatives ):
204
- filename_similar_names = [os .path .basename (f ) for f in filename_similar ]
218
+ # Exact file not found, search for similar files with pattern
219
+ filename_similar_exists , filename_similar_names = _check_similar_files (download_path , filename )
220
+ if filename_similar_exists and not (query and search_alternatives ):
205
221
result ['skipped' ] = True
206
222
result ['msg' ] = f"Similar file(s) already exist: { ', ' .join (filename_similar_names )} "
207
223
module .exit_json (** result )
@@ -213,9 +229,9 @@ def run_module():
213
229
alternative_found = False # True if search_alternatives was successful
214
230
if query :
215
231
download_link , download_filename , alternative_found = search_software_filename (query ,deduplicate ,search_alternatives )
216
-
232
+
217
233
# Search for existing files again with alternative filename
218
- if search_alternatives and alternative_found :
234
+ if search_alternatives and alternative_found :
219
235
result ['filename' ] = download_filename
220
236
result ['alternative' ] = True
221
237
@@ -225,17 +241,29 @@ def run_module():
225
241
result ['msg' ] = f"Alternative file already exists: { download_filename } - original file { query } is not available to download"
226
242
module .exit_json (** result )
227
243
else :
228
- filename_alternative_base = os .path .splitext (download_filename )[0 ]
229
- filename_alternative_ext = os .path .splitext (download_filename )[1 ]
230
- filename_alternative_pattern = os .path .join (download_path , "**" , filename_alternative_base + "*" + filename_alternative_ext )
231
- filename_alternative_similar = glob .glob (filename_alternative_pattern , recursive = True )
232
-
233
- # Skip if similar files were found and search_alternatives was not set.
234
- if filename_alternative_similar :
235
- filename_alternative_similar_names = [os .path .basename (f ) for f in filename_alternative_similar ]
236
- result ['skipped' ] = True
237
- result ['msg' ] = f"Similar alternative file(s) already exist: { ', ' .join (filename_alternative_similar_names )} "
238
- module .exit_json (** result )
244
+ # Exact file not found, search for similar files with pattern
245
+ filename_similar_exists , filename_similar_names = _check_similar_files (download_path , download_filename )
246
+ if filename_similar_exists :
247
+ result ['skipped' ] = True
248
+ result ['msg' ] = f"Similar alternative file(s) already exist: { ', ' .join (filename_similar_names )} "
249
+ module .exit_json (** result )
250
+
251
+ # Triggers for CD Media, where number was changed to name using _get_valid_filename
252
+ elif filename != download_filename and not alternative_found :
253
+ result ['filename' ] = download_filename
254
+
255
+ if os .path .exists (os .path .join (download_path , download_filename )):
256
+ result ['skipped' ] = True
257
+ result ['msg' ] = f"File already exists with correct name: { download_filename } "
258
+ module .exit_json (** result )
259
+ else :
260
+ # Exact file not found, search for similar files with pattern
261
+ filename_similar_exists , filename_similar_names = _check_similar_files (download_path , download_filename )
262
+ if filename_similar_exists :
263
+ result ['skipped' ] = True
264
+ result ['msg' ] = f"Similar file(s) already exist for correct name { download_filename } : { ', ' .join (filename_similar_names )} "
265
+ module .exit_json (** result )
266
+
239
267
240
268
# Ensure that download_link is provided when query was not provided
241
269
if not download_link :
0 commit comments