Skip to content

Commit

Permalink
Improve Case-Insensitive Key Comparison and Path Normalization in Lan…
Browse files Browse the repository at this point in the history
…guage Properties Check Script (#3067)

# Description of Changes

Please provide a summary of the changes, including:

- Updated key comparison logic in `update_missing_keys` function to be
case-insensitive by converting keys to lowercase before comparison.
- Introduced `os.path.normpath` for file path normalization to improve
cross-platform compatibility.
- Replaced direct usage of `file_path` with `file_normpath` in security
checks, file size validation, and duplicate key detection to ensure
consistent path handling.

These changes improve the robustness and maintainability of the script,
ensuring more accurate language property checks while enhancing security
validation.

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
  • Loading branch information
Ludy87 authored Feb 26, 2025
1 parent 7a7338c commit c9c8378
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions .github/scripts/check_language_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def update_missing_keys(reference_file, file_list, branch=""):
if current_entry["type"] == "entry":
if ref_entry_copy["type"] != "entry":
continue
if ref_entry_copy["key"] == current_entry["key"]:
if ref_entry_copy["key"].lower() == current_entry["key"].lower():
ref_entry_copy["value"] = current_entry["value"]
updated_properties.append(ref_entry_copy)
write_json_file(os.path.join(branch, file_path), updated_properties)
Expand Down Expand Up @@ -199,29 +199,30 @@ def check_for_differences(reference_file, file_list, branch, actor):
base_dir = os.path.abspath(os.path.join(os.getcwd(), "src", "main", "resources"))

for file_path in file_arr:
absolute_path = os.path.abspath(file_path)
file_normpath = os.path.normpath(file_path)
absolute_path = os.path.abspath(file_normpath)
# Verify that file is within the expected directory
if not absolute_path.startswith(base_dir):
raise ValueError(f"Unsafe file found: {file_path}")
raise ValueError(f"Unsafe file found: {file_normpath}")
# Verify file size before processing
if os.path.getsize(os.path.join(branch, file_path)) > MAX_FILE_SIZE:
if os.path.getsize(os.path.join(branch, file_normpath)) > MAX_FILE_SIZE:
raise ValueError(
f"The file {file_path} is too large and could pose a security risk."
f"The file {file_normpath} is too large and could pose a security risk."
)

basename_current_file = os.path.basename(os.path.join(branch, file_path))
basename_current_file = os.path.basename(os.path.join(branch, file_normpath))
if (
basename_current_file == basename_reference_file
or (
# only local windows command
not file_path.startswith(
not file_normpath.startswith(
os.path.join("", "src", "main", "resources", "messages_")
)
and not file_path.startswith(
and not file_normpath.startswith(
os.path.join(os.getcwd(), "src", "main", "resources", "messages_")
)
)
or not file_path.endswith(".properties")
or not file_normpath.endswith(".properties")
or not basename_current_file.startswith("messages_")
):
continue
Expand Down Expand Up @@ -292,13 +293,13 @@ def check_for_differences(reference_file, file_list, branch, actor):
else:
report.append("2. **Test Status:** ✅ **_Passed_**")

if find_duplicate_keys(os.path.join(branch, file_path)):
if find_duplicate_keys(os.path.join(branch, file_normpath)):
has_differences = True
output = "\n".join(
[
f" - `{key}`: first at line {first}, duplicate at `line {duplicate}`"
for key, first, duplicate in find_duplicate_keys(
os.path.join(branch, file_path)
os.path.join(branch, file_normpath)
)
]
)
Expand Down

0 comments on commit c9c8378

Please sign in to comment.