-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
303 lines (267 loc) · 11.6 KB
/
Copy pathcli.py
File metadata and controls
303 lines (267 loc) · 11.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
Compressore locale LLM‑ready
File: cli.py
Copyright (C) 2026 Cristian Evangelisti
License: GPL-3.0-or-later
SPDX-License-Identifier: GPL-3.0-or-later
Source: https://github.com/kamaludu/chunk-compress
Descrizione:
Orchestratore della pipeline, con chunking opzionale.
"""
import argparse
import sys
import json
from pathlib import Path
import core
import io_utils
def parse_args():
p = argparse.ArgumentParser(description="Local reversible compressor (LLM-ready)")
p.add_argument("--input", "-i", required=True, help="Directory o file‑lista")
p.add_argument("--output", "-o", default="compressed_output", help="Directory output")
p.add_argument("--L_min", type=int, default=64)
p.add_argument("--N_min", type=int, default=2)
p.add_argument("--B_min_lines", type=int, default=5)
p.add_argument("--B_max_lines", type=int, default=20)
p.add_argument("--verify-roundtrip", action="store_true")
p.add_argument(
"--no-export-mapping",
nargs="?",
const="",
help=(
"Se presente, disabilita l'export completo di mapping_subset.json per default. "
"Puoi passare una lista separata da virgole di file (relativi all'input) da ESCLUDERE "
"dall'export. Se fornito senza valore, esclude tutti i file (nessun mapping_subset.json)."
),
)
p.add_argument(
"--export-manifest",
action="store_true",
help="Esporta manifest.json compatto dei file processati (paths/files/ph/v)",
)
p.add_argument(
"--min_total_saving",
type=int,
default=100,
help="Soglia totale risparmio per accettare una sostituzione (passata a select_replacements)",
)
p.add_argument(
"--placeholder-sub",
default="§§s{:03d}§§",
help="Formato placeholder per substring (es. '§§s{:03d}§§')",
)
p.add_argument(
"--placeholder-blk",
default="§§b{:03d}§§",
help="Formato placeholder per block (es. '§§b{:03d}§§')",
)
# Chunking options (opzionali)
p.add_argument(
"--chunk-output",
action="store_true",
help="Opzionale: genera chunk dei file compressi in OUT_DIR/chunks/",
)
p.add_argument(
"--chunk-size",
type=int,
default=16000,
help="Dimensione massima (caratteri) per chunk quando --chunk-output è attivo (default 16000)",
)
p.add_argument(
"--include-pointless",
action="store_true",
help="Se presente, NON esclude le estensioni binarie/inutili durante lo scan (default: escludi).",
)
return p.parse_args()
def main():
args = parse_args()
input_path = Path(args.input)
if not input_path.exists():
print(f"Input non valido: {input_path}", file=sys.stderr)
sys.exit(1)
output_dir = Path(args.output)
io_utils.ensure_dir(output_dir)
try:
# 1) scan
# comportamento di default: escludi le estensioni binarie o inutili; se --include-pointless è passato, non escludere
file_metas = core.scan_files(str(input_path), exclude_pointless=not getattr(args, "include_pointless", False))
# 2) load
contents = core.load_contents(file_metas)
# 2b) Regola fissa applicata di default:
# Mantieni le righe vuote solo per formati documentazione (.md, .txt, .rst, .html, .tex, .adoc, .org)
# Rimuovile per tutti gli altri formati.
if not hasattr(core, "strip_empty_lines_by_extension"):
print("Errore: funzione 'strip_empty_lines_by_extension' non trovata in core.py", file=sys.stderr)
sys.exit(1)
try:
contents = core.strip_empty_lines_by_extension(contents)
except Exception as e:
print(f"Errore durante l'applicazione di strip_empty_lines_by_extension: {e}", file=sys.stderr)
sys.exit(1)
# 3) analyze
candidates = core.find_repetitions(
contents,
L_min=args.L_min,
N_min=args.N_min,
B_min_lines=args.B_min_lines,
B_max_lines=args.B_max_lines,
)
# 4) select
replacements = core.select_replacements(
candidates,
placeholder_fmt_sub=args.placeholder_sub,
placeholder_fmt_blk=args.placeholder_blk,
min_total_saving=args.min_total_saving,
)
# 5) apply
llm_ready, reverse_map = core.apply_placeholders(contents, replacements)
# 6) write outputs (transformed files + reverse_map completo)
_write_outputs(llm_ready, reverse_map, output_dir, input_path)
# 6b) export reduced mapping per file (LLM-ready)
# New required behavior:
# - Default: export mapping_subset.json for ALL files processed.
# - If --no-export-mapping is provided without value -> exclude all (no export).
# - If --no-export-mapping is provided with a comma-separated value -> treat that as list of files to EXCLUDE from export.
files = []
no_export_val = getattr(args, "no_export_mapping", None)
if no_export_val is None:
# default: include all processed files (relative paths)
for m in file_metas:
try:
rel = Path(m["path"]).relative_to(input_path).as_posix()
except Exception:
rel = Path(m["path"]).name
files.append(rel)
else:
# user requested exclusions
if no_export_val == "":
# provided without value -> exclude all (no export)
files = []
else:
excludes = [s.strip() for s in str(no_export_val).split(",") if s.strip()]
for m in file_metas:
try:
rel = Path(m["path"]).relative_to(input_path).as_posix()
except Exception:
rel = Path(m["path"]).name
if rel in excludes:
continue
files.append(rel)
# ensure llm_subset exists even if no files are selected, to avoid NameError later
llm_subset = {"placeholders": {}}
if files:
ref_files = []
for f in files:
try:
ref_files.append(Path(f).as_posix())
except Exception:
pass
# deduplicate while preserving order
seen = set()
ref_files = [x for x in ref_files if not (x in seen or seen.add(x))]
subset = core.extract_mapping_for_files(reverse_map, ref_files)
for ph, entry in (subset or {}).items():
content = entry.get("content") if isinstance(entry, dict) else None
if content is None:
rm_entry = reverse_map.get("placeholders", {}).get(ph) if isinstance(reverse_map, dict) else None
if isinstance(rm_entry, dict):
content = rm_entry.get("content")
if content is None:
continue
sha = entry.get("sha256") if isinstance(entry, dict) else reverse_map.get("placeholders", {}).get(ph, {}).get("sha256", "")
length = entry.get("length") if isinstance(entry, dict) else len(content)
llm_subset["placeholders"][ph] = {
"content": content,
"sha256": sha or "",
"length": length or len(content),
}
# Export mapping_subset.json usando helper core (normalizza i content prima di scrivere)
if files:
core.write_mapping_subset(reverse_map, files, output_dir / "mapping_subset.json")
print(f"Exported mapping_subset.json for {len(files)} file(s): {', '.join(files)}")
else:
print("No placeholders to export; mapping_subset.json not written.")
# 6c) export manifest compatto se richiesto
if getattr(args, "export_manifest", False):
try:
manifest = core.build_manifest(file_metas, reverse_map, str(input_path))
io_utils.write_atomic(
output_dir / "manifest.json",
json.dumps(manifest, ensure_ascii=False, separators=(",", ":")),
)
print(f"Manifest scritto in: {output_dir / 'manifest.json'}")
except Exception as e:
print(f"Errore durante la generazione del manifest: {e}", file=sys.stderr)
# 6d) optional: chunk outputs (DOPO la scrittura normale)
if getattr(args, "chunk_output", False):
try:
chunks_manifest = core.chunk_outputs(llm_ready, output_dir, args.chunk_size, input_path)
chunks_dir = output_dir / "chunks"
io_utils.ensure_dir(chunks_dir)
if isinstance(chunks_manifest, dict) and "chunks_dir" not in chunks_manifest:
chunks_manifest["chunks_dir"] = "chunks"
io_utils.write_atomic(
chunks_dir / "manifest.json",
json.dumps(chunks_manifest, ensure_ascii=False, indent=2),
)
print(f"Chunks scritti in: {chunks_dir} (manifest.json incluso)")
except Exception as e:
print(f"Errore durante la generazione dei chunk: {e}", file=sys.stderr)
# 7) roundtrip
if args.verify_roundtrip:
ok, details = core.roundtrip_check(file_metas, llm_ready, reverse_map)
print("=== ROUNDTRIP (lossy) REPORT ===")
for d in details:
print(" -", d)
print("=== END ROUNDTRIP REPORT ===")
if not ok:
print("Roundtrip FAILED:", file=sys.stderr)
for d in details:
print(" -", d, file=sys.stderr)
try:
io_utils.write_atomic(output_dir / "roundtrip_failures.json", json.dumps(details, ensure_ascii=False, indent=2))
except Exception:
pass
sys.exit(2)
# 8) report
stats = core.estimate_savings(contents, llm_ready)
_print_report(stats, replacements)
print(f"OK. Output in: {output_dir}")
except Exception as e:
print("Errore:", e, file=sys.stderr)
sys.exit(1)
def _write_outputs(llm_ready, reverse_map, output_dir: Path, input_root: Path):
"""
Scrive i file trasformati preservando la struttura relativa rispetto all'input.
Raccoglie errori di scrittura e li solleva come RuntimeError se presenti.
"""
errors = []
for abs_path, text in llm_ready.items():
abs_p = Path(abs_path)
try:
rel = abs_p.relative_to(input_root)
except Exception:
rel = Path(abs_p.name)
out_path = output_dir / rel
io_utils.ensure_dir(out_path.parent)
try:
io_utils.write_atomic(out_path, text)
except Exception as e:
errors.append((str(out_path), str(e)))
try:
core.write_reverse_map(reverse_map, output_dir / "reverse_map.json")
except Exception as e:
errors.append((str(output_dir / "reverse_map.json"), str(e)))
if errors:
for p, err in errors:
print(f"Errore scrittura {p}: {err}", file=sys.stderr)
raise RuntimeError("Errori durante la scrittura degli output")
def _print_report(stats, replacements):
print("\n=== REPORT ===")
print(f"Original chars: {stats['orig_total']}")
print(f"Compressed chars: {stats['new_total']}")
print(f"Saved chars: {stats['saved_chars']} ({stats['saved_pct']}%)")
print(f"Placeholders: {len(replacements)}")
print("==============\n")
if __name__ == "__main__":
main()