| Left: | ||
| Right: |
| 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-present eyeo GmbH | 2 # Copyright (C) 2006-present 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 os | 16 import os |
| 17 import re | 17 import re |
| 18 import errno | 18 import errno |
| 19 import codecs | 19 import codecs |
| 20 import ConfigParser | 20 import ConfigParser |
| 21 import logging | 21 import logging |
| 22 from argparse import ArgumentParser | 22 from argparse import ArgumentParser |
| 23 import shutil | |
| 23 | 24 |
| 24 from cms.utils import get_page_params, process_page | 25 from cms.utils import get_page_params, process_page |
| 25 from cms.sources import create_source | 26 from cms.sources import create_source |
| 26 | 27 |
| 27 MIN_TRANSLATED = 0.3 | 28 MIN_TRANSLATED = 0.3 |
| 28 | 29 |
| 29 | 30 |
| 30 def generate_pages(repo, output_dir): | 31 def generate_pages(repo, output_dir): |
| 31 known_files = set() | 32 known_files = set() |
| 32 | 33 |
| 33 def write_file(path_parts, contents, binary=False): | 34 def write_file(path_parts, contents, binary=False): |
| 34 encoding = None if binary else 'utf-8' | 35 encoding = None if binary else 'utf-8' |
| 35 outfile = os.path.join(output_dir, *path_parts) | 36 outfile = os.path.join(output_dir, *path_parts) |
| 36 if outfile in known_files: | 37 if outfile in known_files: |
| 37 logging.warning('File %s has multiple sources', outfile) | 38 logging.warning('File %s has multiple sources', outfile) |
| 38 return | 39 return |
| 39 known_files.add(outfile) | 40 known_files.add(outfile) |
| 40 | 41 |
| 41 if os.path.exists(outfile): | 42 if os.path.isfile(outfile): |
|
Vasily Kuznetsov
2018/09/24 11:58:47
Are you sure if the two branches of this if statem
Tudor Avram
2018/09/24 14:04:51
Also, one more thing: do we want to raise an error
Vasily Kuznetsov
2018/09/24 14:42:23
I think if there's some kind of weird object in th
Tudor Avram
2018/09/24 16:10:00
Done.
| |
| 42 with codecs.open(outfile, 'rb', encoding=encoding) as handle: | 43 with codecs.open(outfile, 'rb', encoding=encoding) as handle: |
| 43 if handle.read() == contents: | 44 if handle.read() == contents: |
| 44 return | 45 return |
| 46 elif os.path.isdir(outfile): | |
| 47 shutil.rmtree(outfile) | |
| 45 | 48 |
| 46 try: | 49 try: |
|
Vasily Kuznetsov
2018/09/24 11:58:47
I think it would be good to extract this whole blo
Tudor Avram
2018/09/24 13:26:45
Yeah, I think it makes sense to put this into a di
Vasily Kuznetsov
2018/09/24 14:42:23
I don't expect many directories to be created when
Tudor Avram
2018/09/24 16:10:00
Done.
| |
| 47 os.makedirs(os.path.dirname(outfile)) | 50 os.makedirs(os.path.dirname(outfile)) |
| 48 except OSError as e: | 51 except OSError as e: |
| 49 if e.errno != errno.EEXIST: | 52 if e.errno != errno.EEXIST: |
| 50 raise | 53 raise |
| 54 path_so_far = output_dir | |
| 55 for part in path_parts[:-1]: | |
| 56 path_so_far = os.path.join(path_so_far, part) | |
| 57 if os.path.isfile(path_so_far): | |
| 58 os.remove(path_so_far) | |
| 59 os.makedirs(os.path.dirname(outfile)) | |
| 60 break | |
| 51 | 61 |
| 52 with codecs.open(outfile, 'wb', encoding=encoding) as handle: | 62 with codecs.open(outfile, 'wb', encoding=encoding) as handle: |
| 53 handle.write(contents) | 63 handle.write(contents) |
| 54 | 64 |
| 55 with create_source(repo, cached=True) as source: | 65 with create_source(repo, cached=True) as source: |
| 56 config = source.read_config() | 66 config = source.read_config() |
| 57 defaultlocale = config.get('general', 'defaultlocale') | 67 defaultlocale = config.get('general', 'defaultlocale') |
| 58 locales = list(source.list_locales()) | 68 locales = list(source.list_locales()) |
| 59 if defaultlocale not in locales: | 69 if defaultlocale not in locales: |
| 60 locales.append(defaultlocale) | 70 locales.append(defaultlocale) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 os.rmdir(path) | 132 os.rmdir(path) |
| 123 remove_unknown(output_dir) | 133 remove_unknown(output_dir) |
| 124 | 134 |
| 125 | 135 |
| 126 if __name__ == '__main__': | 136 if __name__ == '__main__': |
| 127 parser = ArgumentParser('Convert website source to static website') | 137 parser = ArgumentParser('Convert website source to static website') |
| 128 parser.add_argument('source', help="Path to website's repository") | 138 parser.add_argument('source', help="Path to website's repository") |
| 129 parser.add_argument('output', help='Path to desired output directory') | 139 parser.add_argument('output', help='Path to desired output directory') |
| 130 args = parser.parse_args() | 140 args = parser.parse_args() |
| 131 generate_pages(args.source, args.output) | 141 generate_pages(args.source, args.output) |
| OLD | NEW |