def _handle_cli_arguments(): parser = argparse.ArgumentParser('Script that executes given beanshell/groovy script') hybris_argparse_helper.add_hybris_hac_arguments(parser) parser.add_argument('script', help='string with beanshell/groovy file ' 'or string with script (use literal \\n for newline) ' 'or "-" if piping script') parser.add_argument('type', help='type of script', choices=['groovy', 'beanshell']) # TODO: maybe instead of "--parameters 1 2 3 4" accept "1 2 3 4" as last parameters? (what about optional limit?) parser.add_argument('--parameters', '-p', nargs='*', help='arguments to put into script by replacing with $1, $2 etc') parser.add_argument('--rollback', action='store_true', help='Execute script in rollback mode') logging_helper.add_logging_arguments_to_parser(parser) args = parser.parse_args() script = argparse_helper.get_text_from_string_or_file_or_pipe(args.script) assert script is not None, 'Cannot load script' if args.parameters: for i, parameter in enumerate(args.parameters): parameter_to_replace = f'${i + 1}' if parameter_to_replace not in script: print(f'WARN: Unexpected parameter {parameter_to_replace} with value {repr(parameter)}') script = script.replace(parameter_to_replace, parameter) next_parameter = f'${len(args.parameters) + 1}' if next_parameter in script: print(f"WARN: Probably you should provide additional parameter for replacing with {next_parameter}") elif '$1' in script: print("No parameters given, but $1 found in query, probably you've forgotten to add parameter") logging.debug('Full script:') logging.debug(script) return args, script
def main(): parser = argparse.ArgumentParser( description= 'Script for printing tabular data (single or multi line per row)') parser.add_argument( 'data', help='String with data to put into table and print on console ' 'OR "-" if piping anything here ' 'CSV must be RFC-4180 compliant and separated by TAB (excel-tab) ' 'OR if using other separator then provide it with --csv-delimiter switch' ) add_common_parser_arguments(parser) logging_helper.add_logging_arguments_to_parser(parser) is_piping_text = shell_helper.is_piping_text() # no parameters given but piping if len(sys.argv) == 1 and is_piping_text: print( 'Seems like you are piping a text into this script, but you didn\'t provide "-" as argument, exiting...' ) parser.print_help() sys.exit(1) args = parser.parse_args() multiline_tabulate_wrapped = logging_helper.decorate_method_with_pysnooper_if_needed( multiline_tabulate, args.logging_level) if is_piping_text and args.data == '-': # piping data = shell_helper.read_text_from_pipe().rstrip('\n').replace( '\r', '') # remove leading whitespaces data = data.lstrip() else: # not piping data = args.data.replace('\\t', '\t').replace('\\n', '\n') data_split_by_lines = get_header_and_data_from_string( data, delimiter=args.csv_delimiter, quotechar=args.csv_quotechar) try: print( multiline_tabulate_wrapped(data_split_by_lines, **extract_arguments_as_kwargs(args))) except MultilineException as exc: data_without_multiple_newlines = data.replace('\n\n', '\n') print( f'Caught exception when trying to tabulate:\n{data_without_multiple_newlines}' )
from lib import logging_helper from lib import requests_helper from lib import shell_helper # TODO: add --threads parameter with 1 as a default value 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)
def _handle_cli_arguments(): parser = argparse.ArgumentParser( 'Script that executes given flexible search') hybris_argparse_helper.add_hybris_hac_arguments(parser) parser.add_argument( 'query', help= 'string with flexible search or path to file with flexible search or "-" if piping' ) parser.add_argument( '--parameters', '-p', nargs='*', help='arguments to put into flexible query by replacing with $1, $2 etc' ) # TODO: if there are more than X (1? 2? 3?) blacklisted columns, but not all then by default show them parser.add_argument( '--no-blacklist', action='store_true', help='Show blacklisted columns (like hjmpTS, createdTS etc') parser.add_argument( '--analyse-short', '-a', action='store_true', help= 'Analyse PK and print short item info, by default: print long item info' ) parser.add_argument( '--no-analyse', '-A', action='store_true', help= 'Do not analyse PK to get info about them, by default: print long item info' ) parser.add_argument('--watch', '-w', type=float, help='Number of seconds to wait before retrying query') multiline_tabulate.add_common_parser_arguments(parser) logging_helper.add_logging_arguments_to_parser(parser) parser.set_defaults(limit=DEFAULT_ENTRIES_LIMIT) args = parser.parse_args() # TODO: expand checking PK to all blacklisted by default columns query_lower = args.query.lower() is_pk_between_select_and_from = query_lower.find( 'select') < query_lower.find('pk') < query_lower.find('from') if not args.no_blacklist and 'creation' not in args.query and ( 'pk' not in args.query or not is_pk_between_select_and_from): args.ignore_columns = f'{args.ignore_columns},' + ','.join( COLUMN_BLACKLIST) # TODO: accept --param key=value --param key2=value2 then replace ?key and ?key2 with value/value2, cleaner than $1, $2 flexible_query = argparse_helper.get_text_from_string_or_file_or_pipe( args.query) flexible_query = flexible_query.rstrip(';') if args.parameters: for i, parameter in enumerate(args.parameters): flexible_query = flexible_query.replace(f'${i + 1}', parameter) if f'${i + 2}' in flexible_query: print( f"Probably you should provide additional parameter for replacing with ${i + 2}" ) elif '$1' in flexible_query: print( "No parameters given, but $1 found in query, probably you've forgotten to add parameter" ) logging.debug('Full flexible search query:') logging.debug(flexible_query) return args, flexible_query
def main(): parser = argparse.ArgumentParser( description='Script that listens server logs for given regex') parser.add_argument('regex', nargs='?', help='regex to search in logs') parser.add_argument('--beans', action='store_true', help='Check info about bean errors in logs') logging_helper.add_logging_arguments_to_parser(parser) parser.add_argument('--launching', action='store_true', help='Listen "Launching a JVM"') parser.add_argument('--startup', action='store_true', help='Listen "Server startup in X ms"') parser.add_argument('--print-from', help='Print logs starting from regex') parser.add_argument('--print-to', help='Print logs ending on regex') args = parser.parse_args() if args.print_from and args.print_to: logging.info( f'Printing text between [{args.print_from}] and [{args.print_to}]') elif args.launching and args.startup: logging.info('Listening for server launching and startup') elif args.launching: logging.info('Listening for server launching') elif args.startup: logging.info('Listening for server startup') else: logging.info(f'Listening server logs for "{args.regex}"') # try to load ignored messages from csv file ignored_messages = None if os.path.exists(f'{__file__}.dat'): with open(f'{__file__}.dat') as ignored_messages: reader = csv.reader(ignored_messages) ignored_messages = set([row[0] for row in reader]) hybris_dir = os.getenv('HYBRIS_DIR') date_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") log_path = f'{hybris_dir}/log/tomcat/console-{date_yyyymmdd}.log' context = Bunch(counter=Counter(), print_from=args.print_from, print_to=args.print_to, regex=args.regex, print_in_progress=False, launching=args.launching, startup=args.startup, check_beans=args.beans) if os.name == 'nt': # handle situation when log file doesn't exist yet if not os.path.exists(log_path): spinner = itertools.cycle('|/-\\') while not os.path.exists(log_path): next_spinner = next(spinner) print( f'\rLog {log_path} does not exist yet, waiting...{next_spinner}', end='', flush=True) time.sleep(0.25) print( ) # add newline after end of waiting (with spinner animation, without end line characters on console) with open(log_path, 'r') as logfile: for line in tailer.follow(logfile): check_line(line, ignored_messages, context) elif os.name == 'posix': with subprocess.Popen(['tail', '-F', '-n', '0', log_path], stdout=subprocess.PIPE) as tail: try: for line_to_decode in tail.stdout: check_line(line_to_decode.decode(), ignored_messages, context, exit_callback=lambda: tail.kill()) except KeyboardInterrupt: logging.info('Bye') else: logging.critical(f'Weird os found: {os.name}, exiting...') sys.exit(1)