Skip to content

Commit 5505186

Browse files
committed
CM-41217 Add support for SBT restore
1 parent bb22262 commit 5505186

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

cycode/cli/files_collector/sca/base_restore_dependencies.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ def build_dep_tree_path(path: str, generated_file_name: str) -> str:
1414

1515

1616
def execute_command(
17-
command: List[str], file_name: str, command_timeout: int, dependencies_file_name: Optional[str] = None
17+
command: List[str],
18+
file_name: str,
19+
command_timeout: int,
20+
dependencies_file_name: Optional[str] = None,
21+
working_directory: Optional[str] = None,
1822
) -> Optional[str]:
1923
try:
20-
dependencies = shell(command=command, timeout=command_timeout)
24+
dependencies = shell(command=command, timeout=command_timeout, working_directory=working_directory)
2125
# Write stdout output to the file if output_file_path is provided
2226
if dependencies_file_name:
2327
with open(dependencies_file_name, 'w') as output_file:
@@ -51,18 +55,26 @@ def get_manifest_file_path(self, document: Document) -> str:
5155
def try_restore_dependencies(self, document: Document) -> Optional[Document]:
5256
manifest_file_path = self.get_manifest_file_path(document)
5357
restore_file_path = build_dep_tree_path(document.path, self.get_lock_file_name())
58+
working_directory_path = self.get_working_directory(document)
5459

5560
if self.verify_restore_file_already_exist(restore_file_path):
5661
restore_file_content = get_file_content(restore_file_path)
5762
else:
5863
output_file_path = restore_file_path if self.create_output_file_manually else None
5964
execute_command(
60-
self.get_command(manifest_file_path), manifest_file_path, self.command_timeout, output_file_path
65+
self.get_command(manifest_file_path),
66+
manifest_file_path,
67+
self.command_timeout,
68+
output_file_path,
69+
working_directory_path,
6170
)
6271
restore_file_content = get_file_content(restore_file_path)
6372

6473
return Document(restore_file_path, restore_file_content, self.is_git_diff)
6574

75+
def get_working_directory(self, document: Document) -> Optional[str]:
76+
return None
77+
6678
@abstractmethod
6779
def verify_restore_file_already_exist(self, restore_file_path: str) -> bool:
6880
pass

cycode/cli/files_collector/sca/sbt/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
from typing import List, Optional
3+
4+
import click
5+
6+
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
7+
from cycode.cli.models import Document
8+
9+
SBT_PROJECT_FILE_EXTENSIONS = ['sbt']
10+
SBT_LOCK_FILE_NAME = 'build.sbt.lock'
11+
12+
13+
class RestoreSbtDependencies(BaseRestoreDependencies):
14+
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int) -> None:
15+
super().__init__(context, is_git_diff, command_timeout)
16+
17+
def is_project(self, document: Document) -> bool:
18+
return any(document.path.endswith(ext) for ext in SBT_PROJECT_FILE_EXTENSIONS)
19+
20+
def get_command(self, manifest_file_path: str) -> List[str]:
21+
return ['sbt', 'dependencyLockWrite', '--verbose']
22+
23+
def get_lock_file_name(self) -> str:
24+
return SBT_LOCK_FILE_NAME
25+
26+
def verify_restore_file_already_exist(self, restore_file_path: str) -> bool:
27+
return os.path.isfile(restore_file_path)
28+
29+
def get_working_directory(self, document: Document) -> Optional[str]:
30+
return os.path.dirname(document.path)

cycode/cli/files_collector/sca/sca_code_scanner.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
88
from cycode.cli.files_collector.sca.maven.restore_gradle_dependencies import RestoreGradleDependencies
99
from cycode.cli.files_collector.sca.maven.restore_maven_dependencies import RestoreMavenDependencies
10-
from cycode.cli.files_collector.sca.npm.restore_npm_dependencies import RestoreNpmDependencies
11-
from cycode.cli.files_collector.sca.nuget.restore_nuget_dependencies import RestoreNugetDependencies
10+
from cycode.cli.files_collector.sca.sbt.restore_sbt_dependencies import RestoreSbtDependencies
1211
from cycode.cli.models import Document
1312
from cycode.cli.utils.git_proxy import git_proxy
1413
from cycode.cli.utils.path_utils import get_file_content, get_file_dir, get_path_from_context, join_paths
@@ -17,9 +16,7 @@
1716
if TYPE_CHECKING:
1817
from git import Repo
1918

20-
BUILD_GRADLE_DEP_TREE_TIMEOUT = 180
21-
BUILD_NUGET_DEP_TREE_TIMEOUT = 180
22-
BUILD_NPM_DEP_TREE_TIMEOUT = 180
19+
BUILD_DEP_TREE_TIMEOUT = 180
2320

2421

2522
def perform_pre_commit_range_scan_actions(
@@ -132,10 +129,9 @@ def add_dependencies_tree_document(
132129

133130
def restore_handlers(context: click.Context, is_git_diff: bool) -> List[BaseRestoreDependencies]:
134131
return [
135-
RestoreGradleDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
136-
RestoreMavenDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
137-
RestoreNugetDependencies(context, is_git_diff, BUILD_NUGET_DEP_TREE_TIMEOUT),
138-
RestoreNpmDependencies(context, is_git_diff, BUILD_NPM_DEP_TREE_TIMEOUT),
132+
RestoreGradleDependencies(context, is_git_diff, BUILD_DEP_TREE_TIMEOUT),
133+
RestoreMavenDependencies(context, is_git_diff, BUILD_DEP_TREE_TIMEOUT),
134+
RestoreSbtDependencies(context, is_git_diff, BUILD_DEP_TREE_TIMEOUT),
139135
]
140136

141137

cycode/cli/utils/shell_executor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
_SUBPROCESS_DEFAULT_TIMEOUT_SEC = 60
99

1010

11-
def shell(command: Union[str, List[str]], timeout: int = _SUBPROCESS_DEFAULT_TIMEOUT_SEC) -> Optional[str]:
11+
def shell(
12+
command: Union[str, List[str]],
13+
timeout: int = _SUBPROCESS_DEFAULT_TIMEOUT_SEC,
14+
working_directory: Optional[str] = None,
15+
) -> Optional[str]:
1216
logger.debug('Executing shell command: %s', command)
1317

1418
try:
1519
result = subprocess.run( # noqa: S603
16-
command, timeout=timeout, check=True, capture_output=True
20+
command, cwd=working_directory, timeout=timeout, check=True, capture_output=True
1721
)
1822

1923
return result.stdout.decode('UTF-8').strip()

0 commit comments

Comments
 (0)