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

Side by Side Diff: sitescripts/web/bin/generate_static_pages.py

Issue 17817001: Simple CMS as Anwiki replacement (Closed)
Patch Set: Created Oct. 23, 2013, 1:52 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 | « sitescripts/web/bin/__init__.py ('k') | sitescripts/web/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
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2013 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import sys, os
19 from sitescripts.utils import setupStderr
20 from sitescripts.web.utils import process_page
21 from sitescripts.web.sources import MercurialSource
22
23 def generate_pages(repo, output_dir):
24 known_files = set()
25
26 def write_file(path_parts, contents):
27 outfile = os.path.join(output_dir, *path_parts)
28 if outfile in known_files:
29 print >>sys.stderr, "Warning: File %s has multiple sources" % outfile
30 return
31 known_files.add(outfile)
32
33 if os.path.exists(outfile):
34 with open(outfile, "rb") as handle:
35 if handle.read() == contents:
36 return
37
38 try:
39 os.makedirs(os.path.dirname(outfile))
40 except OSError:
41 pass
42
43 with open(outfile, "wb") as handle:
44 handle.write(contents)
45
46 with MercurialSource(repo) as source:
47 locales = list(source.list_locales())
48 for page, format in source.list_pages():
49 for locale in locales:
50 if source.has_locale(locale, page):
51 pagedata = process_page(source, locale, page, format)
52 write_file([locale] + page.split("/"), pagedata)
53
54 for filename in source.list_localizable_files():
55 for locale in locales:
56 if source.has_localizable_file(locale, filename):
57 filedata = source.read_localizable_file(locale, filename)
58 write_file([locale] + filename.split("/"), filedata)
59
60 for filename in source.list_static():
61 write_file(filename.split("/"), source.read_static(filename))
62
63 def remove_unknown(dir):
64 files = os.listdir(dir)
65 for filename in files:
66 path = os.path.join(dir, filename)
67 if os.path.isfile(path) and path not in known_files:
68 os.remove(path)
69 elif os.path.isdir(path):
70 remove_unknown(path)
71 if not os.listdir(path):
72 os.rmdir(path)
73 remove_unknown(output_dir)
74
75 if __name__ == "__main__":
76 setupStderr()
77 if len(sys.argv) < 3:
78 print >>sys.stderr, "Usage: %s source_repository output_dir" % sys.argv[0]
79 sys.exit(1)
80
81 repo, output_dir = sys.argv[1:3]
82 generate_pages(repo, output_dir)
OLDNEW
« no previous file with comments | « sitescripts/web/bin/__init__.py ('k') | sitescripts/web/bin/test_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld