-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_pythonanywhere.py
More file actions
139 lines (114 loc) · 4.19 KB
/
Copy pathsetup_pythonanywhere.py
File metadata and controls
139 lines (114 loc) · 4.19 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 python
"""
Setup script for PythonAnywhere environment
This script will:
1. Create necessary directories
2. Initialize the database
3. Verify configuration
"""
import os
import sys
from pathlib import Path
def setup_directories():
"""Create necessary directories for the application"""
print("Setting up directories...")
# List of directories to create
directories = [
"instance",
"logs",
"app/static/uploads/avatars",
"app/static/uploads/covers"
]
# Create each directory
for directory in directories:
path = Path(directory)
if not path.exists():
try:
path.mkdir(parents=True, exist_ok=True)
print(f"✓ Created directory: {directory}")
except Exception as e:
print(f"✗ Failed to create directory {directory}: {str(e)}")
return False
return True
def initialize_database():
"""Initialize the database using Flask-Migrate or SQLAlchemy"""
print("\nInitializing database...")
try:
# First try using Flask-Migrate (preferred method)
from flask.cli import ScriptInfo
from flask_migrate import upgrade as migrate_upgrade
from app import create_app
app = create_app()
# Set up Flask context
with app.app_context():
migrate_upgrade()
print("✓ Database initialized using Flask-Migrate")
except Exception as e:
print(f"! Flask-Migrate failed: {str(e)}")
print("Trying direct SQLAlchemy initialization...")
try:
# Fallback to direct SQLAlchemy initialization
from app import db, create_app
app = create_app()
with app.app_context():
db.create_all()
print("✓ Database initialized using SQLAlchemy")
except Exception as e:
print(f"✗ Failed to initialize database: {str(e)}")
return False
return True
def verify_config():
"""Verify application configuration"""
print("\nVerifying configuration...")
try:
from app import create_app
app = create_app()
with app.app_context():
# Check database URL
db_url = app.config.get('SQLALCHEMY_DATABASE_URI', 'Not configured')
print(f"Database URL: {db_url}")
# Check secret key (without exposing it)
if app.config.get('SECRET_KEY'):
print("✓ Secret key is configured")
else:
print("✗ Secret key is not configured")
# Check if database file exists for SQLite
if db_url.startswith('sqlite:///'):
db_path = db_url.replace('sqlite:///', '')
if os.path.isfile(db_path):
print(f"✓ Database file exists at {db_path}")
else:
print(f"✗ Database file does not exist at {db_path}")
print("Configuration verification complete")
except Exception as e:
print(f"✗ Configuration verification failed: {str(e)}")
return False
return True
def main():
"""Main script execution"""
print("PythonAnywhere Setup Script")
print("==========================")
# Change to the application root directory
try:
# Get the directory of this script
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
print(f"Working directory: {os.getcwd()}")
except Exception as e:
print(f"Failed to set working directory: {str(e)}")
return 1
# Run setup steps
if not setup_directories():
print("Directory setup failed")
return 1
if not initialize_database():
print("Database initialization failed")
return 1
if not verify_config():
print("Configuration verification failed")
return 1
print("\n✓ Setup completed successfully!")
print("You can now reload your web application in the PythonAnywhere dashboard.")
return 0
if __name__ == "__main__":
sys.exit(main())