scripts

Personal scripts that don't fit elsewhere

git clone https://code.pdelong.com/scripts.git

 1#!/usr/bin/env python3
 2
 3from pathlib import Path
 4import sys
 5
 6
 7def handle_owners(repo_root, file):
 8    includes = []
 9    with file.open() as f:
10        print("{}:".format(file.relative_to(repo_root)))
11        for line in f:
12            if line.startswith("include "):
13                includes.append(Path(line.split()[1][1:]).absolute())
14
15            print(f"\t{line}", end="")
16        print()
17
18    for include in includes:
19        handle_owners(repo_root, include)
20
21
22if len(sys.argv) != 2:
23    print("usage: find_owners.py <PATH>")
24    exit(1)
25
26p = Path(sys.argv[1]).absolute()
27if not p.exists():
28    print(f"{sys.argv[1]} does not exist")
29    exit(2)
30
31repo_root = None
32current = p
33while current != current.parent:
34    if (current / ".git").is_dir():
35        repo_root = current
36        break
37    current = current.parent
38
39if repo_root is None:
40    print("Could not find repository root (no .git directory)")
41    exit(3)
42
43if p.is_file() and p.name == "OWNERS":
44    handle_owners(repo_root, p)
45    p = p.parent.parent
46
47while True:
48    inner = p / "OWNERS"
49    if inner.is_file():
50        handle_owners(repo_root, inner)
51
52    if p == repo_root:
53        break
54
55    p = p.parent