Esempio n. 1
0
def call_method(metaObject, args, unknown_args, colorPrint):
    """
    Function for call method through metaObject and args
    """
    method_name = args.method
    stdin_passwd = args.stdin_passwd
    method_view_name = method_name + "_view"
    metaObject.no_progress = args.no_progress
    metaObject.gui_progress = args.gui_progress
    metaObject.gui_warning = args.gui_warning
    view_obj = ViewInfo()
    view_obj.step = None
    view_obj.expert = True
    view_obj.brief = None
    view_obj.onlyhelp = False

    try:
        view = getattr(metaObject, method_view_name)(0, view_obj)
    except AttributeError:
        colorPrint.printERROR(_("Method not found: ") + method_name)
        return None
    method_parser = get_method_argparser(view, args, cl_core=True)
    param_object = create_param_object(view)
    try:
        unknown_args = method_parser.fixBoolVariables(unknown_args)
        args, unknown_args = method_parser.parse_known_args(unknown_args)
        no_questions = args.no_questions
    except SystemExit as e:
        return 1
    except Exception as e:
        import traceback

        for i in apply(traceback.format_exception, sys.exc_info()):
            sys.stderr.write(i)
        sys.stderr.flush()
        raise
    for i in unknown_args:
        if i.startswith("-"):
            if i in parse(True).parse_known_args()[1]:
                _print(_("Unknown parameter"), i)
                return 1
        else:
            _print(_("Unknown argument"), i)
            return 1

    param_object, steps = collect_object(None, param_object, view, args, stdin_passwd=stdin_passwd)
    if view.has_brief:
        setattr(param_object, "CheckOnly", True)
        check_res = {}
        while True:
            method_result = getattr(metaObject, method_name)(0, param_object)
            if not method_result:
                print _("Method not available")
                return None

            if method_result[0].type and method_result[0].type != "pid":
                check_res = check_result_msg(method_result, view, check_res)
                if not check_res:
                    return None
                else:
                    param_object = get_param_pwd(check_res, view, param_object, stdin_passwd=stdin_passwd)
            else:
                break

        view_obj = ViewInfo()
        view_obj.step = None
        view_obj.expert = True
        view_obj.brief = True
        view_obj.onlyhelp = False
        try:
            view = getattr(metaObject, method_view_name)(0, view_obj)
        except AttributeError:
            colorPrint.printERROR(_("Method not found: ") + method_name)

        print_brief(view, steps.label)
        if not no_questions:
            while True:
                try:
                    ask = raw_input("\n" + _("Run process? (yes/no): "))
                except KeyboardInterrupt:
                    ask = "no"
                    print
                if ask.lower() in ["n", "no"]:
                    colorPrint.printERROR(_("Manually interrupted"))
                    return None
                if ask.lower() in ["y", "yes"]:
                    break

    setattr(param_object, "CheckOnly", False)
    try:
        method_result = getattr(metaObject, method_name)(0, param_object)
    except VariableError, e:
        _print(e)
        return None
Esempio n. 2
0
def https_server(client, args, unknown_args, url, clVarsCore, wait_thread):
    client_post_auth(client)

#    sym_link = os.path.basename(sys.argv[0])
#    if sym_link != 'cl-console':
#        wait_thread.stop()
#        results = client.service.get_methods(client.sid, 'console')
#        find_flag = False
#        if hasattr (results, 'stringArray'):
#            for _array in results.stringArray:
#                if _array.string[0] == sym_link:
#                    args.method = _array.string[1]
#                    find_flag = True
#                    break
#        if not find_flag:
#            _print (_('Method not found for %s') %sym_link)

    if args.stop_consoled:
        wait_thread.stop()
        os.system('cl-consoled --stop')
        return 0

    if args.session_clean:
        wait_thread.stop()
        session_clean(client)

    if args.session_info or args.session_num_info:
        wait_thread.stop()
        client_session_info(client, args.session_num_info)
        return 0

    if args.session_list:
        wait_thread.stop()
        client_session_list(client)
        return 0

    if args.list_pid:
        wait_thread.stop()
        if args.dump:
            from pid_information import client_pid_info
            client_pid_info(client)
        else:
            from pid_information import client_list_pid
            client_list_pid(client)

    if args.pid_res:
        wait_thread.stop()
        get_entire_frame(client, args.pid_res)
        return 0

    if args.pid_kill:
        wait_thread.stop()
        from pid_information import client_pid_kill
        return client_pid_kill(client, args.pid_kill)

    retCode = 0
    if not args.method:
        wait_thread.stop()
        client_list_methods(client)
        return 1

    elif args.method and args.help:
        view_params = get_view_params(client, args.method + '_view',
                                      step = None, expert = True,
                                      onlyhelp = True)
        view = get_view(client, args.method, client.sid, view_params)
        wait_thread.stop()
        sys.stdout.write("\b")
        sys.stdout.flush()
        method_parser = get_method_argparser(view, args)
        method_parser.print_help()
        client.service.clear_method_cache(client.sid, args.method)

    else:
        try:
            client.frame_period = clVarsCore.Get('core.cl_core_get_frame_period')
        except:
            client.frame_period = 2
        method_result = call_method(client, args, unknown_args, wait_thread)
        if method_result:
            client.no_progress = args.no_progress
            try:
                analysis(client, client.sid, method_result)
            except urllib2.URLError, e:
                _print (e)
            except KeyboardInterrupt:
                try:
                    print
                    mess = method_result[0][0]
                    pid = int(mess.message)
                    result = client.service.pid_kill(pid, client.sid)
                    if result in [0,2]:
                        print _('Process terminated')
                    elif result == -1:
                        print _("Certificate not found on the server")
                    elif result == -2:
                        print _("Session not matching your certificate")
                    elif result == 1:
                        print _("Failed to terminate the process")
#                    get_entire_frame(client, pid)
                    analysis(client, client.sid, method_result)
                except Exception, e:
                    _print (e.message)
Esempio n. 3
0
def local_method(metaclass, args, unknown_args):
    """
    Call method from metaclass, check method existing.

    Generate help, for method, run method by 'call_method'.
    """
    import os

    sym_link = os.path.basename(sys.argv[0])
    if sym_link != "cl-core":
        if sym_link in metaclass.Dec.conMethods.keys():
            args.method = metaclass.Dec.conMethods[sym_link][0]
        else:
            _print(_("Method not found for %s") % sym_link)
            sys.exit(1)

    if args.list_methods:
        keys = metaclass.Dec.conMethods.keys()
        keys.sort()
        for key in keys:
            print metaclass.Dec.conMethods[key][0], " - ", metaclass.Dec.conMethods[key][2]
        return 0

    colorPrint = color_print()
    metaObject = metaclass()
    method_name = args.method
    method_view_name = method_name + "_view"
    if args.method and args.help:
        view_obj = ViewInfo()
        view_obj.step = None
        view_obj.expert = True
        view_obj.brief = None
        view_obj.onlyhelp = True
        try:
            view = getattr(metaObject, method_view_name)(0, view_obj)
        except AttributeError:
            colorPrint.printERROR(_("Method not found: ") + method_view_name)
            return 1
        try:
            method_parser = get_method_argparser(view, args, cl_core=True)
        except Exception as e:
            import traceback

            for i in apply(traceback.format_exception, sys.exc_info()):
                sys.stderr.write(i)
            sys.stderr.flush()
            metaObject.clear_cache(0, method_name)
            return 1
        method_parser.print_help()
    else:
        try:
            call_method(metaObject, args, unknown_args, colorPrint)
            metaObject.clear_cache(0, method_name)
            return metaObject.method_status
        except (ValueError) as e:
            colorPrint.printERROR(str(e))
            # colorPrint.printERROR(shortTraceback(*sys.exc_info()))
        except (KeyboardInterrupt, EOFError):
            colorPrint.printERROR(_("Manually interrupted"))
        except Exception as e:
            colorPrint.printERROR(shortTraceback(*sys.exc_info()))
            pass
    #            print 'Error: ', e
    metaObject.clear_cache(0, method_name)