By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 celery_tasks.py

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