2025-02-28 11:05:29 +07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import fileinput
|
|
|
|
from colorama import init, Fore, Style
|
|
|
|
|
|
|
|
# Initialize colorama for colored output
|
|
|
|
init()
|
|
|
|
|
|
|
|
# Color definitions
|
|
|
|
GREEN = Fore.GREEN
|
|
|
|
RED = Fore.RED
|
|
|
|
GRAY = Fore.LIGHTBLACK_EX
|
|
|
|
NC = Style.RESET_ALL
|
|
|
|
|
|
|
|
# Check if input file is provided
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print(f"{RED}Usage: {sys.argv[0]} <input_file>{NC}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
input_file = sys.argv[1]
|
|
|
|
|
|
|
|
# Check if input file exists
|
|
|
|
if not os.path.isfile(input_file):
|
|
|
|
print(f"{RED}Error: File '{input_file}' not found{NC}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
changes_made = 0
|
|
|
|
|
|
|
|
# Process each line
|
2025-02-28 12:03:14 +07:00
|
|
|
with open(input_file, "r") as f:
|
2025-02-28 11:05:29 +07:00
|
|
|
for line in f:
|
|
|
|
# Split line by ':' and trim whitespace
|
2025-02-28 12:03:14 +07:00
|
|
|
parts = [part.strip() for part in line.split(":", 4)]
|
2025-02-28 11:05:29 +07:00
|
|
|
if len(parts) < 5:
|
2025-02-28 12:03:14 +07:00
|
|
|
parts.extend([""] * (5 - len(parts))) # Ensure 5 parts
|
2025-02-28 11:05:29 +07:00
|
|
|
file_path, line_err, status, desc, data = parts
|
|
|
|
|
|
|
|
print(f"{GRAY}DEBUG: Processing line:{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: file_path=[{file_path}]{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: line_err=[{line_err}]{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: status=[{status}]{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: desc=[{desc}]{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: data=[{data}]{NC}")
|
|
|
|
|
|
|
|
# Check if file exists and description matches conditions
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
print(f"{GRAY}DEBUG: File exists: {file_path}{NC}")
|
|
|
|
|
|
|
|
if desc == "undefined label":
|
|
|
|
print(f"{GRAY}DEBUG: Found 'undefined label' case{NC}")
|
|
|
|
|
2025-02-28 12:03:14 +07:00
|
|
|
# Remove < and > for processing
|
|
|
|
clean_data = f"<{data}>"
|
|
|
|
# Replace \, /, or _ with - in clean_data
|
|
|
|
new_data = (
|
|
|
|
clean_data.replace("\\", "-").replace("/", "-").replace("_", "-")
|
|
|
|
)
|
|
|
|
print(f"{GRAY}DEBUG: Changing '{data}' to '{new_data}'{NC}")
|
|
|
|
# Check if the data appears in an {image} directive
|
|
|
|
with open(file_path, "r") as file_content:
|
|
|
|
lines = file_content.readlines()
|
|
|
|
skip = False
|
|
|
|
for i, line_content in enumerate(lines):
|
|
|
|
if data in line_content and i > 0 and "{image}" in lines[i - 1]:
|
|
|
|
skip = True
|
|
|
|
break
|
|
|
|
if skip:
|
|
|
|
print(
|
|
|
|
f"{GRAY}DEBUG: Skipping '{data}' as it appears in an {{image}} directive{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
2025-02-28 12:03:14 +07:00
|
|
|
# Replace the old data with new data in the file
|
|
|
|
try:
|
|
|
|
with fileinput.FileInput(file_path, inplace=True) as file:
|
|
|
|
for line_content in file:
|
|
|
|
if "{image}" not in line_content:
|
|
|
|
line_content = line_content.replace(
|
|
|
|
f'<{data}>', f"{new_data}"
|
|
|
|
)
|
|
|
|
print(line_content, end="")
|
|
|
|
changes_made += 1
|
|
|
|
print(
|
|
|
|
f"{GREEN}Processed: {file_path} - Changed '{data}' to '{new_data}'{NC}"
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print(
|
|
|
|
f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
|
|
|
|
elif "toctree contains reference to nonexisting document" in desc:
|
|
|
|
print(f"{GRAY}DEBUG: Found 'toctree' case{NC}")
|
|
|
|
# Extract the document reference from data (assuming it's in single quotes)
|
|
|
|
doc_ref_match = re.search(r"'([^']*)'", data)
|
|
|
|
if doc_ref_match:
|
|
|
|
doc_ref = doc_ref_match.group(1)
|
|
|
|
# Replace \, /, or _ with - in doc_ref
|
2025-02-28 12:03:14 +07:00
|
|
|
new_data = (
|
|
|
|
doc_ref.replace("\\", "-").replace("/", "-").replace("_", "-")
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
print(f"{GRAY}DEBUG: Changing '{doc_ref}' to '{new_data}'{NC}")
|
|
|
|
|
|
|
|
# Check if the doc_ref appears in an {image} directive
|
2025-02-28 12:03:14 +07:00
|
|
|
with open(file_path, "r") as file_content:
|
2025-02-28 11:05:29 +07:00
|
|
|
lines = file_content.readlines()
|
|
|
|
skip = False
|
|
|
|
for i, line_content in enumerate(lines):
|
2025-02-28 12:03:14 +07:00
|
|
|
if (
|
|
|
|
doc_ref in line_content
|
|
|
|
and i > 0
|
|
|
|
and "{image}" in lines[i - 1]
|
|
|
|
):
|
2025-02-28 11:05:29 +07:00
|
|
|
skip = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if skip:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{GRAY}DEBUG: Skipping '{doc_ref}' as it appears in an {{image}} directive{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
|
|
|
# Replace the old data with new data in the file
|
|
|
|
try:
|
|
|
|
with fileinput.FileInput(file_path, inplace=True) as file:
|
|
|
|
for line_content in file:
|
2025-02-28 12:03:14 +07:00
|
|
|
if "{image}" not in line_content:
|
|
|
|
line_content = line_content.replace(
|
|
|
|
doc_ref, new_data
|
|
|
|
)
|
|
|
|
print(line_content, end="")
|
2025-02-28 11:05:29 +07:00
|
|
|
changes_made += 1
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{GREEN}Processed: {file_path} - Changed '{doc_ref}' to '{new_data}'{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
except Exception as e:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{RED}WARNING: Could not extract document reference from '{data}' in {file_path}{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
|
|
|
|
elif "card directive targets nonexisting document" in desc:
|
|
|
|
print(f"{GRAY}DEBUG: Found 'card directive' case{NC}")
|
|
|
|
# Extract the document reference from desc (since data might be empty), remove .rst
|
|
|
|
doc_ref_match = re.search(r"'([^']*)'", desc)
|
|
|
|
if doc_ref_match:
|
2025-02-28 12:03:14 +07:00
|
|
|
doc_ref = doc_ref_match.group(1).replace(".rst", "")
|
2025-02-28 11:05:29 +07:00
|
|
|
# Replace \, /, or _ with - in doc_ref
|
2025-02-28 12:03:14 +07:00
|
|
|
new_data = (
|
|
|
|
doc_ref.replace("\\", "-").replace("/", "-").replace("_", "-")
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
print(f"{GRAY}DEBUG: Changing '{doc_ref}' to '{new_data}'{NC}")
|
|
|
|
|
|
|
|
# Check if the doc_ref appears in an {image} directive
|
2025-02-28 12:03:14 +07:00
|
|
|
with open(file_path, "r") as file_content:
|
2025-02-28 11:05:29 +07:00
|
|
|
lines = file_content.readlines()
|
|
|
|
skip = False
|
|
|
|
for i, line_content in enumerate(lines):
|
2025-02-28 12:03:14 +07:00
|
|
|
if (
|
|
|
|
doc_ref in line_content
|
|
|
|
and i > 0
|
|
|
|
and "{image}" in lines[i - 1]
|
|
|
|
):
|
2025-02-28 11:05:29 +07:00
|
|
|
skip = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if skip:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{GRAY}DEBUG: Skipping '{doc_ref}' as it appears in an {{image}} directive{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
|
|
|
# Replace the old data with new data in the file
|
|
|
|
try:
|
|
|
|
with fileinput.FileInput(file_path, inplace=True) as file:
|
|
|
|
for line_content in file:
|
2025-02-28 12:03:14 +07:00
|
|
|
if "{image}" not in line_content:
|
|
|
|
line_content = line_content.replace(
|
|
|
|
doc_ref, new_data
|
|
|
|
)
|
|
|
|
print(line_content, end="")
|
2025-02-28 11:05:29 +07:00
|
|
|
changes_made += 1
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{GREEN}Processed: {file_path} - Changed '{doc_ref}' to '{new_data}'{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
except Exception as e:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{RED}WARNING: Failed to modify {file_path} - {str(e)}{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{RED}WARNING: Could not extract document reference from '{desc}' in {file_path}{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
|
|
|
|
else:
|
2025-02-28 12:03:14 +07:00
|
|
|
print(
|
|
|
|
f"{GRAY}DEBUG: Description '{desc}' doesn't match any conditions{NC}"
|
|
|
|
)
|
2025-02-28 11:05:29 +07:00
|
|
|
else:
|
|
|
|
print(f"{RED}WARNING: File not found: {file_path}{NC}")
|
|
|
|
print(f"{GRAY}DEBUG: ---{NC}")
|
|
|
|
|
|
|
|
print(f"{GREEN}Processing completed!{NC}")
|
2025-02-28 12:03:14 +07:00
|
|
|
print(f"{GREEN}Total changes made: {changes_made}{NC}")
|