Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

env_sanity_check.py 7.0 KB

You have to be logged in to leave a comment. Sign In
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
161
  1. import logging
  2. import os
  3. import sys
  4. from pip._internal.operations.freeze import freeze
  5. from typing import List, Dict, Union
  6. from pathlib import Path
  7. from packaging.version import Version
  8. from super_gradients.common.abstractions.abstract_logger import get_logger
  9. LIB_CHECK_IMPOSSIBLE_MSG = 'Library check is not supported when super_gradients installed through "git+https://github.com/..." command'
  10. logger = get_logger(__name__, log_level=logging.DEBUG)
  11. def get_requirements_path(requirements_file_name: str) -> Union[None, Path]:
  12. """Get the path of requirement.txt from the root if exist.
  13. There is a difference when installed from artifact or locally.
  14. - In the first case, requirements.txt is copied to the package during the CI.
  15. - In the second case, requirements.txt in the root of the project.
  16. Note: This is because when installed from artifact only the source code is accessible, so requirements.txt has to be
  17. copied to the package root (./src/super_gradients). This is automatically done with the CI to make sure that
  18. in the github we only have 1 source of truth for requirements.txt. The consequence being that when the code
  19. is copied/cloned from github, the requirements.txt was not copied to the super_gradients package root, so we
  20. need to go to the project root (.) to find it.
  21. """
  22. file_path = Path(__file__) # super-gradients/src/super_gradients/sanity_check/env_sanity_check.py
  23. package_root = file_path.parent.parent # moving to super-gradients/src/super_gradients
  24. project_root = package_root.parent.parent # moving to super-gradients
  25. # If installed from artifact, requirements.txt is in package_root, if installed locally it is in project_root
  26. if (package_root / requirements_file_name).exists():
  27. return package_root / requirements_file_name
  28. elif (project_root / requirements_file_name).exists():
  29. return project_root / requirements_file_name
  30. else:
  31. return None # Could happen when installed through github directly ("pip install git+https://github.com/...")
  32. def get_installed_libs_with_version() -> Dict[str, str]:
  33. """Get all the installed libraries, and outputs it as a dict: lib -> version"""
  34. installed_libs_with_version = {}
  35. for lib_with_version in freeze():
  36. if "==" in lib_with_version:
  37. lib, version = lib_with_version.split("==")
  38. installed_libs_with_version[lib.lower()] = version
  39. return installed_libs_with_version
  40. def verify_installed_libraries() -> List[str]:
  41. """Check that all installed libs respect the requirement.txt"""
  42. requirements_path = get_requirements_path("requirements.txt")
  43. pro_requirements_path = get_requirements_path("requirements.pro.txt")
  44. if requirements_path is None:
  45. return [LIB_CHECK_IMPOSSIBLE_MSG]
  46. with open(requirements_path, "r") as f:
  47. requirements = f.readlines()
  48. installed_libs_with_version = get_installed_libs_with_version()
  49. # if pro_requirements_path is not None:
  50. with open(pro_requirements_path, "r") as f:
  51. pro_requirements = f.readlines()
  52. if "deci-lab-client" in installed_libs_with_version:
  53. requirements += pro_requirements
  54. errors = []
  55. for requirement in requirements:
  56. if ">=" in requirement:
  57. constraint = ">="
  58. elif "~=" in requirement:
  59. constraint = "~="
  60. elif "==" in requirement:
  61. constraint = "=="
  62. else:
  63. continue
  64. lib, required_version_str = requirement.split(constraint)
  65. if lib.lower() not in installed_libs_with_version.keys():
  66. errors.append(f"{lib} required but not found")
  67. continue
  68. installed_version_str = installed_libs_with_version[lib.lower()]
  69. installed_version, required_version = Version(installed_version_str), Version(required_version_str)
  70. is_constraint_respected = {
  71. ">=": installed_version >= required_version,
  72. "~=": (installed_version.major == required_version.major and
  73. installed_version.minor == required_version.minor and
  74. installed_version.micro >= required_version.micro),
  75. "==": installed_version == required_version
  76. }
  77. if not is_constraint_respected[constraint]:
  78. errors.append(
  79. f"{lib} is installed with version {installed_version} which does not satisfy {requirement} (based on {requirements_path})")
  80. return errors
  81. def verify_os() -> List[str]:
  82. """Verifying operating system name and platform"""
  83. if 'linux' not in sys.platform.lower():
  84. return ['Deci officially supports only Linux kernels. Some features may not work as expected.']
  85. return []
  86. def env_sanity_check():
  87. """Run the sanity check tests and log everything that does not meet requirements"""
  88. display_sanity_check = os.getenv("DISPLAY_SANITY_CHECK", "False") == "True"
  89. stdout_log_level = logging.INFO if display_sanity_check else logging.DEBUG
  90. logger.setLevel(logging.DEBUG) # We want to log everything regardless of DISPLAY_SANITY_CHECK
  91. requirement_checkers = {
  92. 'operating_system': verify_os,
  93. 'libraries': verify_installed_libraries,
  94. }
  95. logger.log(stdout_log_level, 'SuperGradients Sanity Check Started')
  96. logger.log(stdout_log_level, f'Checking the following components: {list(requirement_checkers.keys())}')
  97. logger.log(stdout_log_level, '_' * 20)
  98. lib_check_is_impossible = False
  99. sanity_check_errors = {}
  100. for test_name, test_function in requirement_checkers.items():
  101. logger.log(stdout_log_level, f"Verifying {test_name}...")
  102. errors = test_function()
  103. if errors == [LIB_CHECK_IMPOSSIBLE_MSG]:
  104. lib_check_is_impossible = True
  105. logger.log(stdout_log_level, LIB_CHECK_IMPOSSIBLE_MSG)
  106. elif len(errors) > 0:
  107. sanity_check_errors[test_name] = errors
  108. for error in errors:
  109. logger.log(logging.ERROR, f"\33[31mFailed to verify {test_name}: {error}\33[0m")
  110. else:
  111. logger.log(stdout_log_level, f'{test_name} OK')
  112. logger.log(stdout_log_level, '_' * 20)
  113. if sanity_check_errors:
  114. logger.log(stdout_log_level, f'The current environment does not meet Deci\'s needs, errors found in: {", ".join(list(sanity_check_errors.keys()))}')
  115. elif lib_check_is_impossible:
  116. logger.log(stdout_log_level, LIB_CHECK_IMPOSSIBLE_MSG)
  117. else:
  118. logger.log(stdout_log_level, 'Great, Looks like the current environment meet\'s Deci\'s requirements!')
  119. # The last message needs to be displayed independently of DISPLAY_SANITY_CHECK
  120. if display_sanity_check:
  121. logger.info('** This check can be hidden by setting the env variable DISPLAY_SANITY_CHECK=False prior to import. **')
  122. else:
  123. logger.info('** A sanity check is done when importing super_gradients for the first time. **\n'
  124. '-> You can see the details by setting the env variable DISPLAY_SANITY_CHECK=True prior to import.')
  125. if __name__ == '__main__':
  126. env_sanity_check()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...