unstructured
242 строки · 5.8 Кб
1import platform2import shutil3import subprocess4
5import pkg_resources6
7from unstructured.utils import dependency_exists8
9
10def command_exists(command):11"""12Check if a command exists in the system
13
14Args:
15command (str): The command to check
16
17Returns:
18bool: True if command exists, False otherwise
19"""
20return shutil.which(command) is not None21
22
23def get_python_version():24"""25Get the current Python version
26
27Returns:
28str: The current Python version
29"""
30return platform.python_version()31
32
33def get_os_version():34"""35Get the current operating system version
36
37Returns:
38str: The current operating system version
39"""
40return platform.platform()41
42
43def is_python_package_installed(package_name: str):44"""45Check if a Python package is installed
46
47Args:
48package_name (str): The Python package to check
49
50Returns:
51bool: True if package is installed, False otherwise
52"""
53result = subprocess.run(54["pip", "list"],55stdout=subprocess.PIPE,56text=True,57check=True,58)59
60return any(line.lower().startswith(package_name.lower()) for line in result.stdout.splitlines())61
62
63def is_brew_package_installed(package_name: str):64"""65Check if a Homebrew package is installed
66
67Args:
68package_name (str): The package to check
69
70Returns:
71bool: True if package is installed, False otherwise
72"""
73if not command_exists("brew"):74return False75
76result = subprocess.run(77["brew", "list"],78stdout=subprocess.PIPE,79text=True,80check=True,81)82
83for line in result.stdout.splitlines():84if line.lower().startswith(package_name.lower()):85return True86
87result = subprocess.run(88["brew", "list", "--cask"],89stdout=subprocess.PIPE,90text=True,91check=True,92)93
94return any(line.lower().startswith(package_name.lower()) for line in result.stdout.splitlines())95
96
97def get_python_package_version(package_name):98"""99Get the version of a Python package
100
101Args:
102package_name (str): The Python package to check
103
104Returns:
105str: Version of the package, None if package is not installed
106"""
107try:108return pkg_resources.get_distribution(package_name).version109except pkg_resources.DistributionNotFound:110return None111
112
113def get_brew_package_version(package_name):114"""115Get the version of a Homebrew package
116
117Args:
118package_name (str): The package to check
119
120Returns:
121str: Version of the package, None if package is not installed
122"""
123if not command_exists("brew"):124return None125
126result = subprocess.run(127["brew", "info", package_name],128stdout=subprocess.PIPE,129stderr=subprocess.DEVNULL,130text=True,131)132
133for line in result.stdout.splitlines():134return line135
136return None137
138
139def get_libmagic_version():140"""141Get the version of libmagic
142
143Returns:
144str: Version of libmagic, None if libmagic is not installed
145"""
146result = subprocess.run(147["file", "--version", "--headless"],148stdout=subprocess.PIPE,149stderr=subprocess.PIPE,150text=True,151)152
153return result.stdout.strip()154
155
156def get_libreoffice_version():157"""158Get the version of LibreOffice
159
160Returns:
161str: Version of LibreOffice, None if LibreOffice is not installed
162"""
163result = subprocess.run(164["libreoffice", "--version", "--headless"],165stdout=subprocess.PIPE,166stderr=subprocess.PIPE,167text=True,168)169
170return result.stdout.strip()171
172
173def main():174"""175The main function to run all checks
176"""
177print("OS version: ", get_os_version())178print("Python version: ", get_python_version())179
180if dependency_exists("unstructured"):181print("unstructured version: ", get_python_package_version("unstructured"))182else:183print("unstructured is not installed")184
185if dependency_exists("unstructured_inference"):186print(187"unstructured-inference version: ",188get_python_package_version("unstructured-inference"),189)190else:191print("unstructured-inference is not installed")192
193if dependency_exists("pytesseract"):194print(195"pytesseract version: ",196get_python_package_version("pytesseract"),197)198else:199print("pytesseract is not installed")200
201if dependency_exists("torch"):202print("Torch version: ", get_python_package_version("torch"))203else:204print("Torch is not installed")205
206if dependency_exists("detectron2"):207print("Detectron2 version: ", get_python_package_version("detectron2"))208else:209print("Detectron2 is not installed")210
211if is_python_package_installed("paddlepaddle") or is_python_package_installed(212"paddleocr",213):214print(215"PaddleOCR version: ",216get_python_package_version("paddlepaddle") or get_python_package_version("paddleocr"),217)218else:219print("PaddleOCR is not installed")220
221if is_brew_package_installed("libmagic"):222print("Libmagic version: ", get_brew_package_version("libmagic"))223else:224libmagic_version = get_libmagic_version()225if libmagic_version:226print(f"Libmagic version: {libmagic_version}")227else:228print("Libmagic is not installed")229
230if platform.system() != "Windows":231if is_brew_package_installed("libreoffice"):232print("LibreOffice version: ", get_brew_package_version("libreoffice"))233else:234libreoffice_version = get_libreoffice_version()235if libreoffice_version:236print("LibreOffice version: ", libreoffice_version)237else:238print("LibreOffice is not installed")239
240
241if __name__ == "__main__":242main()243