motd/add_motd_update

32 lines
899 B
Python
Executable File

#!/usr/bin/env python3
from sys import exit
from os import environ
from datetime import datetime
from tempfile import NamedTemporaryFile
from subprocess import call
updates_file = "/center/etc/updates.txt"
editor = environ.get("EDITOR", "nano")
now = datetime.now().isoformat()[:10]
header = "# Any line beginning with '#' will not show up\n"
update = None
with NamedTemporaryFile(suffix=".tmp", delete=True) as tf:
tf.write(header.encode())
tf.flush()
call([editor, tf.name])
tf.seek(0)
update = tf.read().decode()
lines = list()
for line in update.split("\n"):
if len(line) > 1 and line[0] == "#":
continue
lines.append(line)
update_line = "\n".join(lines)
if len(update_line) == 0:
print("ERROR: Nothing to add to updates")
exit(1)
new_update = "- [" + now + "]: " + update_line
with open(updates_file, "a") as updates:
updates.write(new_update)