| """Shared helpers for the Stage-1 knowledge hub build.""" | |
| import re, glob, os | |
| ROOT = "/workspace/security-dataset" | |
| RAW = f"{ROOT}/raw" | |
| INTER = f"{ROOT}/processed/_intermediate" | |
| os.makedirs(INTER, exist_ok=True) | |
| def one(pattern): | |
| m = sorted(glob.glob(f"{RAW}/{pattern}")) | |
| if len(m) != 1: | |
| raise SystemExit(f"expected exactly 1 match for {pattern}, got {m}") | |
| return m[0] | |
| CVE_RE = re.compile(r"CVE-\d{4}-\d{4,}", re.I) | |
| def norm_cve(s): | |
| if not s: return None | |
| s = s.strip().upper() | |
| if re.fullmatch(r"\d{4}-\d{4,}", s): s = "CVE-" + s | |
| m = CVE_RE.fullmatch(s) | |
| return s if m else None | |
| SHA = r"([0-9a-fA-F]{7,40})" | |
| _PATTERNS = [ | |
| # kernel.org short links | |
| (re.compile(r"^https?://git\.kernel\.org/stable/c/" + SHA + r"(?=$|[/?#.])", re.I), | |
| lambda m: ("https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux", m.group(1))), | |
| (re.compile(r"^https?://git\.kernel\.org/linus/" + SHA + r"(?=$|[/?#.])", re.I), | |
| lambda m: ("https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux", m.group(1))), | |
| # GitHub commit (also /commit/<sha>.patch, #diff..., pull/N/commits/<sha>) | |
| (re.compile(r"^https?://(?:www\.)?github\.com/([^/\s]+)/([^/\s#?]+)/(?:pull/\d+/)?commits?/" + SHA + r"(?=$|[/?#.])", re.I), | |
| lambda m: (f"https://github.com/{m.group(1)}/{m.group(2)}".lower(), m.group(3))), | |
| # GitLab (gitlab.com or self-hosted gitlab.*), /-/commit/<sha> or /commit/<sha> | |
| (re.compile(r"^(https?://[^/\s]*gitlab[^/\s]*/.+?)/(?:-/)?commits?/" + SHA + r"(?=$|[/?#.])", re.I), | |
| lambda m: (m.group(1), m.group(2))), | |
| # Bitbucket | |
| (re.compile(r"^https?://bitbucket\.org/([^/\s]+)/([^/\s]+)/commits?/" + SHA + r"(?=$|[/?#.])", re.I), | |
| lambda m: (f"https://bitbucket.org/{m.group(1)}/{m.group(2)}".lower(), m.group(3))), | |
| # cgit: .../repo.git/commit/?id=<sha> (git.kernel.org etc.) | |
| (re.compile(r"^(https?://[^?#\s]+?)/commit/?\?(?:[^#\s]*&)?id=" + SHA + r"(?=$|[&#;])", re.I), | |
| lambda m: (m.group(1), m.group(2))), | |
| # gitweb: ...?p=proj.git;a=commit;h=<sha> (also a=commitdiff) | |
| (re.compile(r"^(https?://[^?\s]+)\?p=([^;&\s]+)[;&]a=commit(?:diff)?[;&](?:[^\s]*?[;&])?h=" + SHA + r"(?=$|[;&#])", re.I), | |
| lambda m: (f"{m.group(1)}?p={m.group(2)}", m.group(3))), | |
| # generic forge (gitea, codeberg, sourceforge /ci/, googlesource +/<sha>) | |
| (re.compile(r"^(https?://[^\s]+?)/(?:-/)?(?:commits?|ci|\+)/" + SHA + r"(?=$|[/?#.^])", re.I), | |
| lambda m: (m.group(1), m.group(2))), | |
| ] | |
| def parse_commit_url(url): | |
| """Return (repo_url, sha) if url points at a single VCS commit, else None.""" | |
| if not url or len(url) > 2000: return None | |
| u = url.strip() | |
| for rx, fn in _PATTERNS: | |
| m = rx.search(u) | |
| if m: | |
| repo, sha = fn(m) | |
| repo = repo.rstrip("/") | |
| if repo.endswith(".git") and "?p=" not in repo: repo = repo[:-4] | |
| sha = sha.lower() | |
| # reject things that are clearly not hex commit ids (e.g., all digits short) | |
| if sha.isdigit() and len(sha) < 12: return None | |
| return repo, sha | |
| return None | |
| def norm_repo(repo): | |
| if not repo: return None | |
| r = repo.strip().rstrip("/") | |
| if r.endswith(".git") and "?p=" not in r: r = r[:-4] | |
| r = re.sub(r"^git\+", "", r) | |
| r = re.sub(r"^git://", "https://", r) | |
| r = re.sub(r"^http://", "https://", r) | |
| if re.match(r"^https://(www\.)?github\.com/", r, re.I) or re.match(r"^https://bitbucket\.org/", r, re.I): | |
| r = r.lower().replace("://www.", "://") | |
| return r | |
Xet Storage Details
- Size:
- 3.52 kB
- Xet hash:
- 7b974580e5336e88e679565917c7a80a61d48f8677a19baa00fc745fca6ecc65
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.