MCPcopy
hub / github.com/idank/explainshell / _rewrite_target

Function _rewrite_target

tools/postprocess_ubuntu_archive.py:43–71  ·  view source on GitHub ↗

Rewrite a symlink *target* for the destination directory layout. Returns the (possibly rewritten) target string, or ``None`` if the symlink should be skipped (broken or points to a deleted section).

(target: str, src_dir: Path)

Source from the content-addressed store, hash-verified

41
42
43def _rewrite_target(target: str, src_dir: Path) -> str | None:
44 """Rewrite a symlink *target* for the destination directory layout.
45
46 Returns the (possibly rewritten) target string, or ``None`` if the
47 symlink should be skipped (broken or points to a deleted section).
48 """
49 m = _CROSS_SECTION_RE.match(target)
50 if m is None:
51 # Same-section relative link (e.g. ``coqc.1.gz``) or an
52 # unusual path (deep, absolute). For same-section links the
53 # target name must exist as a real file in the source section
54 # directory — that is verified by the caller. Anything else
55 # (absolute paths, ``../../`` chains) is considered broken.
56 if target.startswith("/") or target.startswith("../"):
57 return None
58 return target
59
60 section = m.group("section")
61 rest = m.group("rest")
62
63 if section not in SECTIONS_TO_KEEP:
64 # Target is in a section we don't keep (e.g. man3, man7).
65 return None
66
67 # Verify the target file actually exists in the source tree.
68 if not (src_dir / section / rest).exists():
69 return None
70
71 return f"../{SECTIONS_TO_KEEP[section]}/{rest}"
72
73
74def process_section(

Callers 1

process_sectionFunction · 0.85

Calls 1

matchMethod · 0.80

Tested by

no test coverage detected