dart-sdk/samples-dev/swarm/gen_manifest.py
Nate Bosch 55f81f2210 Mass format python with yapf
- Add `.style.yapf` with configuration to use Google style.
- Run `yapf` on all `.py` files in this repo.
- Manually fix one trailing space in a doc string.
- Run `git cl format runtime` to satisfy presubmit.

Change-Id: I7e6bd11e91f07926b9188362599af398551eed79
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/111600
Commit-Queue: Nate Bosch <nbosch@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2019-08-05 20:34:31 +00:00

81 lines
2.4 KiB
Python
Executable file

# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
#!/usr/bin/python2.6
#
"""
Usage: gen_manifest.py DIRECTORY EXTENSIONS CACHE-FILE HTML-FILES...
Outputs an app cache manifest file including (recursively) all files with the
provided in the directory with the given extensions. Each html files is then
processed and a corresponding <name>-cache.html file is created, pointing at
the appropriate cache manifest file, which is saved as <name>-cache.manifest.
Example:
gen_manifest.py war *.css,*.html,*.js,*.png cache.manifest foo.html bar.html
Produces: foo-cache.html, bar-cache.html, and cache.manifest
"""
import fnmatch
import os
import random
import sys
import datetime
cacheDir = sys.argv[1]
extensions = sys.argv[2].split(',')
manifestName = sys.argv[3]
htmlFiles = sys.argv[4:]
os.chdir(cacheDir)
print "Generating manifest from root path: " + cacheDir
patterns = extensions + htmlFiles
def matches(file):
for pattern in patterns:
if fnmatch.fnmatch(file, pattern):
return True
return False
def findFiles(rootDir):
for root, dirs, files in os.walk(rootDir):
for f in files:
# yields this file relative to the given directory
yield os.path.join(root, f)[(len(rootDir) + 1):]
manifest = []
manifest.append("CACHE MANIFEST")
# print out a random number to force the browser to update the cache manifest
manifest.append("# %s" % datetime.datetime.now().isoformat())
# print out each file to be included in the cache manifest
manifest.append("CACHE:")
manifest += (f for f in findFiles('.') if matches(f))
# force the browser to request any other files over the network,
# even when offline (better failure mode)
manifest.append("NETWORK:")
manifest.append("*")
with open(manifestName, 'w') as f:
f.writelines(m + '\n' for m in manifest)
print "Created manifest file: " + manifestName
for htmlFile in htmlFiles:
cachedHtmlFile = htmlFile.replace('.html', '-cache.html')
text = open(htmlFile, 'r').read()
text = text.replace('<html>', '<html manifest="%s">' % manifestName, 1)
with open(cachedHtmlFile, 'w') as output:
output.write(text)
print "Processed html file: %s -> %s" % (htmlFile, cachedHtmlFile)
print "Successfully generated manifest and html files"