| OLD | NEW |
| 1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2017 eyeo GmbH | 2 # Copyright (C) 2006-2017 eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 |
| 16 import sys | |
| 17 import os | 16 import os |
| 18 import re | 17 import re |
| 19 import errno | 18 import errno |
| 20 import codecs | 19 import codecs |
| 21 import ConfigParser | 20 import ConfigParser |
| 22 import logging | 21 import logging |
| 22 from argparse import ArgumentParser |
| 23 | 23 |
| 24 from cms.utils import get_page_params, process_page | 24 from cms.utils import get_page_params, process_page |
| 25 from cms.sources import MercurialSource | 25 from cms.sources import MercurialSource |
| 26 | 26 |
| 27 MIN_TRANSLATED = 0.3 | 27 MIN_TRANSLATED = 0.3 |
| 28 | 28 |
| 29 | 29 |
| 30 def memoize(func): | 30 def memoize(func): |
| 31 memoized = {} | 31 memoized = {} |
| 32 | 32 |
| 33 def wrapper(*args): | 33 def wrapper(*args): |
| 34 try: | 34 try: |
| 35 return memoized[args] | 35 return memoized[args] |
| 36 except KeyError: | 36 except KeyError: |
| 37 return memoized.setdefault(args, func(*args)) | 37 return memoized.setdefault(args, func(*args)) |
| 38 wrapper.clear_cache = memoized.clear | 38 wrapper.clear_cache = memoized.clear |
| 39 return wrapper | 39 return wrapper |
| 40 | 40 |
| 41 | 41 |
| 42 def generate_pages(repo, output_dir): | 42 def generate_pages(repo, output_dir, revision): |
| 43 known_files = set() | 43 known_files = set() |
| 44 | 44 |
| 45 def write_file(path_parts, contents, binary=False): | 45 def write_file(path_parts, contents, binary=False): |
| 46 encoding = None if binary else 'utf-8' | 46 encoding = None if binary else 'utf-8' |
| 47 outfile = os.path.join(output_dir, *path_parts) | 47 outfile = os.path.join(output_dir, *path_parts) |
| 48 if outfile in known_files: | 48 if outfile in known_files: |
| 49 logging.warning('File %s has multiple sources', outfile) | 49 logging.warning('File %s has multiple sources', outfile) |
| 50 return | 50 return |
| 51 known_files.add(outfile) | 51 known_files.add(outfile) |
| 52 | 52 |
| 53 if os.path.exists(outfile): | 53 if os.path.exists(outfile): |
| 54 with codecs.open(outfile, 'rb', encoding=encoding) as handle: | 54 with codecs.open(outfile, 'rb', encoding=encoding) as handle: |
| 55 if handle.read() == contents: | 55 if handle.read() == contents: |
| 56 return | 56 return |
| 57 | 57 |
| 58 try: | 58 try: |
| 59 os.makedirs(os.path.dirname(outfile)) | 59 os.makedirs(os.path.dirname(outfile)) |
| 60 except OSError as e: | 60 except OSError as e: |
| 61 if e.errno != errno.EEXIST: | 61 if e.errno != errno.EEXIST: |
| 62 raise | 62 raise |
| 63 | 63 |
| 64 with codecs.open(outfile, 'wb', encoding=encoding) as handle: | 64 with codecs.open(outfile, 'wb', encoding=encoding) as handle: |
| 65 handle.write(contents) | 65 handle.write(contents) |
| 66 | 66 |
| 67 with MercurialSource(repo) as source: | 67 with MercurialSource(repo, revision) as source: |
| 68 # Cache the result for some functions - we can assume here that the data | 68 # Cache the result for some functions - we can assume here that the data |
| 69 # never changes | 69 # never changes |
| 70 source.resolve_link = memoize(source.resolve_link) | 70 source.resolve_link = memoize(source.resolve_link) |
| 71 source.read_config = memoize(source.read_config) | 71 source.read_config = memoize(source.read_config) |
| 72 source.read_template = memoize(source.read_template) | 72 source.read_template = memoize(source.read_template) |
| 73 source.read_locale = memoize(source.read_locale) | 73 source.read_locale = memoize(source.read_locale) |
| 74 source.read_include = memoize(source.read_include) | 74 source.read_include = memoize(source.read_include) |
| 75 source.exec_file = memoize(source.exec_file) | 75 source.exec_file = memoize(source.exec_file) |
| 76 | 76 |
| 77 config = source.read_config() | 77 config = source.read_config() |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 path = os.path.join(dir, filename) | 137 path = os.path.join(dir, filename) |
| 138 if os.path.isfile(path) and path not in known_files: | 138 if os.path.isfile(path) and path not in known_files: |
| 139 os.remove(path) | 139 os.remove(path) |
| 140 elif os.path.isdir(path): | 140 elif os.path.isdir(path): |
| 141 remove_unknown(path) | 141 remove_unknown(path) |
| 142 if not os.listdir(path): | 142 if not os.listdir(path): |
| 143 os.rmdir(path) | 143 os.rmdir(path) |
| 144 remove_unknown(output_dir) | 144 remove_unknown(output_dir) |
| 145 | 145 |
| 146 if __name__ == '__main__': | 146 if __name__ == '__main__': |
| 147 if len(sys.argv) < 3: | 147 parser = ArgumentParser() |
| 148 print >>sys.stderr, 'Usage: %s source_repository output_dir' % sys.argv[
0] | 148 parser.add_argument('-r', '--rev', |
| 149 sys.exit(1) | 149 help='see "hg help revisions" for more details', |
| 150 | 150 default='default') |
| 151 repo, output_dir = sys.argv[1:3] | 151 parser.add_argument('source', help="Path to your website's repository") |
| 152 generate_pages(repo, output_dir) | 152 parser.add_argument('output', help='Path to the desired output directory') |
| 153 args = parser.parse_args() |
| 154 generate_pages(args.source, args.output, args.rev) |
| OLD | NEW |