-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_upgrade.py
111 lines (90 loc) · 3.22 KB
/
auto_upgrade.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
#!/usr/bin/python3
# Import some functions
import smtplib
import sys
import time
from distutils.spawn import find_executable
from email.mime.text import MIMEText
from socket import gethostname
from subprocess import run, PIPE
# Import the variables from settings.py
from ./settings.py import *
# Needs to be a TLS protected SMTP server
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
SENDER_EMAIL = sender
SENDER_PASSWORD = senderPassword
RECEIVER_EMAIL = recipient
SUBJECT_TEMPLATE = "Updates available on {hostname}"
MSG_TEMPLATE = """
At {time} on {hostname} there are new updates that should be installed:
{updates}
Please login to {hostname} and run:
\t# pacman -Syu
To upgrade the system.
"""
def add_prefix(prefix, text):
result = ""
for line in text.splitlines():
result += prefix + line + "\n"
return result
def do_upgrade(timeout=timeout):
packages = ""
try:
run(["pacman", "-Syu", "--noconfirm"], timeout=timeout, universal_newlines=True)
except TimeoutExpired:
result = run(["checkupdates"], stdout=PIPE, universal_newlines=True)
packages += "\nOfficial repositories:\n"
packages += add_prefix("\t:: ", result.stdout)
if find_executable("cower"):
result = run(["cower", "--update", "--color=never"], stdout=PIPE, universal_newlines=True)
if result.stdout:
packages += "\nAUR:\n"
# cower already adds "::" as a prefix
packages += add_prefix("\t", result.stdout)
return packages
def do_update_list(timeout=timeout):
# Clear packages variable
packages = ""
# Refresh pacman database and check for updates
run(["pacman", "-Syy", "--noconfirm"], timeout=timeout, universal_newlines=True)
result = run(["checkupdates"], stdout=PIPE, universal_newlines=True)
packages += "\nOfficial repositories:\n"
packages += add_prefix("\t:: ", result.stdout)
# Check for aur updates
if find_executable("pacaur"):
result = run(["pacaur", "-k"], stdout=PIPE, universal_newlines=True)
if result.stdout:
packages += "\npacAUR:\n"
packages += add_prefix("\t:: ", result.stdout)
else:
packages += "\nMissing Package pacAUR\n"
return packages
def send_email(receiver, sender, password, subject, message):
# Connect to SMTP server
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
server.starttls()
server.login(sender, password)
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
server.send_message(msg)
server.quit()
def main():
available_updates = do_update_list()
if available_updates:
print("Updates available, sending e-mail to {}".format(RECEIVER_EMAIL))
hostname = gethostname()
send_email(RECEIVER_EMAIL, SENDER_EMAIL, SENDER_PASSWORD,
SUBJECT_TEMPLATE.format(hostname=hostname),
MSG_TEMPLATE.format(time=time.strftime("%c"),
hostname=hostname,
updates=available_updates)
)
else:
print("No available package to upgrage, not sending e-mail")
sys.exit()
if __name__ == "__main__":
main()