You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.4 KiB

  1. #!/usr/bin/env python3
  2. import os
  3. import json
  4. import sys
  5. from io import open
  6. from shutil import rmtree
  7. from collections import OrderedDict
  8. from xml.sax.saxutils import escape
  9. if len(sys.argv) == 1 or not sys.argv[1]:
  10. raise SystemExit('Build dir missing.')
  11. def mkdirs(path):
  12. try:
  13. os.makedirs(path)
  14. finally:
  15. return os.path.exists(path)
  16. pj = os.path.join
  17. build_dir = os.path.abspath(sys.argv[1])
  18. source_locale_dir = pj(build_dir, '_locales')
  19. target_locale_dir = pj(build_dir, 'locale')
  20. language_codes = []
  21. descriptions = OrderedDict({})
  22. title_case_strings = ['pickerContextMenuEntry']
  23. for alpha2 in sorted(os.listdir(source_locale_dir)):
  24. locale_path = pj(source_locale_dir, alpha2, 'messages.json')
  25. with open(locale_path, encoding='utf-8') as f:
  26. strings = json.load(f, object_pairs_hook=OrderedDict)
  27. alpha2 = alpha2.replace('_', '-')
  28. descriptions[alpha2] = strings['extShortDesc']['message']
  29. del strings['extShortDesc']
  30. language_codes.append(alpha2)
  31. mkdirs(pj(target_locale_dir, alpha2))
  32. locale_path = pj(target_locale_dir, alpha2, 'messages.properties')
  33. with open(locale_path, 'wt', encoding='utf-8', newline='\n') as f:
  34. for string_name in strings:
  35. string = strings[string_name]['message']
  36. if alpha2 == 'en' and string_name in title_case_strings:
  37. string = string.title()
  38. f.write(string_name)
  39. f.write(u'=')
  40. f.write(string.replace('\n', r'\n'))
  41. f.write(u'\n')
  42. # generate chrome.manifest file
  43. chrome_manifest = pj(build_dir, 'chrome.manifest')
  44. with open(chrome_manifest, 'at', encoding='utf-8', newline='\n') as f:
  45. f.write(u'\nlocale umatrix en ./locale/en/\n')
  46. for alpha2 in language_codes:
  47. if alpha2 == 'en':
  48. continue
  49. f.write(u'locale umatrix ' + alpha2 + ' ./locale/' + alpha2 + '/\n')
  50. rmtree(source_locale_dir)
  51. # update install.rdf
  52. proj_dir = pj(os.path.split(os.path.abspath(__file__))[0], '..')
  53. chromium_manifest = pj(proj_dir, 'platform', 'chromium', 'manifest.json')
  54. with open(chromium_manifest, encoding='utf-8') as m:
  55. manifest = json.load(m)
  56. # https://developer.mozilla.org/en-US/Add-ons/AMO/Policy/Maintenance#How_do_I_submit_a_Beta_add-on.3F
  57. # "To create a beta channel [...] '(a|alpha|b|beta|pre|rc)\d*$' "
  58. if sys.argv[2]:
  59. manifest['version'] += sys.argv[2]
  60. manifest['homepage'] = 'https://github.com/gorhill/uMatrix'
  61. manifest['description'] = escape(descriptions['en'])
  62. del descriptions['en']
  63. manifest['localized'] = []
  64. t = ' '
  65. t3 = 3 * t
  66. for alpha2 in descriptions:
  67. if alpha2 == 'en':
  68. continue
  69. manifest['localized'].append(
  70. '\n' + t*2 + '<localized><r:Description>\n' +
  71. t3 + '<locale>' + alpha2 + '</locale>\n' +
  72. t3 + '<name>' + manifest['name'] + '</name>\n' +
  73. t3 + '<description>' + escape(descriptions[alpha2]) + '</description>\n' +
  74. t3 + '<creator>' + manifest['author'] + '</creator>\n' +
  75. # t3 + '<translator>' + ??? + '</translator>\n' +
  76. t3 + '<homepageURL>' + manifest['homepage'] + '</homepageURL>\n' +
  77. t*2 + '</r:Description></localized>'
  78. )
  79. manifest['localized'] = '\n'.join(manifest['localized'])
  80. install_rdf = pj(build_dir, 'install.rdf')
  81. with open(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
  82. install_rdf = f.read()
  83. f.seek(0)
  84. f.write(install_rdf.format(**manifest))