|
1 | 1 | """Functionality to check for the availability and version of dependencies. |
2 | 2 |
|
3 | | -This file is generated by l2tdevtools update-dependencies.py, any dependency |
4 | | -related changes should be made in dependencies.ini. |
| 3 | +This file is generated by l2tdevtools update-dependencies.py, any dependency related |
| 4 | +changes should be made in dependencies.ini. |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import re |
8 | 8 |
|
9 | | - |
10 | 9 | # Dictionary that contains version tuples per module name. |
11 | 10 | # |
12 | 11 | # A version tuple consists of: |
|
15 | 14 | # Where version_attribute_name is either a name of an attribute, |
16 | 15 | # property or method. |
17 | 16 | PYTHON_DEPENDENCIES = { |
18 | | -${python_dependencies}} |
| 17 | +${python_dependencies} |
| 18 | +} |
19 | 19 |
|
20 | | -_VERSION_SPLIT_REGEX = re.compile(r'\.|\-') |
| 20 | +_VERSION_SPLIT_REGEX = re.compile(r"\.|\-") |
21 | 21 |
|
22 | 22 |
|
23 | 23 | def _CheckPythonModule( |
24 | | - module_name, version_attribute_name, minimum_version, |
25 | | - is_required=True, maximum_version=None, verbose_output=True): |
26 | | - """Checks the availability of a Python module. |
27 | | -
|
28 | | - Args: |
29 | | - module_name (str): name of the module. |
30 | | - version_attribute_name (str): name of the attribute that contains |
31 | | - the module version or method to retrieve the module version. |
32 | | - minimum_version (str): minimum required version. |
33 | | - is_required (Optional[bool]): True if the Python module is a required |
34 | | - dependency. |
35 | | - maximum_version (Optional[str]): maximum required version. Should only be |
36 | | - used if there is a later version that is not supported. |
37 | | - verbose_output (Optional[bool]): True if output should be verbose. |
38 | | -
|
39 | | - Returns: |
40 | | - bool: True if the Python module is available and conforms to |
41 | | - the minimum required version, False otherwise. |
42 | | - """ |
43 | | - module_object = _ImportPythonModule(module_name) |
44 | | - if not module_object: |
45 | | - if not is_required: |
46 | | - print(f'[OPTIONAL]\tmissing: {module_name:s}.') |
47 | | - return True |
48 | | - |
49 | | - print(f'[FAILURE]\tmissing: {module_name:s}.') |
50 | | - return False |
51 | | - |
52 | | - if not version_attribute_name or not minimum_version: |
53 | | - if verbose_output: |
54 | | - print(f'[OK]\t\t{module_name:s}') |
55 | | - return True |
56 | | - |
57 | | - module_version = None |
58 | | - if not version_attribute_name.endswith('()'): |
59 | | - module_version = getattr(module_object, version_attribute_name, None) |
60 | | - else: |
61 | | - version_method = getattr(module_object, version_attribute_name[:-2], None) |
62 | | - if version_method: |
63 | | - module_version = version_method() |
64 | | - |
65 | | - if not module_version: |
66 | | - if not is_required: |
67 | | - print(( |
68 | | - f'[OPTIONAL]\tunable to determine version information ' |
69 | | - f'for: {module_name:s}')) |
70 | | - return True |
71 | | - |
72 | | - print(( |
73 | | - f'[FAILURE]\tunable to determine version information ' |
74 | | - f'for: {module_name:s}')) |
75 | | - return False |
76 | | - |
77 | | - # Make sure the module version is a string. |
78 | | - module_version = f'{module_version!s}' |
79 | | - |
80 | | - # Remove a version suffix, such as: 0.7.0~rc1 |
81 | | - module_version_list = _VERSION_SPLIT_REGEX.split(module_version) |
82 | | - |
83 | | - try: |
84 | | - int(module_version_list[-1], 10) |
85 | | - except (TypeError, ValueError): |
86 | | - module_version_list.pop() |
87 | | - |
88 | | - # Split the version string and convert every digit into an integer. |
89 | | - # A string compare of both version strings will yield an incorrect result. |
90 | | - module_version_map = list(map(int, module_version_list)) |
91 | | - minimum_version_map = list( |
92 | | - map(int, _VERSION_SPLIT_REGEX.split(minimum_version))) |
93 | | - |
94 | | - if module_version_map < minimum_version_map: |
95 | | - if not is_required: |
96 | | - print(( |
97 | | - f'[OPTIONAL]\t{module_name:s} version: {module_version!s} is too ' |
98 | | - f'old, {minimum_version!s} or later required.')) |
99 | | - return True |
100 | | - |
101 | | - print(( |
102 | | - f'[FAILURE]\t{module_name:s} version: {module_version!s} is too old, ' |
103 | | - f'{minimum_version!s} or later required.')) |
104 | | - return False |
105 | | - |
106 | | - if maximum_version: |
107 | | - maximum_version_map = list( |
108 | | - map(int, _VERSION_SPLIT_REGEX.split(maximum_version))) |
109 | | - if module_version_map > maximum_version_map: |
110 | | - if not is_required: |
111 | | - print(( |
112 | | - f'[OPTIONAL]\t{module_name:s} version: {module_version!s} is too ' |
113 | | - f'recent, {minimum_version!s} or earlier required.')) |
| 24 | + module_name, |
| 25 | + version_attribute_name, |
| 26 | + minimum_version, |
| 27 | + is_required=True, |
| 28 | + maximum_version=None, |
| 29 | + verbose_output=True, |
| 30 | +): |
| 31 | + """Checks the availability of a Python module. |
| 32 | +
|
| 33 | + Args: |
| 34 | + module_name (str): name of the module. |
| 35 | + version_attribute_name (str): name of the attribute that contains |
| 36 | + the module version or method to retrieve the module version. |
| 37 | + minimum_version (str): minimum required version. |
| 38 | + is_required (Optional[bool]): True if the Python module is a required |
| 39 | + dependency. |
| 40 | + maximum_version (Optional[str]): maximum required version. Should only be |
| 41 | + used if there is a later version that is not supported. |
| 42 | + verbose_output (Optional[bool]): True if output should be verbose. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + bool: True if the Python module is available and conforms to |
| 46 | + the minimum required version, False otherwise. |
| 47 | + """ |
| 48 | + module_object = _ImportPythonModule(module_name) |
| 49 | + if not module_object: |
| 50 | + if not is_required: |
| 51 | + print(f"[OPTIONAL]\tmissing: {module_name:s}.") |
| 52 | + return True |
| 53 | + |
| 54 | + print(f"[FAILURE]\tmissing: {module_name:s}.") |
| 55 | + return False |
| 56 | + |
| 57 | + if not version_attribute_name or not minimum_version: |
| 58 | + if verbose_output: |
| 59 | + print(f"[OK]\t\t{module_name:s}") |
114 | 60 | return True |
115 | 61 |
|
116 | | - print(( |
117 | | - f'[FAILURE]\t{module_name:s} version: {module_version!s} is too ' |
118 | | - f'recent, {maximum_version!s} or earlier required.')) |
119 | | - return False |
| 62 | + module_version = None |
| 63 | + if not version_attribute_name.endswith("()"): |
| 64 | + module_version = getattr(module_object, version_attribute_name, None) |
| 65 | + else: |
| 66 | + version_method = getattr(module_object, version_attribute_name[:-2], None) |
| 67 | + if version_method: |
| 68 | + module_version = version_method() |
| 69 | + |
| 70 | + if not module_version: |
| 71 | + if not is_required: |
| 72 | + print( |
| 73 | + f"[OPTIONAL]\tunable to determine version information " |
| 74 | + f"for: {module_name:s}" |
| 75 | + ) |
| 76 | + return True |
| 77 | + |
| 78 | + print( |
| 79 | + f"[FAILURE]\tunable to determine version information " |
| 80 | + f"for: {module_name:s}" |
| 81 | + ) |
| 82 | + return False |
| 83 | + |
| 84 | + # Make sure the module version is a string. |
| 85 | + module_version = f"{module_version!s}" |
| 86 | + |
| 87 | + # Remove a version suffix, such as: 0.7.0~rc1 |
| 88 | + module_version_list = _VERSION_SPLIT_REGEX.split(module_version) |
| 89 | + |
| 90 | + try: |
| 91 | + int(module_version_list[-1], 10) |
| 92 | + except (TypeError, ValueError): |
| 93 | + module_version_list.pop() |
| 94 | + |
| 95 | + # Split the version string and convert every digit into an integer. |
| 96 | + # A string compare of both version strings will yield an incorrect result. |
| 97 | + module_version_map = list(map(int, module_version_list)) |
| 98 | + minimum_version_map = list(map(int, _VERSION_SPLIT_REGEX.split(minimum_version))) |
| 99 | + |
| 100 | + if module_version_map < minimum_version_map: |
| 101 | + if not is_required: |
| 102 | + print( |
| 103 | + f"[OPTIONAL]\t{module_name:s} version: {module_version!s} is too " |
| 104 | + f"old, {minimum_version!s} or later required." |
| 105 | + ) |
| 106 | + return True |
| 107 | + |
| 108 | + print( |
| 109 | + f"[FAILURE]\t{module_name:s} version: {module_version!s} is too old, " |
| 110 | + f"{minimum_version!s} or later required." |
| 111 | + ) |
| 112 | + return False |
| 113 | + |
| 114 | + if maximum_version: |
| 115 | + maximum_version_map = list( |
| 116 | + map(int, _VERSION_SPLIT_REGEX.split(maximum_version)) |
| 117 | + ) |
| 118 | + if module_version_map > maximum_version_map: |
| 119 | + if not is_required: |
| 120 | + print( |
| 121 | + f"[OPTIONAL]\t{module_name:s} version: {module_version!s} is too " |
| 122 | + f"recent, {minimum_version!s} or earlier required." |
| 123 | + ) |
| 124 | + return True |
| 125 | + |
| 126 | + print( |
| 127 | + f"[FAILURE]\t{module_name:s} version: {module_version!s} is too " |
| 128 | + f"recent, {maximum_version!s} or earlier required." |
| 129 | + ) |
| 130 | + return False |
120 | 131 |
|
121 | | - if verbose_output: |
122 | | - print(f'[OK]\t\t{module_name:s} version: {module_version!s}') |
| 132 | + if verbose_output: |
| 133 | + print(f"[OK]\t\t{module_name:s} version: {module_version!s}") |
123 | 134 |
|
124 | | - return True |
| 135 | + return True |
125 | 136 |
|
126 | 137 |
|
127 | 138 | def _ImportPythonModule(module_name): |
128 | | - """Imports a Python module. |
| 139 | + """Imports a Python module. |
129 | 140 |
|
130 | | - Args: |
131 | | - module_name (str): name of the module. |
| 141 | + Args: |
| 142 | + module_name (str): name of the module. |
132 | 143 |
|
133 | | - Returns: |
134 | | - module: Python module or None if the module cannot be imported. |
135 | | - """ |
136 | | - try: |
137 | | - module_object = list(map(__import__, [module_name]))[0] |
138 | | - except ImportError: |
139 | | - return None |
| 144 | + Returns: |
| 145 | + module: Python module or None if the module cannot be imported. |
| 146 | + """ |
| 147 | + try: |
| 148 | + module_object = list(map(__import__, [module_name]))[0] |
| 149 | + except ImportError: |
| 150 | + return None |
140 | 151 |
|
141 | | - # If the module name contains dots get the upper most module object. |
142 | | - if '.' in module_name: |
143 | | - for submodule_name in module_name.split('.')[1:]: |
144 | | - module_object = getattr(module_object, submodule_name, None) |
| 152 | + # If the module name contains dots get the upper most module object. |
| 153 | + if "." in module_name: |
| 154 | + for submodule_name in module_name.split(".")[1:]: |
| 155 | + module_object = getattr(module_object, submodule_name, None) |
145 | 156 |
|
146 | | - return module_object |
| 157 | + return module_object |
147 | 158 |
|
148 | 159 |
|
149 | 160 | def CheckDependencies(verbose_output=True): |
150 | | - """Checks the availability of the dependencies. |
151 | | -
|
152 | | - Args: |
153 | | - verbose_output (Optional[bool]): True if output should be verbose. |
154 | | -
|
155 | | - Returns: |
156 | | - bool: True if the dependencies are available, False otherwise. |
157 | | - """ |
158 | | - print('Checking availability and versions of dependencies.') |
159 | | - check_result = True |
160 | | - |
161 | | - for module_name, version_tuple in sorted(PYTHON_DEPENDENCIES.items()): |
162 | | - if not _CheckPythonModule( |
163 | | - module_name, version_tuple[0], version_tuple[1], |
164 | | - is_required=version_tuple[3], maximum_version=version_tuple[2], |
165 | | - verbose_output=verbose_output): |
166 | | - check_result = False |
167 | | - |
168 | | - if check_result and not verbose_output: |
169 | | - print('[OK]') |
170 | | - |
171 | | - print('') |
172 | | - return check_result |
| 161 | + """Checks the availability of the dependencies. |
| 162 | +
|
| 163 | + Args: |
| 164 | + verbose_output (Optional[bool]): True if output should be verbose. |
| 165 | +
|
| 166 | + Returns: |
| 167 | + bool: True if the dependencies are available, False otherwise. |
| 168 | + """ |
| 169 | + print("Checking availability and versions of dependencies.") |
| 170 | + check_result = True |
| 171 | + |
| 172 | + for module_name, version_tuple in sorted(PYTHON_DEPENDENCIES.items()): |
| 173 | + if not _CheckPythonModule( |
| 174 | + module_name, |
| 175 | + version_tuple[0], |
| 176 | + version_tuple[1], |
| 177 | + is_required=version_tuple[3], |
| 178 | + maximum_version=version_tuple[2], |
| 179 | + verbose_output=verbose_output, |
| 180 | + ): |
| 181 | + check_result = False |
| 182 | + |
| 183 | + if check_result and not verbose_output: |
| 184 | + print("[OK]") |
| 185 | + |
| 186 | + print("") |
| 187 | + return check_result |
0 commit comments