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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « .hgignore ('k') | cms/bin/test_server.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/bin/generate_static_pages.py
===================================================================
--- a/cms/bin/generate_static_pages.py
+++ b/cms/bin/generate_static_pages.py
@@ -11,20 +11,28 @@
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
import sys, os, re, errno, codecs
-from ...utils import setupStderr, cached
from ..utils import process_page
from ..sources import MercurialSource
+def memoize(func):
+ memoized = {}
+ def wrapper(*args):
+ try:
+ return memoized[args]
+ except KeyError:
+ return memoized.setdefault(args, func(*args))
+ return wrapper
+
def generate_pages(repo, output_dir):
known_files = set()
def write_file(path_parts, contents, binary=False):
encoding = None if binary else "utf-8"
outfile = os.path.join(output_dir, *path_parts)
if outfile in known_files:
print >>sys.stderr, "Warning: File %s has multiple sources" % outfile
@@ -43,21 +51,21 @@ def generate_pages(repo, output_dir):
raise
with codecs.open(outfile, "wb", encoding=encoding) as handle:
handle.write(contents)
with MercurialSource(repo) as source:
# Cache the result for some functions - we can assume here that the data
# never changes
- source.resolve_link = cached(float("Infinity"))(source.resolve_link)
- source.read_config = cached(float("Infinity"))(source.read_config)
- source.read_template = cached(float("Infinity"))(source.read_template)
- source.read_locale = cached(float("Infinity"))(source.read_locale)
- source.read_include = cached(float("Infinity"))(source.read_include)
+ source.resolve_link = memoize(source.resolve_link)
+ source.read_config = memoize(source.read_config)
+ source.read_template = memoize(source.read_template)
+ source.read_locale = memoize(source.read_locale)
+ source.read_include = memoize(source.read_include)
locales = list(source.list_locales())
for page, format in source.list_pages():
for locale in locales:
if source.has_locale(locale, page):
pagedata = process_page(source, locale, page, format)
# Make sure links to static files are versioned
@@ -84,15 +92,14 @@ def generate_pages(repo, output_dir):
os.remove(path)
elif os.path.isdir(path):
remove_unknown(path)
if not os.listdir(path):
os.rmdir(path)
remove_unknown(output_dir)
if __name__ == "__main__":
- setupStderr()
if len(sys.argv) < 3:
print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0]
sys.exit(1)
repo, output_dir = sys.argv[1:3]
generate_pages(repo, output_dir)
« 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