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) |