-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
560 lines (490 loc) · 20.8 KB
/
main.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import json
import os
import uvicorn
from datetime import date, datetime
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI, File, Request, Query, HTTPException, Depends
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse
from fastapi.templating import Jinja2Templates
from starlette.middleware.sessions import SessionMiddleware
from fastapi.staticfiles import StaticFiles
from sqlalchemy import create_engine, Column, Integer, String, Date, desc, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
# Load environment variables from .env file
load_dotenv()
# Database setup
DATABASE_URL = os.environ.get('DATABASE_URL')
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is not set")
if DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
# Add error handling for database connection
try:
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
except Exception as e:
raise RuntimeError(f"Failed to connect to database: {str(e)}")
class Download(Base):
__tablename__ = "downloads"
id = Column(Integer, primary_key=True) # 4 bytes
package = Column(String) # variable
date = Column(Date) # 4 bytes
count = Column(Integer) # 4 bytes
platform = Column(String) # variable
# Create tables
Base.metadata.create_all(bind=engine)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
app = FastAPI()
# Add SessionMiddleware with a secret key
app.add_middleware(
SessionMiddleware,
secret_key="your-secret-key-here" # Replace with a secure secret key
)
# Mount static files directory
app.mount("/css", StaticFiles(directory="templates/css"), name="css")
app.mount("/images", StaticFiles(directory="templates/images"), name="images")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def get_index(request: Request, db: Session = Depends(get_db)):
try:
packages = {}
r_packages_dir = Path("r-packages")
# Get latest version for each package from r-packages/src/contrib
src_contrib = r_packages_dir / "src" / "contrib"
if src_contrib.exists():
for tar_gz in src_contrib.glob("*.tar.gz"):
package_name = tar_gz.name.split("_")[0]
version = tar_gz.name.split("_")[1].replace(".tar.gz", "")
if package_name not in packages:
packages[package_name] = {
"package": package_name,
"version": version,
"platforms": {
"source": {
"status": "SUCCESS", # If file exists, it's a success
"build_time": datetime.fromtimestamp(tar_gz.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S")
}
}
}
# Check Windows binaries for all R versions
for r_version in ["4.2", "4.3", "4.4"]:
win_dir = r_packages_dir / "bin" / "windows" / "contrib" / r_version
if win_dir.exists():
for zip_file in win_dir.glob("*.zip"):
package_name = zip_file.name.split("_")[0]
if package_name in packages:
packages[package_name]["platforms"]["windows"] = {
"status": "SUCCESS",
"build_time": datetime.fromtimestamp(zip_file.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
"r_version": r_version
}
# Check macOS binaries for all R versions
for r_version in ["4.2", "4.3", "4.4"]:
mac_dir = r_packages_dir / "bin" / "macosx" / "contrib" / r_version
if mac_dir.exists():
for tgz_file in mac_dir.glob("*.tgz"):
package_name = tgz_file.name.split("_")[0]
if package_name in packages:
packages[package_name]["platforms"]["macos"] = {
"status": "SUCCESS",
"build_time": datetime.fromtimestamp(tgz_file.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
"r_version": r_version
}
# Read build status files to get list of all packages that should exist
for json_file in r_packages_dir.glob("build_status_*.json"):
with open(json_file) as f:
build_info = json.load(f)
platform = build_info.get("platform", "unknown")
for pkg_name, pkg_info in build_info.get("packages", {}).items():
if pkg_name not in packages:
# Package was in build status but no file exists
if pkg_name not in packages:
packages[pkg_name] = {
"package": pkg_name,
"version": "",
"platforms": {}
}
packages[pkg_name]["platforms"][platform] = {
"status": "FAILED", # No file exists, so it failed
"build_time": pkg_info.get("build_time", ""),
"error_message": "No package file found"
}
return templates.TemplateResponse(
"index.html",
{
"request": request,
"year": datetime.now().year,
"packages": packages,
"downloads": db.query(Download).order_by(desc(Download.date)).all()
}
)
except Exception as e:
print(f"Error in get_index: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/download/{package}")
async def download_package(
package: str,
version: str = Query(..., description="R package version"),
platform: str = Query(default="source", description="Platform (windows/macos/source)"),
r_version: str = Query(default="4.3", description="R version (e.g. 4.3)"),
db: Session = Depends(get_db)
):
try:
today = date.today()
# Log the incoming request
print(f"Download request: package={package}, version={version}, platform={platform}")
# Get or create download record
download = db.query(Download).filter(
Download.package == package,
Download.date == today,
Download.platform == platform
).first()
if not download:
download = Download(
package=package,
date=today,
count=1,
platform=platform
)
db.add(download)
else:
# Use SQL update for atomic increment
db.query(Download).filter(
Download.id == download.id
).update(
{"count": Download.count + 1},
synchronize_session=False
)
db.commit()
# Construct package URL based on platform
base_url = "https://r-packages.techtonique.net" # Using the existing domain
if platform == "windows":
package_url = f"{base_url}/bin/windows/contrib/{r_version}/{package}_{version}.zip"
elif platform == "macos":
package_url = f"{base_url}/bin/macosx/contrib/{package}_{version}.tgz"
else: # source
package_url = f"{base_url}/src/contrib/{package}_{version}.tar.gz"
# Log the redirect URL
print(f"Redirecting to: {package_url}")
return RedirectResponse(url=package_url)
except Exception as e:
print(f"Error in download_package: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/download/source/{package}")
async def download_source_package(
package: str,
version: str = Query(..., description="R package version"),
db: Session = Depends(get_db)
):
try:
today = date.today()
file_path = f"r-packages/src/contrib/{package}_{version}.tar.gz"
# Check if file exists
if not os.path.exists(file_path):
raise HTTPException(
status_code=404,
detail=f"Package file not found: {file_path}"
)
# Record the download
download = db.query(Download).filter(
Download.package == package,
Download.date == today,
Download.platform == "source"
).first()
if not download:
download = Download(
package=package,
date=today,
count=1,
platform="source"
)
db.add(download)
else:
db.query(Download).filter(
Download.id == download.id
).update(
{"count": Download.count + 1},
synchronize_session=False
)
db.commit()
return FileResponse(
path=file_path,
filename=os.path.basename(file_path),
media_type="application/octet-stream"
)
except Exception as e:
print(f"Error in download_source_package: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/stats/{date}/{package}")
async def get_stats(date: str, package: str, db: Session = Depends(get_db)):
try:
downloads = db.query(Download).filter(
Download.package == package,
Download.date == date
).all()
if not downloads:
return {"package": package, "date": date, "total_count": 0, "by_platform": {}}
total_count = sum(d.count for d in downloads)
by_platform = {
d.platform: {
"count": d.count,
"date": d.date.isoformat()
} for d in downloads
}
return {
"package": package,
"date": date,
"total_count": total_count,
"by_platform": by_platform
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/stats/today")
async def get_today_stats(db: Session = Depends(get_db)):
try:
today = date.today()
downloads = db.query(Download).filter(Download.date == today).all()
return [
{
"package": d.package,
"date": d.date.isoformat(),
"count": d.count,
"platform": d.platform
}
for d in downloads
]
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/src/contrib/PACKAGES")
@app.get("/src/contrib/PACKAGES.gz")
@app.get("/src/contrib/PACKAGES.rds")
@app.get("/bin/windows/contrib/{r_version}/PACKAGES")
@app.get("/bin/windows/contrib/{r_version}/PACKAGES.gz")
@app.get("/bin/windows/contrib/{r_version}/PACKAGES.rds")
@app.get("/bin/macosx/contrib/{r_version}/PACKAGES")
@app.get("/bin/macosx/contrib/{r_version}/PACKAGES.gz")
@app.get("/bin/macosx/contrib/{r_version}/PACKAGES.rds")
async def serve_packages_file(request: Request, r_version: str = None):
try:
# Remove the leading slash and add r-packages prefix
url_path = request.url.path.lstrip('/')
file_path = f"r-packages/{url_path}"
# Debug logging
print(f"Attempting to serve PACKAGES file from: {file_path}")
# Check if file exists
if not os.path.exists(file_path):
raise HTTPException(
status_code=404,
detail=f"PACKAGES file not found at: {file_path}"
)
# Determine media type
if file_path.endswith('.gz'):
media_type = 'application/gzip'
elif file_path.endswith('.rds'):
media_type = 'application/octet-stream'
else:
media_type = 'text/plain'
return FileResponse(
file_path,
media_type=media_type
)
except Exception as e:
print(f"Error serving PACKAGES file: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/src/contrib/{file_name}")
@app.get("/bin/windows/contrib/{r_version}/{file_name}")
@app.get("/bin/macosx/contrib/{r_version}/{file_name}")
async def serve_package(
request: Request,
file_name: str,
r_version: str = None,
db: Session = Depends(get_db)
):
try:
# Skip if requesting PACKAGES files
if file_name in ["PACKAGES", "PACKAGES.gz", "PACKAGES.rds"]:
raise HTTPException(status_code=404, detail="Use PACKAGES endpoint")
# Remove the leading slash and add r-packages prefix
url_path = request.url.path.lstrip('/')
file_path = f"r-packages/{url_path}"
# Parse package name and version from file_name
parts = file_name.rsplit("_", 1)
if len(parts) != 2:
raise HTTPException(status_code=400, detail="Invalid file name format")
package = parts[0]
version = parts[1].split(".")[0] # Get version before file extension
# Determine platform from path
if "windows" in str(request.url):
platform = "windows"
elif "macosx" in str(request.url):
platform = "macos"
else:
platform = "source"
# Check if file exists
if not os.path.exists(file_path):
raise HTTPException(
status_code=404,
detail=f"Package file not found: {file_path}"
)
# Record the download
today = date.today()
download = db.query(Download).filter(
Download.package == package,
Download.date == today,
Download.platform == platform
).first()
if not download:
download = Download(
package=package,
date=today,
count=1,
platform=platform
)
db.add(download)
else:
db.query(Download).filter(
Download.id == download.id
).update(
{"count": Download.count + 1},
synchronize_session=False
)
db.commit()
# Log the download
print(f"Package download: {package} {version} for {platform}")
return FileResponse(
file_path,
filename=file_name,
media_type="application/octet-stream"
)
except Exception as e:
print(f"Error serving package: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/downloads", response_class=HTMLResponse)
async def get_downloads(request: Request, db: Session = Depends(get_db)):
try:
# Use SQL to aggregate downloads by month and package
monthly_downloads = db.execute(
text("""
SELECT
package,
DATE_TRUNC('month', date) as month,
platform,
SUM(count) as total_count
FROM downloads
GROUP BY package, DATE_TRUNC('month', date), platform
ORDER BY DATE_TRUNC('month', date) DESC, package, platform
""")
).fetchall()
# Organize the data by month and package
downloads_by_month = {}
for row in monthly_downloads:
month_str = row.month.strftime("%Y-%m")
if month_str not in downloads_by_month:
downloads_by_month[month_str] = {}
if row.package not in downloads_by_month[month_str]:
downloads_by_month[month_str][row.package] = {
'total': 0,
'platforms': {}
}
downloads_by_month[month_str][row.package]['platforms'][row.platform] = row.total_count
downloads_by_month[month_str][row.package]['total'] += row.total_count
return templates.TemplateResponse(
"downloads.html",
{
"request": request,
"downloads_by_month": downloads_by_month,
"year": datetime.now().year
}
)
except Exception as e:
print(f"Error in get_downloads: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/packages", response_class=HTMLResponse)
async def get_packages(request: Request, db: Session = Depends(get_db)):
try:
packages = {}
r_packages_dir = Path("r-packages")
# Get latest version for each package from r-packages/src/contrib
src_contrib = r_packages_dir / "src" / "contrib"
if src_contrib.exists():
for tar_gz in src_contrib.glob("*.tar.gz"):
package_name = tar_gz.name.split("_")[0]
version = tar_gz.name.split("_")[1].replace(".tar.gz", "")
if package_name not in packages:
packages[package_name] = {
"package": package_name,
"version": version,
"platforms": {
"source": {
"status": "SUCCESS", # If file exists, it's a success
"build_time": datetime.fromtimestamp(tar_gz.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S")
}
}
}
# Check Windows binaries for all R versions
for r_version in ["4.2", "4.3", "4.4"]:
win_dir = r_packages_dir / "bin" / "windows" / "contrib" / r_version
if win_dir.exists():
for zip_file in win_dir.glob("*.zip"):
package_name = zip_file.name.split("_")[0]
if package_name in packages:
packages[package_name]["platforms"]["windows"] = {
"status": "SUCCESS",
"build_time": datetime.fromtimestamp(zip_file.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
"r_version": r_version
}
# Check macOS binaries for all R versions
for r_version in ["4.2", "4.3", "4.4"]:
mac_dir = r_packages_dir / "bin" / "macosx" / "contrib" / r_version
if mac_dir.exists():
for tgz_file in mac_dir.glob("*.tgz"):
package_name = tgz_file.name.split("_")[0]
if package_name in packages:
packages[package_name]["platforms"]["macos"] = {
"status": "SUCCESS",
"build_time": datetime.fromtimestamp(tgz_file.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
"r_version": r_version
}
# Read build status files to get list of all packages that should exist
for json_file in r_packages_dir.glob("build_status_*.json"):
with open(json_file) as f:
build_info = json.load(f)
platform = build_info.get("platform", "unknown")
for pkg_name, pkg_info in build_info.get("packages", {}).items():
if pkg_name not in packages:
# Package was in build status but no file exists
if pkg_name not in packages:
packages[pkg_name] = {
"package": pkg_name,
"version": "",
"platforms": {}
}
packages[pkg_name]["platforms"][platform] = {
"status": "FAILED", # No file exists, so it failed
"build_time": pkg_info.get("build_time", ""),
"error_message": "No package file found"
}
return templates.TemplateResponse(
"packages.html",
{
"request": request,
"year": datetime.now().year,
"packages": packages
}
)
except Exception as e:
print(f"Error in get_packages: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
host = os.environ.get("HOST", "0.0.0.0")
uvicorn.run("main:app", host=host, port=port, log_level="info")