2014-12-20 20:20:23 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-12-16 17:02:26 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
2016-08-04 14:24:11 +02:00
|
|
|
import re
|
2014-12-16 17:02:26 +01:00
|
|
|
import sys
|
2014-12-19 21:31:11 +01:00
|
|
|
from io import open
|
2014-12-16 17:02:26 +01:00
|
|
|
from shutil import rmtree
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2014-12-19 21:31:11 +01:00
|
|
|
if len(sys.argv) == 1 or not sys.argv[1]:
|
|
|
|
raise SystemExit('Build dir missing.')
|
2014-12-16 17:02:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def mkdirs(path):
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
|
|
|
finally:
|
|
|
|
return os.path.exists(path)
|
|
|
|
|
|
|
|
pj = os.path.join
|
|
|
|
|
2018-02-28 18:27:41 +01:00
|
|
|
# Find path to project root
|
|
|
|
proj_dir = os.path.split(os.path.abspath(__file__))[0]
|
|
|
|
while not os.path.isdir(os.path.join(proj_dir, '.git')):
|
|
|
|
proj_dir = os.path.normpath(os.path.join(proj_dir, '..'))
|
|
|
|
|
|
|
|
# Check that found project root is valid
|
|
|
|
version_filepath = os.path.join(proj_dir, 'dist', 'version')
|
|
|
|
if not os.path.isfile(version_filepath):
|
|
|
|
print('Version file not found.')
|
|
|
|
exit(1)
|
|
|
|
|
2014-12-16 17:02:26 +01:00
|
|
|
build_dir = os.path.abspath(sys.argv[1])
|
|
|
|
source_locale_dir = pj(build_dir, '_locales')
|
|
|
|
target_locale_dir = pj(build_dir, 'locale')
|
|
|
|
language_codes = []
|
2015-01-27 12:44:35 +01:00
|
|
|
descriptions = OrderedDict({})
|
2016-01-17 19:30:43 +01:00
|
|
|
title_case_strings = ['pickerContextMenuEntry', 'contextMenuTemporarilyAllowLargeMediaElements']
|
2014-12-16 17:02:26 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
for alpha2 in sorted(os.listdir(source_locale_dir)):
|
2014-12-16 17:02:26 +01:00
|
|
|
locale_path = pj(source_locale_dir, alpha2, 'messages.json')
|
|
|
|
with open(locale_path, encoding='utf-8') as f:
|
2015-02-05 07:51:03 +01:00
|
|
|
strings = json.load(f, object_pairs_hook=OrderedDict)
|
2014-12-16 17:02:26 +01:00
|
|
|
alpha2 = alpha2.replace('_', '-')
|
2015-02-05 07:51:03 +01:00
|
|
|
descriptions[alpha2] = strings['extShortDesc']['message']
|
|
|
|
del strings['extShortDesc']
|
2014-12-16 17:02:26 +01:00
|
|
|
language_codes.append(alpha2)
|
|
|
|
mkdirs(pj(target_locale_dir, alpha2))
|
|
|
|
locale_path = pj(target_locale_dir, alpha2, 'messages.properties')
|
|
|
|
with open(locale_path, 'wt', encoding='utf-8', newline='\n') as f:
|
2015-02-05 07:51:03 +01:00
|
|
|
for string_name in strings:
|
|
|
|
string = strings[string_name]['message']
|
|
|
|
if alpha2 == 'en' and string_name in title_case_strings:
|
|
|
|
string = string.title()
|
2014-12-16 17:02:26 +01:00
|
|
|
f.write(string_name)
|
2014-12-20 09:27:53 +01:00
|
|
|
f.write(u'=')
|
2015-02-05 07:51:03 +01:00
|
|
|
f.write(string.replace('\n', r'\n'))
|
2014-12-20 09:27:53 +01:00
|
|
|
f.write(u'\n')
|
2014-12-16 17:02:26 +01:00
|
|
|
|
|
|
|
# generate chrome.manifest file
|
|
|
|
chrome_manifest = pj(build_dir, 'chrome.manifest')
|
|
|
|
|
|
|
|
with open(chrome_manifest, 'at', encoding='utf-8', newline='\n') as f:
|
2015-04-09 13:15:14 +02:00
|
|
|
f.write(u'\nlocale ublock0 en ./locale/en/\n')
|
2014-12-16 17:02:26 +01:00
|
|
|
for alpha2 in language_codes:
|
2015-01-27 12:44:35 +01:00
|
|
|
if alpha2 == 'en':
|
|
|
|
continue
|
2015-04-09 13:15:14 +02:00
|
|
|
f.write(u'locale ublock0 ' + alpha2 + ' ./locale/' + alpha2 + '/\n')
|
2014-12-16 17:02:26 +01:00
|
|
|
|
|
|
|
rmtree(source_locale_dir)
|
|
|
|
|
|
|
|
# update install.rdf
|
2018-02-23 15:56:45 +01:00
|
|
|
|
|
|
|
chromium_manifest = pj(proj_dir, 'platform', 'chromium', 'manifest.json')
|
2014-12-16 17:02:26 +01:00
|
|
|
with open(chromium_manifest, encoding='utf-8') as m:
|
|
|
|
manifest = json.load(m)
|
|
|
|
|
2018-02-28 18:27:41 +01:00
|
|
|
# Fetch extension version
|
2015-08-12 22:31:19 +02:00
|
|
|
# https://developer.mozilla.org/en-US/Add-ons/AMO/Policy/Maintenance#How_do_I_submit_a_Beta_add-on.3F
|
|
|
|
# "To create a beta channel [...] '(a|alpha|b|beta|pre|rc)\d*$' "
|
2016-08-04 14:24:11 +02:00
|
|
|
|
2018-02-28 18:27:41 +01:00
|
|
|
version = ''
|
|
|
|
with open(version_filepath) as f:
|
|
|
|
version = f.read().strip()
|
2018-02-23 15:56:45 +01:00
|
|
|
match = re.search('^(\d+\.\d+\.\d+)(\.\d+)$', version)
|
2016-08-04 14:24:11 +02:00
|
|
|
if match:
|
2016-08-16 20:04:49 +02:00
|
|
|
buildtype = int(match.group(2)[1:])
|
|
|
|
if buildtype < 100:
|
|
|
|
builttype = 'b' + str(buildtype)
|
|
|
|
else:
|
|
|
|
builttype = 'rc' + str(buildtype - 100)
|
2018-02-28 18:27:41 +01:00
|
|
|
version = match.group(1) + builttype
|
|
|
|
manifest['version'] = version
|
2015-08-12 22:31:19 +02:00
|
|
|
|
2015-01-27 12:44:35 +01:00
|
|
|
manifest['homepage'] = 'https://github.com/gorhill/uBlock'
|
|
|
|
manifest['description'] = descriptions['en']
|
|
|
|
del descriptions['en']
|
2014-12-16 17:02:26 +01:00
|
|
|
|
2017-03-05 14:25:55 +01:00
|
|
|
manifest['localized'] = []
|
2015-01-27 12:44:35 +01:00
|
|
|
t = ' '
|
|
|
|
t3 = 3 * t
|
|
|
|
for alpha2 in descriptions:
|
|
|
|
if alpha2 == 'en':
|
|
|
|
continue
|
|
|
|
manifest['localized'].append(
|
2017-03-10 13:01:57 +01:00
|
|
|
'\n' + t*2 + '<em:localized><Description>\n' +
|
|
|
|
t3 + '<em:locale>' + alpha2 + '</em:locale>\n' +
|
|
|
|
t3 + '<em:name>' + manifest['name'] + '</em:name>\n' +
|
|
|
|
t3 + '<em:description>' + descriptions[alpha2] + '</em:description>\n' +
|
|
|
|
t3 + '<em:creator>' + manifest['author'] + '</em:creator>\n' +
|
2015-01-27 12:44:35 +01:00
|
|
|
# t3 + '<translator>' + ??? + '</translator>\n' +
|
2017-03-10 13:01:57 +01:00
|
|
|
t3 + '<em:homepageURL>' + manifest['homepage'] + '</em:homepageURL>\n' +
|
|
|
|
t*2 + '</Description></em:localized>'
|
2015-01-27 12:44:35 +01:00
|
|
|
)
|
|
|
|
manifest['localized'] = '\n'.join(manifest['localized'])
|
|
|
|
|
|
|
|
install_rdf = pj(build_dir, 'install.rdf')
|
2014-12-16 17:02:26 +01:00
|
|
|
with open(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
|
|
|
|
install_rdf = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.write(install_rdf.format(**manifest))
|