|
| 1 | +################################################################################ |
| 2 | +# Copyright IBM Corporation 2026 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +################################################################################ |
| 16 | + |
| 17 | +"""Backend configuration objects for the CLDK analysis facades. |
| 18 | +
|
| 19 | +The CLDK front end selects analysis behavior along two orthogonal axes: the *language* (chosen by |
| 20 | +which :class:`~cldk.core.CLDK` factory method is called) and the *backend* (chosen by the **type** |
| 21 | +of the configuration object passed as ``backend=``). The dataclasses here are those configuration |
| 22 | +objects -- Parameter Objects that the facades ingest and dispatch on. |
| 23 | +
|
| 24 | +Two backend families exist: |
| 25 | +
|
| 26 | +* :class:`CodeAnalyzerConfig` (and its language-specific subclasses) selects the in-process |
| 27 | + codeanalyzer backend, which runs the packaged ``codeanalyzer-*`` binary and caches its |
| 28 | + ``analysis.json`` under a language-keyed cache directory. |
| 29 | +* :class:`Neo4jConnectionConfig` selects the read-only Neo4j/Cypher backend, which answers the |
| 30 | + same queries over a graph populated out of band. |
| 31 | +
|
| 32 | +The per-language ``*Backend`` unions below are the discriminated unions the facades match on. |
| 33 | +""" |
| 34 | + |
| 35 | +from __future__ import annotations |
| 36 | + |
| 37 | +from dataclasses import dataclass |
| 38 | +from pathlib import Path |
| 39 | +from typing import Union |
| 40 | + |
| 41 | +# The canonical sub-directory name each language's artifacts live under inside the shared cache |
| 42 | +# root. Keyed so that a polyglot repository analyzed under more than one language does not have its |
| 43 | +# backends overwrite a single shared ``analysis.json``. |
| 44 | +_CACHE_KEYS = {"java": "java", "python": "python", "typescript": "typescript", "c": "c"} |
| 45 | + |
| 46 | + |
| 47 | +@dataclass |
| 48 | +class CodeAnalyzerConfig: |
| 49 | + """Select the in-process codeanalyzer backend. |
| 50 | +
|
| 51 | + The backend binary is sourced from the packaged ``codeanalyzer-*`` dependency, so the only |
| 52 | + knob is where analysis artifacts are cached. |
| 53 | +
|
| 54 | + Attributes: |
| 55 | + cache_dir: Root directory for analysis artifacts. When ``None`` the facade defaults it to |
| 56 | + ``<project>/.codeanalyzer``. Each backend writes under a language-keyed subdirectory of |
| 57 | + this root (see :func:`cache_subdir`), so the same root can be shared across languages. |
| 58 | + """ |
| 59 | + |
| 60 | + cache_dir: Union[str, Path, None] = None |
| 61 | + |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class PyCodeAnalyzerConfig(CodeAnalyzerConfig): |
| 65 | + """Select the in-process codeanalyzer backend for Python. |
| 66 | +
|
| 67 | + Adds the Python-only call-graph knobs on top of :class:`CodeAnalyzerConfig`. |
| 68 | +
|
| 69 | + Attributes: |
| 70 | + use_codeql: If ``True`` (default), augment Jedi-based call-graph resolution with CodeQL. |
| 71 | + use_ray: If ``True``, enable Ray-based parallel processing for large projects. |
| 72 | + """ |
| 73 | + |
| 74 | + use_codeql: bool = True |
| 75 | + use_ray: bool = False |
| 76 | + |
| 77 | + |
| 78 | +@dataclass |
| 79 | +class Neo4jConnectionConfig: |
| 80 | + """Select the read-only Neo4j-backed analysis backend. |
| 81 | +
|
| 82 | + The graph is always populated out of band (e.g. a job that runs ``codeanalyzer-* --emit |
| 83 | + neo4j``); the SDK only polls it. This config carries the connection details and which |
| 84 | + application to scope queries to. |
| 85 | +
|
| 86 | + Attributes: |
| 87 | + uri: Bolt URI of the Neo4j server (e.g. ``bolt://localhost:7687``). |
| 88 | + username: Neo4j username (read-only credentials are sufficient). |
| 89 | + password: Neo4j password. |
| 90 | + database: Database name (``None`` => server default). |
| 91 | + application_name: The application anchor name to scope queries to. Matches the |
| 92 | + ``--app-name`` the graph was loaded with (defaults to the project directory name). |
| 93 | + """ |
| 94 | + |
| 95 | + uri: str |
| 96 | + username: str = "neo4j" |
| 97 | + password: str = "neo4j" |
| 98 | + database: str | None = None |
| 99 | + application_name: str | None = None |
| 100 | + |
| 101 | + |
| 102 | +# Per-language discriminated unions the facades match on. Java has no Neo4j backend yet, so its |
| 103 | +# only admissible config is the codeanalyzer one. |
| 104 | +JavaBackend = CodeAnalyzerConfig |
| 105 | +PyBackend = Union[PyCodeAnalyzerConfig, Neo4jConnectionConfig] |
| 106 | +TSBackend = Union[CodeAnalyzerConfig, Neo4jConnectionConfig] |
| 107 | + |
| 108 | + |
| 109 | +def cache_subdir(cache_dir: Union[str, Path, None], project_dir: Union[str, Path, None], language: str) -> Path | None: |
| 110 | + """Resolve the language-keyed cache directory for a backend. |
| 111 | +
|
| 112 | + Args: |
| 113 | + cache_dir: The cache root from the backend config. When ``None``, defaults to |
| 114 | + ``<project_dir>/.codeanalyzer``. |
| 115 | + project_dir: The project directory, used to derive the default root. |
| 116 | + language: The canonical language key (``"java"``, ``"python"``, ``"typescript"``, ``"c"``). |
| 117 | +
|
| 118 | + Returns: |
| 119 | + ``<root>/<language>`` as an absolute path, or ``None`` if no root can be determined |
| 120 | + (no ``cache_dir`` and no ``project_dir``). |
| 121 | + """ |
| 122 | + key = _CACHE_KEYS.get(language, language) |
| 123 | + if cache_dir is not None: |
| 124 | + root = Path(cache_dir).expanduser().resolve() |
| 125 | + elif project_dir is not None: |
| 126 | + root = Path(project_dir).expanduser().resolve() / ".codeanalyzer" |
| 127 | + else: |
| 128 | + return None |
| 129 | + return root / key |
0 commit comments