OLD | NEW |
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 logging | 24 import logging |
24 | 25 |
25 from cms.utils import process_page | 26 from cms.utils import get_page_params, process_page |
26 from cms.sources import MercurialSource | 27 from cms.sources import MercurialSource |
27 | 28 |
| 29 MIN_TRANSLATED = 0.3 |
| 30 |
28 def memoize(func): | 31 def memoize(func): |
29 memoized = {} | 32 memoized = {} |
30 def wrapper(*args): | 33 def wrapper(*args): |
31 try: | 34 try: |
32 return memoized[args] | 35 return memoized[args] |
33 except KeyError: | 36 except KeyError: |
34 return memoized.setdefault(args, func(*args)) | 37 return memoized.setdefault(args, func(*args)) |
| 38 wrapper.clear_cache = memoized.clear |
35 return wrapper | 39 return wrapper |
36 | 40 |
37 def generate_pages(repo, output_dir): | 41 def generate_pages(repo, output_dir): |
38 known_files = set() | 42 known_files = set() |
39 | 43 |
40 def write_file(path_parts, contents, binary=False): | 44 def write_file(path_parts, contents, binary=False): |
41 encoding = None if binary else "utf-8" | 45 encoding = None if binary else "utf-8" |
42 outfile = os.path.join(output_dir, *path_parts) | 46 outfile = os.path.join(output_dir, *path_parts) |
43 if outfile in known_files: | 47 if outfile in known_files: |
44 logging.warning("File %s has multiple sources", outfile) | 48 logging.warning("File %s has multiple sources", outfile) |
(...skipping 21 matching lines...) Expand all Loading... |
66 source.read_config = memoize(source.read_config) | 70 source.read_config = memoize(source.read_config) |
67 source.read_template = memoize(source.read_template) | 71 source.read_template = memoize(source.read_template) |
68 source.read_locale = memoize(source.read_locale) | 72 source.read_locale = memoize(source.read_locale) |
69 source.read_include = memoize(source.read_include) | 73 source.read_include = memoize(source.read_include) |
70 | 74 |
71 config = source.read_config() | 75 config = source.read_config() |
72 defaultlocale = config.get("general", "defaultlocale") | 76 defaultlocale = config.get("general", "defaultlocale") |
73 locales = list(source.list_locales()) | 77 locales = list(source.list_locales()) |
74 if defaultlocale not in locales: | 78 if defaultlocale not in locales: |
75 locales.append(defaultlocale) | 79 locales.append(defaultlocale) |
| 80 |
| 81 # First pass: compile the list of pages with given translation level |
| 82 pagelist = set() |
| 83 blacklist = set() |
76 for page, format in source.list_pages(): | 84 for page, format in source.list_pages(): |
77 for locale in locales: | 85 for locale in locales: |
78 if locale == defaultlocale or source.has_locale(locale, page): | 86 if locale == defaultlocale: |
79 pagedata = process_page(source, locale, page, format) | 87 pagelist.add((locale, page)) |
| 88 elif source.has_locale(locale, page): |
| 89 params = get_page_params(source, locale, page, format) |
| 90 if params["translation_ratio"] >= MIN_TRANSLATED: |
| 91 pagelist.add((locale, page)) |
| 92 else: |
| 93 blacklist.add((locale, page)) |
80 | 94 |
81 # Make sure links to static files are versioned | 95 # Override existance check to avoid linking to pages we don't generate |
82 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % sour
ce.version, pagedata) | 96 orig_has_locale = source.has_locale |
83 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % sourc
e.version, pagedata) | 97 def has_locale(locale, page): |
84 pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.
version, pagedata) | 98 try: |
| 99 page = config.get("locale_overrides", page) |
| 100 except ConfigParser.Error: |
| 101 pass |
| 102 if (locale, page) in blacklist: |
| 103 return False |
| 104 return orig_has_locale(locale, page) |
| 105 source.has_locale = has_locale |
| 106 source.resolve_link.clear_cache() |
85 | 107 |
86 write_file([locale] + page.split("/"), pagedata) | 108 # Second pass: actually generate pages this time |
| 109 for locale, page in pagelist: |
| 110 pagedata = process_page(source, locale, page) |
| 111 |
| 112 # Make sure links to static files are versioned |
| 113 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.v
ersion, pagedata) |
| 114 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % source.ve
rsion, pagedata) |
| 115 pagedata = re.sub(r'(<img\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % source.vers
ion, pagedata) |
| 116 |
| 117 write_file([locale] + page.split("/"), pagedata) |
87 | 118 |
88 for filename in source.list_localizable_files(): | 119 for filename in source.list_localizable_files(): |
89 for locale in locales: | 120 for locale in locales: |
90 if source.has_localizable_file(locale, filename): | 121 if source.has_localizable_file(locale, filename): |
91 filedata = source.read_localizable_file(locale, filename) | 122 filedata = source.read_localizable_file(locale, filename) |
92 write_file([locale] + filename.split("/"), filedata, binary=True) | 123 write_file([locale] + filename.split("/"), filedata, binary=True) |
93 | 124 |
94 for filename in source.list_static(): | 125 for filename in source.list_static(): |
95 write_file(filename.split("/"), source.read_static(filename), binary=True) | 126 write_file(filename.split("/"), source.read_static(filename), binary=True) |
96 | 127 |
97 def remove_unknown(dir): | 128 def remove_unknown(dir): |
98 files = os.listdir(dir) | 129 files = os.listdir(dir) |
99 for filename in files: | 130 for filename in files: |
100 path = os.path.join(dir, filename) | 131 path = os.path.join(dir, filename) |
101 if os.path.isfile(path) and path not in known_files: | 132 if os.path.isfile(path) and path not in known_files: |
102 os.remove(path) | 133 os.remove(path) |
103 elif os.path.isdir(path): | 134 elif os.path.isdir(path): |
104 remove_unknown(path) | 135 remove_unknown(path) |
105 if not os.listdir(path): | 136 if not os.listdir(path): |
106 os.rmdir(path) | 137 os.rmdir(path) |
107 remove_unknown(output_dir) | 138 remove_unknown(output_dir) |
108 | 139 |
109 if __name__ == "__main__": | 140 if __name__ == "__main__": |
110 if len(sys.argv) < 3: | 141 if len(sys.argv) < 3: |
111 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] |
112 sys.exit(1) | 143 sys.exit(1) |
113 | 144 |
114 repo, output_dir = sys.argv[1:3] | 145 repo, output_dir = sys.argv[1:3] |
115 generate_pages(repo, output_dir) | 146 generate_pages(repo, output_dir) |
OLD | NEW |