1 |
#!/usr/bin/env python |
2 |
|
3 |
# updatepages.py - Simple web site templating system |
4 |
# Copyright (C) 2006 Torsten Irländer |
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 |
template = file(templatefile).read() |
65 |
includeformat = "<!--%s-->" |
66 |
destdir = "skencil-website" |
67 |
|
68 |
globaldata = { |
69 |
"fileinfo": |
70 |
"<!-- This file was generated by updatepages.py. Please don't modify. -->", |
71 |
"download root": |
72 |
"http://wald.intevation.de/projects/skecil"} |
73 |
|
74 |
def main(): |
75 |
global destdir |
76 |
|
77 |
try: |
78 |
opts, args = getopt.getopt(sys.argv[1:], "fd:", |
79 |
["force","dest-dir","gen-screenshots","gen-gallery"]) |
80 |
except getopt.GetoptError: |
81 |
sys.exit(2) |
82 |
|
83 |
if len (args) < 1: |
84 |
usage(1) |
85 |
|
86 |
force = False |
87 |
gen_gallery = False |
88 |
gen_screenshots = False |
89 |
|
90 |
for o, a in opts: |
91 |
if o in ("-f", "--force"): |
92 |
force = True |
93 |
if o in ("-d", "--dest-dir"): |
94 |
destdir = a |
95 |
if o == "--gen-gallery": |
96 |
gen_gallery = True |
97 |
if o == "--gen-screenshots": |
98 |
gen_screenshots = True |
99 |
doit(force,gen_gallery,gen_screenshots,args) |
100 |
|
101 |
|
102 |
def usage(code, msg=''): |
103 |
print >> sys.stderr, __doc__ |
104 |
if msg: |
105 |
print >> sys.stderr, msg |
106 |
sys.exit(code) |
107 |
|
108 |
def newer(filename1, filename2): |
109 |
"""Check if filename1 was modified after filename2.""" |
110 |
if not os.path.isfile(filename2): |
111 |
return True |
112 |
return ( os.path.getmtime(filename1) > os.path.getmtime(filename2) ) |
113 |
|
114 |
def parseheader(header): |
115 |
d = {} |
116 |
for line in header.split("\n"): |
117 |
name, value = line.split(": ") |
118 |
d[name] = value |
119 |
|
120 |
return d |
121 |
|
122 |
def doit(force=False,gen_gallery=False,gen_screenshots=False,args=[]): |
123 |
|
124 |
print "\n" |
125 |
print "###########################################" |
126 |
print "# Generation #" |
127 |
print "###########################################\n" |
128 |
print "Webpages will be generated now.\n" |
129 |
|
130 |
genpages_flag = False |
131 |
genscreens_flag = False |
132 |
gengallery_flag = False |
133 |
|
134 |
for target in args: |
135 |
if target == "all": |
136 |
genpages_flag = True |
137 |
genscreens_flag = True |
138 |
gengallery_flag= True |
139 |
if target == "pages": |
140 |
genpages_flag = True |
141 |
if target == "screenshots": |
142 |
genscreens_flag = True |
143 |
if target == "gallery": |
144 |
gengallery_flag = True |
145 |
|
146 |
#generate pages |
147 |
if genpages_flag: |
148 |
print("Generating html files... "), |
149 |
genpages(force) |
150 |
print("Done.") |
151 |
#copy common file (.css,license...) |
152 |
copycommon() |
153 |
|
154 |
#copy screenshots |
155 |
if genscreens_flag: |
156 |
if os.path.isdir("screenshots"): |
157 |
print("Copying screenshots... "), |
158 |
genscreenshots(force,gen_screenshots) |
159 |
print("Done.") |
160 |
else: |
161 |
print("No screenshot directory found. Nothing to be done.") |
162 |
|
163 |
#copy gallery |
164 |
if gengallery_flag: |
165 |
if os.path.isdir("gallery"): |
166 |
print("Generating gallery... "), |
167 |
gengallery(force,gen_gallery) |
168 |
print("Done.") |
169 |
else: |
170 |
print("No gallery directory found. Nothing to be done.") |
171 |
|
172 |
print("All files generated. You can now rsync them to the server") |
173 |
|
174 |
def copycommon(): |
175 |
common_files = ("pages/LGPL","pages/skencil.css") |
176 |
for file in common_files: |
177 |
shutil.copy(file,destdir) |
178 |
|
179 |
def genpages(force): |
180 |
|
181 |
faq_xml = 'pages/faq/faq.xml' |
182 |
#generate faq |
183 |
faq_html = os.path.join('pages/','faq.html') |
184 |
if newer(faq_xml,faq_html): |
185 |
os.chdir('pages/faq') |
186 |
output = os.popen('python faqdom.py -x faq.xml ../faq.html') |
187 |
os.chdir('../../') |
188 |
|
189 |
#create destination dir |
190 |
if not os.path.isdir(destdir): |
191 |
os.mkdir(destdir) |
192 |
|
193 |
#generate files |
194 |
pagefiles = glob.glob("pages/*.html") |
195 |
for pagefile in pagefiles: |
196 |
destfile = os.path.splitext(os.path.split(pagefile)[-1])[0]+".html" |
197 |
destpath = os.path.join(destdir, destfile) |
198 |
|
199 |
if newer(templatefile, destpath) or newer(pagefile, destpath) or force: |
200 |
|
201 |
header, body = file(pagefile).read().split("\n\n", 1) |
202 |
metadata = parseheader(header) |
203 |
pagedata = {} |
204 |
pagedata["body text"] = body |
205 |
pagedata["last updated"] = time.ctime(os.path.getmtime(pagefile)) |
206 |
pagedata.update(globaldata) |
207 |
pagedata.update(metadata) |
208 |
|
209 |
page = template |
210 |
for type, value in pagedata.items(): |
211 |
page = page.replace(includeformat % type, value) |
212 |
|
213 |
file(destpath, "w").write(page) |
214 |
#print destpath |
215 |
|
216 |
#copy images |
217 |
images = glob.glob("pages/images/*") |
218 |
destpath = os.path.join(destdir,"images/") |
219 |
if not os.path.isdir(destpath): |
220 |
os.mkdir(destpath) |
221 |
|
222 |
for image in images: |
223 |
destpath_ = os.path.join(destpath,image) |
224 |
if newer(image,destpath_) or force: |
225 |
shutil.copy(image,destpath) |
226 |
|
227 |
def genscreenshots(force,gen_screenshots): |
228 |
"""Copy screenshot to the dest directory""" |
229 |
destpath = os.path.join(destdir, "screenshots/") |
230 |
screenshots = glob.glob("screenshots/*") |
231 |
if not os.path.isdir(destpath): |
232 |
os.mkdir(destpath) |
233 |
|
234 |
for picture in screenshots: |
235 |
destpath_ = os.path.join(destpath,picture) |
236 |
if newer(picture,destpath_) or force: |
237 |
shutil.copy(picture,destpath) |
238 |
|
239 |
|
240 |
def gengallery(force,gen_gallery): |
241 |
"""Copy gallery files in the dest directory after generating""" |
242 |
#First we need to generate the image files from source |
243 |
if gen_gallery: |
244 |
os.chdir("gallery") |
245 |
os.system("make") |
246 |
os.chdir("..") |
247 |
|
248 |
destpath = os.path.join(destdir, "gallery/") |
249 |
|
250 |
# read all files from gallery directory |
251 |
matchobject = re.compile("^.*\\.[jpg|tar.gz|ppm]") |
252 |
allfiles = os.listdir("gallery/") |
253 |
gallery = filter(matchobject.match, allfiles) |
254 |
if not os.path.isdir(destpath): |
255 |
os.mkdir(destpath) |
256 |
|
257 |
for file in gallery: |
258 |
if not os.path.isdir(file): |
259 |
destpath_ = os.path.join(destpath,file) |
260 |
srcpath = os.path.join("gallery",file) |
261 |
if newer(srcpath,destpath_) or force: |
262 |
#as wen do not want any leave additional files |
263 |
#in the repository we will move instead of copying the |
264 |
#files into destdir. |
265 |
shutil.move(srcpath,destpath) |
266 |
|
267 |
if __name__ == "__main__": |
268 |
main() |
269 |
|