-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_code.py
More file actions
32 lines (25 loc) · 1.35 KB
/
Copy pathfetch_code.py
File metadata and controls
32 lines (25 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import urllib.parse
from pathlib import Path
import requests
CACHE_DIR = Path(__file__).parent / "source_code"
def fetch_source(repo: str, branch: str, path: str, max_lines: int | None = None) -> tuple[list[str], bool]:
"""Fetch a source file from GitHub, caching locally.
Returns (lines, is_excerpt) where is_excerpt is True if the file was truncated.
"""
cache_path = CACHE_DIR / repo.replace("/", "_") / urllib.parse.quote(path, safe="")
if not cache_path.exists():
url = f"https://raw.githubusercontent.com/{repo}/{branch}/{urllib.parse.quote(path)}"
resp = requests.get(url, timeout=30)
resp.raise_for_status()
cache_path.parent.mkdir(parents=True, exist_ok=True)
cache_path.write_text(resp.text, encoding="utf-8")
text = cache_path.read_text(encoding="utf-8")
# Strip null bytes and other control characters that come from DOS-era
# source files with sector padding. We keep tab (0x09) and the standard
# line endings; everything else below 0x20 and the DEL (0x7f) is removed.
_CTRL = bytes(b for b in range(32) if b not in (9, 10, 13)) + bytes([127])
_CTRL_TABLE = str.maketrans("", "", _CTRL.decode("latin-1"))
lines = [l.translate(_CTRL_TABLE) for l in text.splitlines()]
if max_lines and len(lines) > max_lines:
return lines[:max_lines], True
return lines, False