mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
122 lines
3.6 KiB
Python
Executable file
122 lines
3.6 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
__credits__ = """
|
|
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>'
|
|
__version__ = '2.0 beta1'
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import gettext
|
|
import getpass
|
|
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)
|
|
import json
|
|
if not __debug__:
|
|
import traceback
|
|
|
|
gettext.install('YunoHost')
|
|
|
|
try:
|
|
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate, win, parse_dict
|
|
except ImportError:
|
|
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
|
|
sys.exit(1)
|
|
|
|
|
|
def main():
|
|
"""
|
|
Main instructions
|
|
|
|
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
|
|
|
|
"""
|
|
if len(sys.argv) < 2:
|
|
sys.argv.append('-h')
|
|
|
|
with open('action_map.yml') as f:
|
|
action_map = yaml.load(f)
|
|
|
|
admin_password_provided = False
|
|
json_print = False
|
|
|
|
for key, arg in enumerate(sys.argv):
|
|
if arg == '--admin-password':
|
|
admin_password_provided = True
|
|
admin_password = sys.argv[key+1]
|
|
sys.argv.pop(key)
|
|
sys.argv.pop(key)
|
|
if arg == '--json':
|
|
json_print = True
|
|
sys.argv.pop(key)
|
|
|
|
try:
|
|
try:
|
|
with open('/etc/yunohost/installed') as f: pass
|
|
except IOError:
|
|
if len(sys.argv) < 3 or sys.argv[1] != 'tools' or sys.argv[2] != 'postinstall':
|
|
raise YunoHostError(17, _("YunoHost is not correctly installed, please execute 'yunohost tools postinstall'"))
|
|
|
|
args = parse_dict(action_map)
|
|
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']
|
|
if admin_password_provided:
|
|
with YunoHostLDAP(password=admin_password):
|
|
result = args.func(**args_dict)
|
|
else:
|
|
result = args.func(**args_dict)
|
|
#except TypeError, error:
|
|
#if not __debug__ :
|
|
#traceback.print_exc()
|
|
#print(_("Not (yet) implemented function"))
|
|
#return 1
|
|
except YunoHostError, error:
|
|
display_error(error, json_print)
|
|
return error.code
|
|
else:
|
|
if json_print or not os.isatty(1):
|
|
if result is None:
|
|
result = {}
|
|
if len(win) > 0:
|
|
result['success'] = []
|
|
for msg in win:
|
|
result['success'].append(msg)
|
|
print(json.dumps(result))
|
|
elif result is not None:
|
|
pretty_print_dict(result)
|
|
else:
|
|
pass
|
|
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|