-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathzip_documents.py
More file actions
53 lines (40 loc) · 1.81 KB
/
Copy pathzip_documents.py
File metadata and controls
53 lines (40 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import timeit
from pathlib import Path
from typing import Optional
from cycode.cli import consts
from cycode.cli.exceptions import custom_exceptions
from cycode.cli.files_collector.models.in_memory_zip import InMemoryZip
from cycode.cli.models import Document
from cycode.logger import get_logger
logger = get_logger('ZIP')
def _validate_zip_file_size(scan_type: str, zip_file_size: int) -> None:
max_size_limit = consts.ZIP_MAX_SIZE_LIMIT_IN_BYTES.get(scan_type, consts.DEFAULT_ZIP_MAX_SIZE_LIMIT_IN_BYTES)
if zip_file_size > max_size_limit:
raise custom_exceptions.ZipTooLargeError(max_size_limit)
def zip_documents(
scan_type: str,
documents: list[Document],
zip_file: Optional[InMemoryZip] = None,
) -> InMemoryZip:
if zip_file is None:
zip_file = InMemoryZip()
start_zip_creation_time = timeit.default_timer()
for index, document in enumerate(documents):
_validate_zip_file_size(scan_type, zip_file.size)
logger.debug(
'Adding file to ZIP, %s',
{'index': index, 'filename': document.path, 'unique_id': document.unique_id},
)
zip_file.append(document.path, document.unique_id, document.content)
zip_file.close()
end_zip_creation_time = timeit.default_timer()
zip_creation_time = int(end_zip_creation_time - start_zip_creation_time)
logger.debug(
'Finished to create ZIP file, %s',
{'zip_creation_time': zip_creation_time, 'zip_size': zip_file.size, 'documents_count': len(documents)},
)
if zip_file.configuration_manager.get_debug_flag():
zip_file_path = Path.joinpath(Path.cwd(), f'{scan_type}_scan_{end_zip_creation_time}.zip')
logger.debug('Writing ZIP file to disk, %s', {'zip_file_path': zip_file_path})
zip_file.write_on_disk(zip_file_path)
return zip_file