MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _bash_assignment_base

Function _bash_assignment_base

graphify/extractors/bash.py:212–259  ·  view source on GitHub ↗

Resolve a top-level assignment's value to a directory, or None if untracked. Covers the forms that make a ``source "${VAR}/lib/x.sh"`` target knowable statically (#2172): * the script-dir idiom ``"$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"``, including any number of traili

(
    value: str,
    script_dir: Path,
    var_bases: dict[str, Path] | None = None,
)

Source from the content-addressed store, hash-verified

210
211
212def _bash_assignment_base(
213 value: str,
214 script_dir: Path,
215 var_bases: dict[str, Path] | None = None,
216) -> Path | None:
217 """Resolve a top-level assignment's value to a directory, or None if untracked.
218
219 Covers the forms that make a ``source "${VAR}/lib/x.sh"`` target knowable
220 statically (#2172):
221
222 * the script-dir idiom ``"$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"``,
223 including any number of trailing ``/..`` hops (and the ``$0`` spelling)
224 * a leading tracked variable plus literal subpath (``"$ROOT/lib"``) when
225 *var_bases* already holds ``ROOT`` and no command substitution is present
226 * a literal path, absolute or relative to the script's own directory
227
228 Anything else -- a value built from untracked variables, or command
229 substitution we do not model -- is left untracked so the caller keeps its
230 previous script-dir guess rather than binding somewhere invented.
231 """
232 val = value.strip().strip("'\"")
233 if not val:
234 return None
235 if "dirname" in val and ("BASH_SOURCE" in val or "$0" in val):
236 m = _BASH_DIRNAME_IDIOM.search(val)
237 base = script_dir
238 for _ in range((m.group(1).count("..") if m else 0)):
239 base = base.parent
240 # A literal suffix after `pwd)` names a folder below that base (#5560).
241 # A suffix with `..` is left out: the base stays the idiom's folder.
242 tail = _BASH_PWD_SUFFIX.search(val)
243 if tail and ".." not in tail.group(1).split("/"):
244 base = base / tail.group(1).lstrip("/")
245 return base
246 if "`" in val or "$(" in val:
247 return None
248 if "$" in val:
249 if var_bases:
250 m = _BASH_LEADING_VAR.match(val)
251 if m:
252 vn = m.group(1) or m.group(2)
253 if vn in var_bases:
254 sub = val[m.end():].lstrip("/")
255 if "$" not in sub:
256 return var_bases[vn] if not sub else Path(os.path.normpath(var_bases[vn] / sub))
257 return None
258 candidate = Path(val)
259 return candidate if candidate.is_absolute() else script_dir / candidate
260
261
262def extract_bash(path: Path) -> dict:

Callers 2

extract_bash_sourceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected