OLD | NEW |
| (Empty) |
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 # for details. All rights reserved. Use of this source code is governed by a | |
3 # BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #!/usr/bin/python2.6 | |
6 # | |
7 | |
8 """ | |
9 Usage: gen_manifest.py DIRECTORY EXTENSIONS CACHE-FILE HTML-FILES... | |
10 | |
11 Outputs an app cache manifest file including (recursively) all files with the | |
12 provided in the directory with the given extensions. Each html files is then | |
13 processed and a corresponding <name>-cache.html file is created, pointing at | |
14 the appropriate cache manifest file, which is saved as <name>-cache.manifest. | |
15 | |
16 Example: | |
17 gen_manifest.py war *.css,*.html,*.js,*.png cache.manifest foo.html bar.html | |
18 | |
19 Produces: foo-cache.html, bar-cache.html, and cache.manifest | |
20 """ | |
21 | |
22 import fnmatch | |
23 import os | |
24 import random | |
25 import sys | |
26 import datetime | |
27 | |
28 cacheDir = sys.argv[1] | |
29 extensions = sys.argv[2].split(',') | |
30 manifestName = sys.argv[3] | |
31 htmlFiles = sys.argv[4:] | |
32 | |
33 os.chdir(cacheDir) | |
34 print "Generating manifest from root path: " + cacheDir | |
35 | |
36 patterns = extensions + htmlFiles | |
37 def matches(file): | |
38 for pattern in patterns: | |
39 if fnmatch.fnmatch(file, pattern): | |
40 return True | |
41 return False | |
42 | |
43 def findFiles(rootDir): | |
44 for root, dirs, files in os.walk(rootDir): | |
45 for f in files: | |
46 # yields this file relative to the given directory | |
47 yield os.path.join(root, f)[(len(rootDir) + 1):] | |
48 | |
49 manifest = [] | |
50 manifest.append("CACHE MANIFEST") | |
51 | |
52 # print out a random number to force the browser to update the cache manifest | |
53 manifest.append("# %s" % datetime.datetime.now().isoformat()) | |
54 | |
55 # print out each file to be included in the cache manifest | |
56 manifest.append("CACHE:") | |
57 | |
58 manifest += (f for f in findFiles('.') if matches(f)) | |
59 | |
60 # force the browser to request any other files over the network, | |
61 # even when offline (better failure mode) | |
62 manifest.append("NETWORK:") | |
63 manifest.append("*") | |
64 | |
65 with open(manifestName, 'w') as f: | |
66 f.writelines(m + '\n' for m in manifest) | |
67 | |
68 print "Created manifest file: " + manifestName | |
69 | |
70 for htmlFile in htmlFiles: | |
71 cachedHtmlFile = htmlFile.replace('.html', '-cache.html') | |
72 text = open(htmlFile, 'r').read() | |
73 text = text.replace('<html>', '<html manifest="%s">' % manifestName, 1) | |
74 with open(cachedHtmlFile, 'w') as output: | |
75 output.write(text) | |
76 print "Processed html file: %s -> %s" % (htmlFile, cachedHtmlFile) | |
77 | |
78 print "Successfully generated manifest and html files" | |
OLD | NEW |