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