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

Delta Between Two Patch Sets: cms/bin/generate_static_pages.py

Issue 5242593268989952: Issue 2340 - Don`t generate pages if less than 30% have been translated (Closed)
Left Patch Set: Don`t link to pages that don`t exist Created May 6, 2015, 3:19 p.m.
Right Patch Set: Addressed comments Created May 6, 2015, 5:20 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | cms/converters.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import sys 18 import sys
19 import os 19 import os
20 import re 20 import re
21 import errno 21 import errno
22 import codecs 22 import codecs
23 import ConfigParser 23 import ConfigParser
24 import functools
25 import logging 24 import logging
26 25
27 from cms.utils import get_page_params, process_page 26 from cms.utils import get_page_params, process_page
28 from cms.sources import MercurialSource 27 from cms.sources import MercurialSource
29 28
30 MIN_TRANSLATED = 0.3 29 MIN_TRANSLATED = 0.3
31 30
32 def memoize(func): 31 def memoize(func):
33 memoized = {} 32 memoized = {}
34 def wrapper(*args): 33 def wrapper(*args):
35 try: 34 try:
36 return memoized[args] 35 return memoized[args]
37 except KeyError: 36 except KeyError:
38 return memoized.setdefault(args, func(*args)) 37 return memoized.setdefault(args, func(*args))
39 wrapper.original = func 38 wrapper.clear_cache = memoized.clear
Sebastian Noack 2015/05/06 16:15:30 I'm not sure if I like that hack. Note that it wou
Wladimir Palant 2015/05/06 17:21:59 True, I don't like passing in a dictionary either
Sebastian Noack 2015/05/06 19:12:15 Well, allowing to pass in a dict-like object, also
Wladimir Palant 2015/05/06 19:20:35 Thank you. Indeed, I do feel that the current appr
40 return wrapper 39 return wrapper
41 40
42 def generate_pages(repo, output_dir): 41 def generate_pages(repo, output_dir):
43 known_files = set() 42 known_files = set()
44 43
45 def write_file(path_parts, contents, binary=False): 44 def write_file(path_parts, contents, binary=False):
46 encoding = None if binary else "utf-8" 45 encoding = None if binary else "utf-8"
47 outfile = os.path.join(output_dir, *path_parts) 46 outfile = os.path.join(output_dir, *path_parts)
48 if outfile in known_files: 47 if outfile in known_files:
49 logging.warning("File %s has multiple sources", outfile) 48 logging.warning("File %s has multiple sources", outfile)
(...skipping 24 matching lines...) Expand all
74 source.read_include = memoize(source.read_include) 73 source.read_include = memoize(source.read_include)
75 74
76 config = source.read_config() 75 config = source.read_config()
77 defaultlocale = config.get("general", "defaultlocale") 76 defaultlocale = config.get("general", "defaultlocale")
78 locales = list(source.list_locales()) 77 locales = list(source.list_locales())
79 if defaultlocale not in locales: 78 if defaultlocale not in locales:
80 locales.append(defaultlocale) 79 locales.append(defaultlocale)
81 80
82 # First pass: compile the list of pages with given translation level 81 # First pass: compile the list of pages with given translation level
83 pagelist = set() 82 pagelist = set()
83 blacklist = set()
84 for page, format in source.list_pages(): 84 for page, format in source.list_pages():
85 for locale in locales: 85 for locale in locales:
86 if locale == defaultlocale: 86 if locale == defaultlocale:
87 pagelist.add((locale, page)) 87 pagelist.add((locale, page))
88 elif source.has_locale(locale, page): 88 elif source.has_locale(locale, page):
89 params = get_page_params(source, locale, page, format) 89 params = get_page_params(source, locale, page, format)
90 if params["translation_ratio"] >= MIN_TRANSLATED: 90 if params["translation_ratio"] >= MIN_TRANSLATED:
91 pagelist.add((locale, page)) 91 pagelist.add((locale, page))
92 else:
93 blacklist.add((locale, page))
92 94
93 # Override existance check to avoid linking to pages we don't generate 95 # Override existance check to avoid linking to pages we don't generate
94 def has_locale(orig_function, locale, page): 96 orig_has_locale = source.has_locale
97 def has_locale(locale, page):
95 try: 98 try:
96 page = config.get("locale_overrides", page) 99 page = config.get("locale_overrides", page)
97 except ConfigParser.Error: 100 except ConfigParser.Error:
98 pass 101 pass
99 if (defaultlocale, page) in pagelist: 102 if (locale, page) in blacklist:
100 return locale != defaultlocale and (locale, page) in pagelist 103 return False
101 else: 104 return orig_has_locale(locale, page)
Sebastian Noack 2015/05/06 16:15:30 Nit: Redundant else statement. (Yes, I know you li
Wladimir Palant 2015/05/06 17:21:59 Done and made the logic here slightly more obvious
102 return orig_function(locale, page) 105 source.has_locale = has_locale
103 source.has_locale = functools.partial(has_locale, source.has_locale) 106 source.resolve_link.clear_cache()
Sebastian Noack 2015/05/06 16:15:30 It took me a while to understand what you are doin
Wladimir Palant 2015/05/06 17:21:59 I thought you were a fan of clever solutions :)
104 source.resolve_link = memoize(source.resolve_link.original)
105 107
106 # Second pass: actually generate pages this time 108 # Second pass: actually generate pages this time
107 for locale, page in pagelist: 109 for locale, page in pagelist:
108 pagedata = process_page(source, locale, page) 110 pagedata = process_page(source, locale, page)
109 111
110 # Make sure links to static files are versioned 112 # Make sure links to static files are versioned
111 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.v ersion, pagedata) 113 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.v ersion, pagedata)
112 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % source.ve rsion, pagedata) 114 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % source.ve rsion, pagedata)
113 pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.vers ion, pagedata) 115 pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.vers ion, pagedata)
114 116
(...skipping 20 matching lines...) Expand all
135 os.rmdir(path) 137 os.rmdir(path)
136 remove_unknown(output_dir) 138 remove_unknown(output_dir)
137 139
138 if __name__ == "__main__": 140 if __name__ == "__main__":
139 if len(sys.argv) < 3: 141 if len(sys.argv) < 3:
140 print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0] 142 print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0]
141 sys.exit(1) 143 sys.exit(1)
142 144
143 repo, output_dir = sys.argv[1:3] 145 repo, output_dir = sys.argv[1:3]
144 generate_pages(repo, output_dir) 146 generate_pages(repo, output_dir)
LEFTRIGHT
« no previous file | cms/converters.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld