entrypoints.py¶
clew/wiki/entrypoints.py
The repository's entry points, as flows the site derives without being told.
_calls_something
bool clew.wiki.entrypoints._calls_something(object conn, str name, str rel)
Whether an entry calls anything the index resolved.
Source · 13 lines
def _calls_something(conn: object, name: str, rel: str) -> bool:
"""@brief Has callees? @return Bool."""
if not table_exists(conn, "call_edges"):
return False
row = conn.execute(
"SELECT 1 FROM call_edges c JOIN memberdef m ON m.rowid = c.caller_rowid "
"JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) WHERE m.name = ? AND p.name = ? "
"AND c.source != 'binding' LIMIT 1",
(name, rel),
).fetchone()
return row is not None
Activity · 4 steps
Flows
_declared_entry_patterns
list[str] clew.wiki.entrypoints._declared_entry_patterns(object conn, Path repo_root)
The entry patterns the repository or its operator stated.
Source · 25 lines
def _declared_entry_patterns(conn: object, repo_root: Path) -> list[str]:
"""THE BUILD'S OWN TIERS: tier 1 (`--entry-patterns`) is recorded in `build_meta`, tier 2 is
the `.clew.yaml`; the built-in heuristics are neither.
@brief Read stated entry patterns.
@return Patterns.
"""
from ..declaration import SECTION_ENTRY_PATTERNS, load_declaration
from ..tiers import EXPLICIT_KEY, OPTIONS_META_PREFIX, VALUE_SEPARATOR
row = None
if table_exists(conn, "build_meta"):
row = conn.execute(
"SELECT value FROM build_meta WHERE key = ?",
(f"{OPTIONS_META_PREFIX}.{SECTION_ENTRY_PATTERNS}.{EXPLICIT_KEY}",),
).fetchone()
if row and row[0]:
return [p for p in str(row[0]).split(VALUE_SEPARATOR) if p]
try:
declared = load_declaration(repo_root).get(SECTION_ENTRY_PATTERNS) or []
except Exception: # a malformed declaration is the build's to refuse, not the site's
declared = []
return [str(p) for p in declared]
Activity · 12 steps
Flows
_defined
bool clew.wiki.entrypoints._defined(object conn, str name, str rel)
Whether the index holds a function of this name in this file.
Source · 10 lines
def _defined(conn: object, name: str, rel: str) -> bool:
"""@brief Look up. @return Defined?"""
row = conn.execute(
"SELECT 1 FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) "
"WHERE m.kind = 'function' AND m.name = ? AND p.name = ? LIMIT 1",
(name, rel),
).fetchone()
return row is not None
Activity · 2 steps
Flows
_mains
list[tuple[str, str, str]] clew.wiki.entrypoints._mains(object conn, Path repo_root)
Entry functions outside test code: the platform facts and the declared patterns.
Source · 22 lines
def _mains(conn: object, repo_root: Path) -> list[tuple[str, str, str]]:
"""THE FACTS AND THE DECLARATION, NOT THE GUESSES. Liveness seeds on the name heuristics too
(`%_cb`, `%callback%`), where a false positive only keeps a function alive; as flows they
would draw every callback as an entry point.
@brief Find entry functions.
@return Rows.
"""
from ..reachability import ENTRY_PATTERN_FACTS
patterns = list(
dict.fromkeys([*ENTRY_PATTERN_FACTS, *_declared_entry_patterns(conn, repo_root)])
)
clause = " OR ".join(["m.name LIKE ?"] * len(patterns))
rows = conn.execute(
"SELECT DISTINCT m.name, p.name FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) " # noqa: S608
f"WHERE m.kind = 'function' AND ({clause}) {_not_test(conn)} ORDER BY p.name, m.name",
patterns,
)
return [(str(n), str(rel), f"`{n}` ({rel})") for n, rel in rows]
Activity · 5 steps
Flows
_not_test
str clew.wiki.entrypoints._not_test(object conn)
A clause excluding test code, when the index has a test-scope layer.
Source · 5 lines
def _not_test(conn: object) -> str:
"""@brief Clause. @return SQL."""
if not table_exists(conn, "test_scope"):
return ""
return "AND p.rowid NOT IN (SELECT path_rowid FROM test_scope)"1 more lines, not shown
Activity · 3 steps
Flows
_scripts
list[tuple[str, str, str]] clew.wiki.entrypoints._scripts(object conn, Path repo_root)
Console scripts declared in pyproject.toml, resolved to indexed functions.
Source · 27 lines
def _scripts(conn: object, repo_root: Path) -> list[tuple[str, str, str]]:
"""`pkg.mod:func` IS LOOKED FOR at `pkg/mod.py`, `pkg/mod/__init__.py` and their `src/` forms;
a script whose function the index does not hold is skipped.
@brief Read scripts.
@return Rows.
"""
from ..py_entrypoints import console_scripts
scripts = console_scripts(repo_root)
out = []
for script, target in scripts.items():
module, _, func = str(target).partition(":")
func = func.split(".")[-1]
base = module.replace(".", "/")
for rel in (
f"{base}.py",
f"{base}/__init__.py",
f"src/{base}.py",
f"src/{base}/__init__.py",
):
if _defined(conn, func, rel):
out.append((func, rel, f"{script} — `{func}` ({rel})"))
break
return out
Activity · 12 steps
Flows
_thread_entries
list[tuple[str, str, str]] clew.wiki.entrypoints._thread_entries(object conn)
Thread entry functions outside test code.
Source · 13 lines
def _thread_entries(conn: object) -> list[tuple[str, str, str]]:
"""@brief Find thread entries. @return Rows."""
if not table_exists(conn, "threads"):
return []
rows = conn.execute(
"SELECT DISTINCT m.name, p.name, t.name FROM threads t "
"JOIN memberdef m ON m.rowid = t.entry_memberdef_rowid "
f"JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) "
f"WHERE 1 = 1 {_not_test(conn)} ORDER BY p.name, m.name"
)
return [(str(n), str(rel), f"thread `{t}` — `{n}` ({rel})") for n, rel, t in rows]
Activity · 4 steps
Flows
entry_point_flows
list[FlowSpec] clew.wiki.entrypoints.entry_point_flows(Path db, Path repo_root, int depth)
The flows of a repository's entry points.
Called by _all_flows, _architecture_entries
Tested by
Calls _calls_something, _mains, _scripts, _thread_entries, connect
Source · 17 lines
def entry_point_flows(db: Path, repo_root: Path, depth: int) -> list[FlowSpec]:
"""@brief Discover entry points. @return Specs."""
found: dict[tuple[str, str], tuple[str, str]] = {}
with connect(db) as conn:
for (name, rel, title), role in (
*((s, "") for s in _scripts(conn, Path(repo_root))),
*((m, "") for m in _mains(conn, Path(repo_root))),
*((e, "threads") for e in _thread_entries(conn)),
):
if (name, rel) not in found and _calls_something(conn, name, rel):
found[(name, rel)] = (title, role)
return [
FlowSpec(entry=name, file=rel, depth=depth, title=title, role=role)
for (name, rel), (title, role) in found.items()
]