Example #1
0
def main(argv):
    if len(argv) <= 1:
        raise app.UsageError("Missing arguments.", 1)
    if not FLAGS.styles:
        raise app.UsageError("Missing styles.", 1)

    user_args = _parse_user_args(FLAGS.arg)

    nb_linter = linter.Linter(verbose=FLAGS.verbose)
    lint_dict = add_styles(FLAGS.styles, FLAGS.exclude_lint, FLAGS.verbose)

    linter_fails = []  # Track failed notebooks for final return code.

    paths, _ = notebook_utils.collect_notebook_paths(argv[1:])

    for path in paths:
        print(f"Lint notebook: {path}")

        status = nb_linter.run(path, lint_dict, user_args)
        if not status.is_success:
            linter_fails.append(path)

        print(status)

    if linter_fails:
        _print_fails(linter_fails)
        sys.exit(1)
    else:
        sys.exit(0)
Example #2
0
def format_nb(
    *,
    notebooks: List[str],
    remove_outputs: bool = False,
    indent: int = 2,
    test: bool = False,
) -> Status:
    """Formats a notebook."""
    found_error = False  # Track errors for final return code.
    test_fail_notebooks = []
    paths, err_paths = notebook_utils.collect_notebook_paths(notebooks)

    if err_paths:
        found_error = True
        test_fail_notebooks.extend(err_paths)

    for path in paths:
        print(f"Format notebook: {path}", file=sys.stderr)

        data, source = notebook_utils.load_notebook(path)

        if not data:
            found_error = True
            test_fail_notebooks.append(path)
            continue

        # Returns formatted JSON byte string.
        expected_output = clean_notebook(data, source, path, remove_outputs,
                                         indent)

        if test:
            # Compare formatted contents with original file contents.
            src_bytes = path.read_bytes()
            if expected_output != src_bytes:
                test_fail_notebooks.append(path)
        else:
            path.write_bytes(expected_output)

    if test:
        if test_fail_notebooks:
            error_template = textwrap.dedent("""
      [test] The following notebooks are not formatted:
      {notebooks}
      Please install `nbfmt` and format:
      $ python3 -m pip install -U --user git+https://github.com/tensorflow/docs
      $ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
      """)
            notebooks = "\n".join(
                [f"- {str(fp)}" for fp in test_fail_notebooks])
            print(error_template.format(notebooks=notebooks), file=sys.stderr)
            return Status.FAIL
        else:
            print("[test] Notebooks are formatted", file=sys.stderr)
            return Status.PASS

    if found_error:
        return Status.FAIL

    return Status.PASS
Example #3
0
def format_nb(
    *,
    notebooks: List[str],
    remove_outputs: bool = False,
    indent: int = 2,
    test: bool = False,
) -> Status:
    """Formats a notebook."""
    found_error = False  # Track errors for final return code.
    test_fail_notebooks = []
    paths, err_paths = notebook_utils.collect_notebook_paths(notebooks)

    if err_paths:
        found_error = True
        test_fail_notebooks.extend(err_paths)

    for path in paths:
        print(f"Format notebook: {path}", file=sys.stderr)

        data, source = notebook_utils.load_notebook(path)

        if not data:
            found_error = True
            test_fail_notebooks.append(path)
            continue

        clean_root(data, filepath=path)  # Top-level notebook fields.
        clean_cells(data, source, remove_outputs)
        update_license_cells(data)

        nbjson = json.dumps(data,
                            sort_keys=True,
                            ensure_ascii=False,
                            indent=indent)

        if not OSS:
            # Serialization differences in enviroments.
            str_replaces = {"<": r"\u003c", ">": r"\u003e", "&": r"\u0026"}
            for str_from, str_to in str_replaces.items():
                nbjson = nbjson.replace(str_from, str_to)

        expected_output = (nbjson + "\n").encode("utf-8")

        if test:
            # Compare formatted contents with original file contents.
            src_bytes = path.read_bytes()
            if expected_output != src_bytes:
                test_fail_notebooks.append(path)
        else:
            path.write_bytes(expected_output)

    if test:
        if test_fail_notebooks:
            error_template = textwrap.dedent("""
      [test] The following notebooks are not formatted:
      {notebooks}
      Please install `nbfmt` and format:
      $ python3 -m pip install -U --user git+https://github.com/tensorflow/docs
      $ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
      """)
            notebooks = "\n".join(
                [f"- {str(fp)}" for fp in test_fail_notebooks])
            print(error_template.format(notebooks=notebooks), file=sys.stderr)
            return Status.FAIL
        else:
            print("[test] Notebooks are formatted", file=sys.stderr)
            return Status.PASS

    if found_error:
        return Status.FAIL

    return Status.PASS
Example #4
0
def main(argv):
    if len(argv) <= 1:
        raise app.UsageError("Missing arguments.")

    found_error = False  # Track errors for final return code.
    test_fail_notebooks = []
    paths, err_paths = notebook_utils.collect_notebook_paths(argv[1:])

    if err_paths:
        found_error = True
        test_fail_notebooks.extend(err_paths)

    for path in paths:
        print(f"Format notebook: {path}", file=sys.stderr)

        data, source = notebook_utils.load_notebook(path)

        if not data:
            found_error = True
            test_fail_notebooks.append(path)
            continue

        # Set top-level notebook defaults.
        data["nbformat"] = 4
        data["nbformat_minor"] = 0

        remove_extra_fields(data)  # Top-level fields.
        clean_cells(data, source, FLAGS.remove_outputs)
        update_metadata(data, filepath=path)
        update_license_cell(data)

        nbjson = json.dumps(data,
                            sort_keys=True,
                            ensure_ascii=False,
                            indent=FLAGS.indent)

        # Some enviroments require a different format.
        if not OSS:
            nbjson = nbjson.replace("<", r"\u003c").replace(">", r"\u003e")

        expected_output = (nbjson + "\n").encode("utf-8")

        if FLAGS.test:
            # Compare formatted contents with original file contents.
            src_bytes = path.read_bytes()
            if expected_output != src_bytes:
                test_fail_notebooks.append(path)
        else:
            path.write_bytes(expected_output)

    if FLAGS.test:
        if test_fail_notebooks:
            error_template = textwrap.dedent("""
      [test] The following notebooks are not formatted:
      {notebooks}
      Please install `nbfmt` and format:
      $ python3 -m pip install -U --user git+https://github.com/tensorflow/docs
      $ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
      """)
            notebooks = "\n".join(
                [f"- {str(fp)}" for fp in test_fail_notebooks])
            print(error_template.format(notebooks=notebooks), file=sys.stderr)
            sys.exit(1)
        else:
            print("[test] Notebooks are formatted", file=sys.stderr)
            sys.exit(0)

    if found_error:
        sys.exit(1)