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.

84 lines
3.0 KiB

  1. #!/usr/bin/env python3
  2. import os
  3. import json
  4. import re
  5. import sys
  6. from io import open as uopen
  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. proj_dir = os.path.join(os.path.split(os.path.abspath(__file__))[0], '..')
  12. build_dir = os.path.abspath(sys.argv[1])
  13. # Import data from chromium platform
  14. chromium_manifest = {}
  15. webext_manifest = {}
  16. chromium_manifest_file = os.path.join(proj_dir, 'platform', 'chromium', 'manifest.json')
  17. with open(chromium_manifest_file) as f1:
  18. chromium_manifest = json.load(f1)
  19. # WebExtension part
  20. webext_manifest_file = os.path.join(build_dir, 'webextension', 'manifest.json')
  21. with open(webext_manifest_file) as f2:
  22. webext_manifest = json.load(f2)
  23. webext_manifest['version'] = chromium_manifest['version']
  24. with open(webext_manifest_file, 'w') as f2:
  25. json.dump(webext_manifest, f2, indent=2, separators=(',', ': '), sort_keys=True)
  26. f2.write('\n')
  27. # Legacy part
  28. descriptions = OrderedDict({})
  29. source_locale_dir = os.path.join(build_dir, 'webextension', '_locales')
  30. for alpha2 in sorted(os.listdir(source_locale_dir)):
  31. locale_path = os.path.join(source_locale_dir, alpha2, 'messages.json')
  32. with uopen(locale_path, encoding='utf-8') as f:
  33. strings = json.load(f, object_pairs_hook=OrderedDict)
  34. alpha2 = alpha2.replace('_', '-')
  35. descriptions[alpha2] = strings['extShortDesc']['message']
  36. webext_manifest['author'] = chromium_manifest['author'];
  37. webext_manifest['name'] = chromium_manifest['name'] + '/webext';
  38. webext_manifest['homepage'] = 'https://github.com/gorhill/uMatrix'
  39. webext_manifest['description'] = escape(descriptions['en'])
  40. del descriptions['en']
  41. match = re.search('^(\d+\.\d+\.\d+)(\.\d+)$', chromium_manifest['version'])
  42. if match:
  43. buildtype = int(match.group(2)[1:])
  44. if buildtype < 100:
  45. builttype = 'b' + str(buildtype)
  46. else:
  47. builttype = 'rc' + str(buildtype - 100)
  48. webext_manifest['version'] = match.group(1) + builttype
  49. webext_manifest['localized'] = []
  50. t = ' '
  51. t3 = 3 * t
  52. for alpha2 in descriptions:
  53. if alpha2 == 'en':
  54. continue
  55. webext_manifest['localized'].append(
  56. '\n' + t*2 + '<em:localized><Description>\n' +
  57. t3 + '<em:locale>' + alpha2 + '</em:locale>\n' +
  58. t3 + '<em:name>' + webext_manifest['name'] + '</em:name>\n' +
  59. t3 + '<em:description>' + escape(descriptions[alpha2]) + '</em:description>\n' +
  60. t3 + '<em:creator>' + webext_manifest['author'] + '</em:creator>\n' +
  61. # t3 + '<translator>' + ??? + '</translator>\n' +
  62. t3 + '<em:homepageURL>' + webext_manifest['homepage'] + '</em:homepageURL>\n' +
  63. t*2 + '</Description></em:localized>'
  64. )
  65. webext_manifest['localized'] = '\n'.join(webext_manifest['localized'])
  66. install_rdf = os.path.join(build_dir, 'install.rdf')
  67. with uopen(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
  68. install_rdf = f.read()
  69. f.seek(0)
  70. f.write(install_rdf.format(**webext_manifest))
  71. f.truncate()