luguog commited on
Commit
4ea3f5e
·
verified ·
1 Parent(s): e2df19b

Upload zip_exporter.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. zip_exporter.py +187 -0
zip_exporter.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ZIP exporter - packages analysis results into downloadable ZIP."""
2
+ import zipfile
3
+ import io
4
+ from typing import Dict, List
5
+ from datetime import datetime
6
+
7
+
8
+ class ZIPExporter:
9
+ """Exports analysis results to ZIP package."""
10
+
11
+ def export(self,
12
+ repo_url: str,
13
+ owner: str,
14
+ repo: str,
15
+ report_markdown: str,
16
+ secret_matches: List,
17
+ claims: List,
18
+ gate_summary: Dict,
19
+ overwork_score_result,
20
+ files: List[tuple]) -> bytes:
21
+ """Create ZIP package with all analysis artifacts."""
22
+
23
+ zip_buffer = io.BytesIO()
24
+
25
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
26
+ # 1. Main report
27
+ zipf.writestr(f"{repo}_overworker_report.md", report_markdown)
28
+
29
+ # 2. JSON summary
30
+ json_summary = self._create_json_summary(
31
+ repo_url, owner, repo, gate_summary, overwork_score_result
32
+ )
33
+ zipf.writestr(f"{repo}_summary.json", json_summary)
34
+
35
+ # 3. Secret scan results
36
+ if secret_matches:
37
+ secret_report = self._create_secret_report(secret_matches)
38
+ zipf.writestr(f"{repo}_secrets.txt", secret_report)
39
+
40
+ # 4. Claims analysis
41
+ if claims:
42
+ claims_report = self._create_claims_report(claims)
43
+ zipf.writestr(f"{repo}_claims.txt", claims_report)
44
+
45
+ # 5. Gate results
46
+ gate_report = self._create_gate_report(gate_summary)
47
+ zipf.writestr(f"{repo}_gates.txt", gate_report)
48
+
49
+ # 6. File inventory
50
+ file_inventory = self._create_file_inventory(files)
51
+ zipf.writestr(f"{repo}_files.txt", file_inventory)
52
+
53
+ # 7. Metadata
54
+ metadata = self._create_metadata(repo_url, owner, repo)
55
+ zipf.writestr("metadata.txt", metadata)
56
+
57
+ zip_buffer.seek(0)
58
+ return zip_buffer.getvalue()
59
+
60
+ def _create_json_summary(self,
61
+ repo_url: str,
62
+ owner: str,
63
+ repo: str,
64
+ gate_summary: Dict,
65
+ overwork_score_result) -> str:
66
+ """Create JSON summary of results."""
67
+ import json
68
+
69
+ data = {
70
+ "repository": {
71
+ "url": repo_url,
72
+ "owner": owner,
73
+ "name": repo
74
+ },
75
+ "timestamp": datetime.utcnow().isoformat(),
76
+ "overwork_score": {
77
+ "score": overwork_score_result.score,
78
+ "band": overwork_score_result.band.value,
79
+ "weakest_link": overwork_score_result.weakest_link,
80
+ "component_scores": overwork_score_result.component_scores
81
+ },
82
+ "verification_firewall": {
83
+ "total_gates": gate_summary["total_gates"],
84
+ "passed": gate_summary["passed"],
85
+ "failed": gate_summary["failed"],
86
+ "warned": gate_summary["warned"]
87
+ }
88
+ }
89
+
90
+ return json.dumps(data, indent=2)
91
+
92
+ def _create_secret_report(self, secret_matches: List) -> str:
93
+ """Create secret scan report with redacted values."""
94
+ lines = ["SECRET SCAN RESULTS", "=" * 50, ""]
95
+
96
+ for match in secret_matches:
97
+ lines.append(f"File: {match.file_path}")
98
+ lines.append(f"Line: {match.line_number}")
99
+ lines.append(f"Type: {match.secret_type}")
100
+ # Redact actual secret value for security
101
+ redacted_value = self._redact_secret(match.matched_text)
102
+ lines.append(f"Matched: {redacted_value}")
103
+ lines.append("-" * 30)
104
+ # Also redact context
105
+ redacted_context = self._redact_secret(match.context)
106
+ lines.append(redacted_context)
107
+ lines.append("")
108
+
109
+ return "\n".join(lines)
110
+
111
+ def _redact_secret(self, text: str) -> str:
112
+ """Redact secret values in text."""
113
+ if not text:
114
+ return text
115
+ # If text is longer than 10 chars, show first 4 and last 4 with asterisks
116
+ if len(text) > 10:
117
+ return text[:4] + "****" + text[-4:]
118
+ # If shorter, just show asterisks
119
+ return "*" * len(text)
120
+
121
+ def _create_claims_report(self, claims: List) -> str:
122
+ """Create claims analysis report."""
123
+ lines = ["CLAIM ANALYSIS", "=" * 50, ""]
124
+
125
+ for claim in claims:
126
+ lines.append(f"Line {claim.line_number}: {claim.text}")
127
+ lines.append(f"Category: {claim.category.value}")
128
+ lines.append(f"Evidence: {claim.evidence_level.value}")
129
+ lines.append("")
130
+
131
+ return "\n".join(lines)
132
+
133
+ def _create_gate_report(self, gate_summary: Dict) -> str:
134
+ """Create verification firewall report."""
135
+ lines = ["VERIFICATION FIREWALL RESULTS", "=" * 50, ""]
136
+
137
+ lines.append(f"Total Gates: {gate_summary['total_gates']}")
138
+ lines.append(f"Passed: {gate_summary['passed']}")
139
+ lines.append(f"Failed: {gate_summary['failed']}")
140
+ lines.append(f"Warned: {gate_summary['warned']}")
141
+ lines.append("")
142
+ lines.append("Gate Details:")
143
+ lines.append("-" * 30)
144
+
145
+ for gate in gate_summary["gates"]:
146
+ lines.append(f"{gate['name']}: {gate['status'].upper()}")
147
+ lines.append(f" {gate['message']}")
148
+ lines.append("")
149
+
150
+ return "\n".join(lines)
151
+
152
+ def _create_file_inventory(self, files: List[tuple]) -> str:
153
+ """Create file inventory."""
154
+ lines = ["FILE INVENTORY", "=" * 50, ""]
155
+
156
+ for file_path, content in files:
157
+ lines.append(f"{file_path} ({len(content)} bytes)")
158
+
159
+ lines.append("")
160
+ lines.append(f"Total files: {len(files)}")
161
+
162
+ return "\n".join(lines)
163
+
164
+ def _create_metadata(self, repo_url: str, owner: str, repo: str) -> str:
165
+ """Create metadata file."""
166
+ lines = [
167
+ "OVERWORKER PACKAGE METADATA",
168
+ "=" * 50,
169
+ "",
170
+ f"Repository: {owner}/{repo}",
171
+ f"URL: {repo_url}",
172
+ f"Generated: {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}",
173
+ "",
174
+ "This package was generated by Overworker, an AI execution layer",
175
+ "that converts repositories into verified, inspectable assets.",
176
+ "",
177
+ "Contents:",
178
+ "- overworker_report.md: Main verification report",
179
+ "- summary.json: JSON summary of results",
180
+ "- secrets.txt: Secret scan details (if found)",
181
+ "- claims.txt: Claim analysis details (if found)",
182
+ "- gates.txt: Verification firewall details",
183
+ "- files.txt: Complete file inventory",
184
+ "- metadata.txt: This file"
185
+ ]
186
+
187
+ return "\n".join(lines)