-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup.py
More file actions
139 lines (107 loc) Β· 4.23 KB
/
Copy pathsetup.py
File metadata and controls
139 lines (107 loc) Β· 4.23 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
#!/usr/bin/env python3
"""
Setup script for PostgreSQL MCP Server
This script helps set up the development environment and dependencies.
"""
import os
import sys
import subprocess
import platform
def run_command(command: str, description: str) -> bool:
"""Run a shell command and report whether it succeeded.
Args:
command: Command to execute.
description: Human-readable description used for console output.
Returns:
True if the command succeeded.
Raises:
OSError: If the command cannot be executed.
"""
print(f"π {description}...")
try:
subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"β
{description} completed successfully")
return True
except subprocess.CalledProcessError as called_process_error:
print(f"β {description} failed: {called_process_error.stderr}")
return False
def check_python_version() -> bool:
"""Validate that the running Python version meets the project requirement.
Returns:
True if the current interpreter is Python 3.10 or newer.
"""
version = sys.version_info
if version.major == 3 and version.minor >= 10:
print(f"β
Python {version.major}.{version.minor}.{version.micro} is compatible")
return True
else:
print(f"β Python {version.major}.{version.minor}.{version.micro} is not compatible. Requires Python 3.10+")
return False
def setup_virtual_environment() -> bool:
"""Create a local virtual environment if it does not already exist.
Returns:
True if the virtual environment exists or was created successfully.
"""
if os.path.exists('venv'):
print("β
Virtual environment already exists")
return True
return run_command("python -m venv venv", "Creating virtual environment")
def install_dependencies() -> bool:
"""Install dependencies from requirements.txt into the virtual environment.
Returns:
True if dependency installation completed successfully.
"""
pip_command = "venv\\Scripts\\pip" if platform.system() == "Windows" else "venv/bin/pip"
return run_command(f"{pip_command} install -r requirements.txt", "Installing dependencies")
def setup_env_file() -> bool:
"""Create a .env file from .env.example if needed.
Returns:
True if the .env file exists or was created successfully.
"""
if os.path.exists('.env'):
print("β
.env file already exists")
return True
if os.path.exists('.env.example'):
try:
with open('.env.example', 'r') as src, open('.env', 'w') as dst:
dst.write(src.read())
print("β
Created .env file from .env.example")
print("β οΈ Please edit .env file with your actual database credentials")
return True
except OSError as operating_system_error:
print(f"β Failed to create .env file: {operating_system_error}")
return False
else:
print("β .env.example file not found")
return False
def main() -> None:
"""Set up the local development environment for this repository."""
print("π Setting up PostgreSQL MCP Server Development Environment")
print("=" * 60)
# Check Python version
if not check_python_version():
sys.exit(1)
# Setup virtual environment
if not setup_virtual_environment():
print("β Failed to set up virtual environment")
sys.exit(1)
# Install dependencies
if not install_dependencies():
print("β Failed to install dependencies")
sys.exit(1)
# Setup .env file
setup_env_file()
print("\nπ Setup completed successfully!")
print("\nNext steps:")
print("1. Edit .env file with your PostgreSQL credentials")
print("2. Ensure PostgreSQL is running on your system")
print("3. Test the server: python test_server.py")
print("4. Run the server: python postgres_server.py")
if platform.system() == "Windows":
print("\nTo activate the virtual environment:")
print(" venv\\Scripts\\activate")
else:
print("\nTo activate the virtual environment:")
print(" source venv/bin/activate")
if __name__ == "__main__":
main()