-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_poetry_setup.py
More file actions
127 lines (112 loc) · 4.16 KB
/
test_poetry_setup.py
File metadata and controls
127 lines (112 loc) · 4.16 KB
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
#!/usr/bin/env python
"""
Script to test that the Poetry environment is set up correctly.
This script checks that all required dependencies are installed and working
in the Poetry environment.
"""
import sys
import importlib
import subprocess
import logging
def setup_logging():
"""Set up logging configuration."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
return logging.getLogger("test_poetry_setup")
def check_dependency(logger, module_name, import_name=None):
"""
Check if a dependency is installed and working.
Args:
logger: The logger to use.
module_name (str): The name of the module to import.
import_name (str, optional): The name to import from the module.
If None, the module itself is imported.
Returns:
bool: True if the dependency is installed and working, False otherwise.
"""
try:
if import_name:
# Import a specific name from the module
module = importlib.import_module(module_name)
getattr(module, import_name)
logger.info(f"[SUCCESS] Successfully imported {import_name} from {module_name}")
else:
# Import the module itself
importlib.import_module(module_name)
logger.info(f"[SUCCESS] Successfully imported {module_name}")
return True
except ImportError:
logger.error(f"[ERROR] Failed to import {module_name}")
return False
except AttributeError:
logger.error(f"[ERROR] Failed to import {import_name} from {module_name}")
return False
def check_poetry_environment(logger):
"""
Check that we're running in a Poetry environment.
Returns:
bool: True if we're in a Poetry environment, False otherwise.
"""
try:
# Run 'poetry env info' to check if we're in a Poetry environment
result = subprocess.run(
["poetry", "env", "info"],
capture_output=True,
text=True,
check=True
)
logger.info("[SUCCESS] Running in a Poetry environment")
logger.info(result.stdout)
return True
except subprocess.CalledProcessError:
logger.error("[ERROR] Not running in a Poetry environment")
return False
except FileNotFoundError:
logger.error("[ERROR] Poetry is not installed or not in PATH")
return False
def main():
"""Run the Poetry environment test."""
logger = setup_logging()
logger.info("Testing Poetry environment setup...")
# Check that we're running in a Poetry environment
if not check_poetry_environment(logger):
logger.error("Please run this script within a Poetry environment.")
logger.error("You can activate the Poetry environment with 'poetry shell'")
logger.error("or run this script with 'poetry run python test_poetry_setup.py'")
return 1
# Check core dependencies
dependencies = [
("selenium", None),
("selenium.webdriver", "Chrome"),
("selenium.webdriver.chrome.options", "Options"),
("selenium.webdriver.common.by", "By"),
("selenium.webdriver.support.ui", "WebDriverWait"),
("selenium.webdriver.support.expected_conditions", None),
("webdriver_manager.chrome", "ChromeDriverManager"),
("requests", None),
("pandas", None),
("PyPDF2", None),
("json", None),
("os", None),
("sys", None),
("logging", None),
]
all_dependencies_ok = True
for module_name, import_name in dependencies:
if not check_dependency(logger, module_name, import_name):
all_dependencies_ok = False
# Print summary
if all_dependencies_ok:
logger.info("[SUCCESS] Poetry environment is set up correctly!")
return 0
else:
logger.error("[ERROR] Some dependencies are missing or not working.")
logger.error("Try running 'poetry install' to install all dependencies.")
return 1
if __name__ == "__main__":
sys.exit(main())