File size: 12,357 Bytes
c293f7c | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | #!/usr/bin/env python3
"""
Environment validation script for the misinformation heatmap application.
Checks system requirements, dependencies, and configuration.
"""
import argparse
import importlib
import logging
import os
import platform
import subprocess
import sys
from pathlib import Path
from typing import Dict, List, Tuple, Optional
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class EnvironmentValidator:
"""Validates the development environment setup."""
def __init__(self, mode: str = "local"):
self.mode = mode
self.errors: List[str] = []
self.warnings: List[str] = []
def check_python_version(self) -> bool:
"""Check Python version requirements."""
logger.info("Checking Python version...")
version = sys.version_info
required_major, required_minor = 3, 8
if version.major < required_major or (version.major == required_major and version.minor < required_minor):
self.errors.append(
f"Python {required_major}.{required_minor}+ required, found {version.major}.{version.minor}.{version.micro}"
)
return False
logger.info(f"Python version: {version.major}.{version.minor}.{version.micro} ✓")
return True
def check_system_requirements(self) -> bool:
"""Check system-level requirements."""
logger.info("Checking system requirements...")
# Check available memory
try:
if platform.system() == "Linux":
with open('/proc/meminfo', 'r') as f:
meminfo = f.read()
for line in meminfo.split('\n'):
if 'MemTotal:' in line:
total_mem = int(line.split()[1]) // 1024 # Convert to MB
break
elif platform.system() == "Darwin": # macOS
result = subprocess.run(['sysctl', 'hw.memsize'], capture_output=True, text=True)
total_mem = int(result.stdout.split()[1]) // (1024 * 1024) # Convert to MB
elif platform.system() == "Windows":
import psutil
total_mem = psutil.virtual_memory().total // (1024 * 1024) # Convert to MB
else:
total_mem = 4096 # Assume 4GB if unknown
if total_mem < 2048: # 2GB minimum
self.warnings.append(f"Low memory detected: {total_mem}MB (recommended: 4GB+)")
else:
logger.info(f"Available memory: {total_mem}MB ✓")
except Exception as e:
self.warnings.append(f"Could not check memory: {e}")
# Check disk space
try:
disk_usage = os.statvfs('.')
free_space = (disk_usage.f_frsize * disk_usage.f_bavail) // (1024 * 1024) # MB
if free_space < 1024: # 1GB minimum
self.warnings.append(f"Low disk space: {free_space}MB (recommended: 2GB+)")
else:
logger.info(f"Available disk space: {free_space}MB ✓")
except Exception as e:
self.warnings.append(f"Could not check disk space: {e}")
return True
def check_python_packages(self) -> bool:
"""Check required Python packages."""
logger.info("Checking Python packages...")
required_packages = [
'fastapi',
'uvicorn',
'pydantic',
'sqlalchemy',
'transformers',
'torch',
'numpy',
'pandas',
'requests',
'python-dotenv'
]
if self.mode == "cloud":
required_packages.extend([
'google-cloud-pubsub',
'google-cloud-bigquery',
'google-cloud-storage',
'ibm-watson'
])
missing_packages = []
for package in required_packages:
try:
importlib.import_module(package.replace('-', '_'))
logger.debug(f"Package {package} ✓")
except ImportError:
missing_packages.append(package)
if missing_packages:
self.errors.append(f"Missing Python packages: {', '.join(missing_packages)}")
return False
logger.info("All required Python packages found ✓")
return True
def check_external_tools(self) -> bool:
"""Check external tools and utilities."""
logger.info("Checking external tools...")
tools = {
'curl': 'curl --version',
'git': 'git --version'
}
if self.mode == "cloud":
tools['gcloud'] = 'gcloud version'
missing_tools = []
for tool, command in tools.items():
try:
result = subprocess.run(
command.split(),
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
logger.debug(f"Tool {tool} ✓")
else:
missing_tools.append(tool)
except (subprocess.TimeoutExpired, FileNotFoundError):
missing_tools.append(tool)
if missing_tools:
if 'gcloud' in missing_tools and self.mode == "cloud":
self.errors.append("gcloud CLI required for cloud mode")
else:
self.warnings.append(f"Optional tools not found: {', '.join(missing_tools)}")
return True
def check_environment_variables(self) -> bool:
"""Check required environment variables."""
logger.info("Checking environment variables...")
required_vars = []
optional_vars = [
'DEBUG',
'LOG_LEVEL',
'API_HOST',
'API_PORT'
]
if self.mode == "cloud":
required_vars.extend([
'GOOGLE_CLOUD_PROJECT',
'GOOGLE_APPLICATION_CREDENTIALS'
])
optional_vars.extend([
'WATSON_DISCOVERY_API_KEY',
'WATSON_DISCOVERY_URL'
])
missing_required = []
for var in required_vars:
if not os.getenv(var):
missing_required.append(var)
if missing_required:
self.errors.append(f"Missing required environment variables: {', '.join(missing_required)}")
return False
# Check optional variables
missing_optional = [var for var in optional_vars if not os.getenv(var)]
if missing_optional:
self.warnings.append(f"Optional environment variables not set: {', '.join(missing_optional)}")
logger.info("Environment variables check completed ✓")
return True
def check_file_structure(self) -> bool:
"""Check required file and directory structure."""
logger.info("Checking file structure...")
required_files = [
'backend/api.py',
'backend/config.py',
'backend/database.py',
'backend/requirements.txt',
'frontend/index.html',
'frontend/js/app.js',
'frontend/styles.css'
]
required_dirs = [
'backend',
'frontend',
'data',
'scripts'
]
missing_files = []
missing_dirs = []
# Check files
for file_path in required_files:
if not Path(file_path).exists():
missing_files.append(file_path)
# Check directories
for dir_path in required_dirs:
if not Path(dir_path).exists():
missing_dirs.append(dir_path)
if missing_files:
self.errors.append(f"Missing required files: {', '.join(missing_files)}")
if missing_dirs:
self.errors.append(f"Missing required directories: {', '.join(missing_dirs)}")
if not missing_files and not missing_dirs:
logger.info("File structure check completed ✓")
return True
return False
def check_network_connectivity(self) -> bool:
"""Check network connectivity for external services."""
logger.info("Checking network connectivity...")
test_urls = [
'https://www.google.com',
'https://huggingface.co'
]
if self.mode == "cloud":
test_urls.extend([
'https://cloud.google.com',
'https://api.us-south.discovery.watson.cloud.ibm.com'
])
failed_connections = []
for url in test_urls:
try:
result = subprocess.run(
['curl', '-s', '--connect-timeout', '5', url],
capture_output=True,
timeout=10
)
if result.returncode != 0:
failed_connections.append(url)
except (subprocess.TimeoutExpired, FileNotFoundError):
failed_connections.append(url)
if failed_connections:
self.warnings.append(f"Network connectivity issues: {', '.join(failed_connections)}")
logger.info("Network connectivity check completed")
return True
def validate_all(self) -> bool:
"""Run all validation checks."""
logger.info(f"Starting environment validation for {self.mode} mode...")
checks = [
self.check_python_version,
self.check_system_requirements,
self.check_file_structure,
self.check_python_packages,
self.check_external_tools,
self.check_environment_variables,
self.check_network_connectivity
]
success = True
for check in checks:
try:
if not check():
success = False
except Exception as e:
self.errors.append(f"Validation check failed: {e}")
success = False
return success
def print_results(self) -> None:
"""Print validation results."""
print("\n" + "="*60)
print("ENVIRONMENT VALIDATION RESULTS")
print("="*60)
if not self.errors and not self.warnings:
print("✅ All checks passed! Environment is ready.")
else:
if self.errors:
print(f"\n❌ ERRORS ({len(self.errors)}):")
for i, error in enumerate(self.errors, 1):
print(f" {i}. {error}")
if self.warnings:
print(f"\n⚠️ WARNINGS ({len(self.warnings)}):")
for i, warning in enumerate(self.warnings, 1):
print(f" {i}. {warning}")
print("\n" + "="*60)
def main():
"""Main function."""
parser = argparse.ArgumentParser(
description="Validate environment for misinformation heatmap application"
)
parser.add_argument(
"--mode",
choices=["local", "cloud"],
default="local",
help="Deployment mode to validate"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable verbose output"
)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
validator = EnvironmentValidator(mode=args.mode)
success = validator.validate_all()
validator.print_results()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main() |