-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
105 lines (81 loc) · 2.44 KB
/
Copy pathutils.py
File metadata and controls
105 lines (81 loc) · 2.44 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import shutil
import time
from urllib.parse import urlparse
from fpdf import FPDF
CHROMA_PATH = "/tmp/chroma_db"
TEMP_REPO_PATH = "/tmp/temp_repo_clone"
def remove_readonly(func, path, excinfo):
try:
os.chmod(path, os.stat.S_IWRITE)
func(path)
except Exception:
pass
def cleanup_temp_data(*args, **kwargs) -> None:
if os.path.exists(TEMP_REPO_PATH):
try:
shutil.rmtree(TEMP_REPO_PATH, onerror=remove_readonly)
except Exception as e:
print(
f"[cleanup] Warning: could not entirely remove "
f"{TEMP_REPO_PATH}: {e}"
)
def inject_github_token(repo_url: str, token: str) -> str:
if not token:
return repo_url
parsed = urlparse(repo_url)
if parsed.scheme != "https":
raise ValueError(
"GitHub Token authentication requires an HTTPS repository URL."
)
netloc = parsed.netloc.split("@")[-1]
authenticated_netloc = f"{token}@{netloc}"
return parsed._replace(netloc=authenticated_netloc).geturl()
def create_pdf(repo_url: str, messages: list) -> bytes:
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", "B", 16)
pdf.cell(
200,
10,
"Git Archaeologist - Audit Report",
ln=True,
align="C"
)
pdf.set_font("Arial", "I", 10)
pdf.cell(200, 10, f"Repository: {repo_url}", ln=True, align="C")
pdf.cell(
200,
10,
f"Date: {time.strftime('%Y-%m-%d %H:%M:%S')}",
ln=True,
align="C"
)
pdf.ln(10)
for msg in messages:
role = "User" if msg["role"] == "user" else "Archaeologist"
pdf.set_font("Arial", "B", 12)
pdf.set_text_color(0, 51, 102)
pdf.cell(0, 10, f"{role}:", ln=True)
pdf.set_font("Arial", "", 11)
pdf.set_text_color(0, 0, 0)
clean_content = (
msg["content"]
.encode("latin-1", "replace")
.decode("latin-1")
)
pdf.multi_cell(0, 7, clean_content)
pdf.ln(5)
pdf.set_draw_color(200, 200, 200)
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
pdf.ln(5)
output = pdf.output(dest="S")
if isinstance(output, bytearray):
return bytes(output)
elif isinstance(output, bytes):
return output
elif output:
return output.encode("latin-1")
else:
return b""