-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_pythonanywhere.py
More file actions
313 lines (254 loc) · 8.67 KB
/
setup_pythonanywhere.py
File metadata and controls
313 lines (254 loc) · 8.67 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
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
#!/usr/bin/env python3
"""
PythonAnywhere Otomatik Kurulum Scripti
Bu script PythonAnywhere'de uygulamayı otomatik olarak kurar ve yapılandırır
"""
import os
import sys
import subprocess
import shutil
from pathlib import Path
def run_command(command, description=""):
"""Komut çalıştır ve sonucu göster"""
print(f"🔄 {description}")
print(f" Komut: {command}")
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print(f"✅ Başarılı: {description}")
if result.stdout:
print(f" Çıktı: {result.stdout.strip()}")
else:
print(f"❌ Hata: {description}")
print(f" Hata: {result.stderr.strip()}")
return False
except Exception as e:
print(f"❌ İstisna: {description}")
print(f" Hata: {e}")
return False
return True
def check_python_version():
"""Python versiyonunu kontrol et"""
print("🐍 Python versiyonu kontrol ediliyor...")
version = sys.version_info
print(f" Python {version.major}.{version.minor}.{version.micro}")
if version.major < 3 or (version.major == 3 and version.minor < 8):
print("❌ Python 3.8 veya üstü gerekli!")
return False
print("✅ Python versiyonu uygun")
return True
def install_requirements():
"""Gerekli paketleri yükle"""
print("📦 Gerekli paketler yükleniyor...")
# Önce temel paketleri yükle
basic_packages = [
"flask",
"requests",
"python-dotenv",
"tweepy",
"beautifulsoup4"
]
for package in basic_packages:
if not run_command(f"pip install --user {package}", f"{package} yükleniyor"):
return False
# Sonra requirements dosyasını yükle
if os.path.exists("requirements_pythonanywhere.txt"):
if not run_command("pip install --user -r requirements_pythonanywhere.txt", "Requirements dosyası yükleniyor"):
print("⚠️ Requirements dosyası yüklenemedi, temel paketlerle devam ediliyor")
return True
def download_static_files():
"""Static dosyaları indir"""
print("📁 Static dosyalar indiriliyor...")
if os.path.exists("download_static_files.py"):
if not run_command("python download_static_files.py", "Static dosyalar indiriliyor"):
print("⚠️ Static dosyalar indirilemedi")
return False
else:
print("⚠️ download_static_files.py bulunamadı")
return False
return True
def create_env_file():
"""Örnek .env dosyası oluştur"""
print("🔧 .env dosyası oluşturuluyor...")
env_content = """# PythonAnywhere Environment Variables
FLASK_ENV=production
SECRET_KEY=your-secret-key-here-change-this
DEBUG=False
USE_LOCAL_ASSETS=True
PYTHONANYWHERE_MODE=True
# API Keys (Bu değerleri kendi API anahtarlarınızla değiştirin)
OPENROUTER_API_KEY=your-openrouter-api-key
TWITTER_API_KEY=your-twitter-api-key
TWITTER_API_SECRET=your-twitter-api-secret
GOOGLE_API_KEY=your-google-api-key
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
# Email Settings
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
EMAIL_USERNAME=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
"""
with open(".env", "w", encoding="utf-8") as f:
f.write(env_content)
print("✅ .env dosyası oluşturuldu")
print("⚠️ Lütfen .env dosyasındaki API anahtarlarını kendi değerlerinizle değiştirin!")
return True
def setup_directories():
"""Gerekli dizinleri oluştur"""
print("📁 Dizinler oluşturuluyor...")
directories = [
"static/css",
"static/js",
"static/webfonts",
"static/images",
"logs",
"backups",
"data"
]
for directory in directories:
Path(directory).mkdir(parents=True, exist_ok=True)
print(f" ✅ {directory}")
return True
def test_imports():
"""Gerekli modüllerin import edilebilirliğini test et"""
print("🧪 Modül import testleri...")
modules = [
"flask",
"requests",
"dotenv",
"tweepy",
"bs4"
]
for module in modules:
try:
__import__(module)
print(f" ✅ {module}")
except ImportError as e:
print(f" ❌ {module}: {e}")
return False
return True
def create_test_script():
"""Test scripti oluştur"""
print("🧪 Test scripti oluşturuluyor...")
test_content = """#!/usr/bin/env python3
\"\"\"
PythonAnywhere Test Scripti
Bu script uygulamanın doğru çalışıp çalışmadığını test eder
\"\"\"
import sys
import os
def test_basic_imports():
\"\"\"Temel import'ları test et\"\"\"
print("🧪 Temel import testleri...")
try:
import flask
print("✅ Flask import edildi")
except ImportError as e:
print(f"❌ Flask import hatası: {e}")
return False
try:
import requests
print("✅ Requests import edildi")
except ImportError as e:
print(f"❌ Requests import hatası: {e}")
return False
try:
from dotenv import load_dotenv
print("✅ Python-dotenv import edildi")
except ImportError as e:
print(f"❌ Python-dotenv import hatası: {e}")
return False
return True
def test_app_import():
\"\"\"Ana uygulamayı import etmeyi test et\"\"\"
print("🧪 Uygulama import testi...")
try:
from app import app
print("✅ Ana uygulama import edildi")
return True
except ImportError as e:
print(f"❌ Ana uygulama import hatası: {e}")
return False
def test_static_files():
\"\"\"Static dosyaların varlığını test et\"\"\"
print("🧪 Static dosya testi...")
required_files = [
"static/css/bootstrap.min.css",
"static/css/all.min.css",
"static/js/bootstrap.bundle.min.js"
]
for file_path in required_files:
if os.path.exists(file_path):
print(f"✅ {file_path}")
else:
print(f"❌ {file_path} bulunamadı")
return False
return True
def main():
\"\"\"Ana test fonksiyonu\"\"\"
print("🚀 PythonAnywhere Test Scripti Başlatılıyor...")
tests = [
test_basic_imports,
test_app_import,
test_static_files
]
passed = 0
total = len(tests)
for test in tests:
if test():
passed += 1
print()
print(f"📊 Test Sonuçları: {passed}/{total} başarılı")
if passed == total:
print("🎉 Tüm testler başarılı! Uygulama hazır.")
return True
else:
print("⚠️ Bazı testler başarısız. Lütfen hataları kontrol edin.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)
"""
with open("test_pythonanywhere.py", "w", encoding="utf-8") as f:
f.write(test_content)
print("✅ Test scripti oluşturuldu")
return True
def main():
"""Ana kurulum fonksiyonu"""
print("🚀 PythonAnywhere Otomatik Kurulum Başlatılıyor...")
print("=" * 50)
# Kurulum adımları
steps = [
("Python versiyonu kontrol ediliyor", check_python_version),
("Dizinler oluşturuluyor", setup_directories),
("Gerekli paketler yükleniyor", install_requirements),
("Static dosyalar indiriliyor", download_static_files),
("Environment dosyası oluşturuluyor", create_env_file),
("Test scripti oluşturuluyor", create_test_script),
("Modül import testleri", test_imports)
]
successful_steps = 0
total_steps = len(steps)
for step_name, step_func in steps:
print(f"\n📋 {step_name}...")
if step_func():
successful_steps += 1
else:
print(f"❌ {step_name} başarısız!")
break
print("\n" + "=" * 50)
print(f"📊 Kurulum Sonucu: {successful_steps}/{total_steps} adım başarılı")
if successful_steps == total_steps:
print("🎉 Kurulum tamamlandı!")
print("\n📋 Sonraki Adımlar:")
print("1. .env dosyasındaki API anahtarlarını güncelleyin")
print("2. python test_pythonanywhere.py komutunu çalıştırın")
print("3. PythonAnywhere Web sekmesinde Reload butonuna tıklayın")
print("4. Uygulamanızı test edin")
return True
else:
print("⚠️ Kurulum tamamlanamadı. Lütfen hataları kontrol edin.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)