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