celery_tasks.py
Python script, ASCII text executable
1import common 2import time 3import os 4import config 5import email_send 6from celery import shared_task 7from app import db 8from smtplib import SMTP 9from celery.utils.log import get_task_logger 10 11 12@shared_task(ignore_result=False) 13def send_notification(user_notification_id): 14from models import UserNotification 15user_notification = db.session.get(UserNotification, user_notification_id) 16user = user_notification.user 17notification = user_notification.notification 18 19if user.email: 20with (SMTP(config.MAIL_SERVER) as mail): 21if notification.data.get("type") == "welcome": 22message = email_send.make_multipart_message( 23f"Welcome, {user.username}", 24config.NOTIFICATION_EMAIL, 25user.email, 26"welcome", 27username=user.username) 28 29mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message) 30 31return 0 # notification sent successfully 32 33 34@shared_task(ignore_result=False) 35def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True): 36from models import Repo 37server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 38if not os.path.isdir(server_repo_location): 39raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 40 41if base_route == head_route: 42common.git_command(server_repo_location, b"", "checkout", f"{base_branch}") 43if simulate: 44out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 45return_err=True, return_exit=True) 46 47# Undo the merge. 48common.git_command(server_repo_location, b"", "merge", "--abort") 49else: 50out, err, merge_exit = common.git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 51return_err=True, return_exit=True) 52 53return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 54 55# Otherwise, we need to fetch the head repo. 56remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 57 58out, err = b"", b"" 59part_out, part_err = common.git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 60out += part_out 61err += part_err 62part_out, part_err = common.git_command(server_repo_location, b"", "remote", "update", return_err=True) 63out += part_out 64err += part_err 65part_out, part_err = common.git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 66out += part_out 67err += part_err 68if simulate: 69part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 70"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True, return_exit=True) 71else: 72part_out, part_err, merge_exit = common.git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 73f"NEW/{head_branch}", return_err=True, return_exit=True) 74 75diff, diff_exit = common.git_command(server_repo_location, b"", "diff", "--check", return_exit=True) 76 77out += part_out 78err += part_err 79part_out, part_err = common.git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 80out += part_out 81err += part_err 82if simulate: 83# Undo the merge. 84common.git_command(server_repo_location, b"", "merge", "--abort") 85 86return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit 87 88 89@shared_task(ignore_result=False) 90def copy_site(route): 91from models import Repo 92# Create the site directory if it doesn't exist. 93if not os.path.isdir(os.path.join(config.SITE_PATH, route.lstrip("/"))): 94os.makedirs(os.path.join(config.SITE_PATH, route.lstrip("/"))) 95repo = db.session.get(Repo, route) 96server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/")) 97site_location = os.path.join(config.SITE_PATH, route.lstrip("/")) 98# Get the branch to be used for the site; if it somehow doesn't exist, use the default branch. 99branch = repo.site_branch or repo.default_branch 100# Make a shallow clone of the repo; this prevents getting the full git database when it's not needed. 101common.git_command(site_location, b"", "clone", "--depth=1", f"--branch={branch}", os.path.join(os.getcwd(), server_repo_location)) 102