Bug 997772 - Generate suggestedsites.json from locale (r=nalexander)

This commit is contained in:
Lucas Rocha 2014-05-09 09:39:14 +01:00
parent 199c561cf6
commit 2976a51962
5 changed files with 180 additions and 6 deletions

View File

@ -49,6 +49,7 @@ GARBAGE += \
classes.dex \
gecko.ap_ \
res/values/strings.xml \
res/raw/suggestedsites.json \
.aapt.deps \
fennec_ids.txt \
javah.out \
@ -205,9 +206,27 @@ $(ANDROID_GENERATED_RESFILES): $(call mkdir_deps,$(sort $(dir $(ANDROID_GENERATE
# makefile, and trying to work around the lack of information, we
# force a rebuild of gecko.ap_ during packaging. See below.
res/values/strings.xml: FORCE
# Since the sub-Make is forced, it doesn't matter that we touch the
# target file before the command. If in the future we stop forcing
# the sub-Make, touching the target file first is better, because the
# sub-Make outputs will be fresher than the target, and not require
# rebuilding. This is all safe because Make removes the target file
# if any recipe command fails. It is crucial that the sub-Make touch
# the target files (those depending on .locales.deps) only when there
# contents have changed; otherwise, this will force rebuild them as
# part of every build.
.locales.deps: FORCE
$(TOUCH) $@
$(MAKE) -C locales
# This .deps pattern saves an invocation of the sub-Make: the single
# invocation generates both strings.xml and suggestedsites.json. The
# trailing semi-colon defines an empty recipe: defining no recipe at
# all causes Make to treat the target differently, in a way that
# defeats our dependencies.
res/values/strings.xml: .locales.deps ;
res/raw/suggestedsites.json: .locales.deps ;
all_resources = \
$(CURDIR)/AndroidManifest.xml \
$(CURDIR)/WebappManifestFragment.xml.frag \

View File

@ -25,11 +25,24 @@ strings-xml-in := $(srcdir)/../strings.xml.in
GARBAGE += $(strings-xml)
libs realchrome:: $(strings-xml)
dir-res-raw := ../res/raw
suggestedsites-json := $(dir-res-raw)/suggestedsites.json
GARBAGE += \
$(suggestedsites-json) \
$(NULL)
libs realchrome:: \
$(strings-xml) \
$(suggestedsites-json) \
$(NULL)
chrome-%:: AB_CD=$*
chrome-%::
@$(MAKE) $(dir-res-values)-$(AB_rCD)/strings.xml AB_CD=$*
chrome-%::
@$(MAKE) \
$(dir-res-values)-$(AB_rCD)/strings.xml \
$(dir-res-raw)-$(AB_rCD)/suggestedsites.json \
AB_CD=$*
# setup the path to bookmarks.inc. copied and tweaked version of MERGE_FILE from config/config.mk
MOBILE_LOCALE_SRCDIR = $(if $(filter en-US,$(AB_CD)),$(topsrcdir)/mobile/locales/en-US,$(or $(realpath $(L10NBASEDIR)),$(abspath $(L10NBASEDIR)))/$(AB_CD)/mobile)
@ -75,3 +88,22 @@ $(dir-strings-xml)/strings.xml: $(strings-xml-preqs)
-DSYNCSTRINGSPATH='$(SYNCSTRINGSPATH)' \
$< \
-o $@)
suggestedsites-srcdir := $(dir $(abspath $(call MERGE_FILE,suggestedsites/list.txt)))
# Determine the ../res/raw[-*] path. This can be ../res/raw when no
# locale is explicitly specified.
suggestedsites-json-bypath = $(filter %/suggestedsites.json,$(MAKECMDGOALS))
ifeq (,$(strip $(suggestedsites-json-bypath)))
suggestedsites-json-bypath = $(suggestedsites-json)
endif
suggestedsites-dstdir-raw = $(patsubst %/,%,$(dir $(suggestedsites-json-bypath)))
$(suggestedsites-dstdir-raw)/suggestedsites.json: FORCE
$(call py_action,generate_suggestedsites, \
--verbose \
--android-package-name=$(ANDROID_PACKAGE_NAME) \
--resources=$(srcdir)/../resources \
$(if $(filter en-US,$(AB_CD)),,--srcdir=$(suggestedsites-srcdir)) \
--srcdir=$(addprefix $(srcdir)/,en-US/suggestedsites) \
$@)

View File

@ -507,6 +507,7 @@ ANDROID_RES_DIRS += [
]
ANDROID_GENERATED_RESFILES += [
'res/raw/suggestedsites.json',
'res/values/strings.xml',
]

View File

@ -29,11 +29,13 @@ clobber-zip:
# need to kill stage for repacks for now due to the library moves
# in PACKAGE and UNPACKAGE
# also clean up potential left-overs of multi-locale builds, notably
# values-*/strings.xml. Those would be in the way of a single locale build,
# which this target is for
# values-*/strings.xml and raw-*/suggestedsites.json.
# Those would be in the way of a single locale build, which this
# target is for
clobber-stage:
$(RM) -rf $(STAGEDIST)
$(RM) $(DEPTH)/mobile/android/base/res/values-*/strings.xml
$(RM) $(DEPTH)/mobile/android/base/res/raw-*/suggestedsites.json
libs-%:
@$(MAKE) -C $(DEPTH)/mobile/locales libs-$*

View File

@ -0,0 +1,120 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
''' Script to generate the suggestedsites.json file for Fennec.
This script follows these steps:
1. Looks for a 'list.txt' file in one of the given source directories
(see srcdir). The list.txt contains the list of site names, one per line.
2. For each site name found in 'list.txt', it tries to find a matching
.json file in one of the source directories.
3. For each json file, load it and define the respective imageurl
based on a image URL template composed by the target Android package name
and the site name.
4. Join the JSON representation of each site into a JSON array and write
the result to suggestedsites.json on the locale-specific raw resource
directory e.g. raw/suggestedsites.json, raw-pt-rBR/suggestedsites.json.
'''
from __future__ import print_function
import argparse
import json
import sys
import os
from mozbuild.util import (
FileAvoidWrite,
)
from mozpack.files import (
FileFinder,
)
import mozpack.path as mozpath
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', default=False, action='store_true',
help='be verbose')
parser.add_argument('--silent', '-s', default=False, action='store_true',
help='be silent')
parser.add_argument('--android-package-name', metavar='NAME',
required=True,
help='Android package name')
parser.add_argument('--resources', metavar='RESOURCES',
default=None,
help='optional Android resource directory to find drawables in')
parser.add_argument('--srcdir', metavar='SRCDIR',
action='append', required=True,
help='directories to read inputs from, in order of priority')
parser.add_argument('output', metavar='OUTPUT',
help='output')
opts = parser.parse_args(args)
def resolve_filename(filename):
for srcdir in opts.srcdir:
path = mozpath.join(srcdir, filename)
if os.path.exists(path):
return path
return None
# The list.txt file has one site name per line.
names = [s.strip() for s in open(resolve_filename('list.txt'), 'rt').readlines()]
if opts.verbose:
print('Reading {len} suggested sites: {names}'.format(len=len(names), names=names))
# Keep these two in sync.
image_url_template = 'android.resource://%s/drawable/suggestedsites_{name}' % opts.android_package_name
drawables_template = 'drawable*/suggestedsites_{name}.*'
# Load json files corresponding to each site name and define their
# respective image URL.
sites = []
for name in names:
filename = resolve_filename(name + '.json')
if opts.verbose:
print("Reading '{name}' from {filename}"
.format(name=name, filename=filename))
site = json.load(open(filename, 'rt'))
site['imageurl'] = image_url_template.format(name=name)
sites.append(site)
# Now check for existence of an appropriately named drawable. If none
# exists, throw. This stops a locale discovering, at runtime, that the
# corresponding drawable was not added to en-US.
if not opts.resources:
continue
resources = os.path.abspath(opts.resources)
finder = FileFinder(resources)
matches = [p for p, _ in finder.find(drawables_template.format(name=name))]
if not matches:
raise Exception("Could not find drawable in '{resources}' for '{name}'"
.format(resources=resources, name=name))
else:
if opts.verbose:
print("Found {len} drawables in '{resources}' for '{name}': {matches}"
.format(len=len(matches), resources=resources, name=name, matches=matches))
# FileAvoidWrite creates its parent directories.
output = os.path.abspath(opts.output)
fh = FileAvoidWrite(output)
json.dump(sites, fh)
existed, updated = fh.close()
if not opts.silent:
if updated:
print('{output} updated'.format(output=output))
else:
print('{output} already up-to-date'.format(output=output))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))