-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_for_dependencies.py
160 lines (129 loc) · 4.4 KB
/
check_for_dependencies.py
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
#!/bin/env python3
"""This program checks that the needed dependencies are installed on the host machine. It will check dependencies
installed explicitly by pip, those which are bundled with other programs (not explicitly tracked by pip), and
some system dependencies.
Attributes
----------
pip_dependencies : dict
a set of pip tracked dependencies and some associated dependencies
try_dependencies : list
a list of dependencies not explicitly tracked by pip, but which are nevertheless used by this module
system_dependencies : list
a list of system-level, non-python dependencies which are needed for some of the other modules to work
Functions
---------
is_tool(name)
Checks if `name` is within PATH and is marked as executable
check_for_dependencies()
Runs the checks for all of the dependencies
"""
import importlib
from shutil import which
import pkg_resources
pip_dependencies = {
"urllib3" : [],
"magiconfig" : [],
"beautifulsoup4" : [],
"googlesearch-python" : [],
}
try_dependencies = []
system_dependencies = []
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable.
Suggested from: https://stackoverflow.com/questions/11210104/check-if-a-program-exists-from-a-python-script
"""
return which(name) is not None
def check_pip_dependencies(dependency_dict):
"""Checks for the pip-installed dependencies.
Parameters
----------
dependency_dict : dict
The keys of the dictionary contain the major dependencies and the values contain a list of the sub-dependencies
Returns
-------
list
A list of the missing packages
"""
# pylint: disable=E1133
installed_packages = pkg_resources.working_set
installed_packages_list = sorted([f"{i.key}" for i in installed_packages])
missing = []
for dependency, subdependencies in dependency_dict.items():
print(f"Checking for {dependency} ... ", end="")
if dependency not in installed_packages_list:
missing.append(dependency)
print("MISSING")
else:
print("FOUND")
for subdependency in subdependencies:
print(f"\tChecking for {subdependency} ... ", end="")
if subdependency not in installed_packages_list:
missing.append(subdependency)
print("MISSING")
else:
print("FOUND")
return missing
def check_try_dependencies(dependency_list):
"""Checks for the python dependencies not tracked by pip.
Parameters
----------
dependency_list : list
The list of modules to try to import to see if they are installed
Returns
-------
list
A list of the missing modules
"""
missing = []
for dependency in dependency_list:
print(f"Checking for {dependency} ... ", end="")
try:
importlib.import_module(dependency)
except ImportError:
missing.append(dependency)
print("MISSING")
else:
print("FOUND")
return missing
def check_system_dependencies(dependency_list):
"""Checks for the system dependencies.
Parameters
----------
dependency_list : list
The list of modules to look for
Returns
-------
list
A list of the missing modules
"""
missing = []
for dependency in dependency_list:
print(f"Checking for {dependency} ... ", end="")
if not is_tool(dependency):
missing.append(dependency)
print("MISSING")
else:
print("FOUND")
return missing
def check_for_dependencies():
"""Checks whether or not the dependencies are installed
Raises
------
ModuleNotFoundError
If the number of missing packages isn't zero
Returns
-------
list
The number of missing packages
Suggested from: https://www.activestate.com/resources/quick-reads/how-to-list-installed-python-packages/
"""
missing_pip = check_pip_dependencies(pip_dependencies)
missing_import = check_try_dependencies(try_dependencies)
missing_system = check_system_dependencies(system_dependencies)
n_missing = len(missing_pip) + len(missing_import) + len(missing_system)
if n_missing > 0:
raise ModuleNotFoundError("There are missing dependencies!")
print("\nAll dependencies installed!")
return n_missing
if __name__ == "__main__":
check_for_dependencies()