Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: cms/bin/generate_static_pages.py

Issue 5242593268989952: Issue 2340 - Don`t generate pages if less than 30% have been translated (Closed)
Patch Set: Addressed comments Created May 6, 2015, 5:20 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cms/converters.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/bin/generate_static_pages.py
===================================================================
--- a/cms/bin/generate_static_pages.py
+++ b/cms/bin/generate_static_pages.py
@@ -15,28 +15,32 @@
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import re
import errno
import codecs
+import ConfigParser
import logging
-from cms.utils import process_page
+from cms.utils import get_page_params, process_page
from cms.sources import MercurialSource
+MIN_TRANSLATED = 0.3
+
def memoize(func):
memoized = {}
def wrapper(*args):
try:
return memoized[args]
except KeyError:
return memoized.setdefault(args, func(*args))
+ wrapper.clear_cache = memoized.clear
return wrapper
def generate_pages(repo, output_dir):
known_files = set()
def write_file(path_parts, contents, binary=False):
encoding = None if binary else "utf-8"
outfile = os.path.join(output_dir, *path_parts)
@@ -68,27 +72,54 @@ def generate_pages(repo, output_dir):
source.read_locale = memoize(source.read_locale)
source.read_include = memoize(source.read_include)
config = source.read_config()
defaultlocale = config.get("general", "defaultlocale")
locales = list(source.list_locales())
if defaultlocale not in locales:
locales.append(defaultlocale)
+
+ # First pass: compile the list of pages with given translation level
+ pagelist = set()
+ blacklist = set()
for page, format in source.list_pages():
for locale in locales:
- if locale == defaultlocale or source.has_locale(locale, page):
- pagedata = process_page(source, locale, page, format)
+ if locale == defaultlocale:
+ pagelist.add((locale, page))
+ elif source.has_locale(locale, page):
+ params = get_page_params(source, locale, page, format)
+ if params["translation_ratio"] >= MIN_TRANSLATED:
+ pagelist.add((locale, page))
+ else:
+ blacklist.add((locale, page))
- # Make sure links to static files are versioned
- pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
- pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
- pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
+ # Override existance check to avoid linking to pages we don't generate
+ orig_has_locale = source.has_locale
+ def has_locale(locale, page):
+ try:
+ page = config.get("locale_overrides", page)
+ except ConfigParser.Error:
+ pass
+ if (locale, page) in blacklist:
+ return False
+ return orig_has_locale(locale, page)
+ source.has_locale = has_locale
+ source.resolve_link.clear_cache()
- write_file([locale] + page.split("/"), pagedata)
+ # Second pass: actually generate pages this time
+ for locale, page in pagelist:
+ pagedata = process_page(source, locale, page)
+
+ # Make sure links to static files are versioned
+ pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
+ pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
+ pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.version, pagedata)
+
+ write_file([locale] + page.split("/"), pagedata)
for filename in source.list_localizable_files():
for locale in locales:
if source.has_localizable_file(locale, filename):
filedata = source.read_localizable_file(locale, filename)
write_file([locale] + filename.split("/"), filedata, binary=True)
for filename in source.list_static():
« no previous file with comments | « no previous file | cms/converters.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld