|
#!/usr/bin/python3 |
|
|
|
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 |
|
|
|
# Needs to be a TLS protected SMTP server |
|
SMTP_SERVER = "smtp.gmail.com" |
|
SMTP_PORT = 587 |
|
SENDER_EMAIL = "INSERT EMAIL HERE" |
|
SENDER_PASSWORD = "INSERT PASSWORD HERE" |
|
RECEIVER_EMAIL = SENDER_EMAIL |
|
SUBJECT_TEMPLATE = "Updates available in {hostname}" |
|
MSG_TEMPLATE = """ |
|
At {time}, there are updates available in {hostname} due to an impossibility to do an auto-upgrade. |
|
{updates} |
|
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_auto_upgrade(timeout=1800): |
|
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 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_auto_upgrade() |
|
if available_updates: |
|
print("Available updates thanks to a failure to auto-upgrade, 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 or auto-upgrade successful, not sending e-mail") |
|
|
|
sys.exit() |
|
|
|
if __name__ == "__main__": |
|
main() |
Not providing a setting to disable auto-upgrades is kind of irritating for someone like me who just wants email notifications. I also would write a PKGBUILD for this script, but every time I would use it the settings would be overwritten since they are tied into the code. Using an external file for settings would be more sufficient. Settings for custom update commands for official repositories and AUR would also be handy. The password could be encrypted through python-keyring or other means for those who don't use keyring. One more thing: email_updates would be a better name for this script if my suggestions were applied.