1
+ """Handle Tesseract installation during package setup."""
2
+
3
+ import os
4
+ import platform
5
+ import subprocess
6
+ import sys
7
+ from typing import Optional
8
+
9
+ def get_package_manager () -> Optional [str ]:
10
+ """Detect the system's package manager."""
11
+ system = platform .system ().lower ()
12
+
13
+ if system == "darwin" :
14
+ # Check if Homebrew is installed
15
+ if subprocess .run (["which" , "brew" ], capture_output = True ).returncode == 0 :
16
+ return "brew"
17
+ elif system == "linux" :
18
+ # Check for apt (Debian/Ubuntu)
19
+ if os .path .exists ("/usr/bin/apt" ):
20
+ return "apt"
21
+ # Check for dnf (Fedora)
22
+ elif os .path .exists ("/usr/bin/dnf" ):
23
+ return "dnf"
24
+ # Check for pacman (Arch)
25
+ elif os .path .exists ("/usr/bin/pacman" ):
26
+ return "pacman"
27
+
28
+ return None
29
+
30
+ def install_tesseract ():
31
+ """Install Tesseract OCR based on the operating system."""
32
+ system = platform .system ().lower ()
33
+ pkg_manager = get_package_manager ()
34
+
35
+ try :
36
+ if system == "darwin" and pkg_manager == "brew" :
37
+ subprocess .run (["brew" , "install" , "tesseract" ], check = True )
38
+
39
+ elif system == "linux" :
40
+ if pkg_manager == "apt" :
41
+ subprocess .run (["sudo" , "apt-get" , "update" ], check = True )
42
+ subprocess .run (["sudo" , "apt-get" , "install" , "-y" , "tesseract-ocr" ], check = True )
43
+ elif pkg_manager == "dnf" :
44
+ subprocess .run (["sudo" , "dnf" , "install" , "-y" , "tesseract" ], check = True )
45
+ elif pkg_manager == "pacman" :
46
+ subprocess .run (["sudo" , "pacman" , "-S" , "--noconfirm" , "tesseract" ], check = True )
47
+
48
+ elif system == "windows" :
49
+ print ("For Windows users:" )
50
+ print ("Please download and install Tesseract from: https://github.com/UB-Mannheim/tesseract/wiki" )
51
+ print ("After installation, ensure the Tesseract installation directory is in your system PATH." )
52
+ return
53
+
54
+ print ("Tesseract OCR installed successfully!" )
55
+
56
+ except subprocess .CalledProcessError as e :
57
+ print (f"Error installing Tesseract: { str (e )} " , file = sys .stderr )
58
+ print ("Please install Tesseract manually:" , file = sys .stderr )
59
+ print ("- macOS: brew install tesseract" , file = sys .stderr )
60
+ print ("- Ubuntu/Debian: sudo apt-get install tesseract-ocr" , file = sys .stderr )
61
+ print ("- Fedora: sudo dnf install tesseract" , file = sys .stderr )
62
+ print ("- Arch: sudo pacman -S tesseract" , file = sys .stderr )
63
+ print ("- Windows: https://github.com/UB-Mannheim/tesseract/wiki" , file = sys .stderr )
64
+ sys .exit (1 )
65
+
66
+ if __name__ == "__main__" :
67
+ install_tesseract ()
0 commit comments