@allansantos-dev/smart-tool 0.9.79 → 0.9.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/README.md +82 -18
  3. package/browser_control.py +10 -3
  4. package/browser_service_client.py +3 -32
  5. package/chromium_flags.py +30 -0
  6. package/client_hooks.py +10 -8
  7. package/code_health.py +182 -0
  8. package/code_impact.py +14 -2
  9. package/config.py +76 -8
  10. package/credential_check.py +38 -0
  11. package/dependency_notice.py +262 -14
  12. package/document_converter.py +221 -0
  13. package/document_read.py +100 -25
  14. package/document_text.py +194 -6
  15. package/dossier.py +71 -9
  16. package/embedding_cache.py +32 -8
  17. package/freshness.py +3 -2
  18. package/hook_decision.py +289 -18
  19. package/hooks/pretool_router.py +5 -4
  20. package/indexer.py +92 -3
  21. package/install.py +66 -27
  22. package/install_runtime.py +24 -10
  23. package/layer_check.py +57 -0
  24. package/link_check.py +69 -0
  25. package/local_embedder.py +115 -58
  26. package/locales/pt-BR.json +122 -18
  27. package/low_priority.py +41 -0
  28. package/model_client.py +1 -1
  29. package/model_health.py +24 -6
  30. package/notice_action.py +9 -0
  31. package/package.json +1 -1
  32. package/pr_comment.py +94 -0
  33. package/pr_impact.py +292 -8
  34. package/process_limits.py +108 -0
  35. package/project_admission.py +17 -15
  36. package/project_monitor.py +33 -7
  37. package/project_scheduler.py +12 -4
  38. package/removed_callers.py +146 -0
  39. package/scout.py +6 -2
  40. package/setup_audit.py +34 -1
  41. package/setup_ui.py +241 -41
  42. package/smart_tool_daemon.py +403 -91
  43. package/tray.py +29 -9
  44. package/update_check.py +18 -0
  45. package/update_splash.py +2 -2
  46. package/usage_meter.py +78 -0
  47. package/version.py +1 -1
  48. package/web_adapters/browser/crawl4ai_search.py +7 -3
  49. package/web_document_graph.py +12 -1
  50. package/web_fetch.py +24 -0
  51. package/web_search_health.py +31 -0
package/install.py CHANGED
@@ -15,6 +15,8 @@ from pathlib import Path
15
15
 
16
16
  import atomic_io
17
17
  import client_hooks
18
+ import config
19
+ import i18n
18
20
  import paths
19
21
  import version
20
22
 
@@ -36,7 +38,32 @@ BLOCKED = {"__pycache__", ".token-guard", ".git", ".pytest_cache", ".data", ".ar
36
38
  ".scratch", ".memory", "docs", "reports", "site", "design", "release", "npm", ".github", "AGENTS.md", "CLAUDE.md", "DESIGN.md", "PRODUCT.md", "install.py", "uninstall.py", ".gitignore", ".gitattributes", MANIFEST}
37
39
  PACKAGE_ONLY = {"bin", "package.json", ".npmignore"}
38
40
  REQUIRED_TOOLS = {"smart_search", "smart_search_result", "web_search", "project_manage"}
41
+ LANGUAGE = "en"
39
42
  _OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
43
+ def installer_language(requested=None):
44
+ """The language of the installer's messages: requested (--language), else the one picked in the settings, else the
45
+ Windows display language. Settings that cannot be read, or that hold an unknown language, are reported on stderr
46
+ and the Windows language is used."""
47
+ if requested:
48
+ return requested
49
+ problem, chosen = config.corrupted_reason(), ""
50
+ if not problem:
51
+ try:
52
+ chosen = config.language()
53
+ except (ValueError, config.ConfigCorruptedError) as exc:
54
+ problem = str(exc)
55
+ fallback = i18n.system_language()
56
+ if problem:
57
+ print(i18n.t("Could not read the Smart Tool settings, so the Windows language is used instead: {problem}",
58
+ lang=fallback, problem=problem), file=sys.stderr)
59
+ return chosen or fallback
60
+
61
+
62
+ def t(text, **values):
63
+ """text (an English catalog key) in LANGUAGE, with its {placeholders} filled from values."""
64
+ return i18n.t(text, lang=LANGUAGE, **values)
65
+
66
+
40
67
  def ships(rel):
41
68
  parts = Path(rel).parts
42
69
  *folders, name = parts
@@ -98,7 +125,7 @@ def stop_processes(target):
98
125
  if not left:
99
126
  return
100
127
  if time.monotonic() > deadline:
101
- raise RuntimeError(f"Could not close the running Smart Tool ({len(left)} process(es) still active).")
128
+ raise RuntimeError(t("Could not close the running Smart Tool ({count} process(es) still active).", count=len(left)))
102
129
  time.sleep(0.5)
103
130
 
104
131
 
@@ -123,7 +150,7 @@ def wait_port_free(target):
123
150
  stop_processes(target)
124
151
  time.sleep(0.5)
125
152
  if _daemon_up():
126
- raise RuntimeError("Another Smart Tool daemon is still active on the registered port; close it before updating.")
153
+ raise RuntimeError(t("Another Smart Tool daemon is still active on the registered port; close it before updating."))
127
154
 
128
155
 
129
156
  def _daemon_up():
@@ -193,7 +220,7 @@ def remove_other_installs(target):
193
220
  removed = []
194
221
  codex = hook_files()[1]
195
222
  for folder in other_installs(target):
196
- print(f"Older Smart Tool installation found at {folder}; removing it.")
223
+ print(t("Older Smart Tool installation found at {folder}; removing it.", folder=folder))
197
224
  stop_processes(folder)
198
225
  for path in hook_files():
199
226
  original = path.read_text(encoding="utf-8") if path.is_file() else ""
@@ -203,7 +230,7 @@ def remove_other_installs(target):
203
230
  atomic_io.backup_file(str(path))
204
231
  path.write_text(text, encoding="utf-8")
205
232
  client_hooks.record_codex_trust(trust, str(path.with_name("config.toml")))
206
- print(f"Hooks in {path} now point to {target}.")
233
+ print(t("Hooks in {path} now point to {target}.", path=path, target=target))
207
234
  startup = STARTUP_SCRIPT.read_text(encoding="utf-8", errors="replace") if STARTUP_SCRIPT.is_file() else ""
208
235
  if any(_same_folder(_script_folder(script), folder) for script in _script_paths(startup)):
209
236
  STARTUP_SCRIPT.unlink()
@@ -327,7 +354,7 @@ def prune_camoufox(target):
327
354
  browser = target / "web_adapters" / "browser"
328
355
  result = subprocess.run([str(browser / ".venv" / "Scripts" / "python.exe"), str(browser / "prune_camoufox.py")])
329
356
  if result.returncode:
330
- print(f"Old Camoufox builds not pruned: prune_camoufox.py exited {result.returncode}.", file=sys.stderr)
357
+ print(t("Old Camoufox builds not pruned: prune_camoufox.py exited {code}.", code=result.returncode), file=sys.stderr)
331
358
 
332
359
 
333
360
  def install_dependencies(target, python):
@@ -335,7 +362,7 @@ def install_dependencies(target, python):
335
362
  venv.create(python.parent.parent, with_pip=True)
336
363
  subprocess.run([str(python), "-m", "pip", "install", "--disable-pip-version-check", "-q", "-r",
337
364
  str(target / "requirements.txt")], check=True)
338
- subprocess.run([str(python), str(target / "install_runtime.py")], check=True)
365
+ subprocess.run([str(python), str(target / "install_runtime.py"), "--language", LANGUAGE], check=True)
339
366
 
340
367
 
341
368
  def start(target, python, pythonw):
@@ -357,17 +384,20 @@ def verify(target, python):
357
384
  "print(json.dumps({'version':running,'tools':sorted(r['name'] for r in json.load(opener.open(req,timeout=10))['result']['tools'])}))")
358
385
  result = subprocess.run([str(python), "-c", script], cwd=target, capture_output=True, text=True, timeout=60)
359
386
  if result.returncode:
360
- raise RuntimeError("Daemon did not respond: " + ((result.stderr or result.stdout).strip().splitlines() or ["no output"])[-1])
387
+ detail = ((result.stderr or result.stdout).strip().splitlines() or [t("no output")])[-1]
388
+ raise RuntimeError(t("Daemon did not respond: {detail}", detail=detail))
361
389
  served = json.loads(result.stdout.strip().splitlines()[-1])
362
390
  missing = REQUIRED_TOOLS - set(served["tools"])
363
391
  if missing:
364
- raise RuntimeError("Missing MCP tools: " + ", ".join(sorted(missing)))
392
+ raise RuntimeError(t("Missing MCP tools: {tools}", tools=", ".join(sorted(missing))))
365
393
  if served["version"] != version.VERSION:
366
- raise RuntimeError(f"The daemon runs version {served['version']}, not the {version.VERSION} being installed")
394
+ raise RuntimeError(t("The daemon runs version {running}, not the {expected} being installed",
395
+ running=served["version"], expected=version.VERSION))
367
396
  pid = json.loads(REGISTRY.read_text(encoding="utf-8"))["pid"]
368
397
  command = powershell(f"(Get-CimInstance Win32_Process -Filter 'ProcessId = {int(pid)}').CommandLine").stdout
369
398
  if str(target / "smart_tool_daemon.py").lower() not in command.lower():
370
- raise RuntimeError(f"The active daemon (pid {pid}) does not belong to this installation: {command.strip()[:200]}")
399
+ raise RuntimeError(t("The active daemon (pid {pid}) does not belong to this installation: {command}",
400
+ pid=pid, command=command.strip()[:200]))
371
401
 
372
402
 
373
403
  def enable_browser_control(target, python):
@@ -382,11 +412,12 @@ def enable_browser_control(target, python):
382
412
  except (OSError, subprocess.TimeoutExpired, ValueError, IndexError) as exc:
383
413
  result, outcome = None, f"{type(exc).__name__}: {exc}"
384
414
  if not isinstance(outcome, dict):
385
- detail = outcome or ((result.stderr or result.stdout).strip().splitlines() or ["no output"])[-1]
386
- print(f"Browser control not set up: {detail}")
415
+ detail = outcome or ((result.stderr or result.stdout).strip().splitlines() or [t("no output")])[-1]
416
+ print(t("Browser control not set up: {detail}", detail=detail))
387
417
  return
388
418
  for client, state in outcome.items():
389
- print(f"Browser control in {client}: {state['state']}" + (f" ({state['reason']})" if state.get("reason") else ""))
419
+ print(t("Browser control in {client}: {state}", client=client, state=state["state"])
420
+ + (f" ({state['reason']})" if state.get("reason") else ""))
390
421
 
391
422
 
392
423
  MIN_PYTHON = (3, 11)
@@ -404,18 +435,23 @@ def main():
404
435
  help="install without running tests/run.py (npm package, whose tests ran in CI; emergency only otherwise)")
405
436
  parser.add_argument("--expect-version",
406
437
  help="refuse to install unless this package is that version (the update screen passes the one it announced)")
438
+ parser.add_argument("--language", choices=config.LANGUAGES,
439
+ help="language of the messages (default: the setting, else the Windows display language)")
407
440
  args = parser.parse_args()
441
+ global LANGUAGE
442
+ LANGUAGE = installer_language(args.language)
408
443
  if args.expect_version and args.expect_version != version.VERSION:
409
- raise SystemExit(f"This package is Smart Tool {version.VERSION}, not the {args.expect_version} requested; "
410
- "nothing was changed.")
444
+ raise SystemExit(t("This package is Smart Tool {version}, not the {requested} requested; nothing was changed.",
445
+ version=version.VERSION, requested=args.expect_version))
411
446
  paths.refuse_overridden_folders("install")
412
447
  target = args.target.resolve()
413
448
  if target == SOURCE:
414
- raise SystemExit("The target cannot be the source folder itself.")
449
+ raise SystemExit(t("The target cannot be the source folder itself."))
415
450
  if args.skip_tests:
416
- print("Tests skipped by --skip-tests.")
451
+ print(t("Tests skipped by --skip-tests."))
417
452
  elif not (SOURCE / "tests" / "run.py").is_file():
418
- raise SystemExit(f"No test suite in {SOURCE}; a packaged install runs with --skip-tests (npx @allansantos-dev/smart-tool@{version.VERSION} install).")
453
+ raise SystemExit(t("No test suite in {source}; a packaged install runs with --skip-tests "
454
+ "(npx @allansantos-dev/smart-tool@{version} install).", source=SOURCE, version=version.VERSION))
419
455
  else:
420
456
  interpreter = python_paths(target)[0] if python_paths(target)[0].is_file() else Path(sys.executable)
421
457
  tests = subprocess.run([str(interpreter), str(SOURCE / "tests" / "run.py")], capture_output=True, text=True,
@@ -424,11 +460,13 @@ def main():
424
460
  if tests.stderr.strip():
425
461
  print(tests.stderr.rstrip()[-3000:], file=sys.stderr)
426
462
  if tests.returncode:
427
- raise SystemExit("Tests failed; installation aborted without touching the installed version.")
463
+ raise SystemExit(t("Tests failed; installation aborted without touching the installed version."))
428
464
  python, pythonw = python_paths(target)
429
465
  files = source_files()
430
466
  existed = (target / "smart_tool_daemon.py").is_file()
431
- print(("Updating" if existed else "Installing") + f" Smart Tool at {target} ({len(files)} files from {SOURCE})")
467
+ where = {"target": target, "count": len(files), "source": SOURCE}
468
+ print(t("Updating Smart Tool at {target} ({count} files from {source})", **where) if existed
469
+ else t("Installing Smart Tool at {target} ({count} files from {source})", **where))
432
470
  target.mkdir(parents=True, exist_ok=True)
433
471
  remove_other_installs(target)
434
472
  mark_updating(True)
@@ -439,8 +477,8 @@ def main():
439
477
  if not args.no_start:
440
478
  enable_browser_control(target, python)
441
479
  for folder in prune_backups(target):
442
- print(f"Removed the old copy {folder}.")
443
- print("Smart Tool ready." if not args.no_start else "Files installed; daemon not restarted (--no-start).")
480
+ print(t("Removed the old copy {folder}.", folder=folder))
481
+ print(t("Smart Tool ready.") if not args.no_start else t("Files installed; daemon not restarted (--no-start)."))
444
482
 
445
483
 
446
484
  def prune_backups(target):
@@ -456,7 +494,7 @@ def prune_backups(target):
456
494
  shutil.rmtree(folder)
457
495
  removed.append(str(folder))
458
496
  except OSError as exc:
459
- print(f"Could not remove the old copy {folder}: {exc}", file=sys.stderr)
497
+ print(t("Could not remove the old copy {folder}: {error}", folder=folder, error=exc), file=sys.stderr)
460
498
  return removed
461
499
 
462
500
 
@@ -474,19 +512,19 @@ def _update(args, target, files, existed, python, pythonw):
474
512
  try:
475
513
  copy_files(target, files, backup, changed, removed)
476
514
  clear_bytecode(target)
477
- print(f"Files changed: {len(changed)}; removed as obsolete: {len(removed)}")
515
+ print(t("Files changed: {changed}; removed as obsolete: {removed}", changed=len(changed), removed=len(removed)))
478
516
  if run_deps:
479
517
  frozen = freeze(python)
480
518
  deps_touched = True
481
519
  install_dependencies(target, python)
482
520
  elif not args.skip_deps:
483
- print("Dependencies unchanged since the last update; pip and install_runtime.py skipped.")
521
+ print(t("Dependencies unchanged since the last update; pip and install_runtime.py skipped."))
484
522
  if not args.no_start:
485
523
  mark_updating(False)
486
524
  start(target, python, pythonw)
487
525
  verify(target, python)
488
526
  except BaseException as exc:
489
- print(f"Failed: {exc}. Restoring the previous version.", file=sys.stderr)
527
+ print(t("Failed: {error}. Restoring the previous version.", error=exc), file=sys.stderr)
490
528
  problems = []
491
529
  steps = [lambda: mark_updating(True), lambda: stop_processes(target),
492
530
  lambda: restore(target, backup, changed, removed)]
@@ -500,7 +538,8 @@ def _update(args, target, files, existed, python, pythonw):
500
538
  except Exception as error:
501
539
  problems.append(str(error))
502
540
  if problems:
503
- print(f"Incomplete rollback; backup kept at {backup}: " + " | ".join(problems), file=sys.stderr)
541
+ print(t("Incomplete rollback; backup kept at {backup}: {problems}", backup=backup, problems=" | ".join(problems)),
542
+ file=sys.stderr)
504
543
  raise SystemExit(1)
505
544
  shutil.rmtree(backup, ignore_errors=True)
506
545
  raise SystemExit(1)
@@ -21,6 +21,7 @@ import venv
21
21
  import zipfile
22
22
  from pathlib import Path
23
23
 
24
+ import i18n
24
25
  import node_locator
25
26
 
26
27
 
@@ -43,6 +44,12 @@ MOLI_URL = f"https://github.com/lexmount/moli/releases/download/v{MOLI_VERSION}/
43
44
  MOLI_SHA256 = "64c00ce02d6db8e1c22c55e88fbcaaf3e7517e930f24c25ff46a457b421040c8"
44
45
  MOLI_DIR = BROWSER_DIR / "moli"
45
46
  MOLI_EXE = MOLI_DIR / "moli.exe"
47
+ LANGUAGE = "en"
48
+
49
+
50
+ def t(text, **values):
51
+ """text (an English catalog key) in LANGUAGE, with its {placeholders} filled from values."""
52
+ return i18n.t(text, lang=LANGUAGE, **values)
46
53
 
47
54
 
48
55
  def _node_supported(node):
@@ -119,7 +126,7 @@ def _replace_folder(unpacked, final, temp_dir):
119
126
 
120
127
  def _install_node():
121
128
  """Baixa o Node LTS oficial e confere SHA-256 antes de extrair."""
122
- print("Downloading Node LTS for the Smart Tool web runtime...", flush=True)
129
+ print(t("Downloading Node LTS for the Smart Tool web runtime..."), flush=True)
123
130
  with urllib.request.urlopen("https://nodejs.org/dist/index.json", timeout=30) as response:
124
131
  releases = json.load(response)
125
132
  release = next((row for row in releases if row.get("lts") and
@@ -144,7 +151,7 @@ def _install_node():
144
151
  if not (unpacked / "node.exe").is_file() or not (unpacked / "npm.cmd").is_file():
145
152
  raise RuntimeError("Node package does not contain the expected executables.")
146
153
  _replace_folder(unpacked, NODE_RUNTIME, temp_dir)
147
- print(f"Node {version} installed inside Smart Tool.")
154
+ print(t("Node {version} installed inside Smart Tool.", version=version))
148
155
 
149
156
 
150
157
  def _moli_ready():
@@ -156,7 +163,7 @@ def _moli_ready():
156
163
 
157
164
  def _install_moli():
158
165
  """Downloads the pinned Moli release from GitHub, checks its SHA-256 and unpacks it into web_adapters/browser."""
159
- print(f"Downloading Moli {MOLI_VERSION} (structured-first headless browser, 43 MB)...", flush=True)
166
+ print(t("Downloading Moli {version} (structured-first headless browser, 43 MB)...", version=MOLI_VERSION), flush=True)
160
167
  BROWSER_DIR.mkdir(parents=True, exist_ok=True)
161
168
  with tempfile.TemporaryDirectory(prefix="moli-setup-", dir=BROWSER_DIR) as temp:
162
169
  temp_dir = Path(temp)
@@ -167,7 +174,7 @@ def _install_moli():
167
174
  if not (unpacked / "moli.exe").is_file():
168
175
  raise RuntimeError("The Moli package does not contain moli.exe.")
169
176
  _replace_folder(unpacked, MOLI_DIR, temp_dir)
170
- print(f"Moli {MOLI_VERSION} installed inside Smart Tool.")
177
+ print(t("Moli {version} installed inside Smart Tool.", version=MOLI_VERSION))
171
178
 
172
179
 
173
180
  def _run(args, *, cwd=None, label="Web dependency", idle_s=600, stream=False, attempts=1):
@@ -179,15 +186,19 @@ def _run(args, *, cwd=None, label="Web dependency", idle_s=600, stream=False, at
179
186
  env.setdefault("NODE_USE_SYSTEM_CA", "1")
180
187
  flags = (subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NO_WINDOW) if os.name == "nt" else 0
181
188
  for attempt in range(1, attempts + 1):
182
- print(f"{label}: starting{f' (attempt {attempt} of {attempts})' if attempt > 1 else ''}...", flush=True)
189
+ if attempt > 1:
190
+ print(t("{label}: starting (attempt {attempt} of {attempts})...", label=label, attempt=attempt,
191
+ attempts=attempts), flush=True)
192
+ else:
193
+ print(t("{label}: starting...", label=label), flush=True)
183
194
  try:
184
195
  _run_once(args, cwd, env, flags, label, idle_s, stream)
185
- print(f"{label}: done.", flush=True)
196
+ print(t("{label}: done.", label=label), flush=True)
186
197
  return
187
198
  except RuntimeError as exc:
188
199
  if attempt == attempts:
189
200
  raise
190
- print(f"{exc}; trying again.", flush=True)
201
+ print(t("{error}; trying again.", error=exc), flush=True)
191
202
 
192
203
 
193
204
  def _run_once(args, cwd, env, flags, label, idle_s, stream):
@@ -229,7 +240,7 @@ def _run_once(args, cwd, env, flags, label, idle_s, stream):
229
240
  raise RuntimeError(f"{label} stopped: no output for {idle_s}s; last output: {tail[-1] if tail else 'none'}")
230
241
  if not stream and now - noted >= 30:
231
242
  noted = now
232
- print(f"{label}: waiting for {int(now - started)}s...", flush=True)
243
+ print(t("{label}: waiting for {seconds}s...", label=label, seconds=int(now - started)), flush=True)
233
244
  time.sleep(0.5)
234
245
  reader.join(timeout=10)
235
246
  if process.returncode:
@@ -374,7 +385,7 @@ def install(reuse_browser_runtime=None):
374
385
  status = _check()
375
386
  if not all(status.values()):
376
387
  raise RuntimeError(f"Incomplete web runtime: {status}")
377
- print("Smart Tool runtime installed: Node, open-websearch, TypeScript, Java, Angular, Playwright MCP, Camoufox, Crawl4AI and Moli.")
388
+ print(t("Smart Tool runtime installed: Node, open-websearch, TypeScript, Java, Angular, Playwright MCP, Camoufox, Crawl4AI and Moli."))
378
389
 
379
390
 
380
391
  def main():
@@ -382,7 +393,10 @@ def main():
382
393
  parser.add_argument("--check", action="store_true", help="Show the status without installing")
383
394
  parser.add_argument("--reuse-browser-runtime", metavar="FOLDER",
384
395
  help="Reuse an existing venv without duplicating files (useful when disk is low)")
396
+ parser.add_argument("--language", choices=i18n.config.LANGUAGES, default="en", help="language of the messages")
385
397
  args = parser.parse_args()
398
+ global LANGUAGE
399
+ LANGUAGE = args.language
386
400
  if args.check:
387
401
  status = _check()
388
402
  for name, ready in status.items():
@@ -391,7 +405,7 @@ def main():
391
405
  try:
392
406
  install(args.reuse_browser_runtime)
393
407
  except (OSError, RuntimeError, subprocess.SubprocessError) as exc:
394
- print(f"Web runtime installation failed: {exc}", file=sys.stderr)
408
+ print(t("Web runtime installation failed: {error}", error=exc), file=sys.stderr)
395
409
  return 1
396
410
  return 0
397
411
 
package/layer_check.py ADDED
@@ -0,0 +1,57 @@
1
+ """Layer rule of the project applied at the moment a function is written: a call that goes up the declared layers.
2
+
3
+ The rule is the one confirmed in `.smart-tool/layers.json` (`layers`); a project without it is never checked. A call is
4
+ recognised by the name it names, so only a function name that is unique among the code files of the project and not
5
+ defined in the edited file counts, and only a call the edit adds (the name is not called in the same function before it).
6
+ """
7
+ import re
8
+
9
+ import code_graph
10
+ import doc_check
11
+ import layers
12
+
13
+ _CALL = re.compile(r"\b([A-Za-z_]\w*)\s*\(")
14
+ _COMMENT = re.compile(r"^\s*(#|//|\*|/\*)")
15
+ MAX_FINDINGS = 5
16
+
17
+
18
+ def _called(source, function):
19
+ """Names called in the lines of one function of `source`."""
20
+ lines = source.splitlines()[function["start"] - 1:function["end"]]
21
+ return {name for line in lines if not _COMMENT.match(line) for name in _CALL.findall(line)} - {function["name"]}
22
+
23
+
24
+ def review(root, rel, before, after, edited):
25
+ """The calls an edit adds that go from a lower layer to a higher one.
26
+
27
+ Parameters: root (str) project folder; rel (str) project-relative path of the edited file; before (str or None) text
28
+ before the edit; after (str) text after it; edited (dict) `doc_check.written` of the same edit.
29
+ Returns: list of dicts {"function", "line", "called", "path", "from", "to"}, empty when the project declares no layers
30
+ or the file belongs to none. Raises: ValueError when the layers file is unreadable; RuntimeError when the graph is.
31
+ """
32
+ rule = layers.load(root)
33
+ if rule is None:
34
+ return []
35
+ source_layer = rule.assign(rel, after)
36
+ if source_layer is None:
37
+ return []
38
+ order = {layer["name"]: index for index, layer in enumerate(rule.layers)}
39
+ owners = {}
40
+ for symbol in code_graph.build_current(root, None, shared=True).get("symbols") or []:
41
+ owners.setdefault(symbol["name"].split(".")[-1], set()).add(symbol["path"])
42
+ old = {function["name"]: function for function in doc_check.functions(rel, before)} if before else {}
43
+ found = []
44
+ for function in edited["touched"]:
45
+ previous = old.get(function["name"])
46
+ known = _called(before, previous) if previous else set()
47
+ for name in sorted(_called(after, function) - known):
48
+ paths = owners.get(name, set())
49
+ if len(paths) != 1 or rel in paths:
50
+ continue
51
+ path = next(iter(paths))
52
+ target_layer = rule.assign(path)
53
+ if target_layer and order[target_layer] < order[source_layer]:
54
+ found.append({"function": function["name"], "line": function["start"], "called": name, "path": path,
55
+ "from": source_layer, "to": target_layer})
56
+ return found[:MAX_FINDINGS]
57
+
package/link_check.py ADDED
@@ -0,0 +1,69 @@
1
+ """Local links an edit of an HTML or Markdown file adds or changes, checked against the files on disk, for the edit hook.
2
+ Links come from `web_document_graph.analyze` (the same reader the code graph uses). A link counts only when its text is
3
+ new in the file (a link that was already broken is not the edit's), and only relative paths are looked up: http(s),
4
+ mailto, tel, data and #anchors are skipped. What cannot be decided is returned as unchecked with the reason instead of
5
+ being dropped: a root-relative path (the web root is not known), a template expression, a page with a <base> element,
6
+ a file that cannot be read, HTML that did not parse."""
7
+ import collections
8
+ import os
9
+ import re
10
+
11
+ import web_document_graph
12
+
13
+ EXTENSIONS = (".html", ".htm", ".md", ".markdown")
14
+ _TEMPLATE = re.compile(r"\{\{|\{%|<%|<\?|\$\{")
15
+ _BASE = re.compile(r"<base[\s>]", re.I)
16
+
17
+
18
+ def _links(origin, text):
19
+ """The local links of text (their specifiers and lines) and the parser diagnostics, in document order."""
20
+ found = web_document_graph.analyze({origin: text}, ())
21
+ return [item for item in found["dependencies"] if item["resolution"] == "unresolved"], found["diagnostics"]
22
+
23
+
24
+ def _missing(target):
25
+ """Whether target does not exist; any other failure to look it up raises OSError."""
26
+ try:
27
+ os.stat(target)
28
+ except (FileNotFoundError, NotADirectoryError):
29
+ return True
30
+ return False
31
+
32
+
33
+ def review(path, before, after):
34
+ """The local links the edit introduced that point to nothing, and the ones that could not be checked.
35
+
36
+ Parameters: path (str) absolute path of the edited file; before (str or None) text before the edit; after (str) text
37
+ after it.
38
+ Returns: dict with `broken` (list of {line, link}) and `unchecked` (list of {line, link, why}; line and link are None
39
+ for a parse failure of the whole file); both empty for a file that is not HTML or Markdown.
40
+ """
41
+ result = {"broken": [], "unchecked": []}
42
+ if os.path.splitext(path)[1].lower() not in EXTENSIONS:
43
+ return result
44
+ origin = os.path.abspath(path).replace(os.sep, "/")
45
+ known = collections.Counter(item["specifier"] for item in _links(origin, before)[0]) if before else collections.Counter()
46
+ links, diagnostics = _links(origin, after)
47
+ result["unchecked"] += [{"line": None, "link": None, "why": item["reason"]} for item in diagnostics]
48
+ has_base = bool(_BASE.search(after)) and origin.lower().endswith((".html", ".htm"))
49
+ for item in links:
50
+ link = item["specifier"]
51
+ if known[link] > 0:
52
+ known[link] -= 1
53
+ continue
54
+ why = None
55
+ if has_base:
56
+ why = "the page has a <base> element, which changes where relative links lead"
57
+ elif _TEMPLATE.search(link):
58
+ why = "it is a template expression, not a path"
59
+ elif link.startswith("/"):
60
+ why = "it is relative to the root of a site or repository, which is not known here"
61
+ else:
62
+ try:
63
+ if _missing(web_document_graph.local_target(origin, link)):
64
+ result["broken"].append({"line": item["line"], "link": link})
65
+ except (OSError, ValueError) as exc:
66
+ why = f"it could not be looked up: {exc}"
67
+ if why:
68
+ result["unchecked"].append({"line": item["line"], "link": link, "why": why})
69
+ return result