moulinette/yunohost

123 lines
3.6 KiB
Text
Raw Normal View History

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__ = """
2013-03-03 01:18:28 +01:00
Copyright (C) 2012 YunoHost
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
"""
__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:
2013-09-23 18:35:17 +02:00
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate, win, reset_win_messages, parse_dict
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-22 20:52:15 +02:00
def main():
2012-10-28 15:55:35 +01:00
"""
2013-03-03 01:18:28 +01:00
Main instructions
2012-10-28 15:55:35 +01:00
2013-03-03 01:18:28 +01:00
Parse the action_dict and execute the action-specific function,
then print json or pretty result if executed in a tty :)
2012-10-14 17:40:13 +02:00
2013-03-03 01:18:28 +01:00
Returns:
int -- 0 or error code
2012-10-28 15:55:35 +01:00
2013-03-03 01:18:28 +01:00
"""
2013-09-23 18:35:17 +02:00
reset_win_messages()
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
2013-03-13 13:22:27 +01:00
json_print = False
2013-01-26 21:34:02 +01:00
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)
2013-03-13 13:21:23 +01:00
if arg == '--json':
json_print = True
sys.argv.pop(key)
2013-01-26 21:34:02 +01:00
2012-10-08 18:16:43 +02:00
try:
2013-06-13 14:20:22 +02:00
try:
with open('/etc/yunohost/installed') as f: pass
except IOError:
2013-06-13 14:27:34 +02:00
if len(sys.argv) < 3 or sys.argv[1] != 'tools' or sys.argv[2] != 'postinstall':
2013-06-13 14:23:39 +02:00
raise YunoHostError(17, _("YunoHost is not correctly installed, please execute 'yunohost tools postinstall'"))
2013-06-13 14:20:22 +02:00
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)
2013-02-12 13:52:11 +01:00
#except TypeError, error:
#if not __debug__ :
#traceback.print_exc()
#print(_("Not (yet) implemented function"))
#return 1
2012-10-08 18:16:43 +02:00
except YunoHostError, error:
2013-03-15 16:59:00 +01:00
display_error(error, json_print)
2012-10-08 18:16:43 +02:00
return error.code
2012-12-01 15:33:56 +01:00
else:
2013-03-15 16:59:00 +01:00
if json_print or not os.isatty(1):
if result is None:
result = {}
if len(win) > 0:
2013-09-23 18:35:17 +02:00
result['success'] = win
2013-03-15 16:59:00 +01:00
print(json.dumps(result))
elif result is not None:
2012-10-10 14:19:17 +02:00
pretty_print_dict(result)
2012-10-08 18:16:43 +02:00
else:
2013-03-15 16:59:00 +01:00
pass
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())