2012-10-06 17:57:08 +02:00
|
|
|
#!/usr/bin/env python
|
2012-10-06 16:22:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-10-07 16:20:20 +02:00
|
|
|
__credits__ = """
|
|
|
|
Copyright (C) 2012 YunoHost
|
2012-10-06 20:29:00 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, see http://www.gnu.org/licenses
|
2012-10-07 16:20:20 +02:00
|
|
|
"""
|
2012-10-07 18:20:06 +02:00
|
|
|
__author__ = 'Kload <kload@kload.fr>'
|
2012-10-07 13:37:59 +02:00
|
|
|
__version__ = '2.0 beta1'
|
2012-10-06 20:29:00 +02:00
|
|
|
|
2012-10-08 18:16:43 +02:00
|
|
|
import os
|
2012-10-06 16:22:15 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
2012-10-07 16:20:20 +02:00
|
|
|
import gettext
|
2012-11-08 19:20:13 +01:00
|
|
|
import getpass
|
2012-10-28 15:55:35 +01:00
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
sys.stderr.write('Error: Yunohost CLI Require yaml lib\n')
|
|
|
|
sys.stderr.write('apt-get install python-yaml\n')
|
|
|
|
sys.exit(1)
|
2012-10-08 18:16:43 +02:00
|
|
|
import json
|
|
|
|
if not __debug__:
|
|
|
|
import traceback
|
|
|
|
|
2012-10-07 16:20:20 +02:00
|
|
|
gettext.install('YunoHost')
|
2012-10-07 13:16:19 +02:00
|
|
|
|
2012-10-10 16:01:54 +02:00
|
|
|
try:
|
2012-11-08 19:20:13 +01:00
|
|
|
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate
|
2012-10-10 16:01:54 +02:00
|
|
|
except ImportError:
|
2012-10-16 14:56:54 +02:00
|
|
|
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
|
2012-10-10 16:01:54 +02:00
|
|
|
sys.exit(1)
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-06 16:22:15 +02:00
|
|
|
|
2012-10-14 17:01:54 +02:00
|
|
|
def parse_dict(action_map):
|
2012-10-08 20:46:52 +02:00
|
|
|
"""
|
2012-10-10 11:10:57 +02:00
|
|
|
Turn action dictionnary to parser, subparsers and arguments
|
2012-10-28 15:55:35 +01:00
|
|
|
|
2012-10-08 20:46:52 +02:00
|
|
|
Keyword arguments:
|
2012-10-14 17:01:54 +02:00
|
|
|
action_map -- Multi-level dictionnary of categories/actions/arguments list
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
Returns:
|
2012-10-10 11:10:57 +02:00
|
|
|
Namespace of args
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
# Intialize parsers
|
2012-11-16 14:11:51 +01:00
|
|
|
parsers = subparsers_category = subparsers_action = {}
|
2012-10-08 20:46:52 +02:00
|
|
|
parsers['general'] = argparse.ArgumentParser()
|
|
|
|
subparsers = parsers['general'].add_subparsers()
|
2012-11-08 19:20:13 +01:00
|
|
|
new_args = []
|
2012-11-16 14:11:51 +01:00
|
|
|
patterns = {}
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-10 12:20:28 +02:00
|
|
|
# Add general arguments
|
2012-10-14 17:01:54 +02:00
|
|
|
for arg_name, arg_params in action_map['general_arguments'].items():
|
2012-11-09 18:04:15 +01:00
|
|
|
if 'full' in arg_params:
|
|
|
|
arg_names = [arg_name, arg_params['full']]
|
|
|
|
arg_fullname = arg_params['full']
|
|
|
|
del arg_params['full']
|
|
|
|
else: arg_names = [arg_name]
|
|
|
|
parsers['general'].add_argument(*arg_names, **arg_params)
|
2012-10-10 12:20:28 +02:00
|
|
|
|
2012-10-28 15:55:35 +01:00
|
|
|
del action_map['general_arguments']
|
2012-10-10 12:20:28 +02:00
|
|
|
|
|
|
|
# Split categories into subparsers
|
2012-10-14 17:01:54 +02:00
|
|
|
for category, category_params in action_map.items():
|
2012-10-10 16:01:54 +02:00
|
|
|
if 'category_help' not in category_params: category_params['category_help'] = ''
|
|
|
|
subparsers_category[category] = subparsers.add_parser(category, help=category_params['category_help'])
|
2012-10-08 20:46:52 +02:00
|
|
|
subparsers_action[category] = subparsers_category[category].add_subparsers()
|
2012-10-10 12:20:28 +02:00
|
|
|
# Split actions
|
2012-10-10 16:01:54 +02:00
|
|
|
if 'actions' in category_params:
|
|
|
|
for action, action_params in category_params['actions'].items():
|
|
|
|
if 'action_help' not in action_params: action_params['action_help'] = ''
|
|
|
|
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help'])
|
2012-10-10 16:13:46 +02:00
|
|
|
# Set the action s related function
|
2012-10-10 16:01:54 +02:00
|
|
|
parsers[category + '_' + action].set_defaults(
|
2012-10-28 15:55:35 +01:00
|
|
|
func=str_to_func('yunohost_' + category + '.'
|
|
|
|
+ category + '_' + action))
|
2012-10-10 16:01:54 +02:00
|
|
|
# Add arguments
|
|
|
|
if 'arguments' in action_params:
|
|
|
|
for arg_name, arg_params in action_params['arguments'].items():
|
2012-11-09 18:04:15 +01:00
|
|
|
arg_fullname = False
|
|
|
|
|
2012-11-08 19:20:13 +01:00
|
|
|
if 'password' in arg_params:
|
|
|
|
if arg_params['password']: is_password = True
|
|
|
|
del arg_params['password']
|
|
|
|
else: is_password = False
|
|
|
|
|
2012-10-10 19:13:38 +02:00
|
|
|
if 'full' in arg_params:
|
2012-11-08 19:20:13 +01:00
|
|
|
arg_names = [arg_name, arg_params['full']]
|
2012-11-09 18:04:15 +01:00
|
|
|
arg_fullname = arg_params['full']
|
2012-10-10 16:01:54 +02:00
|
|
|
del arg_params['full']
|
2012-11-08 19:20:13 +01:00
|
|
|
else: arg_names = [arg_name]
|
|
|
|
|
|
|
|
if 'ask' in arg_params:
|
|
|
|
require_input = True
|
|
|
|
if (category != sys.argv[1]) or (action != sys.argv[2]):
|
|
|
|
require_input = False
|
|
|
|
for name in arg_names:
|
2012-11-09 18:04:15 +01:00
|
|
|
if name in sys.argv[2:]: require_input = False
|
2012-11-08 19:20:13 +01:00
|
|
|
|
|
|
|
if require_input:
|
|
|
|
if is_password:
|
|
|
|
if os.isatty(1):
|
|
|
|
pwd1 = getpass.getpass(colorize(arg_params['ask'] + ': ', 'cyan'))
|
|
|
|
pwd2 = getpass.getpass(colorize('Retype ' + arg_params['ask'][0].lower() + arg_params['ask'][1:] + ': ', 'cyan'))
|
|
|
|
if pwd1 != pwd2:
|
|
|
|
raise YunoHostError(22, _("Passwords don't match"))
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
raise YunoHostError(22, _("Missing arguments") + ': ' + arg_name)
|
2012-11-09 18:04:15 +01:00
|
|
|
if arg_name[0] == '-': arg_extend = [arg_name, pwd1]
|
|
|
|
else: arg_extend = [pwd1]
|
2012-11-08 19:20:13 +01:00
|
|
|
else:
|
|
|
|
if os.isatty(1):
|
|
|
|
arg_value = raw_input(colorize(arg_params['ask'] + ': ', 'cyan'))
|
|
|
|
else:
|
|
|
|
raise YunoHostError(22, _("Missing arguments") + ': ' + arg_name)
|
2012-11-09 18:04:15 +01:00
|
|
|
if arg_name[0] == '-': arg_extend = [arg_name, arg_value]
|
|
|
|
else: arg_extend = [arg_value]
|
|
|
|
new_args.extend(arg_extend)
|
2012-11-08 19:20:13 +01:00
|
|
|
del arg_params['ask']
|
2012-11-07 19:58:43 +01:00
|
|
|
|
|
|
|
if 'pattern' in arg_params:
|
2012-11-08 19:20:13 +01:00
|
|
|
if (category == sys.argv[1]) and (action == sys.argv[2]):
|
|
|
|
if 'dest' in arg_params: name = arg_params['dest']
|
2012-11-09 18:04:15 +01:00
|
|
|
elif arg_fullname: name = arg_fullname[2:]
|
2012-11-08 19:20:13 +01:00
|
|
|
else: name = arg_name
|
|
|
|
patterns[name] = arg_params['pattern']
|
2012-11-09 18:04:15 +01:00
|
|
|
del arg_params['pattern']
|
2012-11-08 19:20:13 +01:00
|
|
|
|
|
|
|
parsers[category + '_' + action].add_argument(*arg_names, **arg_params)
|
|
|
|
|
|
|
|
args = parsers['general'].parse_args(sys.argv.extend(new_args))
|
|
|
|
args_dict = vars(args)
|
|
|
|
for key, value in patterns.items():
|
2012-11-09 18:04:15 +01:00
|
|
|
validate(value, args_dict[key])
|
2012-11-08 19:20:13 +01:00
|
|
|
|
|
|
|
return args
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-28 15:55:35 +01:00
|
|
|
|
2012-10-22 20:52:15 +02:00
|
|
|
def main():
|
2012-10-28 15:55:35 +01:00
|
|
|
"""
|
|
|
|
Main instructions
|
|
|
|
|
2012-10-14 17:40:13 +02:00
|
|
|
Parse the action_dict and execute the action-specific function,
|
|
|
|
then print json or pretty result if executed in a tty :)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int -- 0 or error code
|
2012-10-28 15:55:35 +01:00
|
|
|
|
2012-10-22 20:52:15 +02:00
|
|
|
"""
|
2013-02-10 20:31:29 +01:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
sys.argv.append('-h')
|
|
|
|
|
2012-10-22 20:52:15 +02:00
|
|
|
with open('action_map.yml') as f:
|
|
|
|
action_map = yaml.load(f)
|
|
|
|
|
2013-01-26 21:34:02 +01:00
|
|
|
admin_password_provided = False
|
|
|
|
|
|
|
|
for key, arg in enumerate(sys.argv):
|
|
|
|
if arg == '--admin-password':
|
|
|
|
admin_password_provided = True
|
2013-02-10 20:31:29 +01:00
|
|
|
admin_password = sys.argv[key+1]
|
2013-01-26 21:34:02 +01:00
|
|
|
sys.argv.pop(key)
|
|
|
|
sys.argv.pop(key)
|
|
|
|
|
2012-10-08 18:16:43 +02:00
|
|
|
try:
|
2012-11-08 19:20:13 +01:00
|
|
|
args = parse_dict(action_map)
|
2012-12-01 15:33:56 +01:00
|
|
|
args_dict = vars(args).copy()
|
|
|
|
for key in args_dict.keys():
|
|
|
|
sanitized_key = key.replace('-', '_')
|
|
|
|
if sanitized_key is not key:
|
|
|
|
args_dict[sanitized_key] = args_dict[key]
|
|
|
|
del args_dict[key]
|
|
|
|
del args_dict['func']
|
2013-01-26 21:34:02 +01:00
|
|
|
if admin_password_provided:
|
|
|
|
with YunoHostLDAP(password=admin_password):
|
|
|
|
result = args.func(**args_dict)
|
|
|
|
else:
|
|
|
|
result = args.func(**args_dict)
|
2012-10-15 22:48:05 +02:00
|
|
|
except TypeError, error:
|
2012-10-22 19:45:05 +02:00
|
|
|
if not __debug__ :
|
|
|
|
traceback.print_exc()
|
2012-10-10 20:53:42 +02:00
|
|
|
print(_("Not (yet) implemented function"))
|
|
|
|
return 1
|
2012-10-08 18:16:43 +02:00
|
|
|
except YunoHostError, error:
|
2012-10-10 20:53:42 +02:00
|
|
|
display_error(error)
|
2012-10-08 18:16:43 +02:00
|
|
|
return error.code
|
2012-12-01 15:33:56 +01:00
|
|
|
else:
|
2012-10-28 15:24:10 +01:00
|
|
|
if result is None:
|
2012-10-29 16:06:46 +01:00
|
|
|
pass
|
|
|
|
elif os.isatty(1):
|
2012-10-10 14:19:17 +02:00
|
|
|
pretty_print_dict(result)
|
2012-10-08 18:16:43 +02:00
|
|
|
else:
|
|
|
|
print(json.dumps(result))
|
2012-10-14 17:40:13 +02:00
|
|
|
|
2012-10-28 15:55:35 +01:00
|
|
|
return 0
|
2012-10-07 13:16:19 +02:00
|
|
|
|
2012-10-06 16:22:15 +02:00
|
|
|
if __name__ == '__main__':
|
2012-10-22 20:52:15 +02:00
|
|
|
sys.exit(main())
|