1 |
#!/usr/bin/env python |
2 |
|
3 |
# updatepages.py - Simple web site templating system |
4 |
# Copyright (C) 2004 Joonas Paalasmaa |
5 |
|
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
|
11 |
# This program is distributed in the hope that it will be useful, |
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
# GNU General Public License for more details. |
15 |
|
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
#"""Format webpages from templates. |
21 |
|
22 |
#Update a file only if it does not exist or when the sources are newer. |
23 |
#You can also use "-f" or "--force" which does what you think it does. |
24 |
#""" |
25 |
|
26 |
|
27 |
"""\n |
28 |
Generate Skencil website from templates into a given directory. If no |
29 |
directory is set, a directory "skencil-webiste" will be created in |
30 |
this directory. |
31 |
|
32 |
On default only changed sites will be generated but you can force to |
33 |
generate all pages by providing -f option. |
34 |
|
35 |
Usage: updatepages.py [Option] target |
36 |
|
37 |
Possible targets are |
38 |
|
39 |
all - generate all |
40 |
pages - generate website and images |
41 |
gallery - generate the gallery |
42 |
screenshots - generate screenshots |
43 |
|
44 |
Options: |
45 |
-h / --help Print this message and exit. |
46 |
|
47 |
-f force generation of all pages |
48 |
|
49 |
-d dir |
50 |
--dest-dir=dir |
51 |
skencil website will be generated in this directory |
52 |
|
53 |
--gen-gallery |
54 |
you may want to generate the gallery files. |
55 |
|
56 |
--gen-screenshots |
57 |
you may want to generate the screenshot files. |
58 |
|
59 |
""" |
60 |
|
61 |
__version__="$Revison:$"[9:-1] |
62 |
|
63 |
|
64 |
import glob, os, time, string, getopt, sys, shutil, re |
65 |
|
66 |
templatefile = "pages/template.xhtml" |
67 |
template = file(templatefile).read() |
68 |
includeformat = "<!--%s-->" |
69 |
destdir = "skencil-website" |
70 |
|
71 |
globaldata = { |
72 |
"fileinfo": |
73 |
"<!-- This file was generated by updatepages.py. Please don't modify. -->", |
74 |
"download root": |
75 |
"http://wald.intevation.de/projects/skecil"} |
76 |
|
77 |
def main(): |
78 |
global destdir |
79 |
|
80 |
try: |
81 |
opts, args = getopt.getopt(sys.argv[1:], "fd:", |
82 |
["force","dest-dir","gen-screenshots","gen-gallery"]) |
83 |
except getopt.GetoptError: |
84 |
sys.exit(2) |
85 |
|
86 |
if len (args) < 1: |
87 |
usage(1) |
88 |
|
89 |
force = False |
90 |
gen_gallery = False |
91 |
gen_screenshots = False |
92 |
|
93 |
for o, a in opts: |
94 |
if o in ("-f", "--force"): |
95 |
force = True |
96 |
if o in ("-d", "--dest-dir"): |
97 |
destdir = a |
98 |
if o == "--gen-gallery": |
99 |
gen_gallery = True |
100 |
if o == "--gen-screenshots": |
101 |
gen_screenshots = True |
102 |
doit(force,gen_gallery,gen_screenshots,args) |
103 |
|
104 |
|
105 |
def usage(code, msg=''): |
106 |
print >> sys.stderr, __doc__ |
107 |
if msg: |
108 |
print >> sys.stderr, msg |
109 |
sys.exit(code) |
110 |
|
111 |
def newer(filename1, filename2): |
112 |
"""Check if filename1 was modified after filename2.""" |
113 |
if not os.path.isfile(filename2): |
114 |
return True |
115 |
return ( os.path.getmtime(filename1) > os.path.getmtime(filename2) ) |
116 |
|
117 |
def parseheader(header): |
118 |
d = {} |
119 |
for line in header.split("\n"): |
120 |
name, value = line.split(": ") |
121 |
d[name] = value |
122 |
|
123 |
return d |
124 |
|
125 |
def doit(force=False,gen_gallery=False,gen_screenshots=False,args=[]): |
126 |
|
127 |
print "\n" |
128 |
print "###########################################" |
129 |
print "# Generation #" |
130 |
print "###########################################\n" |
131 |
print "Webpages will be generated now.\n" |
132 |
|
133 |
genpages_flag = False |
134 |
genscreens_flag = False |
135 |
gengallery_flag = False |
136 |
|
137 |
for target in args: |
138 |
if target == "all": |
139 |
genpages_flag = True |
140 |
genscreens_flag = True |
141 |
gengallery_flag= True |
142 |
if target == "pages": |
143 |
genpages_flag = True |
144 |
if target == "screenshots": |
145 |
genscreens_flag = True |
146 |
if target == "gallery": |
147 |
gengallery_flag = True |
148 |
|
149 |
#generate pages |
150 |
if genpages_flag: |
151 |
print("Generating html files... "), |
152 |
genpages(force) |
153 |
print("Done.") |
154 |
#copy common file (.css,license...) |
155 |
copycommon() |
156 |
|
157 |
#copy screenshots |
158 |
if genscreens_flag: |
159 |
if os.path.isdir("screenshots"): |
160 |
print("Copying screenshots... "), |
161 |
genscreenshots(force,gen_screenshots) |
162 |
print("Done.") |
163 |
else: |
164 |
print("No screenshot directory found. Nothing to be done.") |
165 |
|
166 |
#copy gallery |
167 |
if gengallery_flag: |
168 |
if os.path.isdir("gallery"): |
169 |
print("Generating gallery... "), |
170 |
gengallery(force,gen_gallery) |
171 |
print("Done.") |
172 |
else: |
173 |
print("No gallery directory found. Nothing to be done.") |
174 |
|
175 |
print("All files generated. You can now rsync them to the server") |
176 |
|
177 |
def copycommon(): |
178 |
common_files = ("pages/LGPL","pages/skencil.css") |
179 |
for file in common_files: |
180 |
shutil.copy(file,destdir) |
181 |
|
182 |
def genpages(force): |
183 |
|
184 |
faq_xml = 'pages/faq/faq.xml' |
185 |
#generate faq |
186 |
faq_html = os.path.join('pages/','faq.html') |
187 |
if newer(faq_xml,faq_html): |
188 |
os.chdir('pages/faq') |
189 |
output = os.popen('python faqdom.py -x faq.xml ../faq.html') |
190 |
os.chdir('../../') |
191 |
|
192 |
#create destination dir |
193 |
if not os.path.isdir(destdir): |
194 |
os.mkdir(destdir) |
195 |
|
196 |
#generate files |
197 |
pagefiles = glob.glob("pages/*.html") |
198 |
for pagefile in pagefiles: |
199 |
destfile = os.path.splitext(os.path.split(pagefile)[-1])[0]+".html" |
200 |
destpath = os.path.join(destdir, destfile) |
201 |
|
202 |
if newer(templatefile, destpath) or newer(pagefile, destpath) or force: |
203 |
|
204 |
header, body = file(pagefile).read().split("\n\n", 1) |
205 |
metadata = parseheader(header) |
206 |
pagedata = {} |
207 |
pagedata["body text"] = body |
208 |
pagedata["last updated"] = time.ctime(os.path.getmtime(pagefile)) |
209 |
pagedata.update(globaldata) |
210 |
pagedata.update(metadata) |
211 |
|
212 |
page = template |
213 |
for type, value in pagedata.items(): |
214 |
page = page.replace(includeformat % type, value) |
215 |
|
216 |
file(destpath, "w").write(page) |
217 |
#print destpath |
218 |
|
219 |
#copy images |
220 |
images = glob.glob("pages/images/*") |
221 |
destpath = os.path.join(destdir,"images/") |
222 |
if not os.path.isdir(destpath): |
223 |
os.mkdir(destpath) |
224 |
|
225 |
for image in images: |
226 |
destpath_ = os.path.join(destpath,image) |
227 |
if newer(image,destpath_) or force: |
228 |
shutil.copy(image,destpath) |
229 |
|
230 |
def genscreenshots(force,gen_screenshots): |
231 |
"""Copy screenshot to the dest directory""" |
232 |
destpath = os.path.join(destdir, "screenshots/") |
233 |
screenshots = glob.glob("screenshots/*") |
234 |
if not os.path.isdir(destpath): |
235 |
os.mkdir(destpath) |
236 |
|
237 |
for picture in screenshots: |
238 |
destpath_ = os.path.join(destpath,picture) |
239 |
if newer(picture,destpath_) or force: |
240 |
shutil.copy(picture,destpath) |
241 |
|
242 |
|
243 |
def gengallery(force,gen_gallery): |
244 |
"""Copy gallery files in the dest directory after generating""" |
245 |
#First we need to generate the image files from source |
246 |
if gen_gallery: |
247 |
os.chdir("gallery") |
248 |
os.system("make") |
249 |
os.chdir("..") |
250 |
|
251 |
destpath = os.path.join(destdir, "gallery/") |
252 |
|
253 |
# read all files from gallery directory |
254 |
matchobject = re.compile("^.*\\.[jpg|tar.gz|ppm]") |
255 |
allfiles = os.listdir("gallery/") |
256 |
gallery = filter(matchobject.match, allfiles) |
257 |
if not os.path.isdir(destpath): |
258 |
os.mkdir(destpath) |
259 |
|
260 |
for file in gallery: |
261 |
if not os.path.isdir(file): |
262 |
destpath_ = os.path.join(destpath,file) |
263 |
srcpath = os.path.join("gallery",file) |
264 |
if newer(srcpath,destpath_) or force: |
265 |
#as wen do not want any leave additional files |
266 |
#in the repository we will move instead of copying the |
267 |
#files into destdir. |
268 |
shutil.move(srcpath,destpath) |
269 |
|
270 |
if __name__ == "__main__": |
271 |
main() |
272 |
|