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

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

Issue 29887585: Issue #5352 - generate_static_pages cannot deal with directories being turned into regular pages (Closed)
Left Patch Set: Addressed comments from Patch #1 Created Sept. 24, 2018, 4:05 p.m.
Right Patch Set: Addressed comments from Patch Set #4 Created Oct. 5, 2018, 11:28 a.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 | « .hgignore ('k') | tests/expected_output/en/sitemap » ('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 # 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 codecs 18 import codecs
19 import ConfigParser 19 import ConfigParser
20 import logging 20 import logging
21 from argparse import ArgumentParser 21 from argparse import ArgumentParser
22 import shutil 22 import shutil
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 create_source 25 from cms.sources import create_source
26 26
27 MIN_TRANSLATED = 0.3 27 MIN_TRANSLATED = 0.3
28 28
29 29
30 def resolve_dirs(partial_path, path_parts): 30 def ensure_dirs(partial_path, path_parts):
Vasily Kuznetsov 2018/09/27 11:05:07 It seems like resolve_dirs doesn't quite capture w
Tudor Avram 2018/10/04 06:34:18 Done.
31 """Create an entire path of directories. 31 """Create an entire path of directories.
32 32
33 This is a recursive function, that also treats these special cases, if 33 This is a recursive function, that also treats these special cases, if
34 the partial path we reached so far exists and is: 34 the partial path we reached so far exists and is:
35 35
36 1. a directory - do nothing, just move on, 36 1. a directory - do nothing, just move on,
37 2. a file - remove the file, create a directory in its place, and move on. 37 2. a file - remove the file, create a directory in its place, and move on.
38 3. neither a file, nor a directory - raise and exception. 38 3. neither a file, nor a directory - raise and exception.
39 39
40 Parameters 40 Parameters
(...skipping 15 matching lines...) Expand all
56 elif os.path.exists(partial_path) and not os.path.isdir(partial_path): 56 elif os.path.exists(partial_path) and not os.path.isdir(partial_path):
57 raise Exception('The object at {} is not recognisable! It is neither ' 57 raise Exception('The object at {} is not recognisable! It is neither '
58 'a file, nor a directory!'.format(partial_path)) 58 'a file, nor a directory!'.format(partial_path))
59 59
60 if not os.path.isdir(partial_path): 60 if not os.path.isdir(partial_path):
61 os.mkdir(partial_path) 61 os.mkdir(partial_path)
62 62
63 if len(path_parts) == 0: 63 if len(path_parts) == 0:
64 return 64 return
65 65
66 resolve_dirs(os.path.join(partial_path, path_parts[0]), path_parts[1:]) 66 ensure_dirs(os.path.join(partial_path, path_parts[0]), path_parts[1:])
67 67
68 68
69 def is_in_previous_version(path, new_contents, encoding): 69 def is_in_previous_version(path, new_contents, encoding):
70 """Test if a file we try to create already is in the output directory. 70 """Test if a file we try to create already is in the output directory.
71 71
72 It tests if the pre-existent file has all the expected content. 72 It tests if the pre-existent file has all the expected content.
73 It also handles the following two cases: 73 It also handles the following two cases:
74 74
75 1. The path is a directory - If this happens, it removes the directory from 75 1. The path is a directory - If this happens, it removes the directory from
76 the file tree. 76 the file tree.
(...skipping 28 matching lines...) Expand all
105 return True 105 return True
106 elif os.path.isdir(path): 106 elif os.path.isdir(path):
107 shutil.rmtree(path) 107 shutil.rmtree(path)
108 elif os.path.exists(path): 108 elif os.path.exists(path):
109 raise Exception('The object at {} is not recognisable! It is ' 109 raise Exception('The object at {} is not recognisable! It is '
110 'neither a file, nor a directory!'.format(path)) 110 'neither a file, nor a directory!'.format(path))
111 111
112 return False 112 return False
113 113
114 114
115 def generate_pages(repo, output_dir): 115 def generate_pages(repo, output_dir):
Tudor Avram 2018/10/04 06:34:18 Nit: Also, does `repo` really make sense here now?
Vasily Kuznetsov 2018/10/05 09:40:43 Yes, makes sense, `repo` is a confusing name. Perh
116 known_files = set() 116 known_files = set()
117 117
118 def write_file(path_parts, contents, binary=False): 118 def write_file(path_parts, contents, binary=False):
119 encoding = None if binary else 'utf-8' 119 encoding = None if binary else 'utf-8'
120 outfile = os.path.join(output_dir, *path_parts) 120 outfile = os.path.join(output_dir, *path_parts)
121 if outfile in known_files: 121 if outfile in known_files:
122 logging.warning('File %s has multiple sources', outfile) 122 logging.warning('File %s has multiple sources', outfile)
123 return 123 return
124 known_files.add(outfile) 124 known_files.add(outfile)
125 125
126 if is_in_previous_version(outfile, contents, encoding): 126 if is_in_previous_version(outfile, contents, encoding):
127 return 127 return
128 128
129 resolve_dirs(output_dir, path_parts[:-1]) 129 ensure_dirs(output_dir, path_parts[:-1])
130 130
131 with codecs.open(outfile, 'wb', encoding=encoding) as handle: 131 with codecs.open(outfile, 'wb', encoding=encoding) as handle:
132 handle.write(contents) 132 handle.write(contents)
133 133
134 with create_source(repo, cached=True) as source: 134 with create_source(repo, cached=True) as source:
135 config = source.read_config() 135 config = source.read_config()
136 defaultlocale = config.get('general', 'defaultlocale') 136 defaultlocale = config.get('general', 'defaultlocale')
137 locales = list(source.list_locales()) 137 locales = list(source.list_locales())
138 if defaultlocale not in locales: 138 if defaultlocale not in locales:
139 locales.append(defaultlocale) 139 locales.append(defaultlocale)
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 os.rmdir(path) 201 os.rmdir(path)
202 remove_unknown(output_dir) 202 remove_unknown(output_dir)
203 203
204 204
205 if __name__ == '__main__': 205 if __name__ == '__main__':
206 parser = ArgumentParser('Convert website source to static website') 206 parser = ArgumentParser('Convert website source to static website')
207 parser.add_argument('source', help="Path to website's repository") 207 parser.add_argument('source', help="Path to website's repository")
208 parser.add_argument('output', help='Path to desired output directory') 208 parser.add_argument('output', help='Path to desired output directory')
209 args = parser.parse_args() 209 args = parser.parse_args()
210 generate_pages(args.source, args.output) 210 generate_pages(args.source, args.output)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld