user-scripts/uscripts

123 lines
4.1 KiB
Python
Executable File

#!/usr/bin/env python3
from os import symlink, listdir, remove
from os.path import exists
from argparse import ArgumentParser
from sys import exit, stderr
from argparse import ArgumentParser
from getpass import getuser
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
from socket import gethostname
from shlex import split as shell_split
def eprint(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def mail(sender, user, subject, body):
domain = gethostname()
message = MIMEText(body)
message["From"] = "{}@{}".format(sender, domain)
message["To"] = "{}@{}".format(user, domain)
message["Subject"] = subject
command = shell_split("/usr/sbin/sendmail -t -oi")
proc = Popen(command, stdin=PIPE, universal_newlines=True)
proc.communicate(message.as_string())
description = "Administrative tool for handling shared user script submissions"
parser = ArgumentParser(prog="uscript", description=description)
parser.add_argument("-l", "--list", dest="list", action="store_true",
help="list all submitted scripts")
parser.add_argument("-a", "--approve", dest="approve", nargs="?",
help="approval of script index based on position from --list")
parser.add_argument("-R", "--reject", dest="reject", nargs="?",
help="rejection of script index based on position from --list")
arguments = parser.parse_args()
config_file = "/center/etc/shared"
queue_dir = "{}_queue".format(config_file[:-1])
queue_files = listdir(queue_dir)
splitter = "%%"
queued = dict()
if len(queue_files) == 0:
print("No scripts are queued")
exit(0)
for queue_file in queue_files:
with open("{}/{}".format(queue_dir, queue_file), "r") as qfile:
contents = qfile.read().strip()
if len(contents) > 0:
queued[queue_file] = contents
if len(queued) == 0:
print("No scripts are queued")
exit(0)
scripts = dict()
files = dict()
names = list()
for filename, queue in queued:
name, path, desc, user = queue.split(splitter)
scripts[name] = {
"path": path,
"desc": desc,
"user": user
}
names.append(name)
files[filename] = name
names = sorted(names)
if arguments.list:
header = "Index\tScript\tAuthor".expandtabs()
print(header)
print("-" * len(header))
for n, name in enumerate(names):
author = scripts[name]["user"]
print("{}\t{}\t{}".format(n, name, author).expandtabs())
approved = arguments.approve
rejected = arguments.reject
if approved is not None:
approved = int(approved)
if 0 > approved >= len(names):
eprint("ERROR: That index does not exist, see --list")
exit(1)
script_name = names[approved]
queued_file = files[script_name]
script = scripts[script_name]
script_path = script["path"]
user = script["user"]
sender = getuser()
user_bin = "/center/bin"
symlink(script_path, "{}/{}".format(user_bin, script_name))
remove(queued_file)
subject = "{} has been approved".format(script_name)
body = "".join([
"Congratulations! Your script '{}' has been ".format(script_name),
"approved by {} and has been added to the server's ".format(sender),
"user script repository found at /center/bin."
])
mail(sender, user, subject, body)
print("'{}' has been approved, email has been sent".format(script_name))
if rejected is not None:
rejected = int(rejected)
if 0 > rejected >= len(names):
eprint("ERROR: That index does not exist, see --list")
exit(1)
script_name = names[rejected]
queued_file = files[script_name]
script = scripts[script_name]
user = script["user"]
sender = getuser()
remove(queued_file)
print("'{}' has been rejected".format(script_name))
reason = input("Provide reason for user ({}): ".format(author))
subject = "{} has been rejected".format(script_name)
body = "".join([
"Unfortunately, your script '{}' has been ".format(script_name),
"rejected by {} for the following reason(s):\n{}".format(sender)
])
mail(sender, user, subject, body)
print("'{}' has been rejected, email has been sent".format(script_name))