debugging

This commit is contained in:
Greg DiCristofaro 2021-01-08 13:05:11 -05:00
parent 0613e305f8
commit 287d3aaec5
5 changed files with 46 additions and 44 deletions

View File

@ -5,7 +5,7 @@ As a consequence, it also requires git >= 1.7.0 and python >= 3.4.
import sys import sys
from envutil import get_proj_dir from envutil import get_proj_dir
from excelutil import write_results_to_xlsx from excelutil import write_results_to_xlsx
from gitutil import get_property_files_diff, get_git_root, get_commit_id, get_tree from gitutil import get_property_files_diff, get_git_root, get_commit_id, get_tree, list_paths
from itemchange import convert_to_output from itemchange import convert_to_output
from csvutil import write_results_to_csv from csvutil import write_results_to_csv
import argparse import argparse
@ -43,7 +43,7 @@ def main():
help='Specify the path to the properties file containing key value pairs of language mapped to ' help='Specify the path to the properties file containing key value pairs of language mapped to '
'the commit of when bundles for that language were most recently updated.') 'the commit of when bundles for that language were most recently updated.')
parser.add_argument('-td', '--translation-dict', dest='translation_dict', type=bool, required=False, default=False, parser.add_argument('-td', '--translation-dict', dest='translation_dict', action='store_true', required=False,
help='If this flag is specified, a dictionary mapping original prop key values to translated ' help='If this flag is specified, a dictionary mapping original prop key values to translated '
'values. If this flag is specified, it will ') 'values. If this flag is specified, it will ')
@ -71,7 +71,7 @@ def main():
translation_dict = None translation_dict = None
if use_translation_dict and lang: if use_translation_dict and lang:
translation_dict = extract_translations( translation_dict = extract_translations(
file_iter=get_tree(repo_path, commit_1_id), file_iter=list_paths(get_tree(repo_path, commit_1_id)),
orig_filename=DEFAULT_PROPS_FILENAME, orig_filename=DEFAULT_PROPS_FILENAME,
translated_filename=get_lang_bundle_name(lang)) translated_filename=get_lang_bundle_name(lang))

View File

@ -0,0 +1,29 @@
class FoundValue:
"""
A class containing a record of a prop key existing in both an original props file and a translated props file
"""
common_path: str
original_file: str
translated_file: str
key: str
orig_val: str
translated_val: str
def __init__(self, common_path, original_file, translated_file, key, orig_val, translated_val):
"""
Constructor.
Args:
common_path: The folder common to both files.
original_file: The original file path.
translated_file: The translated file path.
key: The common prop key.
orig_val: The original (English) value.
translated_val: The translated value.
"""
self.common_path = common_path
self.original_file = original_file
self.translated_file = translated_file
self.key = key
self.orig_val = orig_val
self.translated_val = translated_val

View File

@ -128,7 +128,7 @@ def get_property_files_diff(repo_path: str, commit_1_id: str, commit_2_id: str,
yield from get_changed_from_diff(rel_path, diff) yield from get_changed_from_diff(rel_path, diff)
def list_paths(root_tree, path: Path = Path('.')) -> Iterator[Tuple[str, Blob]]: def list_paths(root_tree: Tree, path: Path = Path('.')) -> Iterator[Tuple[str, Blob]]:
""" """
Given the root path to serve as a prefix, walks the tree of a git commit returning all files and blobs. Given the root path to serve as a prefix, walks the tree of a git commit returning all files and blobs.
Repurposed from: https://www.enricozini.org/blog/2019/debian/gitpython-list-all-files-in-a-git-commit/ Repurposed from: https://www.enricozini.org/blog/2019/debian/gitpython-list-all-files-in-a-git-commit/
@ -174,7 +174,7 @@ def get_property_file_entries(repo_path: str, at_commit: str = 'HEAD',
Returns: An iterator of PropEntry objects. Returns: An iterator of PropEntry objects.
""" """
for item in get_tree(repo_path, at_commit): for item in list_paths(get_tree(repo_path, at_commit)):
path, blob = item path, blob = item
if path.endswith(property_file_extension): if path.endswith(property_file_extension):
for key, val in get_entry_dict(get_text(blob)).items(): for key, val in get_entry_dict(get_text(blob)).items():

View File

@ -1,6 +1,6 @@
from typing import Iterator, List, Union, Dict from typing import Iterator, List, Union, Dict
from languagedictutil import FoundValue from foundvalue import FoundValue
from outputresult import OutputResult from outputresult import OutputResult
from propsutil import get_entry_dict from propsutil import get_entry_dict
from enum import Enum from enum import Enum
@ -127,7 +127,7 @@ def convert_to_output(items: Iterator[ItemChange],
elif value_regex is not None and re.match(value_regex, item.cur_val): elif value_regex is not None and re.match(value_regex, item.cur_val):
omitted.append(item_row) omitted.append(item_row)
elif translation_dict is not None and item.cur_val.strip() in translation_dict: elif translation_dict is not None and item.cur_val.strip() in translation_dict:
found_translation.append(item_row + [translation_dict[item.cur_val.strim()].translated_val]) found_translation.append(item_row + [translation_dict[item.cur_val.strip()].translated_val])
else: else:
results.append(item_row) results.append(item_row)

View File

@ -2,40 +2,12 @@ import os
from pathlib import Path from pathlib import Path
from typing import Dict, Iterator, Tuple, TypeVar from typing import Dict, Iterator, Tuple, TypeVar
from git import Blob from git import Blob
from foundvalue import FoundValue
from gitutil import get_text
from propsutil import get_entry_dict from propsutil import get_entry_dict
class FoundValue:
"""
A class containing a record of a prop key existing in both an original props file and a translated props file
"""
common_path: str
original_file: str
translated_file: str
key: str
orig_val: str
translated_val: str
def __init__(self, common_path, original_file, translated_file, key, orig_val, translated_val):
"""
Constructor.
Args:
common_path: The folder common to both files.
original_file: The original file path.
translated_file: The translated file path.
key: The common prop key.
orig_val: The original (English) value.
translated_val: The translated value.
"""
self.common_path = common_path
self.original_file = original_file
self.translated_file = translated_file
self.key = key
self.orig_val = orig_val
self.translated_val = translated_val
def extract_translations(file_iter: Iterator[Tuple[str, Blob]], orig_filename: str, translated_filename: str) \ def extract_translations(file_iter: Iterator[Tuple[str, Blob]], orig_filename: str, translated_filename: str) \
-> Dict[str, FoundValue]: -> Dict[str, FoundValue]:
""" """
@ -61,18 +33,19 @@ def extract_translations(file_iter: Iterator[Tuple[str, Blob]], orig_filename: s
for path, content in file_iter: for path, content in file_iter:
parent_dir, file_name = os.path.split(str(Path(path))) parent_dir, file_name = os.path.split(str(Path(path)))
if file_name.strip().lower() == orig_filename.strip().lower(): if file_name.strip().lower() == orig_filename.strip().lower():
original_files[file_name] = (parent_dir, content) original_files[parent_dir.strip().lower()] = (path, content)
elif file_name.strip().lower() == translated_filename.strip().lower(): elif file_name.strip().lower() == translated_filename.strip().lower():
translated_files[file_name] = (parent_dir, content) translated_files[parent_dir.strip().lower()] = (path, content)
# determine original and translated files with common parent folders and find common keys # determine original and translated files with common parent folders and find common keys
to_ret: Dict[str, FoundValue] = dict() to_ret: Dict[str, FoundValue] = dict()
for common_folder, ((original_path, original_blob), (translated_path, translated_blob))\ for (common_folder, (original_path, original_blob), (translated_path, translated_blob))\
in common_entries(original_files, translated_files): in common_entries(original_files, translated_files):
orig_dict = sanitize_prop_dict_keys(get_entry_dict(original_blob))
translated_dict = sanitize_prop_dict_keys(get_entry_dict(translated_blob))
for common_key, (orig_value, translated_value) in common_entries(orig_dict, translated_dict): orig_dict = sanitize_prop_dict_keys(get_entry_dict(get_text(original_blob)))
translated_dict = sanitize_prop_dict_keys(get_entry_dict(get_text(translated_blob)))
for common_key, orig_value, translated_value in common_entries(orig_dict, translated_dict):
to_ret[orig_value] = FoundValue( to_ret[orig_value] = FoundValue(
common_path=common_folder, common_path=common_folder,
original_file=original_path, original_file=original_path,