Skip to content

Commit

Permalink
Add exception handling and prevent reformatting of ts files and disab…
Browse files Browse the repository at this point in the history
…le multi threading
  • Loading branch information
daschuer committed Jul 3, 2024
1 parent 66c0b17 commit a2cdac2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repos:
- id: check-yaml
exclude: ^\.clang-format$
- id: end-of-file-fixer
exclude: ^.*UTF-8-BOM.txt$
exclude: ^.*(UTF-8-BOM\.txt|\.ts)$
- id: mixed-line-ending
- id: trailing-whitespace
exclude: \.(c|cc|cxx|cpp|ts|frag|glsl|h|hpp|hxx|ih|ispc|ipp|java|js|m|mm|proto|vert)$
Expand Down Expand Up @@ -178,6 +178,7 @@ repos:
stages:
- commit
- manual
require_serial: true
language: python
additional_dependencies:
- lxml==4.9.3
Expand Down
74 changes: 54 additions & 20 deletions tools/ts_source_copy_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,73 @@
def is_untranstaled_allowed(source_text, language):
if os.path.exists(ALLOW_LIST_PATH):
parser = etree.XMLParser(recover=True)
tree = etree.parse(ALLOW_LIST_PATH, parser)
try:
tree = etree.parse(ALLOW_LIST_PATH, parser)
except Exception as e:
print("XML parsing failed:")
print(e)
raise e
root = tree.getroot()
else:
return False

for message in root.findall("message"):
if message.find("source").text == source_text:
if message.find("allow_all_languages").text == "true":
return True
languages_elem = message.find("allowed_languages")
if languages_elem is None:
return False
allowed_languages = {
lang.text for lang in languages_elem.findall("language")
}
if language in allowed_languages:
return True
try:
for message in root.findall("message"):
if message.find("source").text == source_text:
if message.find("allow_all_languages").text == "true":
return True
languages_elem = message.find("allowed_languages")
if languages_elem is None:
return False
allowed_languages = {
lang.text for lang in languages_elem.findall("language")
}
if language in allowed_languages:
return True

except Exception as e:
print(f"Parsing failed at {message.base}:{message.sourceline}")
print(e)
raise e

return False


def add_to_allow_list(source_text, language):
if os.path.exists(PROPOSED_ALLOW_LIST_PATH):
parser = etree.XMLParser(recover=True)
tree = etree.parse(PROPOSED_ALLOW_LIST_PATH, parser)
try:
tree = etree.parse(PROPOSED_ALLOW_LIST_PATH, parser)
except Exception as e:
print("XML parsing failed:")
print(e)
raise e
root = tree.getroot()
else:
if os.path.exists(ALLOW_LIST_PATH):
parser = etree.XMLParser(recover=True)
tree = etree.parse(ALLOW_LIST_PATH, parser)
try:
tree = etree.parse(ALLOW_LIST_PATH, parser)
except Exception as e:
print("XML parsing failed:")
print(e)
return
root = tree.getroot()
else:
root = etree.Element("allow_list")
tree = etree.ElementTree(root)

# Check if the message already exists
existing_message = None
for message in root.findall("message"):
if message.find("source").text == source_text:
existing_message = message
break
try:
for message in root.findall("message"):
if message.find("source").text == source_text:
existing_message = message
break
except Exception as e:
print(f"Parsing failed at {message.base}:{message.sourceline}")
print(e)
raise e

if existing_message is not None:
allow_all_languages = (
Expand Down Expand Up @@ -110,7 +137,14 @@ def check_copied_source_on_lines(rootdir, file_to_format, stylepath=None):
return False

parser = etree.XMLParser(recover=True)
tree = etree.parse(filename, parser)

try:
tree = etree.parse(filename, parser)
except Exception as e:
print("XML parsing failed:")
print(e)
return False

root = tree.getroot()

found = False
Expand Down

0 comments on commit a2cdac2

Please sign in to comment.