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

Side by Side Diff: cms/bin/generate_static_pages.py

Issue 6364489058353152: Issue 2126 - Adapted CMS for living in its own repository rather than sitescripts (Closed)
Patch Set: Added .pyo to ignore files Created March 12, 2015, 4:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « .hgignore ('k') | cms/bin/test_server.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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, os, re, errno, codecs 18 import sys, os, re, errno, codecs
19 from ...utils import setupStderr, cached
20 from ..utils import process_page 19 from ..utils import process_page
21 from ..sources import MercurialSource 20 from ..sources import MercurialSource
22 21
22 def memoize(func):
23 memoized = {}
24 def wrapper(*args):
25 try:
26 return memoized[args]
27 except KeyError:
28 return memoized.setdefault(args, func(*args))
29 return wrapper
30
23 def generate_pages(repo, output_dir): 31 def generate_pages(repo, output_dir):
24 known_files = set() 32 known_files = set()
25 33
26 def write_file(path_parts, contents, binary=False): 34 def write_file(path_parts, contents, binary=False):
27 encoding = None if binary else "utf-8" 35 encoding = None if binary else "utf-8"
28 outfile = os.path.join(output_dir, *path_parts) 36 outfile = os.path.join(output_dir, *path_parts)
29 if outfile in known_files: 37 if outfile in known_files:
30 print >>sys.stderr, "Warning: File %s has multiple sources" % outfile 38 print >>sys.stderr, "Warning: File %s has multiple sources" % outfile
31 return 39 return
32 known_files.add(outfile) 40 known_files.add(outfile)
33 41
34 if os.path.exists(outfile): 42 if os.path.exists(outfile):
35 with codecs.open(outfile, "rb", encoding=encoding) as handle: 43 with codecs.open(outfile, "rb", encoding=encoding) as handle:
36 if handle.read() == contents: 44 if handle.read() == contents:
37 return 45 return
38 46
39 try: 47 try:
40 os.makedirs(os.path.dirname(outfile)) 48 os.makedirs(os.path.dirname(outfile))
41 except OSError, e: 49 except OSError, e:
42 if e.errno != errno.EEXIST: 50 if e.errno != errno.EEXIST:
43 raise 51 raise
44 52
45 with codecs.open(outfile, "wb", encoding=encoding) as handle: 53 with codecs.open(outfile, "wb", encoding=encoding) as handle:
46 handle.write(contents) 54 handle.write(contents)
47 55
48 with MercurialSource(repo) as source: 56 with MercurialSource(repo) as source:
49 # Cache the result for some functions - we can assume here that the data 57 # Cache the result for some functions - we can assume here that the data
50 # never changes 58 # never changes
51 source.resolve_link = cached(float("Infinity"))(source.resolve_link) 59 source.resolve_link = memoize(source.resolve_link)
52 source.read_config = cached(float("Infinity"))(source.read_config) 60 source.read_config = memoize(source.read_config)
53 source.read_template = cached(float("Infinity"))(source.read_template) 61 source.read_template = memoize(source.read_template)
54 source.read_locale = cached(float("Infinity"))(source.read_locale) 62 source.read_locale = memoize(source.read_locale)
55 source.read_include = cached(float("Infinity"))(source.read_include) 63 source.read_include = memoize(source.read_include)
56 64
57 locales = list(source.list_locales()) 65 locales = list(source.list_locales())
58 for page, format in source.list_pages(): 66 for page, format in source.list_pages():
59 for locale in locales: 67 for locale in locales:
60 if source.has_locale(locale, page): 68 if source.has_locale(locale, page):
61 pagedata = process_page(source, locale, page, format) 69 pagedata = process_page(source, locale, page, format)
62 70
63 # Make sure links to static files are versioned 71 # Make sure links to static files are versioned
64 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % sour ce.version, pagedata) 72 pagedata = re.sub(r'(<script\s[^<>]*\bsrc="/[^"<>]+)', r"\1?%s" % sour ce.version, pagedata)
65 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % sourc e.version, pagedata) 73 pagedata = re.sub(r'(<link\s[^<>]*\bhref="/[^"<>]+)', r"\1?%s" % sourc e.version, pagedata)
(...skipping 16 matching lines...) Expand all
82 path = os.path.join(dir, filename) 90 path = os.path.join(dir, filename)
83 if os.path.isfile(path) and path not in known_files: 91 if os.path.isfile(path) and path not in known_files:
84 os.remove(path) 92 os.remove(path)
85 elif os.path.isdir(path): 93 elif os.path.isdir(path):
86 remove_unknown(path) 94 remove_unknown(path)
87 if not os.listdir(path): 95 if not os.listdir(path):
88 os.rmdir(path) 96 os.rmdir(path)
89 remove_unknown(output_dir) 97 remove_unknown(output_dir)
90 98
91 if __name__ == "__main__": 99 if __name__ == "__main__":
92 setupStderr()
93 if len(sys.argv) < 3: 100 if len(sys.argv) < 3:
94 print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0] 101 print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0]
95 sys.exit(1) 102 sys.exit(1)
96 103
97 repo, output_dir = sys.argv[1:3] 104 repo, output_dir = sys.argv[1:3]
98 generate_pages(repo, output_dir) 105 generate_pages(repo, output_dir)
OLDNEW
« no previous file with comments | « .hgignore ('k') | cms/bin/test_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld