cycode 3.27.1.dev11__py3-none-any.whl → 3.27.1.dev12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cycode/__init__.py CHANGED
@@ -5,4 +5,4 @@ import time as _time
5
5
  # end-to-end scan duration from the moment the user actually triggered it.
6
6
  _BOOT_WALL: float = _time.time()
7
7
 
8
- __version__ = '3.27.1.dev11' # DON'T TOUCH. Placeholder. Will be filled automatically on poetry build from Git Tag
8
+ __version__ = '3.27.1.dev12' # DON'T TOUCH. Placeholder. Will be filled automatically on poetry build from Git Tag
@@ -1,6 +1,7 @@
1
1
  import os
2
2
  import platform
3
3
  import re
4
+ from pathlib import Path
4
5
 
5
6
  import typer
6
7
 
@@ -8,11 +9,17 @@ from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestore
8
9
  from cycode.cli.models import Document
9
10
  from cycode.cli.utils.path_utils import get_path_from_context
10
11
  from cycode.cli.utils.shell_executor import shell
12
+ from cycode.logger import get_logger
13
+
14
+ logger = get_logger('Gradle Restore')
11
15
 
12
16
  BUILD_GRADLE_FILE_NAME = 'build.gradle'
13
17
  BUILD_GRADLE_KTS_FILE_NAME = 'build.gradle.kts'
14
18
  BUILD_GRADLE_DEP_TREE_FILE_NAME = 'gradle-dependencies-generated.txt'
15
19
  ALL_PROJECTS_REGEX = r"[+-]{3} Project '(.*?)'"
20
+ PROJECT_LOCATION_REGEX = r"^project '(.+?)' - (.+?)\s*$"
21
+ CURRENT_PROJECT_REGEX = r"^Project '(.+?)'\s*$"
22
+ ROOT_PROJECT_PATH = ':'
16
23
 
17
24
  GRADLE_EXECUTABLE = 'gradle'
18
25
  GRADLEW_FILE_NAME = 'gradlew'
@@ -27,6 +34,7 @@ class RestoreGradleDependencies(BaseRestoreDependencies):
27
34
  self.gradle_executable = self._resolve_gradle_executable()
28
35
  if projects is None:
29
36
  projects = set()
37
+ self.project_locations: dict[Path, str] = {}
30
38
  self.projects = self.get_all_projects() if self.is_gradle_sub_projects() else projects
31
39
 
32
40
  def _resolve_gradle_executable(self) -> str:
@@ -45,11 +53,13 @@ class RestoreGradleDependencies(BaseRestoreDependencies):
45
53
  return document.path.endswith(BUILD_GRADLE_FILE_NAME) or document.path.endswith(BUILD_GRADLE_KTS_FILE_NAME)
46
54
 
47
55
  def get_commands(self, manifest_file_path: str) -> list[list[str]]:
48
- return (
49
- self.get_commands_for_sub_projects(manifest_file_path)
50
- if self.is_gradle_sub_projects()
51
- else [[self.gradle_executable, 'dependencies', '-b', manifest_file_path, '-q', '--console', 'plain']]
52
- )
56
+ if self.is_gradle_sub_projects():
57
+ return self.get_commands_for_sub_projects(manifest_file_path)
58
+
59
+ # Gradle finds the root settings file above it and selects the project whose projectDir
60
+ # is that directory.
61
+ manifest_dir = str(Path(manifest_file_path).parent)
62
+ return [[self.gradle_executable, '-p', manifest_dir, 'dependencies', '-q', '--console', 'plain']]
53
63
 
54
64
  def get_lock_file_name(self) -> str:
55
65
  return BUILD_GRADLE_DEP_TREE_FILE_NAME
@@ -61,21 +71,66 @@ class RestoreGradleDependencies(BaseRestoreDependencies):
61
71
  return get_path_from_context(self.ctx) if self.is_gradle_sub_projects() else None
62
72
 
63
73
  def get_all_projects(self) -> set[str]:
74
+ scan_root = get_path_from_context(self.ctx)
64
75
  output = shell(
65
76
  command=[self.gradle_executable, 'projects'],
66
77
  timeout=self.command_timeout,
67
- working_directory=get_path_from_context(self.ctx),
78
+ working_directory=scan_root,
68
79
  )
69
80
  if not output:
70
81
  return set()
71
82
 
83
+ if scan_root:
84
+ self.project_locations = self._parse_project_locations(output, Path(scan_root))
85
+
72
86
  return set(re.findall(ALL_PROJECTS_REGEX, output))
73
87
 
88
+ @staticmethod
89
+ def _parse_project_locations(output: str, scan_root: Path) -> dict[Path, str]:
90
+ """Map each project directory to its Gradle project path, from the "Project locations" section."""
91
+ # A location starts with a separator ("/x/y"). Strip it, or pathlib treats it as an absolute path.
92
+ relative_dirs = {
93
+ project_path: Path(relative_dir.lstrip('/\\'))
94
+ for project_path, relative_dir in re.findall(PROJECT_LOCATION_REGEX, output, flags=re.MULTILINE)
95
+ }
96
+ if not relative_dirs:
97
+ return {}
98
+
99
+ build_root = RestoreGradleDependencies._find_build_root(output, scan_root, relative_dirs)
100
+ locations = {(build_root / relative_dir).resolve(): path for path, relative_dir in relative_dirs.items()}
101
+ locations[build_root] = ROOT_PROJECT_PATH
102
+ return locations
103
+
104
+ @staticmethod
105
+ def _find_build_root(output: str, scan_root: Path, relative_dirs: dict[str, Path]) -> Path:
106
+ build_root = scan_root.resolve()
107
+ current_project = re.search(CURRENT_PROJECT_REGEX, output, flags=re.MULTILINE)
108
+ if current_project and current_project.group(1) in relative_dirs:
109
+ for _ in relative_dirs[current_project.group(1)].parts:
110
+ build_root = build_root.parent
111
+
112
+ return build_root
113
+
114
+ def _get_sub_project_path(self, manifest_file_path: str) -> str | None:
115
+ manifest_dir = Path(manifest_file_path).parent
116
+ if self.project_locations:
117
+ return self.project_locations.get(manifest_dir.resolve())
118
+
119
+ scan_root = get_path_from_context(self.ctx)
120
+ if self.projects and scan_root and manifest_dir.resolve() == Path(scan_root).resolve():
121
+ return ROOT_PROJECT_PATH
122
+
123
+ project_path = f':{manifest_dir.name}'
124
+ return project_path if project_path in self.projects else None
125
+
74
126
  def get_commands_for_sub_projects(self, manifest_file_path: str) -> list[list[str]]:
75
- project_name = os.path.basename(os.path.dirname(manifest_file_path))
76
- project_name = f':{project_name}'
77
- return (
78
- [[self.gradle_executable, f'{project_name}:dependencies', '-q', '--console', 'plain']]
79
- if project_name in self.projects
80
- else []
81
- )
127
+ project_path = self._get_sub_project_path(manifest_file_path)
128
+ if project_path is None:
129
+ logger.debug(
130
+ 'Manifest does not match a Gradle project, skipping restore, %s',
131
+ {'manifest_file_path': manifest_file_path},
132
+ )
133
+ return []
134
+
135
+ task = 'dependencies' if project_path == ROOT_PROJECT_PATH else f'{project_path}:dependencies'
136
+ return [[self.gradle_executable, task, '-q', '--console', 'plain']]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cycode
3
- Version: 3.27.1.dev11
3
+ Version: 3.27.1.dev12
4
4
  Summary: Boost security in your dev lifecycle via SAST, SCA, Secrets & IaC scanning.
5
5
  License-Expression: MIT
6
6
  License-File: LICENCE
@@ -1,4 +1,4 @@
1
- cycode/__init__.py,sha256=662oEouYyywiE4bzqGxYuM04FRJMS1yiJ_No3tvbCMY,397
1
+ cycode/__init__.py,sha256=p4A2yXn98TDpXOiNwao5YOloZufgIDJspKsAh7ll174,397
2
2
  cycode/__main__.py,sha256=Z3bD5yrA7yPvAChcADQrqCaZd0ChGI1gdiwALwbWJ6U,104
3
3
  cycode/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  cycode/cli/app.py,sha256=sI6wBHmfo7RchO9rnOXaQyUHRG9fm4r9aIduV4ZOD5c,10167
@@ -122,7 +122,7 @@ cycode/cli/files_collector/sca/base_restore_dependencies.py,sha256=8wMKoh15CYLcm
122
122
  cycode/cli/files_collector/sca/go/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
123
123
  cycode/cli/files_collector/sca/go/restore_go_dependencies.py,sha256=_i4UBfhQ97Z14OYxVj5iUuckMSiVtbw-vYCBOowIHKQ,1868
124
124
  cycode/cli/files_collector/sca/maven/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
125
- cycode/cli/files_collector/sca/maven/restore_gradle_dependencies.py,sha256=4ajpSFkVHgz1U_LBYTeK5U7zJ7B8wQMsaIhQPKrZv9w,3227
125
+ cycode/cli/files_collector/sca/maven/restore_gradle_dependencies.py,sha256=Gf7KO3VTwVfmUgkhYFDjIT8bTUmA_KdHrvE5n729XmI,5848
126
126
  cycode/cli/files_collector/sca/maven/restore_maven_dependencies.py,sha256=zaXirOpVDMiZRUVjQzrHoLBfUpeAdP3ol_whNBbXkCY,3966
127
127
  cycode/cli/files_collector/sca/npm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
128
128
  cycode/cli/files_collector/sca/npm/restore_bun_dependencies.py,sha256=r5MdGsezSRNeBMhGwxF1sPUMGr27SrQQC72-W0Aokz8,4817
@@ -224,8 +224,8 @@ cycode/cyclient/report_client.py,sha256=PaKEo6Q0GFVdpNC18vvR4uBoUX1MnllVmKkS72eA
224
224
  cycode/cyclient/scan_client.py,sha256=ft80sGTsW-BW4bECuGdfLv0CrSdIkp_RLlGfwHVf_Fs,17325
225
225
  cycode/cyclient/scan_config_base.py,sha256=mXsPZGYCtp85rv5GIige40yQZXuRcEKUW-VQJ0vgFzk,1201
226
226
  cycode/logger.py,sha256=OHmfkf4dnyZgfixGqkWf3js3uysrOXU_4cS8TChtAzc,2079
227
- cycode-3.27.1.dev11.dist-info/METADATA,sha256=RVOTrkHAfCBqOQpDvT3CgsnPmhN8_yuRa4GsFjQOYl4,97033
228
- cycode-3.27.1.dev11.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
229
- cycode-3.27.1.dev11.dist-info/entry_points.txt,sha256=iDcVJM8ByLElVgvBgtYxDjw1kT7O8Mo0LcWZIT5L3Ig,45
230
- cycode-3.27.1.dev11.dist-info/licenses/LICENCE,sha256=2Wx4N6mD_4xB7-E3hPkZ3MPhpJy__k_I8MaCSO-PDRo,1068
231
- cycode-3.27.1.dev11.dist-info/RECORD,,
227
+ cycode-3.27.1.dev12.dist-info/METADATA,sha256=IR0EKmtdVs1rPWEf9-gINkMCsJ_QmnJM8wFLdG-TCQw,97033
228
+ cycode-3.27.1.dev12.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
229
+ cycode-3.27.1.dev12.dist-info/entry_points.txt,sha256=iDcVJM8ByLElVgvBgtYxDjw1kT7O8Mo0LcWZIT5L3Ig,45
230
+ cycode-3.27.1.dev12.dist-info/licenses/LICENCE,sha256=2Wx4N6mD_4xB7-E3hPkZ3MPhpJy__k_I8MaCSO-PDRo,1068
231
+ cycode-3.27.1.dev12.dist-info/RECORD,,