Skip to content

Instantly share code, notes, and snippets.

@flodolo
Created October 14, 2016 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flodolo/d969c1f7f24dd6b9b646cfaf8f38200c to your computer and use it in GitHub Desktop.
Save flodolo/d969c1f7f24dd6b9b646cfaf8f38200c to your computer and use it in GitHub Desktop.
Thunderbird list.json verification
#! /usr/bin/env python
import argparse
import glob
import json
import os
import sys
import urllib2
def diff(a, b):
b = set(b)
return [aa for aa in a if aa not in b]
def analyze_differences(supported_locales, search_locales):
''' Analyze searchplugin differences between the two lists '''
missing_locales = diff(supported_locales, search_locales)
print '\nLocales available in supported locales but not in list.json:',
if missing_locales:
missing_locales.sort()
print ', '.join(missing_locales)
else:
print '(none)'
added_locales = diff(search_locales, supported_locales)
print '\nLocales available in list.json but not in supported locales:',
if added_locales:
added_locales.sort()
print ', '.join(added_locales)
else:
print '(none)'
def main():
# Parse command line options
parser = argparse.ArgumentParser()
parser.add_argument('search_file', help='Path to .json file')
args = parser.parse_args()
search_file = os.path.abspath(args.search_file)
# Path to JSON file with p12n settings. You can point to a local file if you
# plan to run this script often (file is over 2 MB)
# e.g. p12n_json_url = 'file://{0}'.format(os.path.abspath('./searchplugins.json'))
p12n_json_url = 'https://l10n.mozilla-community.org/~flod/p12n/searchplugins.json'
try:
p12n_json = json.load(urllib2.urlopen(p12n_json_url))
except Exception as e:
print 'Error reading {0}:'.format(p12n_json_url), e
# Read full JSON data
with open(search_file) as data_file:
search_json_data = json.load(data_file)
search_json_locales = search_json_data['locales'].keys()
# Get list of supported locales From http://hg.mozilla.org/releases/comm-aurora/raw-file/default/mail/locales/all-locales
try:
url = 'http://hg.mozilla.org/releases/comm-aurora/raw-file/default/mail/locales/all-locales'
response = urllib2.urlopen(url)
supported_locales = ['en-US']
for locale in response:
supported_locales.append(locale.replace('\n', ''))
except Exception as e:
print 'Error reading {0}:'.format(url), e
# Analyze differences
analyze_differences(supported_locales, search_json_locales)
# Create a list of locales to analyze (elements in common), exclude en-US
common_locales = list(set(supported_locales).intersection(search_json_locales))
common_locales.sort()
common_locales.remove('en-US')
for locale in common_locales:
# Map searchplugin names to filenames
if 'aurora' in p12n_json['locales'][locale]['mail']:
p12n_searchplugins = p12n_json['locales'][locale]['mail']['aurora']['searchplugins'];
else:
print '{0} is not available for Thunderbird Aurora'.format(locale)
continue
name_mapping = {}
for sp_filename, sp_data in p12n_searchplugins.iteritems():
name_mapping[sp_data['name']] = sp_filename
# Get searchplugins explicitly set in search_order
search_order = p12n_json['locales'][locale]['mail']['aurora']['p12n']['searchorder'];
ordered_searchplugins = []
for x in range(1,10):
search_index = str(x)
if search_index in search_order:
if search_order[search_index] != '':
ordered_searchplugins.append(search_order[search_index])
other_searchplugins = []
for sp_filename, sp_data in p12n_searchplugins.iteritems():
if sp_data['name'] not in ordered_searchplugins:
other_searchplugins.append(sp_data['name'])
# Case insensitive sort
other_searchplugins = sorted(other_searchplugins, key=lambda s: s.lower())
# Map shortNames to file names
all_searchplugins = []
errors = []
for sp in ordered_searchplugins + other_searchplugins:
try:
all_searchplugins.append(name_mapping[sp])
except Exception as e:
errors.append('* can\'t find name for {0} (set in search.order, but not shipping)'.format(sp))
search_json_searchplugins = search_json_data['locales'][locale]['default']['visibleDefaultEngines']
if search_json_searchplugins != all_searchplugins:
print '\nLocale: {0}'.format(locale)
print 'There are differences.'
print 'list.json: ', ', '.join(search_json_searchplugins)
print 'expected settings:', ', '.join(all_searchplugins)
if errors:
print '\nERRORS:'
print '\n'.join(errors)
#sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment