예제 #1
0
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",
        )
예제 #2
0
    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
예제 #3
0
    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 == {}
예제 #4
0
    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 == {}
예제 #5
0
파일: cli.py 프로젝트: mr-exz/WADebug
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
예제 #6
0
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
예제 #7
0
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
예제 #8
0
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,
        )
예제 #9
0
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)
예제 #10
0
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.",
        )
예제 #11
0
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)
예제 #12
0
    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
예제 #13
0
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
예제 #14
0
    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))
예제 #15
0
    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,
            )
예제 #16
0
파일: ui.py 프로젝트: WhatsApp/WADebug
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))
예제 #17
0
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)
예제 #18
0
 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"
예제 #19
0
def load_config():
    return Config().values
예제 #20
0
 def test_should_not_be_in_dev_mode(self):
     assert (
         Config().development_mode is False
     ), "Config().development_mode should be False before committing code"