check_missing_translations.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #! /usr/bin/python3
  2. import polib
  3. import git
  4. import os
  5. import argparse
  6. from collections import OrderedDict
  7. missing_keys = {}
  8. def list_entries(file):
  9. entries = OrderedDict()
  10. for entry in file:
  11. if not entry.obsolete:
  12. msgctxt = entry.msgctxt
  13. if msgctxt is None:
  14. msgctxt = ''
  15. entries[(msgctxt, entry.msgid)] = entry
  16. return entries
  17. def process_file(actual_file_path, previous_version_file_data, restore_missing):
  18. actual_file = polib.pofile(actual_file_path, wrapwidth=10000)
  19. previous_file = polib.pofile(previous_version_file_data, wrapwidth=10000)
  20. previous_entries = list_entries(previous_file)
  21. actual_entries = list_entries(actual_file)
  22. changed = False
  23. for key, entry in previous_entries.items():
  24. if key not in actual_entries:
  25. if key in missing_keys:
  26. missing_keys[key].append(actual_file_path)
  27. else:
  28. missing_keys[key] = [actual_file_path]
  29. if restore_missing:
  30. # Find the entry that was just before the one we need to re-insert
  31. previous_entry = None
  32. for entry_in_previous_file in previous_file:
  33. if entry_in_previous_file == entry:
  34. break
  35. else:
  36. previous_entry = entry_in_previous_file
  37. if previous_entry is None:
  38. # The entry was at the very beginning
  39. actual_file.insert(0, entry)
  40. else:
  41. # Now find the position in the new file and re-insert the missing entry
  42. index = 0
  43. inserted = False
  44. for entry_in_actual_file in actual_file:
  45. if entry_in_actual_file.msgctxt == previous_entry.msgctxt and entry_in_actual_file.msgid == previous_entry.msgid:
  46. actual_file.insert(index + 1, entry)
  47. inserted = True
  48. break
  49. else:
  50. index += 1
  51. if not inserted:
  52. actual_file.append(entry)
  53. changed = True
  54. if changed:
  55. print(f"Fixed file {actual_file_path}")
  56. actual_file.save()
  57. args_parser = argparse.ArgumentParser(description="Compares the translations present in the current folder with the same files from an other commit/branch. This will write a report.csv file containing the missing translations and the locations where they were expected. Make sure to run this script at the root of Cura/Uranium")
  58. args_parser.add_argument("previous_version", help="Git branch or commit hash of the version to be compared with")
  59. args_parser.add_argument("--restore-missing", action="store_true", help="* Superbonus * This will also restore the translations missing from the previous file into the actual one")
  60. args = args_parser.parse_args()
  61. repo = git.Repo('.')
  62. languages_dir = '/'.join(['resources', 'i18n'])
  63. language_dirs = ['/'.join([languages_dir, dir_path]) for dir_path in os.listdir('resources/i18n')]
  64. language_dirs = [language for language in language_dirs if os.path.isdir(language)]
  65. for language_dir in language_dirs:
  66. for translation_file in os.listdir(language_dir):
  67. if translation_file.endswith('.po'):
  68. translation_file_path = '/'.join([language_dir, translation_file])
  69. print(f'Processing file {translation_file_path}')
  70. blob = repo.commit(args.previous_version).tree / translation_file_path
  71. process_file(translation_file_path, blob.data_stream.read().decode('utf-8'), args.restore_missing)
  72. with open('report.csv', 'w') as report_file:
  73. for missing_key, files in missing_keys.items():
  74. report_file.write(';'.join(list(missing_key) + files))
  75. report_file.write('\n')
  76. print(f"Saved report to {report_file.name}")