comparison env/lib/python3.9/site-packages/galaxy/util/zipstream.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import os
2 from urllib.parse import quote
3
4 import zipstream
5
6 from .path import safe_walk
7
8
9 class ZipstreamWrapper:
10
11 def __init__(self, archive_name=None, upstream_mod_zip=False, upstream_gzip=False):
12 self.upstream_mod_zip = upstream_mod_zip
13 self.archive_name = archive_name
14 if not self.upstream_mod_zip:
15 self.archive = zipstream.ZipFile(allowZip64=True, compression=zipstream.ZIP_STORED if upstream_gzip else zipstream.ZIP_DEFLATED)
16 self.files = []
17 self.size = 0
18
19 def response(self):
20 if self.upstream_mod_zip:
21 yield "\n".join(self.files).encode()
22 else:
23 yield iter(self.archive)
24
25 def get_headers(self):
26 headers = {}
27 if self.archive_name:
28 headers['Content-Disposition'] = f'attachment; filename="{self.archive_name}.zip"'
29 if self.upstream_mod_zip:
30 headers['X-Archive-Files'] = 'zip'
31 else:
32 headers['Content-Type'] = 'application/x-zip-compressed'
33 return headers
34
35 def add_path(self, path, archive_name):
36 size = int(os.stat(path).st_size)
37 if self.upstream_mod_zip:
38 # calculating crc32 would defeat the point of using mod-zip, but if we ever calculate hashsums we should consider this
39 crc32 = "-"
40 line = f"{crc32} {size} {quote(path)} {archive_name}"
41 self.files.append(line)
42 else:
43 self.size += size
44 self.archive.write(path, archive_name)
45
46 def write(self, path, archive_name=None):
47 if os.path.isdir(path):
48 pardir = os.path.join(path, os.pardir)
49 for root, directories, files in safe_walk(path):
50 for directory in directories:
51 dir_path = os.path.join(root, directory)
52 self.add_path(dir_path, os.path.relpath(dir_path, pardir))
53 for file in files:
54 file_path = os.path.join(root, file)
55 self.add_path(file_path, os.path.relpath(file_path, pardir))
56 else:
57 self.add_path(path, archive_name or os.path.basename(path))