def handle_config_missing(): permission_granted = click.confirm( click.style( "\nWADebug requires a config file: wadebug.conf.yml " "in the current directory in order to run full checks, " "but none has been found. " "Do you want to create the file now?", fg="yellow", )) if permission_granted: if Config().create_default_config_file(): click.echo( "The config file has been created at {}. " "Please fill in the values and run wadebug commands again\n". format(os.getcwd())) sys.exit(0) else: click.secho( "\nUnable to create config file at {}. Error: {}\n" "Some checks will be skipped as a result.\n".format( os.getcwd(), Config().create_exception), fg="yellow", ) else: click.secho( "\nYou have chosen not to create the config file. " "Some checks will be skipped as a result.\n", fg="yellow", )
def test_should_return_correct_config(self): mock_config = {"mock": "config"} with patch( "wadebug.config.Config._load_config_from_file", return_value=mock_config ): assert Config().values == mock_config assert Config().load_error == ConfigLoadError.NONE
def test_should_return_config_missing(self): mock_exception = Exception("something goes wrong!") with patch( "wadebug.config.Config._load_config_from_file", side_effect=mock_exception ): assert Config().load_error == ConfigLoadError.CONFIG_MISSING assert Config().values == {}
def load_config_interactive(): if Config().load_error == ConfigLoadError.CONFIG_MISSING: handle_config_missing() return {} elif Config().load_error == ConfigLoadError.CONFIG_INVALID: ui.print_invalid_config_message(Config.CONFIG_FILE, Config().load_exception) sys.exit(-1) else: return Config().values
def test_should_return_config_invalid(self): mock_parse_exception = ParserError("something goes wrong!") with patch( "wadebug.config.Config._load_config_from_file", side_effect=mock_parse_exception, ): assert Config().load_error == ConfigLoadError.CONFIG_INVALID assert Config().values == {}
def execute_actions_interactive(actions): config = load_config_interactive() # execution logic is duplicated so that we print results as they appear # this way, if something gets stuck, users can ctrl+c or take other actions ui.print_program_header() if Config().development_mode: ui.print_dev_mode_header() else: click.echo() result = {} problems = [] for act in actions: res = act.run(config) result[res.action.user_facing_name] = res.to_dict() ui.print_result_header(res) if isinstance(res, results._NotOK): ui.print_result_details(res) problems.append(res) click.echo() if problems: click.echo("! WADebug found {} issues.".format(len(problems))) return result
def debug_interactive(acts, opt_out): result = execute_actions_interactive(acts) if not opt_out and not Config().disable_send_data: cli_utils.send_results_to_fb( result, success_callback=send_usage_result_interactive_success, failure_callback=send_result_interactive_failure, )
def run(): try: main() except Exception as e: if Config().development_mode: raise print( "An error occurred with WADebug:\n{}\n".format(e), "Please report this via Direct Support " "(https://business.facebook.com/direct-support) ", "and paste this full error message.", )
def to_dict(self): ret = { "class": self.action.__name__, "user_facing_name": self.action.user_facing_name, "result": self.result, "short_description": self.action.short_description, "message": self.message, "details": self.details, "remediation": self.remediation, } if Config().development_mode and hasattr(self, "traceback"): ret["traceback"] = self.traceback return ret
def get_support_info(): support_info_filename = os.path.join(OUTPUT_FOLDER, SUPPORT_INFO_LOG_FILE) try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) support_info_content = api.get_support_info() else: return except Exception: return docker_utils.write_to_file(support_info_filename, json.dumps(support_info_content, indent=2)) return support_info_filename
def prompt_upgrade(version): from outdated import check_outdated try: is_outdated, latest_version = check_outdated("wadebug", version) if is_outdated: click.secho( "The current version of wadebug ({}) is out of date. " "Run `pip3 install wadebug --upgrade` " "to upgrade to the latest version ({})\n".format( version, latest_version), fg="yellow", ) except Exception: if Config().development_mode: raise
def handle_upload_results(output, zipped_logs_file_handle): if Config().development_mode: return if send: send_logs_result = cli_utils.send_logs_to_fb( zipped_logs_file_handle, success_callback=send_logs_result_json_success, failure_callback=send_result_json_failure, ) output.update(send_logs_result) elif not opt_out: cli_utils.send_results_to_fb( output, success_callback=send_usage_result_json_success, failure_callback=send_result_json_failure, ) click.echo(json.dumps(output))
def handle_upload_results(output, zipped_logs_file_handle): if Config().disable_send_data: return if send: click.echo("Sending logs to Facebook\nPlease wait...") cli_utils.send_logs_to_fb( zipped_logs_file_handle, success_callback=send_logs_result_interactive_success, failure_callback=send_result_interactive_failure, ) elif not opt_out: click.echo("Sending report to Facebook\nPlease wait...") cli_utils.send_results_to_fb( output, success_callback=send_usage_result_interactive_success, failure_callback=send_result_interactive_failure, )
def send_logs_to_fb( zipped_logs_file_handle, success_callback=None, failure_callback=None ): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: run_id = Analytics.send_logs_to_fb(zipped_logs_file_handle, phone_number) if success_callback: return success_callback(run_id) except Exception as e: if failure_callback: return failure_callback(e)
def send_results_to_fb(result, success_callback=None, failure_callback=None): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: event = Events.RUN_ACTIONS_AND_SEND_RESULTS data = json.dumps(result) run_id = Analytics.send_event(event, data, phone_number) result["run_id"] = run_id if success_callback: success_callback(result) except Exception as e: if failure_callback: failure_callback(e)
def print_result_details(result): for field in [result.message, result.details, result.remediation]: click.echo(add_indentation_to_result_field(field)) if Config().development_mode and hasattr(result, "traceback"): click.echo(add_indentation_to_result_field(result.traceback))
def debug_json(acts, opt_out): result = execute_actions(acts) if not opt_out and not Config().disable_send_data: cli_utils.send_results_to_fb(result)
def test_network_should_not_be_disabled(self): assert ( Config().disable_send_data is False ), "Config().disable_send_data should be False before committing code"
def test_should_not_be_in_dev_mode(self): assert ( Config().development_mode is False ), "Config().development_mode should be False before committing code"
def load_config(): return Config().values