1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#!/usr/bin/env python
# Copyright (C) 2019-2020 CS GROUP - France. All Rights Reserved.
# Author: Antoine Luong <antoine.luong@c-s.fr>
#
# This file is part of the Prewikka program.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
import cmd
import getpass
import glob
import io
import locale
import logging
import os
import re
import sys
from prewikka import cli, localization, log, main, siteconfig, usergroup, utils, version
from prewikka import FakeRequest
COMMAND_ERROR = 1
PARSE_ERROR = 2
SYSTEM_ERROR = 3
UNEXPECTED_ERROR = 10
class PrewikkaCLI(cmd.Cmd):
prompt = "(prewikka-cli) "
_interactive = False
def _complete_command(self, command, text, line):
if len(line.split(" ")) != 2:
return
last_term = line.split(" ")[-1]
offset = len(last_term) - len(text)
return [category[offset:] for category in cli.get(command) if category.startswith(last_term)]
def _do_command(self, command, category, *args, **kwargs):
c = cli.get(command).get(category)
if not c:
print("*** %s" % _("Unknown command"))
return COMMAND_ERROR
method, permissions, help, options = c
env.request.user.check(permissions)
try:
result = method(*args, **kwargs)
except Exception as e:
print("*** %s" % _("An unexpected error occurred: %s") % e)
return UNEXPECTED_ERROR
if command == "list":
for elem in result:
print(elem)
return 0
def _help_command(self, command, description):
additional_help = []
for category, (method, permissions, help, options) in sorted(cli.get(command).items()):
if help:
additional_help.append(_(help))
print("\n".join(description + [" - %s" % h for h in additional_help]))
def _get_option(self, command, category, option):
c = cli.get(command).get(category)
if not c:
return None
return c[-1].get(option)
def complete_create(self, text, line, begidx, endidx):
return self._complete_command("create", text, line)
def complete_delete(self, text, line, begidx, endidx):
return self._complete_command("delete", text, line)
def complete_import(self, text, line, begidx, endidx):
if len(line.split(" ")) < 3:
return self._complete_command("import", text, line)
path = line.split(" ")[-1]
offset = len(path) - len(text)
return [f[offset:] + (os.sep if os.path.isdir(f) else "") for f in glob.glob(path + "*")]
def complete_list(self, text, line, begidx, endidx):
return self._complete_command("list", text, line)
def complete_sync(self, text, line, begidx, endidx):
return self._complete_command("sync", text, line)
def do_create(self, arg):
try:
category, name, data = re.findall("^(\S+) (\S+)(?: ({.*}))?$", arg)[0]
data = utils.json.loads(data) if data else {}
except (IndexError, ValueError):
print("*** %s" % _("Could not parse input"))
return PARSE_ERROR
return self._do_command("create", category, name=name, data=data)
def do_delete(self, arg):
try:
category, name = arg.split()
except ValueError:
print("*** %s" % _("Could not parse input"))
return PARSE_ERROR
return self._do_command("delete", category, name=name)
def do_import(self, arg):
args = arg.split()
try:
category = args[0]
filenames = args[1:]
except IndexError:
print("*** %s" % _("Could not parse input"))
return PARSE_ERROR
files = []
mode = "rb" if self._get_option("import", category, "mode") == "binary" else "r"
for pattern in filenames:
for path in glob.glob(pattern):
with io.open(path, mode) as f:
files.append({
"name": os.path.basename(f.name),
"data": f.read()
})
return self._do_command("import", category, files=files)
def do_list(self, arg):
return self._do_command("list", arg)
def do_sync(self, arg):
return self._do_command("sync", arg)
def do_update(self, arg):
return self._do_command("update", arg)
def help_create(self):
return self._help_command("create", ["create <type> <name> <data>", _("Create an object of the specified type")])
def help_delete(self):
return self._help_command("delete", ["delete <type> <name>", _("Delete an object of the specified type")])
def help_import(self):
return self._help_command("import", ["import <type> <files...>", _("Import objects of the specified type from files")])
def help_list(self):
return self._help_command("list", ["list <type>", _("List objects of the specified type")])
def help_sync(self):
return self._help_command("sync", ["sync <type>", _("Synchronize objects of the specified type")])
def help_update(self):
return self._help_command("update", ["update <type> <data>", _("Update objects of the specified type")])
def do_EOF(self, line):
return True
def get_names(self):
# Hide not implemented actions from the list of commands
return [name for name in cmd.Cmd.get_names(self) if not name.startswith(("do_", "help_")) or name.endswith("help") or cli.get(name.split("_")[-1])]
def emptyline(self):
pass
def preloop(self):
self._interactive = True
def postcmd(self, stop, line):
if line and line.strip() not in ("help", "?"):
print()
if self._interactive and not isinstance(stop, bool):
# Do not exit on error in interactive mode
return False
return stop
def set_locale(lang):
if lang[0] not in localization.get_languages():
lang = "en_GB.utf8"
else:
lang = ".".join(lang)
localization.translation.set_locale(lang)
if __name__ == "__main__":
set_locale(locale.getdefaultlocale())
parser = argparse.ArgumentParser(description=_("Prewikka command-line tool"))
parser.add_argument("-u", "--user", default="admin", help=_("name of the user"))
parser.add_argument("-c", "--config", default="%s/prewikka.conf" % siteconfig.conf_dir, help=_("configuration file to use (default: %(default)s)"))
parser.add_argument("-C", "--command", help=_("command to execute"))
parser.add_argument("-D", "--debug", action="store_true", help=_("enable debugging output"))
args = parser.parse_args()
interpreter = PrewikkaCLI()
env.request = FakeRequest()
if getpass.getuser() != "prewikka":
print("*** %s" % _("prewikka-cli must be executed as the prewikka user"))
sys.exit(SYSTEM_ERROR)
main.Core.from_config(args.config)
env.request.user = env.auth.authenticate(args.user, no_password_check=True)
env.request.user.set_locale()
if args.debug:
formatter = logging.Formatter('%(asctime)s prewikka-cli (pid:%(process)d) %(levelname)s: %(message)s', '%X')
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
env.log._logger.addHandler(handler)
if args.command:
ret = 0
for command in args.command.split(";"):
r = interpreter.onecmd(command)
if r > ret:
ret = r
sys.exit(ret)
else:
interpreter.cmdloop("\n".join([
_("Prewikka command-line tool (%s)") % version.__version__,
_("Type \"help\" or \"?\" to list commands"),
""
]))
|