Skip to content

Commit bb01104

Browse files
committed
Update arg in PythonAnalysis facade
Signed-off-by: Rahul Krishna <rkrsn@ibm.com>
1 parent cd8d53d commit bb01104

5 files changed

Lines changed: 64 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ Each language has a dedicated analysis backend implemented under `cldk.analysis.
217217
> Jedi-only analysis. The CodeQL flag is part of the analysis cache key, so
218218
> toggling it — or upgrading from a version that defaulted it off — triggers
219219
> a **one-time** rebuild under a new key (no stale data is served). If you
220-
> instead point `analysis_backend_path` / `analysis_json_path` inside a
221-
> project, add those directories to your `.gitignore` — they are large and
222-
> environment-specific.
220+
> instead point `cache_dir` (the backend virtualenv / CodeQL database) or
221+
> `analysis_json_path` inside a project, add those directories to your
222+
> `.gitignore` — they are large and environment-specific.
223223
224224
#### C
225225
- **Backend:** `cldk.analysis.c`

cldk/analysis/python/codeanalyzer/codeanalyzer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class PyCodeanalyzer:
6666
:mod:`cldk.analysis.python.codeanalyzer.cache`).
6767
eager_analysis: If True, always re-runs the analyzer even when a
6868
cached analysis.json is available.
69-
analysis_backend_path: Cache directory for the analyzer's virtualenv
70-
and CodeQL artifacts. Forwarded to ``AnalysisOptions.cache_dir``.
69+
cache_dir: Cache directory for the analyzer's virtualenv and CodeQL
70+
artifacts. Forwarded verbatim to ``AnalysisOptions.cache_dir``.
7171
When omitted, a dependency-hash-keyed location under the CLDK
7272
cache root is used so the virtualenv survives source edits.
7373
target_files: Optional single target file (relative to project_dir).
@@ -80,7 +80,7 @@ def __init__(
8080
analysis_level: str,
8181
analysis_json_path: Union[str, Path, None],
8282
eager_analysis: bool,
83-
analysis_backend_path: Union[str, Path, None] = None,
83+
cache_dir: Union[str, Path, None] = None,
8484
target_files: List[str] | None = None,
8585
use_codeql: bool = True,
8686
) -> None:
@@ -94,10 +94,10 @@ def __init__(
9494

9595
# Cache locations. Explicit args win; otherwise fall back to the
9696
# content-addressed CLDK cache (two independently-keyed tiers).
97-
if analysis_backend_path:
98-
self.analysis_backend_path = Path(analysis_backend_path)
97+
if cache_dir:
98+
self.cache_dir = Path(cache_dir)
9999
else:
100-
self.analysis_backend_path = default_backend_cache_dir(self.project_dir)
100+
self.cache_dir = default_backend_cache_dir(self.project_dir)
101101
if analysis_json_path:
102102
self.analysis_json_path = Path(analysis_json_path)
103103
else:
@@ -106,7 +106,7 @@ def __init__(
106106
)
107107
logger.info(
108108
"CLDK cache — backend: %s | analysis: %s",
109-
self.analysis_backend_path,
109+
self.cache_dir,
110110
self.analysis_json_path,
111111
)
112112

@@ -145,7 +145,7 @@ def _load_or_run_analyzer(self) -> PyApplication:
145145
rebuild_analysis=self.eager_analysis,
146146
skip_tests=True,
147147
file_name=target_file,
148-
cache_dir=self.analysis_backend_path,
148+
cache_dir=self.cache_dir,
149149
clear_cache=False,
150150
verbosity=0,
151151
)

cldk/analysis/python/python_analysis.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ class PythonAnalysis:
4747
4848
Args:
4949
project_dir: Directory path of the project (required).
50-
analysis_backend_path: Cache directory for the backend's virtualenv
51-
and intermediate artifacts.
50+
cache_dir: Writable directory where the ``codeanalyzer-python``
51+
backend provisions its virtualenv and CodeQL database (forwarded
52+
as the backend's ``cache_dir``). If None, a dependency-hash-keyed
53+
location under the CLDK cache root is used.
5254
analysis_json_path: Directory to persist analysis.json. If None, the
5355
analysis is not persisted across runs.
5456
analysis_level: Analysis level (symbol-table or call-graph).
@@ -59,7 +61,7 @@ class PythonAnalysis:
5961
def __init__(
6062
self,
6163
project_dir: str | Path,
62-
analysis_backend_path: str | None,
64+
cache_dir: str | Path | None,
6365
analysis_json_path: str | Path | None,
6466
analysis_level: str,
6567
target_files: List[str] | None,
@@ -73,7 +75,7 @@ def __init__(
7375
self.project_dir = project_dir
7476
self.analysis_level = analysis_level
7577
self.analysis_json_path = analysis_json_path
76-
self.analysis_backend_path = analysis_backend_path
78+
self.cache_dir = cache_dir
7779
self.eager_analysis = eager_analysis
7880
self.target_files = target_files
7981
self.treesitter_python: TreesitterPython = TreesitterPython()
@@ -82,7 +84,7 @@ def __init__(
8284
analysis_level=analysis_level,
8385
analysis_json_path=analysis_json_path,
8486
eager_analysis=eager_analysis,
85-
analysis_backend_path=analysis_backend_path,
87+
cache_dir=cache_dir,
8688
target_files=target_files,
8789
use_codeql=use_codeql,
8890
)

cldk/core.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def analysis(
6161
target_files: List[str] | None = None,
6262
analysis_backend_path: str | None = None,
6363
analysis_json_path: str | Path = None,
64+
cache_dir: str | Path | None = None,
6465
use_codeql: bool = True,
6566
) -> JavaAnalysis | PythonAnalysis | CAnalysis:
6667
"""Initialize a language-specific analysis façade.
@@ -71,8 +72,16 @@ def analysis(
7172
eager (bool): If True, forces regeneration of analysis databases.
7273
analysis_level (str): Analysis level. See AnalysisLevel.
7374
target_files (list[str] | None): Files to constrain analysis (optional).
74-
analysis_backend_path (str | None): Path to the analysis backend.
75-
analysis_json_path (str | Path | None): Path to persist analysis database.
75+
analysis_backend_path (str | None): Java only. Directory containing
76+
the ``codeanalyzer-*.jar`` to run. Not valid for Python — pass
77+
``cache_dir`` instead.
78+
analysis_json_path (str | Path | None): Path to persist the analysis
79+
database / ``analysis.json``.
80+
cache_dir (str | Path | None): Python only. Writable directory where
81+
the ``codeanalyzer-python`` backend provisions its virtualenv and
82+
CodeQL database (forwarded as the backend's ``cache_dir``). When
83+
omitted, a dependency-hash-keyed location under the CLDK cache
84+
root is used. Ignored for other languages.
7685
use_codeql (bool): Python only, default True. Augments Jedi-resolved
7786
call edges with CodeQL-resolved edges; set False for a faster,
7887
Jedi-only analysis. Ignored for other languages.
@@ -81,7 +90,9 @@ def analysis(
8190
JavaAnalysis | PythonAnalysis | CAnalysis: Initialized analysis façade for the chosen language.
8291
8392
Raises:
84-
CldkInitializationException: If both or neither of project_path and source_code are provided.
93+
CldkInitializationException: If both or neither of project_path and
94+
source_code are provided, or if the Java-only
95+
``analysis_backend_path`` is passed in Python mode.
8596
NotImplementedError: If the specified language is unsupported.
8697
8798
Examples:
@@ -114,10 +125,15 @@ def analysis(
114125
elif self.language == "python":
115126
if source_code is not None:
116127
raise CldkInitializationException("source_code mode is not supported for Python; please pass project_path.")
128+
if analysis_backend_path is not None:
129+
raise CldkInitializationException(
130+
"analysis_backend_path is Java-only (it locates codeanalyzer-*.jar). "
131+
"For Python, use cache_dir for the backend's virtualenv/CodeQL cache."
132+
)
117133
return PythonAnalysis(
118134
project_dir=project_path,
119135
analysis_level=analysis_level,
120-
analysis_backend_path=analysis_backend_path,
136+
cache_dir=cache_dir,
121137
analysis_json_path=analysis_json_path,
122138
target_files=target_files,
123139
eager_analysis=eager,

tests/analysis/python/test_python_analysis.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,29 @@ def __init__(self, **kwargs):
6060
captured.clear()
6161
CLDK(language="python").analysis(project_path=tmp_path)
6262
assert captured["use_codeql"] is True
63+
64+
65+
def test_cache_dir_forwarded_through_facade(monkeypatch, tmp_path):
66+
"""cache_dir must reach the backend as cache_dir (not analysis_backend_path)."""
67+
captured = {}
68+
69+
class FakeBackend:
70+
def __init__(self, **kwargs):
71+
captured.update(kwargs)
72+
73+
monkeypatch.setattr(
74+
"cldk.analysis.python.python_analysis.PyCodeanalyzer", FakeBackend
75+
)
76+
77+
cache = tmp_path / "mycache"
78+
CLDK(language="python").analysis(project_path=tmp_path, cache_dir=cache)
79+
assert captured["cache_dir"] == cache
80+
assert "analysis_backend_path" not in captured
81+
82+
83+
def test_python_rejects_java_only_analysis_backend_path(tmp_path):
84+
"""analysis_backend_path is Java-only; Python mode must reject it."""
85+
with pytest.raises(CldkInitializationException, match="Java-only"):
86+
CLDK(language="python").analysis(
87+
project_path=tmp_path, analysis_backend_path="/some/jar/dir"
88+
)

0 commit comments

Comments
 (0)