-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_project.py
More file actions
97 lines (80 loc) · 2.84 KB
/
Copy pathsetup_project.py
File metadata and controls
97 lines (80 loc) · 2.84 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
#!/usr/bin/env python3
"""
setup_project.py — Run this FIRST to validate your environment
and initialize the PostgreSQL database.
Usage:
python setup_project.py
"""
import os
import sys
def check_env():
print("🔍 Checking environment variables...")
missing = []
for var in ("GITHUB_TOKEN", "OPENAI_API_KEY", "DATABASE_URL"):
if not os.getenv(var):
missing.append(var)
if missing:
print(f"❌ Missing variables in .env: {', '.join(missing)}")
print(" → Copy .env.example to .env and fill in your values.")
sys.exit(1)
print("✅ All required environment variables found.")
def check_db():
print("\n🔍 Testing database connection...")
try:
from sqlalchemy import create_engine, text
engine = create_engine(os.getenv("DATABASE_URL"), pool_pre_ping=True)
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
print("✅ Database connection successful.")
return engine
except Exception as e:
print(f"❌ Cannot connect to PostgreSQL: {e}")
print(" → Make sure PostgreSQL is running and DATABASE_URL is correct.")
sys.exit(1)
def init_tables(engine):
print("\n📦 Creating database tables...")
from src.database.models import Base
Base.metadata.create_all(engine)
print("✅ Tables created (or already exist).")
def check_github_token():
print("\n🔍 Testing GitHub API token...")
import requests
token = os.getenv("GITHUB_TOKEN")
r = requests.get(
"https://api.github.com/rate_limit",
headers={"Authorization": f"token {token}"}
)
if r.status_code == 200:
core = r.json()["resources"]["core"]
print(f"✅ GitHub token valid. Rate limit: {core['remaining']}/{core['limit']}")
else:
print(f"❌ GitHub token invalid (status {r.status_code}). Check your GITHUB_TOKEN.")
sys.exit(1)
def check_openai_key():
print("\n🔍 Testing OpenAI API key...")
try:
from openai import OpenAI
client = OpenAI()
models = client.models.list()
print("✅ OpenAI API key valid.")
except Exception as e:
print(f"❌ OpenAI key error: {e}")
sys.exit(1)
def antigravity_reminder():
print("\n🚀 Easter Egg Reminder:")
print(" Run this in Python and take a screenshot for your submission:")
print(" >>> import antigravity")
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
print("=" * 50)
print(" GitHub Peru Analytics — Project Setup")
print("=" * 50)
check_env()
engine = check_db()
init_tables(engine)
check_github_token()
check_openai_key()
antigravity_reminder()
print("\n🎉 Setup complete! You're ready to start collecting data.")
print(" Next step: python scripts/extract_data.py")