Пример #1
0
def execute_script(script,
                   script_type,
                   rollback,
                   address,
                   user,
                   password,
                   session=None):
    # TODO: check if address is set here, because it will fail
    if session is None:
        session, address = requests_helper.get_session_with_basic_http_auth_and_cleaned_address(
            address)

    credentials = {'user': user, 'password': password}
    hybris_requests_helper.log_into_hac_and_return_csrf_or_exit(
        session, address, credentials)
    script_get_result = session.get(address + '/console/scripting/')
    script_csrf_token = re.search(r'name="_csrf"\s+value="(.+?)"\s*/>',
                                  script_get_result.text).group(1)
    form_data = {
        'script': script,
        '_csrf': script_csrf_token,
        'scriptType': script_type,
        'commit': not rollback
    }
    form_data_without_script = {
        k: v
        for k, v in form_data.items() if k != 'script'
    }
    logging.debug(f'form_data_without_script: {form_data_without_script}')
    logging.debug('...executing...')
    script_post_result = session.post(address + '/console/scripting/execute',
                                      data=form_data)
    logging.debug('done, printing results:')
    if script_post_result.status_code == 500:
        bs = BeautifulSoup(script_post_result.text, 'html.parser')
        html = bs.find('textarea').text
        number_of_lines_to_show = 20
        first_n_lines = '\n'.join(
            html.strip().split('\n')[0:number_of_lines_to_show])
        msg = f'Received HTTP500, printing first {number_of_lines_to_show} lines of result:\n{first_n_lines}'
        return ScriptExecutionResponse(None, None, msg)

    result_json = script_post_result.json()
    logging.debug(result_json)

    if not result_json:
        return ScriptExecutionResponse('No result', None, None)
    elif result_json.get('stacktraceText', None):
        return ScriptExecutionResponse(result_json['outputText'].strip(), None,
                                       result_json['stacktraceText'])
    else:
        return ScriptExecutionResponse(result_json['outputText'].strip(),
                                       result_json['executionResult'], None)
def main():
    logging_helper.run_ipython_on_exception()
    args, flexible_query = _handle_cli_arguments()
    wrapped_execute_flexible_search_and_return_header_and_data = logging_helper.decorate_method_with_pysnooper_if_needed(
        _execute_flexible_search_and_return_header_and_data,
        args.logging_level)

    session, address = requests_helper.get_session_with_basic_http_auth_and_cleaned_address(
        args.address)
    credentials = {'user': args.user, 'password': args.password}
    csrf_token = hybris_requests_helper.log_into_hac_and_return_csrf_or_exit(
        session, address, credentials)

    if args.watch:
        try:
            iteration = 0
            lines_in_last_output = 0
            while True:
                last_update_time = time.asctime()
                last_update_message = f'Last update: {last_update_time} {time.time()}'
                header_and_data = wrapped_execute_flexible_search_and_return_header_and_data(
                    session, address, csrf_token, flexible_query,
                    args.analyse_short, args.no_analyse, args.limit,
                    args.ignore_columns)
                output = last_update_message + '\n' + _use_pager_for_header_and_data(
                    header_and_data, args.pager, args)
                if iteration == 0:
                    print(output, end='', flush=True)
                else:
                    move_up = shell_helper.get_move_up(lines_in_last_output)
                    clear_to_end_of_screen = shell_helper.clear_to_end_of_screen(
                    )
                    print(f'{move_up}{clear_to_end_of_screen}{output}',
                          end='',
                          flush=True)

                lines_in_last_output = output.count('\n')
                iteration += 1

                time.sleep(args.watch)
        except KeyboardInterrupt:
            print('\r  ')
    else:
        prepared_string = wrapped_execute_flexible_search_and_return_header_and_data(
            session, address, csrf_token, flexible_query, args.analyse_short,
            args.no_analyse, args.limit, args.ignore_columns)
        print(_use_pager_for_header_and_data(prepared_string, args.pager,
                                             args),
              end='',
              flush=True)
Пример #3
0
is_piping_text = shell_helper.is_piping_text()

parser = argparse.ArgumentParser(
    'Script for importing impex from file or text')
hybris_argparse_helper.add_hybris_hac_arguments(parser)
parser.add_argument('impex',
                    help='string with impex (use literal \\n for newline) '
                    'OR path to impex file '
                    'OR "-" if piping text into this script')
logging_helper.add_logging_arguments_to_parser(parser)
args = parser.parse_args()

is_using_file_with_impex = os.path.exists(args.impex)

session, address = requests_helper.get_session_with_basic_http_auth_and_cleaned_address(
    args.address)

credentials = {'user': args.user, 'password': args.password}
hybris_requests_helper.log_into_hac_and_return_csrf_or_exit(
    session, address, credentials)

impex_get_result = session.get(address + '/console/impex/import')
impex_csrf_token = re.search(r'name="_csrf"\s+value="(.+?)"\s*/>',
                             impex_get_result.text).group(1)

if is_using_file_with_impex:
    logging.debug(f'Using file with impex: {args.impex}')
    hybris_dir = os.getenv('HYBRIS_DIR')

    logging.debug(f'File size: {os.path.getsize(args.impex)}')
    if os.path.getsize(args.impex) == 0: