|
2 | 2 | import tempfile |
3 | 3 | import zipfile |
4 | 4 |
|
| 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 | + |
5 | 18 | def _read_file(path): |
6 | 19 | with open(path, "rb") as f: |
7 | 20 | return f.read() |
8 | 21 |
|
9 | | -def create_predictable_zip(path): |
| 22 | + |
| 23 | +def create_predictable_zip(path, entrypoint=None): |
10 | 24 | """ |
11 | 25 | Create a zip file with predictable sort order and metadata so that MD5 will |
12 | 26 | stay consistent if zipping the same content twice. |
13 | 27 | Args: |
14 | 28 | 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 |
15 | 30 | Returns: path (str) to the output zip file |
16 | 31 | """ |
17 | 32 | # 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): |
19 | 34 | 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 | + |
20 | 41 | for root, directories, filenames in os.walk(path): |
21 | 42 | paths += [os.path.join(root, filename)[len(path)+1:] for filename in filenames] |
22 | 43 | reader = lambda x: _read_file(os.path.join(path, x)) |
|
0 commit comments