Commit d2147f46 authored by Martin Potthast's avatar Martin Potthast

*** empty log message ***

parent 517b2578
......@@ -20,8 +20,21 @@ sys.path.insert(0, os.path.abspath(os.path.join(script_dir, "..", "..", "libs"))
from log import *
def main(params):
def get_random_msg():
cmd = "wget -qO - http://www.asciiartfarts.com/random.cgi | sed -n '/<pre>/,/<\/pre>/p' | sed -n '/<table*/,/<\/table>/p' | sed '1d' | sed '$d'"
import subprocess
output = subprocess.check_output(cmd, shell=True).decode("ascii")
from html import unescape
return unescape(output)
def main(params):
print(get_random_msg())
print("\n\n")
max_i = shutil.get_terminal_size().columns
for j in range(3):
for i in range(0, max_i):
......
#!/usr/bin/env python3
"""
Webis command
Copyright 2015-today
"""The main webis command.
Project WEBIS
Author: Steve Göring
Copyright webis.de 2015-today
Authors: Steve Göring, Martin Potthast
"""
import sys
import os
import argparse
import collections
import json
import loader
import os
import sys
from log import *
from lib import *
from log import *
from system import *
def get_modules(config):
"""
read all modules from moduls_directory
:return list of modules
"""
lInfo("read all available moduls in " + config["moduls_directory"])
modules = {}
for directory in os.listdir(config["moduls_directory"]):
if os.path.isdir(config["moduls_directory"] + directory) and directory not in config["ignored_dirs"]:
modules[directory] = config["moduls_directory"] + directory
return modules
def check_allowed_shebang(scriptpath, allowed_scripts):
"""
check for given script if it is allowed
:return true if scriptpath has a valid shebang
"""
shebang = get_shebang(scriptpath)
for interpreter in allowed_scripts:
if interpreter in shebang:
return True
return False
def get_submodules(config, modulepath):
"""
get all submodules based on modulepath
:return list of submodules
"""
submodules = {}
for script in os.listdir(modulepath):
scriptpath = modulepath + "/" + script
if os.path.isfile(scriptpath):
if check_allowed_shebang(scriptpath, config["allowed_scripts"]):
# store scriptname without extension as shortcut
submodules[script.rsplit(".", 1)[0]] = scriptpath
return submodules
def load_config():
"""Returns the configuration, based on the JSON configuration file."""
try:
return json.loads(read_file(os.path.dirname(
os.path.realpath(__file__)) + "/config.json"))
except Exception as e:
lError("The file config.json is invalid.")
sys.exit(1)
def get_submodule_description(modulepath):
"""
extract submodul description based on modulepath
:return description
"""
if not os.path.isfile(modulepath):
return os.path.basename(modulepath)
f = open(modulepath)
def get_commands(config):
"""Returns commands mapped to directories containing their subcommands."""
commands = collections.OrderedDict()
for directory in os.listdir(config["moduls_directory"]):
if (os.path.isdir(config["moduls_directory"] + directory) and
directory not in config["ignored_dirs"]):
commands[directory] = config["moduls_directory"] + directory
return commands
def get_subcommands(config, commandpath):
"""Returns subcommands of a given command mapped to their script files."""
subcommands = collections.OrderedDict()
for script in os.listdir(commandpath):
subcommandpath = commandpath + "/" + script
if os.path.isfile(subcommandpath):
allowed_scripts = config["allowed_scripts"]
if check_subcommand_shebang(subcommandpath, allowed_scripts):
subcommand = script.rsplit(".", 1)[0]
subcommands[subcommand] = subcommandpath
return subcommands
def get_subcommand_help(subcommandpath):
"""Returns the help message expected at the beginning of a command."""
if not os.path.isfile(subcommandpath):
return os.path.basename(subcommandpath)
f = open(subcommandpath)
lines = f.readlines()
f.close()
if len(lines) < 1 or lines[1][0] != "#":
......@@ -76,105 +63,70 @@ def get_submodule_description(modulepath):
return lines[1][1:].replace("\n", "").strip()
def handle_script(submodul, scriptfile, params=[]):
"""
run extracted submodul based on scriptpath with all given params as parameter
:return exit code of submodul
"""
lInfo("start script")
lDbg("run: " + submodul + ", path: " + scriptfile)
def check_subcommand_shebang(subcommandpath, allowed_scripts):
"""Checks if the shebang of a given script is among the allowed shebangs."""
shebang = get_shebang(subcommandpath)
for interpreter in allowed_scripts:
if interpreter in shebang:
return True
return False
lInfo("output:")
cmd = " ".join([scriptfile] + params)
# make script executable
os.system("chmod +x {}".format(scriptfile))
def run_subcommand(subcommand, subcommandpath, params=[]):
"""Runs subcommand passing params and returns its exit code."""
lDbg("Running " + subcommand + " at " + subcommandpath)
# FIXME: When the script is installed at /usr/lib, this won't work, anymore:
os.system("chmod +x {}".format(subcommandpath))
cmd = " ".join([subcommandpath] + params)
return_value = os.system(cmd)
if return_value == 0:
lInfo("done")
lInfo("Done.")
else:
lError("something was wrong with your call: " + cmd)
lError("An error occurred while executing " + cmd)
return return_value
def get_keys_startswith(dictionary, searchkey):
"""
extract keys that have a prefix match
"""
needed_keys = [x for x in dictionary.keys() if x.startswith(searchkey)]
return needed_keys
def get_random_msg():
cmd = "wget -qO - http://www.asciiartfarts.com/random.cgi | sed -n '/<pre>/,/<\/pre>/p' | sed -n '/<table*/,/<\/table>/p' | sed '1d' | sed '$d'"
import subprocess
output = subprocess.check_output(cmd, shell=True).decode("ascii")
from html import unescape
return unescape(output)
def main(args):
lInfo("read config")
lInfo("\n" + get_random_msg())
# read config file
try:
config = json.loads(read_file(os.path.dirname(os.path.realpath(__file__)) + "/config.json"))
except Exception as e:
lError("configuration file 'config.json' is corupt (not json conform).")
return 1
# config check
if not os.path.isdir(config["moduls_directory"]):
config["moduls_directory"] = os.path.dirname(os.path.realpath(__file__)) + "/" + config["moduls_directory"]
if not os.path.isdir(config["moduls_directory"]):
lError("moduls_directory " + config["moduls_directory"] + " is not a valid directory, check config.json")
return 1
modules = get_modules(config)
parser = argparse.ArgumentParser(description='supercmd -- meta cmd controller', epilog="Steve Göring 2015", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('modul', type=str, choices=list(modules.keys()), help='modulname')
parser.add_argument('submodul', type=str, nargs='?', default="help", help='submodul')
parser.add_argument('parameter', type=str, nargs='*', help='parameters for submodul command')
if len(args) > 0 and len(get_keys_startswith(modules, args[0])) == 1:
args[0] = get_keys_startswith(modules, args[0])[0]
argsdict = vars(parser.parse_args(args[0:2])) # using args[..] is necessary because of unknown arguments in the parameters for submoduls
# set parameters
argsdict["parameter"] = args[2:]
lInfo("available moduls: " + str(len(modules)))
if argsdict["modul"] not in modules:
lError("selected modul " + argsdict["modul"] + " is not available, " + str(list(modules.keys())))
return 1
lInfo("selected modul: " + argsdict["modul"])
submodules = get_submodules(config, modules[argsdict["modul"]])
submodules["help"] = "integrated help"
if len(get_keys_startswith(submodules, argsdict["submodul"])) == 1:
argsdict["submodul"] = get_keys_startswith(submodules, argsdict["submodul"])[0]
if argsdict["submodul"] == "help":
lHelp("submodules of " + argsdict["modul"])
max_size = len(max(submodules.keys(), key=len))
format_str = "{:<" + str(max_size) + "}"
for sub in sorted(submodules.keys()):
lHelp(" " + format_str.format(sub) + " : " + get_submodule_description(submodules[sub]))
config = load_config()
commands = get_commands(config)
parser = argparse.ArgumentParser()
subparsers = {}
allcommands = {}
commandparsers = parser.add_subparsers(
metavar='<command>', dest='<command>', help='Available commands:')
for command in commands:
# TODO: It is unclear where to obtain the help messages for commands.
commandparser = commandparsers.add_parser(command,
help='TODO: Help message for commands.')
subparsers[command] = commandparser
subcommandparsers = commandparser.add_subparsers(
metavar='<subcommand>', dest='<subcommand>',
help='Available subcommands:')
subcommands = get_subcommands(config, commands[command])
allcommands[command] = subcommands
for subcommand in subcommands:
subcommandparser = subcommandparsers.add_parser(
subcommand, help=str(
get_subcommand_help(subcommands[subcommand])),
add_help=False)
subparsers[subcommand] = subcommandparser
if len(args) == 0:
parser.print_help()
return 0
if argsdict["submodul"] not in submodules:
lError("selected submodul " + argsdict["submodul"] + " is not available use help, " + str(list(submodules.keys())))
return 1
parseresults = parser.parse_known_args(args)
argsdict = vars(parseresults[0])
parameter = parseresults[1]
if len(argsdict) == 2 and argsdict['<subcommand>'] == None:
subparsers[argsdict['<command>']].print_help()
return 0
lInfo("selected submodul: " + argsdict["submodul"])
return handle_script(argsdict["submodul"], submodules[argsdict["submodul"]], argsdict["parameter"])
return run_subcommand(argsdict['<subcommand>'],
allcommands[argsdict['<command>']][argsdict['<subcommand>']], parameter)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment