unstructured

Форк
0
/
collect_env.py 
242 строки · 5.8 Кб
1
import platform
2
import shutil
3
import subprocess
4

5
import pkg_resources
6

7
from unstructured.utils import dependency_exists
8

9

10
def command_exists(command):
11
    """
12
    Check if a command exists in the system
13

14
    Args:
15
        command (str): The command to check
16

17
    Returns:
18
        bool: True if command exists, False otherwise
19
    """
20
    return shutil.which(command) is not None
21

22

23
def get_python_version():
24
    """
25
    Get the current Python version
26

27
    Returns:
28
        str: The current Python version
29
    """
30
    return platform.python_version()
31

32

33
def get_os_version():
34
    """
35
    Get the current operating system version
36

37
    Returns:
38
        str: The current operating system version
39
    """
40
    return platform.platform()
41

42

43
def is_python_package_installed(package_name: str):
44
    """
45
    Check if a Python package is installed
46

47
    Args:
48
        package_name (str): The Python package to check
49

50
    Returns:
51
        bool: True if package is installed, False otherwise
52
    """
53
    result = subprocess.run(
54
        ["pip", "list"],
55
        stdout=subprocess.PIPE,
56
        text=True,
57
        check=True,
58
    )
59

60
    return any(line.lower().startswith(package_name.lower()) for line in result.stdout.splitlines())
61

62

63
def is_brew_package_installed(package_name: str):
64
    """
65
    Check if a Homebrew package is installed
66

67
    Args:
68
        package_name (str): The package to check
69

70
    Returns:
71
        bool: True if package is installed, False otherwise
72
    """
73
    if not command_exists("brew"):
74
        return False
75

76
    result = subprocess.run(
77
        ["brew", "list"],
78
        stdout=subprocess.PIPE,
79
        text=True,
80
        check=True,
81
    )
82

83
    for line in result.stdout.splitlines():
84
        if line.lower().startswith(package_name.lower()):
85
            return True
86

87
    result = subprocess.run(
88
        ["brew", "list", "--cask"],
89
        stdout=subprocess.PIPE,
90
        text=True,
91
        check=True,
92
    )
93

94
    return any(line.lower().startswith(package_name.lower()) for line in result.stdout.splitlines())
95

96

97
def get_python_package_version(package_name):
98
    """
99
    Get the version of a Python package
100

101
    Args:
102
        package_name (str): The Python package to check
103

104
    Returns:
105
        str: Version of the package, None if package is not installed
106
    """
107
    try:
108
        return pkg_resources.get_distribution(package_name).version
109
    except pkg_resources.DistributionNotFound:
110
        return None
111

112

113
def get_brew_package_version(package_name):
114
    """
115
    Get the version of a Homebrew package
116

117
    Args:
118
        package_name (str): The package to check
119

120
    Returns:
121
        str: Version of the package, None if package is not installed
122
    """
123
    if not command_exists("brew"):
124
        return None
125

126
    result = subprocess.run(
127
        ["brew", "info", package_name],
128
        stdout=subprocess.PIPE,
129
        stderr=subprocess.DEVNULL,
130
        text=True,
131
    )
132

133
    for line in result.stdout.splitlines():
134
        return line
135

136
    return None
137

138

139
def get_libmagic_version():
140
    """
141
    Get the version of libmagic
142

143
    Returns:
144
        str: Version of libmagic, None if libmagic is not installed
145
    """
146
    result = subprocess.run(
147
        ["file", "--version", "--headless"],
148
        stdout=subprocess.PIPE,
149
        stderr=subprocess.PIPE,
150
        text=True,
151
    )
152

153
    return result.stdout.strip()
154

155

156
def get_libreoffice_version():
157
    """
158
    Get the version of LibreOffice
159

160
    Returns:
161
        str: Version of LibreOffice, None if LibreOffice is not installed
162
    """
163
    result = subprocess.run(
164
        ["libreoffice", "--version", "--headless"],
165
        stdout=subprocess.PIPE,
166
        stderr=subprocess.PIPE,
167
        text=True,
168
    )
169

170
    return result.stdout.strip()
171

172

173
def main():
174
    """
175
    The main function to run all checks
176
    """
177
    print("OS version: ", get_os_version())
178
    print("Python version: ", get_python_version())
179

180
    if dependency_exists("unstructured"):
181
        print("unstructured version: ", get_python_package_version("unstructured"))
182
    else:
183
        print("unstructured is not installed")
184

185
    if dependency_exists("unstructured_inference"):
186
        print(
187
            "unstructured-inference version: ",
188
            get_python_package_version("unstructured-inference"),
189
        )
190
    else:
191
        print("unstructured-inference is not installed")
192

193
    if dependency_exists("pytesseract"):
194
        print(
195
            "pytesseract version: ",
196
            get_python_package_version("pytesseract"),
197
        )
198
    else:
199
        print("pytesseract is not installed")
200

201
    if dependency_exists("torch"):
202
        print("Torch version: ", get_python_package_version("torch"))
203
    else:
204
        print("Torch is not installed")
205

206
    if dependency_exists("detectron2"):
207
        print("Detectron2 version: ", get_python_package_version("detectron2"))
208
    else:
209
        print("Detectron2 is not installed")
210

211
    if is_python_package_installed("paddlepaddle") or is_python_package_installed(
212
        "paddleocr",
213
    ):
214
        print(
215
            "PaddleOCR version: ",
216
            get_python_package_version("paddlepaddle") or get_python_package_version("paddleocr"),
217
        )
218
    else:
219
        print("PaddleOCR is not installed")
220

221
    if is_brew_package_installed("libmagic"):
222
        print("Libmagic version: ", get_brew_package_version("libmagic"))
223
    else:
224
        libmagic_version = get_libmagic_version()
225
        if libmagic_version:
226
            print(f"Libmagic version: {libmagic_version}")
227
        else:
228
            print("Libmagic is not installed")
229

230
    if platform.system() != "Windows":
231
        if is_brew_package_installed("libreoffice"):
232
            print("LibreOffice version: ", get_brew_package_version("libreoffice"))
233
        else:
234
            libreoffice_version = get_libreoffice_version()
235
            if libreoffice_version:
236
                print("LibreOffice version: ", libreoffice_version)
237
            else:
238
                print("LibreOffice is not installed")
239

240

241
if __name__ == "__main__":
242
    main()
243

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.