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.

120 lines
3.6 KiB

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