Fixed permissions bug with shared.queue file, now in separate queue files
This commit is contained in:
parent
01e8b3bba0
commit
5f0e05f372
7
share
7
share
|
@ -11,6 +11,7 @@ from getpass import getuser
|
|||
from pwd import getpwuid
|
||||
from socket import gethostname
|
||||
from shlex import split as shell_split
|
||||
from uuid import uuid4 as uuid
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, file=stderr, **kwargs)
|
||||
|
@ -57,7 +58,7 @@ def share_submit(queue_file, splitter):
|
|||
exit(1)
|
||||
script_name = script_path.split("/")[-1]
|
||||
script_desc = input("Description of script: ")
|
||||
with open(queue_file, "a") as queue:
|
||||
with open(queue_file, "w") as queue:
|
||||
queue.write("{0}{4}{1}{4}{2}{4}{3}".format(script_name, script_path,
|
||||
script_desc, user, splitter))
|
||||
subject = "Submission of '{}' script".format(script_name)
|
||||
|
@ -74,7 +75,7 @@ splitter = "%%"
|
|||
if not exists(config_file):
|
||||
touch(config_file)
|
||||
|
||||
queue_file = "{}.queue".format(config_file)
|
||||
queue_file = "{}_queue/{}.queue".format(config_file[:-1], uuid.urn[9:])
|
||||
if not exists(queue_file):
|
||||
touch(queue_file)
|
||||
|
||||
|
@ -92,4 +93,4 @@ if arguments.commands == "list":
|
|||
elif arguments.commands == "submit":
|
||||
share_submit(queue_file, splitter)
|
||||
else:
|
||||
parser.print_help()
|
||||
parser.print_help()
|
||||
|
|
44
uscripts
44
uscripts
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from os import symlink
|
||||
from os import symlink, listdir, remove
|
||||
from os.path import exists
|
||||
from argparse import ArgumentParser
|
||||
from sys import exit, stderr
|
||||
|
@ -9,6 +9,7 @@ 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)
|
||||
|
@ -23,18 +24,6 @@ def mail(sender, user, subject, body):
|
|||
proc = Popen(command, stdin=PIPE, universal_newlines=True)
|
||||
proc.communicate(message.as_string())
|
||||
|
||||
def remove_script_from_queue(queue_file, splitter, scripts, index):
|
||||
script_name = sorted(list(scripts.keys()))[index]
|
||||
del scripts[script_name]
|
||||
lines = list()
|
||||
for name, attrs in scripts:
|
||||
path = attrs.get("path", "")
|
||||
desc = attrs.get("desc", "")
|
||||
user = attrs.get("user", "")
|
||||
lines.append(splitter.join([name, path, desc, user]))
|
||||
with open(queue_file, "w") as qfile:
|
||||
qfile.write("\n".join(lines))
|
||||
|
||||
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",
|
||||
|
@ -46,24 +35,28 @@ parser.add_argument("-R", "--reject", dest="reject", nargs="?",
|
|||
arguments = parser.parse_args()
|
||||
|
||||
config_file = "/center/etc/shared"
|
||||
queue_file = "{}.queue".format("/center/etc/shared")
|
||||
queue_dir = "{}_queue".format(config_file[:-1])
|
||||
queue_files = listdir(queue_dir)
|
||||
splitter = "%%"
|
||||
queued = None
|
||||
queued = dict()
|
||||
|
||||
if not exists(queue_file):
|
||||
if len(queue_files) == 0:
|
||||
print("No scripts are queued")
|
||||
exit(0)
|
||||
|
||||
with open(queue_file, "r") as qfile:
|
||||
queued = qfile.read().split("\n")
|
||||
|
||||
if len(queued) == 1 and len(queued[0]) == 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 queue in queued:
|
||||
for filename, queue in queued:
|
||||
name, path, desc, user = queue.split(splitter)
|
||||
scripts[name] = {
|
||||
"path": path,
|
||||
|
@ -71,6 +64,7 @@ for queue in queued:
|
|||
"user": user
|
||||
}
|
||||
names.append(name)
|
||||
files[filename] = name
|
||||
names = sorted(names)
|
||||
|
||||
if arguments.list:
|
||||
|
@ -89,13 +83,14 @@ if approved is not None:
|
|||
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_script_from_queue(queue_file, splitter, scripts, approved)
|
||||
remove(queued_file)
|
||||
subject = "{} has been approved".format(script_name)
|
||||
body = "".join([
|
||||
"Congratulations! Your script '{}' has been ".format(script_name),
|
||||
|
@ -111,10 +106,11 @@ if rejected is not None:
|
|||
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_script_from_queue(queue_file, splitter, scripts, rejected)
|
||||
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)
|
||||
|
@ -123,4 +119,4 @@ if rejected is not None:
|
|||
"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))
|
||||
print("'{}' has been rejected, email has been sent".format(script_name))
|
||||
|
|
Loading…
Reference in New Issue