Skip to content

Commit

Permalink
CM-40907 format
Browse files Browse the repository at this point in the history
  • Loading branch information
naftalicy committed Oct 21, 2024
1 parent 70176cc commit ee7a5c6
Showing 10 changed files with 69 additions and 51 deletions.
12 changes: 8 additions & 4 deletions cycode/cli/exceptions/custom_exceptions.py
Original file line number Diff line number Diff line change
@@ -7,16 +7,20 @@ class CycodeError(Exception):
"""Base class for all custom exceptions"""


class RequestError(CycodeError): ...
class RequestError(CycodeError):
...


class RequestTimeout(RequestError): ...
class RequestTimeout(RequestError):
...


class RequestConnectionError(RequestError): ...
class RequestConnectionError(RequestError):
...


class RequestSslError(RequestConnectionError): ...
class RequestSslError(RequestConnectionError):
...


class RequestHttpError(RequestError):
12 changes: 6 additions & 6 deletions cycode/cli/files_collector/sca/base_restore_dependencies.py
Original file line number Diff line number Diff line change
@@ -13,10 +13,9 @@ def build_dep_tree_path(path: str, generated_file_name: str) -> str:
return join_paths(get_file_dir(path), generated_file_name)


def execute_command(command: List[str],
file_name: str,
command_timeout: int,
dependencies_file_name: Optional[str] = None) -> Optional[str]:
def execute_command(
command: List[str], file_name: str, command_timeout: int, dependencies_file_name: Optional[str] = None
) -> Optional[str]:
try:
dependencies = shell(command=command, timeout=command_timeout)
# Write stdout output to the file if output_file_path is provided
@@ -31,8 +30,9 @@ def execute_command(command: List[str],


class BaseRestoreDependencies(ABC):
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int,
create_output_file_manually: bool = False) -> None:
def __init__(
self, context: click.Context, is_git_diff: bool, command_timeout: int, create_output_file_manually: bool = False
) -> None:
self.context = context
self.is_git_diff = is_git_diff
self.command_timeout = command_timeout
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
return restore_dependencies_document

def restore_from_secondary_command(
self, document: Document, manifest_file_path: str, restore_dependencies_document: Optional[Document]
self, document: Document, manifest_file_path: str, restore_dependencies_document: Optional[Document]
) -> Optional[Document]:
# TODO(MarshalX): does it even work? Ignored restore_dependencies_document arg
secondary_restore_command = create_secondary_restore_command(manifest_file_path)
11 changes: 9 additions & 2 deletions cycode/cli/files_collector/sca/npm/restore_npm_dependencies.py
Original file line number Diff line number Diff line change
@@ -19,8 +19,15 @@ def is_project(self, document: Document) -> bool:
return any(document.path.endswith(ext) for ext in NPM_PROJECT_FILE_EXTENSIONS)

def get_command(self, manifest_file_path: str) -> List[str]:
return ['npm', 'install', '--prefix', self.prepare_manifest_file_path_for_command(manifest_file_path),
'--package-lock-only', '--ignore-scripts', '--no-audit']
return [
'npm',
'install',
'--prefix',
self.prepare_manifest_file_path_for_command(manifest_file_path),
'--package-lock-only',
'--ignore-scripts',
'--no-audit',
]

def get_lock_file_name(self) -> str:
return NPM_LOCK_FILE_NAME
30 changes: 15 additions & 15 deletions cycode/cli/files_collector/sca/sca_code_scanner.py
Original file line number Diff line number Diff line change
@@ -23,27 +23,27 @@


def perform_pre_commit_range_scan_actions(
path: str,
from_commit_documents: List[Document],
from_commit_rev: str,
to_commit_documents: List[Document],
to_commit_rev: str,
path: str,
from_commit_documents: List[Document],
from_commit_rev: str,
to_commit_documents: List[Document],
to_commit_rev: str,
) -> None:
repo = git_proxy.get_repo(path)
add_ecosystem_related_files_if_exists(from_commit_documents, repo, from_commit_rev)
add_ecosystem_related_files_if_exists(to_commit_documents, repo, to_commit_rev)


def perform_pre_hook_range_scan_actions(
git_head_documents: List[Document], pre_committed_documents: List[Document]
git_head_documents: List[Document], pre_committed_documents: List[Document]
) -> None:
repo = git_proxy.get_repo(os.getcwd())
add_ecosystem_related_files_if_exists(git_head_documents, repo, consts.GIT_HEAD_COMMIT_REV)
add_ecosystem_related_files_if_exists(pre_committed_documents)


def add_ecosystem_related_files_if_exists(
documents: List[Document], repo: Optional['Repo'] = None, commit_rev: Optional[str] = None
documents: List[Document], repo: Optional['Repo'] = None, commit_rev: Optional[str] = None
) -> None:
documents_to_add: List[Document] = []
for doc in documents:
@@ -58,7 +58,7 @@ def add_ecosystem_related_files_if_exists(


def get_doc_ecosystem_related_project_files(
doc: Document, documents: List[Document], ecosystem: str, commit_rev: Optional[str], repo: Optional['Repo']
doc: Document, documents: List[Document], ecosystem: str, commit_rev: Optional[str], repo: Optional['Repo']
) -> List[Document]:
documents_to_add: List[Document] = []
for ecosystem_project_file in consts.PROJECT_FILES_BY_ECOSYSTEM_MAP.get(ecosystem):
@@ -88,10 +88,10 @@ def get_project_file_ecosystem(document: Document) -> Optional[str]:


def try_restore_dependencies(
context: click.Context,
documents_to_add: Dict[str, Document],
restore_dependencies: 'BaseRestoreDependencies',
document: Document,
context: click.Context,
documents_to_add: Dict[str, Document],
restore_dependencies: 'BaseRestoreDependencies',
document: Document,
) -> None:
if restore_dependencies.is_project(document):
restore_dependencies_document = restore_dependencies.restore(document)
@@ -117,7 +117,7 @@ def try_restore_dependencies(


def add_dependencies_tree_document(
context: click.Context, documents_to_scan: List[Document], is_git_diff: bool = False
context: click.Context, documents_to_scan: List[Document], is_git_diff: bool = False
) -> None:
documents_to_add: Dict[str, Document] = {}
restore_dependencies_list = restore_handlers(context, is_git_diff)
@@ -134,7 +134,7 @@ def restore_handlers(context: click.Context, is_git_diff: bool) -> List[BaseRest
RestoreGradleDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
RestoreMavenDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
RestoreNugetDependencies(context, is_git_diff, BUILD_NUGET_DEP_TREE_TIMEOUT),
RestoreNpmDependencies(context, is_git_diff, BUILD_NPM_DEP_TREE_TIMEOUT)
RestoreNpmDependencies(context, is_git_diff, BUILD_NPM_DEP_TREE_TIMEOUT),
]


@@ -150,7 +150,7 @@ def get_file_content_from_commit(repo: 'Repo', commit: str, file_path: str) -> O


def perform_pre_scan_documents_actions(
context: click.Context, scan_type: str, documents_to_scan: List[Document], is_git_diff: bool = False
context: click.Context, scan_type: str, documents_to_scan: List[Document], is_git_diff: bool = False
) -> None:
if scan_type == consts.SCA_SCAN_TYPE and not context.obj.get(consts.SCA_SKIP_RESTORE_DEPENDENCIES_FLAG):
logger.debug('Perform pre-scan document add_dependencies_tree_document action')
3 changes: 2 additions & 1 deletion cycode/cli/user_settings/base_file_manager.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@

class BaseFileManager(ABC):
@abstractmethod
def get_filename(self) -> str: ...
def get_filename(self) -> str:
...

def read_file(self) -> Dict[Hashable, Any]:
return read_file(self.get_filename())
12 changes: 8 additions & 4 deletions cycode/cli/utils/git_proxy.py
Original file line number Diff line number Diff line change
@@ -25,16 +25,20 @@ class GitProxyError(Exception):

class _AbstractGitProxy(ABC):
@abstractmethod
def get_repo(self, path: Optional['PathLike'] = None, *args, **kwargs) -> 'Repo': ...
def get_repo(self, path: Optional['PathLike'] = None, *args, **kwargs) -> 'Repo':
...

@abstractmethod
def get_null_tree(self) -> object: ...
def get_null_tree(self) -> object:
...

@abstractmethod
def get_invalid_git_repository_error(self) -> Type[BaseException]: ...
def get_invalid_git_repository_error(self) -> Type[BaseException]:
...

@abstractmethod
def get_git_command_error(self) -> Type[BaseException]: ...
def get_git_command_error(self) -> Type[BaseException]:
...


class _DummyGitProxy(_AbstractGitProxy):
21 changes: 14 additions & 7 deletions cycode/cli/utils/progress_bar.py
Original file line number Diff line number Diff line change
@@ -92,25 +92,32 @@ def __init__(self, *args, **kwargs) -> None:
pass

@abstractmethod
def __enter__(self) -> 'BaseProgressBar': ...
def __enter__(self) -> 'BaseProgressBar':
...

@abstractmethod
def __exit__(self, *args, **kwargs) -> None: ...
def __exit__(self, *args, **kwargs) -> None:
...

@abstractmethod
def start(self) -> None: ...
def start(self) -> None:
...

@abstractmethod
def stop(self) -> None: ...
def stop(self) -> None:
...

@abstractmethod
def set_section_length(self, section: 'ProgressBarSection', length: int = 0) -> None: ...
def set_section_length(self, section: 'ProgressBarSection', length: int = 0) -> None:
...

@abstractmethod
def update(self, section: 'ProgressBarSection') -> None: ...
def update(self, section: 'ProgressBarSection') -> None:
...

@abstractmethod
def update_label(self, label: Optional[str] = None) -> None: ...
def update_label(self, label: Optional[str] = None) -> None:
...


class DummyProgressBar(BaseProgressBar):
11 changes: 2 additions & 9 deletions cycode/cli/utils/shell_executor.py
Original file line number Diff line number Diff line change
@@ -8,18 +8,11 @@
_SUBPROCESS_DEFAULT_TIMEOUT_SEC = 60


def shell(
command: Union[str, List[str]], timeout: int = _SUBPROCESS_DEFAULT_TIMEOUT_SEC
) -> Optional[str]:
def shell(command: Union[str, List[str]], timeout: int = _SUBPROCESS_DEFAULT_TIMEOUT_SEC) -> Optional[str]:
logger.debug('Executing shell command: %s', command)

try:
result = subprocess.run(
command,
timeout=timeout,
check=True,
capture_output=True
)
result = subprocess.run(command, timeout=timeout, check=True, capture_output=True)

Check failure on line 15 in cycode/cli/utils/shell_executor.py

GitHub Actions / ruff

Ruff (S603)

cycode/cli/utils/shell_executor.py:15:18: S603 `subprocess` call: check for execution of untrusted input

return result.stdout.decode('UTF-8').strip()
except subprocess.CalledProcessError as e:
6 changes: 4 additions & 2 deletions cycode/cyclient/scan_config_base.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@

class ScanConfigBase(ABC):
@abstractmethod
def get_service_name(self, scan_type: str, should_use_scan_service: bool = False) -> str: ...
def get_service_name(self, scan_type: str, should_use_scan_service: bool = False) -> str:
...

@staticmethod
def get_async_scan_type(scan_type: str) -> str:
@@ -24,7 +25,8 @@ def get_async_entity_type(scan_type: str) -> str:
return 'repository'

@abstractmethod
def get_detections_prefix(self) -> str: ...
def get_detections_prefix(self) -> str:
...


class DevScanConfig(ScanConfigBase):

0 comments on commit ee7a5c6

Please sign in to comment.