Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Azure
GitHub Repository: Azure/Azure-Sentinel-Notebooks
Path: blob/master/src/SentinelUtilities/SentinelUtils/version_management.py
3255 views
1
# -------------------------------------------------------------------------
2
# Copyright (c) Microsoft Corporation. All rights reserved.
3
# Licensed under the MIT License. See License.txt in the project root for
4
# license information.
5
# --------------------------------------------------------------------------
6
"""
7
Version Management:
8
This module is used to validate installed Python packages
9
that are required by Microsoft Sentinel Notebooks.
10
"""
11
12
import sys
13
import pkg_resources
14
15
# pylint: disable-msg=R0903
16
class VersionInformation:
17
""" object model for version information """
18
name = ''
19
current_version = ''
20
required_version = ''
21
requirement_met = False
22
message = ''
23
24
# pylint: disable-msg=R0201
25
class ModuleVersionCheck:
26
""" class implements version checking """
27
28
# pylint: disable=line-too-long
29
def validate_python(self, required_version):
30
""" validating Python version """
31
version = VersionInformation()
32
version.name = 'Python'
33
version.current_version = sys.version
34
version.required_version = required_version
35
version.requirement_met = sys.version_info >= tuple(int(x) for x in required_version.split("."))
36
version.message = VersionInformation.name + required_version + ' is required' if not VersionInformation.requirement_met else ''
37
return version
38
39
# pylint: disable-msg=W0702
40
def validate_installed_modules(self, module_list):
41
""" validating installed modules' version """
42
module_versions = []
43
for mod_info in module_list:
44
version = VersionInformation()
45
version.name, version.required_version = mod_info.split(">=")
46
try:
47
pkg_resources.require(mod_info)
48
version.requirement_met = True
49
except:
50
version.requirement_met = False
51
try:
52
version.message = str(sys.exc_info()[0].report)
53
except:
54
version.message = 'Unknown error'
55
finally:
56
if 'azure' in version.name and version.message.find("VersionConflict") >= 0:
57
version.requirement_met = True
58
elif version.message == '' or version.message.find("DistributionNotFound") < 0:
59
version = self.get_version_information(version, mod_info)
60
61
module_versions.append(version)
62
return module_versions
63
64
# pylint: disable-msg=W0613
65
def get_version_information(self, version, mod_info):
66
""" get version information """
67
mod_list = pkg_resources.WorkingSet()
68
items = list(filter(lambda x: x.project_name.startswith(version.name), mod_list))
69
if not items:
70
version.current_version = items[0].version
71
return version
72
73
# end of the class
74
75