1 |
jpaalasm |
609 |
#!/usr/bin/env python |
2 |
jpaalasm |
607 |
|
3 |
jpaalasm |
612 |
# 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 |
bernhard |
624 |
"""Format webpages from templates. |
20 |
jpaalasm |
612 |
|
21 |
bernhard |
624 |
Update a file only if it does not exist or when the sources are newer. |
22 |
|
|
You can also use "-f" or "--force" which does what you think it does. |
23 |
|
|
""" |
24 |
|
|
__version__="$Revison:$"[9:-1] |
25 |
jpaalasm |
612 |
|
26 |
jpaalasm |
607 |
|
27 |
bernhard |
624 |
import glob, os, time, string, getopt, sys |
28 |
|
|
|
29 |
|
|
templatefile = "template.xhtml" |
30 |
|
|
|
31 |
jpaalasm |
607 |
pagefiles = glob.glob("pages/*.html") |
32 |
bernhard |
624 |
template = file(templatefile).read() |
33 |
jpaalasm |
607 |
includeformat = "<!--%s-->" |
34 |
|
|
destdir = "skencil" |
35 |
|
|
|
36 |
jpaalasm |
612 |
globaldata = { |
37 |
|
|
"fileinfo": |
38 |
|
|
"<!-- This file was generated by updatepages.py. Please don't modify. -->", |
39 |
|
|
"download root": |
40 |
|
|
"http://sketch.sourceforge.net/files"} |
41 |
jpaalasm |
607 |
|
42 |
bernhard |
624 |
def newer(filename1, filename2): |
43 |
|
|
"""Check if filename1 was modified after filename2.""" |
44 |
|
|
if not os.path.isfile(filename2): |
45 |
|
|
return True |
46 |
|
|
return ( os.path.getctime(filename1) > os.path.getctime(filename2) ) |
47 |
|
|
|
48 |
jpaalasm |
607 |
def parseheader(header): |
49 |
jpaalasm |
609 |
d = {} |
50 |
|
|
for line in header.split("\n"): |
51 |
|
|
name, value = line.split(": ") |
52 |
|
|
d[name] = value |
53 |
|
|
|
54 |
|
|
return d |
55 |
|
|
# import yaml |
56 |
|
|
# return yaml.load(header).next() |
57 |
|
|
|
58 |
bernhard |
624 |
def doit(force=False): |
59 |
|
|
if not os.path.isdir(destdir): |
60 |
|
|
os.mkdir(destdir) |
61 |
jpaalasm |
607 |
|
62 |
bernhard |
624 |
for pagefile in pagefiles: |
63 |
|
|
destfile = os.path.splitext(os.path.split(pagefile)[-1])[0]+".html" |
64 |
|
|
destpath = os.path.join(destdir, destfile) |
65 |
jpaalasm |
607 |
|
66 |
bernhard |
624 |
if newer(templatefile, destpath) or newer(pagefile, destpath) or force: |
67 |
jpaalasm |
607 |
|
68 |
bernhard |
624 |
header, body = file(pagefile).read().split("\n\n", 1) |
69 |
|
|
metadata = parseheader(header) |
70 |
|
|
pagedata = {} |
71 |
|
|
pagedata["body text"] = body |
72 |
|
|
pagedata["last updated"] = time.ctime(os.path.getmtime(pagefile)) |
73 |
|
|
pagedata.update(globaldata) |
74 |
|
|
pagedata.update(metadata) |
75 |
jpaalasm |
607 |
|
76 |
bernhard |
624 |
page = template |
77 |
|
|
for type, value in pagedata.items(): |
78 |
|
|
page = page.replace(includeformat % type, value) |
79 |
|
|
|
80 |
|
|
file(destpath, "w").write(page) |
81 |
|
|
print destpath |
82 |
|
|
|
83 |
|
|
def main(): |
84 |
|
|
try: |
85 |
|
|
opts, args = getopt.getopt(sys.argv[1:], "f", ["force"]) |
86 |
|
|
except getopt.GetoptError: |
87 |
|
|
sys.exit(2) |
88 |
|
|
force = False |
89 |
|
|
for o, a in opts: |
90 |
|
|
if o in ("-f", "--force"): |
91 |
|
|
force = True |
92 |
|
|
doit(force) |
93 |
|
|
|
94 |
|
|
if __name__ == "__main__": |
95 |
|
|
main() |
96 |
|
|
|