Repository navigation
Expand file tree
/
Copy pathcheck_gs.py
More file actions
executable file
·79 lines (57 loc) · 1.79 KB
/
Copy pathcheck_gs.py
File metadata and controls
executable file
·79 lines (57 loc) · 1.79 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
#!/usr/bin/env python3
"""
A script that rewrites all PDFs in the store with GhostScript and checks
whether any errors/warnings were emitted.
"""
import subprocess
import sys
from pathlib import Path
from concurrent.futures import ProcessPoolExecutor, as_completed
import os
def process_pdf(pdf_path, gs_bin):
print(f"Processing: {pdf_path}")
try:
result = subprocess.run(
[
gs_bin,
"-sDEVICE=pdfwrite",
"-dNOPAUSE",
"-dBATCH",
"-sOutputFile=/dev/null",
str(pdf_path),
],
capture_output=True,
text=True,
)
output = result.stdout + result.stderr
if "error" in output.lower():
print(output)
return (pdf_path, False)
return (pdf_path, True)
except Exception as e:
print(f"Exception processing {pdf_path}: {e}")
return (pdf_path, False)
def main():
pdf_dir = Path("store/")
gs_bin = os.environ.get("GHOSTSCRIPT_BIN", "gs")
exception_files = [
"validate_pdf_a4f_full_example",
]
pdf_files = [
str(file) for file in list(pdf_dir.rglob("*.pdf"))
if not any(exc in str(file) for exc in exception_files)
]
if not pdf_files:
print("No PDF files found")
return 0
had_errors = False
max_workers = os.cpu_count() or 1
with ProcessPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(process_pdf, pdf, gs_bin): pdf for pdf in pdf_files}
for future in as_completed(futures):
pdf_path, success = future.result()
if not success:
had_errors = True
return 1 if had_errors else 0
if __name__ == "__main__":
sys.exit(main())