Diff of /environment_setup.py [000000] .. [46c9de]

Switch to unified view

a b/environment_setup.py
1
import subprocess
2
import sys
3
import logging
4
from typing import List, Tuple
5
import importlib.metadata
6
7
8
def setup_logging():
9
    """Configure logging for the installation process"""
10
    logging.basicConfig(
11
        level=logging.INFO,
12
        format='%(asctime)s - %(levelname)s - %(message)s',
13
        handlers=[
14
            logging.StreamHandler(sys.stdout),
15
            logging.FileHandler('logs/installation.log')
16
        ]
17
    )
18
    return logging.getLogger(__name__)
19
20
21
def parse_requirements(filename: str) -> List[Tuple[str, str]]:
22
    """
23
    Parse requirements.txt and return list of (package_name, version) tuples
24
    Ignores comments and empty lines
25
    """
26
    requirements = []
27
    with open(filename, 'r') as file:
28
        for line in file:
29
            # Remove comments if they exist on the same line as a package
30
            if '#' in line:
31
                line = line[:line.index('#')]
32
33
            line = line.strip()
34
            if line and not line.startswith('#'):
35
                # Handle version specifiers
36
                if '==' in line:
37
                    package, version = line.split('==')
38
                    requirements.append((package.strip(), version.strip()))
39
                else:
40
                    requirements.append((line, ''))
41
    return requirements
42
43
44
def is_package_installed(package_name: str, required_version: str = '') -> bool:
45
    """Check if a package is installed with the required version"""
46
    try:
47
        installed_version = importlib.metadata.version(package_name)
48
        if required_version:
49
            return installed_version == required_version
50
        return True
51
    except importlib.metadata.PackageNotFoundError:
52
        return False
53
54
55
def install_package(package_name: str, version: str, logger) -> bool:
56
    """Install a package using pip"""
57
    try:
58
        package_spec = f"{package_name}=={version}" if version else package_name
59
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_spec])
60
        return True
61
    except subprocess.CalledProcessError as e:
62
        logger.error(f"Failed to install {package_name}: {str(e)}")
63
        return False
64
65
66
def setup_nltk(logger):
67
    """Download required NLTK data"""
68
    try:
69
        import nltk
70
        nltk_packages = ['punkt', 'stopwords', 'wordnet']
71
        for package in nltk_packages:
72
            logger.info(f"Downloading NLTK package: {package}")
73
            nltk.download(package, quiet=True)
74
        return True
75
    except Exception as e:
76
        logger.error(f"Failed to download NLTK packages: {str(e)}")
77
        return False
78
79
80
def setup_spacy(logger):
81
    """Download SpaCy English language model"""
82
    try:
83
        subprocess.check_call([sys.executable, '-m', 'spacy', 'download', 'en_core_web_sm'])
84
        return True
85
    except subprocess.CalledProcessError as e:
86
        logger.error(f"Failed to download SpaCy model: {str(e)}")
87
        return False
88
89
90
def check_and_install_dependencies():
91
    """Main function to check and install all dependencies"""
92
    logger = setup_logging()
93
    logger.info("Starting dependency check and installation...")
94
95
    # Parse requirements.txt
96
    try:
97
        requirements = parse_requirements('requirements.txt')
98
    except FileNotFoundError:
99
        logger.error("requirements.txt not found!")
100
        return False
101
102
    # Check and install each package
103
    all_successful = True
104
    for package_name, version in requirements:
105
        if not is_package_installed(package_name, version):
106
            logger.info(f"Installing {package_name} {'version ' + version if version else ''}")
107
            if not install_package(package_name, version, logger):
108
                all_successful = False
109
        else:
110
            logger.info(f"{package_name} {'version ' + version if version else ''} is already installed")
111
112
    # Setup NLTK
113
    if all_successful:
114
        logger.info("Setting up NLTK...")
115
        if not setup_nltk(logger):
116
            all_successful = False
117
118
    # Setup SpaCy
119
    if all_successful:
120
        logger.info("Setting up SpaCy...")
121
        if not setup_spacy(logger):
122
            all_successful = False
123
124
    if all_successful:
125
        logger.info("All dependencies installed successfully!")
126
    else:
127
        logger.error("Some installations failed. Check the logs for details.")
128
129
    return all_successful
130
131
132
if __name__ == "__main__":
133
    success = check_and_install_dependencies()
134
    sys.exit(0 if success else 1)