Skip to content

Commit baf376b

Browse files
authored
CM-34895 - Add debug option to export in-memory zip archive on disk (#219)
1 parent e1e159c commit baf376b

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

cycode/cli/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
CYCODE_CLI_REQUEST_TIMEOUT_ENV_VAR_NAME = 'CYCODE_CLI_REQUEST_TIMEOUT'
117117
LOGGING_LEVEL_ENV_VAR_NAME = 'LOGGING_LEVEL'
118118
VERBOSE_ENV_VAR_NAME = 'CYCODE_CLI_VERBOSE'
119+
DEBUG_ENV_VAR_NAME = 'CYCODE_CLI_DEBUG'
119120

120121
CYCODE_CONFIGURATION_DIRECTORY: str = '.cycode'
121122

cycode/cli/files_collector/models/in_memory_zip.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from io import BytesIO
22
from sys import getsizeof
3-
from typing import Optional
3+
from typing import TYPE_CHECKING, Optional
44
from zipfile import ZIP_DEFLATED, ZipFile
55

6+
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
67
from cycode.cli.utils.path_utils import concat_unique_id
78

9+
if TYPE_CHECKING:
10+
from pathlib import Path
11+
812

913
class InMemoryZip(object):
1014
def __init__(self) -> None:
15+
self.configuration_manager = ConfigurationManager()
16+
1117
# Create the in-memory file-like object
1218
self.in_memory_zip = BytesIO()
1319
self.zip = ZipFile(self.in_memory_zip, 'a', ZIP_DEFLATED, False)
@@ -27,6 +33,10 @@ def read(self) -> bytes:
2733
self.in_memory_zip.seek(0)
2834
return self.in_memory_zip.read()
2935

36+
def write_on_disk(self, path: 'Path') -> None:
37+
with open(path, 'wb') as f:
38+
f.write(self.read())
39+
3040
@property
3141
def size(self) -> int:
3242
return getsizeof(self.in_memory_zip)

cycode/cli/files_collector/zip_documents.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
from pathlib import Path
23
from typing import List, Optional
34

45
from cycode.cli import consts
@@ -37,4 +38,9 @@ def zip_documents(scan_type: str, documents: List[Document], zip_file: Optional[
3738
zip_creation_time = int(end_zip_creation_time - start_zip_creation_time)
3839
logger.debug('finished to create zip file, %s', {'zip_creation_time': zip_creation_time})
3940

41+
if zip_file.configuration_manager.get_debug_flag():
42+
zip_file_path = Path.joinpath(Path.cwd(), f'{scan_type}_scan_{end_zip_creation_time}.zip')
43+
logger.debug('writing zip file to disk, %s', {'zip_file_path': zip_file_path})
44+
zip_file.write_on_disk(zip_file_path)
45+
4046
return zip_file

cycode/cli/user_settings/configuration_manager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def get_cycode_app_url(self) -> str:
4646

4747
return consts.DEFAULT_CYCODE_APP_URL
4848

49+
def get_debug_flag_from_environment_variables(self) -> bool:
50+
value = self._get_value_from_environment_variables(consts.DEBUG_ENV_VAR_NAME, '')
51+
return value.lower() in {'true', '1'}
52+
53+
def get_debug_flag(self) -> bool:
54+
return self.get_debug_flag_from_environment_variables()
55+
4956
def get_verbose_flag(self) -> bool:
5057
verbose_flag_env_var = self.get_verbose_flag_from_environment_variables()
5158
verbose_flag_local_config = self.local_config_file_manager.get_verbose_flag()

0 commit comments

Comments
 (0)