Begin bug tracking.
[featherfall2.git] / tools / generate-appcache
1 #!/usr/bin/env python
2
3 # Turn a website into an HTML5 application with an application cache
4 # manifest.
5 #
6 # http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html
7 # https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache
8
9 import os
10 import re
11 import shutil
12 import time
13
14 def is_html(filename):
15 return filename.lower().endswith(".html")
16
17 def main(appdir):
18 if not os.path.isdir(appdir):
19 raise StandardError("input (%r) is not a directory" % appdir)
20 all_files = []
21 for root, dirnames, filenames in os.walk(appdir):
22 root = os.path.relpath(root, appdir)
23 for filename in filenames:
24 all_files.append(os.path.join(root, filename))
25 all_files.sort()
26 appcache = os.path.join(appdir, "manifest.appcache")
27 with open(appcache, "w") as fobj:
28 fobj.write("CACHE MANIFEST\n")
29 fobj.write("# Generated on %s\n" % time.strftime("%Y %T %z"))
30 for filename in all_files:
31 fobj.write(filename + "\n")
32
33 for filename in filter(is_html, all_files):
34 filename = os.path.join(appdir, filename)
35 # This call to relpath is the entire reason this tool is in
36 # Python and not a shell script.
37 relpath = os.path.relpath(appcache, os.path.dirname(filename))
38 html = open(filename).read()
39 html = re.sub("<html([> ])", '<html manifest="%s"\\1' % relpath, html)
40 with open(filename, "w") as fobj:
41 fobj.write(html)
42
43 if __name__ == "__main__":
44 import sys
45 try:
46 appdir = sys.argv[1]
47 except IndexError:
48 raise SystemExit("Usage: %s appdir" %(
49 sys.argv[0]))
50 else:
51 main(appdir)
52