Skip to content

software_center_download: Improved logic for skipping existing files and getting valid filename #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ def _get_valid_filename(software_found):
"""

# Check if Title contains filename and extension
if re.match(r'^[^/\\\0]+\.[^/\\\0]+$', software_found['Title']):
return software_found['Title']
else:
# Check if Description contains filename and extension
if re.match(r'^[^/\\\0]+\.[^/\\\0]+$', software_found['Description']):
if re.match(r'^\d+$', software_found['Title']):
# Check if Description attribute exists and that it does not contain empty spaces
if software_found['Description'] and ' ' not in software_found['Description']:
return software_found['Description']
else:
# Default to Title if Description does not help
return software_found['Title']
else:
# Default to Title if Description does not help
return software_found['Title']
14 changes: 10 additions & 4 deletions plugins/modules/software_center_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,15 @@ def _check_similar_files(dest, filename):
bool: True if similar files exist, False otherwise.
filename_similar_names: A list of similar filenames if they exist, empty list otherwise.
"""
# pattern = dest + '/**/' + os.path.splitext(filename)[0]
filename_base = os.path.splitext(filename)[0]
filename_pattern = os.path.join(dest, "**", filename_base)

# Check if filename has has extension and remove it for search
if os.path.splitext(filename)[1]:
filename_base = os.path.splitext(filename)[0]
filename_pattern = os.path.join(dest, "**", filename_base + ".*")
else:
filename_pattern = os.path.join(dest, "**", filename + ".*")

# Find all similar files in dest and sub-folders.
filename_similar = glob.glob(filename_pattern, recursive=True)

if filename_similar:
Expand Down Expand Up @@ -217,7 +223,7 @@ def run_module():
else:
# Exact file not found, search for similar files with pattern
filename_similar_exists, filename_similar_names = _check_similar_files(dest, filename)
if filename_similar_exists and not (query and search_alternatives):
if filename_similar_exists:
result['skipped'] = True
result['msg'] = f"Similar file(s) already exist: {', '.join(filename_similar_names)}"
module.exit_json(**result)
Expand Down