context_settings=CLICK_CONTEXT_SETTINGS) @click.version_option(version=__init__conf__.version, prog_name=__init__conf__.shell_command, message='{} version %(version)s'.format( __init__conf__.shell_command)) @click.option('--traceback/--no-traceback', is_flag=True, type=bool, default=None, help='return traceback information on cli') def cli_main(traceback: Optional[bool] = None) -> None: if traceback is not None: cli_exit_tools.config.traceback = traceback @cli_main.command('info', context_settings=CLICK_CONTEXT_SETTINGS) def cli_info() -> None: """ get program informations """ info() # entry point if main if __name__ == '__main__': try: cli_main() except Exception as exc: cli_exit_tools.print_exception_message() sys.exit(cli_exit_tools.get_system_exit_code(exc)) finally: cli_exit_tools.flush_streams()
def run( description: str, command: str, retry: int = 3, sleep: int = 30, banner: bool = True, show_command: bool = True, ) -> None: """ runs and retries a command passed as string and wrap it in "success" or "error" banners Parameter --------- description description of the action, shown in the banner command the command to launch retry retry the command n times, default = 3 sleep sleep for n seconds between the commands, default = 30 banner if to use banner for run/success or just colored lines. Errors will be always shown as banner show_command if the command is shown - take care not to reveal secrets here ! Result --------- none Exceptions ------------ none Examples ------------ >>> run('test', "unknown command", sleep=0) Traceback (most recent call last): ... SystemExit: ... >>> run('test', "unknown command", sleep=0, show_command=False) Traceback (most recent call last): ... SystemExit: ... >>> run('test', "echo test") >>> run('test', "echo test", show_command=False) """ # run}}} command = command.strip() lib_log_utils.setup_handler() if show_command: command_description = command else: command_description = "***secret***" lib_log_utils.banner_success( f"Action: {description}\nCommand: {command_description}", banner=banner, ) tries = retry while True: try: subprocess.run(command, shell=True, check=True) lib_log_utils.banner_success(f"Success: {description}", banner=False) break except Exception as exc: tries = tries - 1 # try 3 times, because sometimes connection or other errors on travis if tries: lib_log_utils.banner_notice( f"Retry in {sleep} seconds: {description}\nCommand: {command_description}", banner=False, ) time.sleep(sleep) else: if show_command: exc_message = str(exc) else: exc_message = "Command ***secret*** returned non-zero exit status" lib_log_utils.banner_error( f"Error: {description}\nCommand: {command_description}\n{exc_message}", banner=True, ) if hasattr(exc, "returncode"): if exc.returncode is not None: # type: ignore sys.exit(exc.returncode) # type: ignore sys.exit(1) # pragma: no cover finally: cli_exit_tools.flush_streams()