-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_venv.py
More file actions
205 lines (172 loc) · 7.61 KB
/
setup_venv.py
File metadata and controls
205 lines (172 loc) · 7.61 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
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
#!/usr/bin/env python3
"""
Setup script for creating a clean virtual environment.
This ensures all users can set up the project correctly.
"""
import subprocess
import sys
import os
from pathlib import Path
def main():
"""Create clean virtual environment and install dependencies."""
# Fix Windows Unicode output
import io
if sys.platform == "win32":
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
print("=" * 70)
print("Predictive Maintenance MCP - Clean Environment Setup")
print("=" * 70)
print()
# Get project root
project_root = Path(__file__).parent
venv_path = project_root / ".venv"
# Check Python version
print("1. Checking Python version...")
py_version = sys.version_info
print(f" ✓ Python {py_version.major}.{py_version.minor}.{py_version.micro}")
if py_version < (3, 11):
print(f" ✗ ERROR: Python 3.11+ required (you have {py_version.major}.{py_version.minor})")
sys.exit(1)
# Remove old venv if exists
if venv_path.exists():
print("\n2. Checking for existing virtual environment...")
print(f" ⚠️ Virtual environment already exists at: {venv_path}")
response = input(" Remove and recreate? [y/N]: ").strip().lower()
if response in ('y', 'yes'):
print(" Removing old virtual environment...")
try:
import shutil
shutil.rmtree(venv_path)
print(" ✓ Old .venv removed")
except PermissionError:
print(" ✗ ERROR: Cannot remove .venv (may be active in another process)")
print(" Please deactivate any active virtual environments and try again.")
print("\n To deactivate:")
if sys.platform == "win32":
print(" - Close terminals with activated .venv")
print(" - Or run: deactivate")
else:
print(" - Run: deactivate")
sys.exit(1)
else:
print(" ✓ Using existing virtual environment")
print(" (skipping venv creation)")
# Skip to pip upgrade
if sys.platform == "win32":
venv_python = venv_path / "Scripts" / "python.exe"
venv_pip = venv_path / "Scripts" / "pip.exe"
else:
venv_python = venv_path / "bin" / "python"
venv_pip = venv_path / "bin" / "pip"
# Upgrade pip
print("\n3. Upgrading pip...")
subprocess.run([str(venv_python), "-m", "pip", "install", "--upgrade", "pip"], check=True)
print(" ✓ pip upgraded")
# Install package
print("\n4. Installing predictive-maintenance-mcp...")
subprocess.run([str(venv_pip), "install", "-e", "."], cwd=project_root, check=True)
print(" ✓ Package installed")
# Ask about dev dependencies
print("\n5. Development dependencies:")
response = input(" Install dev dependencies (pytest, black, flake8, mypy)? [y/N]: ").strip().lower()
if response in ('y', 'yes'):
print(" Installing dev dependencies...")
subprocess.run([str(venv_pip), "install", "-e", ".[dev]"], cwd=project_root, check=True)
print(" ✓ Dev dependencies installed")
else:
print(" ✓ Skipped dev dependencies")
# Verify installation
print("\n6. Verifying installation...")
result = subprocess.run(
[str(venv_python), "-c", "import mcp; import numpy; import pandas; import scipy; import sklearn; import plotly; print('All core packages imported successfully')"],
capture_output=True,
text=True
)
if result.returncode == 0:
print(" ✓ All core packages working")
else:
print(" ✗ Import error:")
print(result.stderr)
sys.exit(1)
# Final instructions
print("\n" + "=" * 70)
print("✅ SETUP COMPLETE!")
print("=" * 70)
print("\nExisting virtual environment updated successfully.")
print("\nNext steps:")
print("\n1. Virtual environment already active in some terminals")
print(" No action needed if already activated")
print("\n2. Validate server:")
print(" python validate_server.py")
print("\n3. Configure Claude Desktop:")
if sys.platform == "win32":
print(" .\\setup_claude.ps1")
else:
print(" See INSTALL.md for manual setup")
print()
return
# Create new venv
print("\n3. Creating new virtual environment...")
subprocess.run([sys.executable, "-m", "venv", str(venv_path)], check=True)
print(" ✓ Virtual environment created")
# Determine venv python path
if sys.platform == "win32":
venv_python = venv_path / "Scripts" / "python.exe"
venv_pip = venv_path / "Scripts" / "pip.exe"
else:
venv_python = venv_path / "bin" / "python"
venv_pip = venv_path / "bin" / "pip"
# Upgrade pip
print("\n4. Upgrading pip...")
subprocess.run([str(venv_python), "-m", "pip", "install", "--upgrade", "pip"], check=True)
print(" ✓ pip upgraded")
# Install package
print("\n5. Installing predictive-maintenance-mcp...")
subprocess.run([str(venv_pip), "install", "-e", "."], cwd=project_root, check=True)
print(" ✓ Package installed")
# Ask about dev dependencies
print("\n6. Development dependencies:")
response = input(" Install dev dependencies (pytest, black, flake8, mypy)? [y/N]: ").strip().lower()
if response in ('y', 'yes'):
print(" Installing dev dependencies...")
subprocess.run([str(venv_pip), "install", "-e", ".[dev]"], cwd=project_root, check=True)
print(" ✓ Dev dependencies installed")
else:
print(" ✓ Skipped dev dependencies")
# Verify installation
print("\n7. Verifying installation...")
result = subprocess.run(
[str(venv_python), "-c", "import mcp; import numpy; import pandas; import scipy; import sklearn; import plotly; print('All core packages imported successfully')"],
capture_output=True,
text=True
)
if result.returncode == 0:
print(" ✓ All core packages working")
else:
print(" ✗ Import error:")
print(result.stderr)
sys.exit(1)
# Final instructions
print("\n" + "=" * 70)
print("✅ SETUP COMPLETE!")
print("=" * 70)
print("\nNext steps:")
print("\n1. Activate virtual environment:")
if sys.platform == "win32":
print(" .venv\\Scripts\\activate")
else:
print(" source .venv/bin/activate")
print("\n2. Validate server:")
print(" python validate_server.py")
print("\n3. Configure Claude Desktop:")
if sys.platform == "win32":
print(" .\\setup_claude.ps1")
else:
print(" See INSTALL.md for manual setup")
print("\n4. Read documentation:")
print(" - README.md - Project overview and quick examples")
print(" - EXAMPLES.md - Complete usage examples and tutorials")
print(" - INSTALL.md - Detailed installation guide")
print()
if __name__ == "__main__":
main()