Skip to content

Commit 7ebfd1a

Browse files
authored
Merge pull request #286 from kollivier/entrypoint
Make it possible for HTML zips to specify which page to load in cases…
2 parents 3a64b80 + b603397 commit 7ebfd1a

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

ricecooker/utils/zip.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,42 @@
22
import tempfile
33
import zipfile
44

5+
ENTRYPOINT_TEMPLATE = """
6+
<!DOCTYPE html>
7+
<html>
8+
<head>
9+
<title>HTML Meta Tag</title>
10+
<meta http-equiv = "refresh" content = "0; url = {}" />
11+
</head>
12+
<body>
13+
</body>
14+
</html>
15+
"""
16+
17+
518
def _read_file(path):
619
with open(path, "rb") as f:
720
return f.read()
821

9-
def create_predictable_zip(path):
22+
23+
def create_predictable_zip(path, entrypoint=None):
1024
"""
1125
Create a zip file with predictable sort order and metadata so that MD5 will
1226
stay consistent if zipping the same content twice.
1327
Args:
1428
path (str): absolute path either to a directory to zip up, or an existing zip file to convert.
29+
entrypoint (str or None): if specified, a relative file path in the zip to serve as the first page to load
1530
Returns: path (str) to the output zip file
1631
"""
1732
# if path is a directory, recursively enumerate all the files under the directory
18-
if os.path.isdir(path):
33+
if os.path.isdir(path):
1934
paths = []
35+
if entrypoint:
36+
index = os.path.join(path, "index.html")
37+
f = open(index, "w", encoding="utf-8")
38+
f.write(ENTRYPOINT_TEMPLATE.format(entrypoint.replace("\\", "/")))
39+
f.close()
40+
2041
for root, directories, filenames in os.walk(path):
2142
paths += [os.path.join(root, filename)[len(path)+1:] for filename in filenames]
2243
reader = lambda x: _read_file(os.path.join(path, x))

0 commit comments

Comments
 (0)