示例#1
0
文件: cli.py 项目: luke-chang/gecko-1
def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **lintargs):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_results

    if list_linters:
        lint_paths = find_linters(linters)
        print("Available linters: {}".format(
            [os.path.splitext(os.path.basename(l))[0] for l in lint_paths]
        ))
        return 0

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, outgoing=outgoing, workdir=workdir)

    if edit and results:
        edit_results(results)
        results = lint.roll(results.keys())

    formatter = formatters.get(fmt)

    # Encode output with 'replace' to avoid UnicodeEncodeErrors on
    # environments that aren't using utf-8.
    out = formatter(results, failed=lint.failed).encode(
                    sys.stdout.encoding or 'ascii', 'replace')
    if out:
        print(out)
    return 1 if results or lint.failed else 0
示例#2
0
def run(paths, linters, fmt, outgoing, workdir, edit, list_linters=None, **lintargs):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_results

    if list_linters:
        lint_paths = find_linters(linters)
        print("Available linters: {}".format(
            [os.path.splitext(os.path.basename(l))[0] for l in lint_paths]
        ))
        return 0

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, outgoing=outgoing, workdir=workdir)

    if edit and results:
        edit_results(results)
        results = lint.roll(results.keys())

    formatter = formatters.get(fmt)

    # Encode output with 'replace' to avoid UnicodeEncodeErrors on
    # environments that aren't using utf-8.
    out = formatter(results, failed=lint.failed).encode(
                    sys.stdout.encoding or 'ascii', 'replace')
    if out:
        print(out)
    return 1 if results or lint.failed else 0
示例#3
0
def run(paths,
        linters,
        formats,
        outgoing,
        workdir,
        edit,
        setup=False,
        list_linters=False,
        num_procs=None,
        **lintargs):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_issues

    if list_linters:
        lint_paths = find_linters(linters)
        print("Available linters: {}".format(
            [os.path.splitext(os.path.basename(l))[0] for l in lint_paths]))
        return 0

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # Always run bootstrapping, but return early if --setup was passed in.
    ret = lint.setup()
    if setup:
        return ret

    # run all linters
    result = lint.roll(paths,
                       outgoing=outgoing,
                       workdir=workdir,
                       num_procs=num_procs)

    if edit and result.issues:
        edit_issues(result)
        result = lint.roll(result.issues.keys(), num_procs=num_procs)

    for formatter_name, path in formats:
        formatter = formatters.get(formatter_name)

        out = formatter(result)
        if out:
            fh = open(path, 'w') if path else sys.stdout

            if not path and fh.encoding == 'ascii':
                # If sys.stdout.encoding is ascii, printing output will fail
                # due to the stylish formatter's use of unicode characters.
                # Ideally the user should fix their environment by setting
                # `LC_ALL=C.UTF-8` or similar. But this is a common enough
                # problem that we help them out a little here by manually
                # encoding and writing to the stdout buffer directly.
                out += '\n'
                fh.buffer.write(out.encode('utf-8', errors='replace'))
                fh.buffer.flush()
            else:
                print(out, file=fh)

    return result.returncode
示例#4
0
    def test_treeherder_formatter(self):
        expected = """
TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | a/b/c.txt:4 | oh no baz (baz)
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
""".strip()

        fmt = formatters.get('treeherder')
        self.assertEqual(expected, fmt(self.results))
def test_treeherder_formatter(results):
    expected = """
TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | a/b/c.txt:4 | oh no baz (baz)
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
""".strip()

    fmt = formatters.get('treeherder')
    assert expected == fmt(results)
示例#6
0
    def test_json_formatter(self):
        fmt = formatters.get('json')
        formatted = json.loads(fmt(self.results))

        self.assertEqual(set(formatted.keys()), set(self.results.keys()))

        slots = ResultContainer.__slots__
        for errors in formatted.values():
            for err in errors:
                self.assertTrue(all(s in err for s in slots))
def test_json_formatter(result):
    fmt = formatters.get("json")
    formatted = json.loads(fmt(result))

    assert set(formatted.keys()) == set(result.issues.keys())

    slots = Issue.__slots__
    for errors in formatted.values():
        for err in errors:
            assert all(s in err for s in slots)
示例#8
0
    def test_json_formatter(self):
        fmt = formatters.get('json')
        formatted = json.loads(fmt(self.results))

        self.assertEqual(set(formatted.keys()), set(self.results.keys()))

        slots = ResultContainer.__slots__
        for errors in formatted.values():
            for err in errors:
                self.assertTrue(all(s in err for s in slots))
def test_json_formatter(result):
    fmt = formatters.get("json")
    formatted = json.loads(fmt(result))

    assert set(formatted.keys()) == set(result.issues.keys())

    attrs = attr.fields(Issue)
    for errors in formatted.values():
        for err in errors:
            assert all(a.name in err for a in attrs)
示例#10
0
def test_json_formatter(results):
    fmt = formatters.get('json')
    formatted = json.loads(fmt(results))

    assert set(formatted.keys()) == set(results.keys())

    slots = ResultContainer.__slots__
    for errors in formatted.values():
        for err in errors:
            assert all(s in err for s in slots)
示例#11
0
def test_json_formatter(results):
    fmt = formatters.get('json')
    formatted = json.loads(fmt(results))

    assert set(formatted.keys()) == set(results.keys())

    slots = ResultContainer.__slots__
    for errors in formatted.values():
        for err in errors:
            assert all(s in err for s in slots)
示例#12
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, rev=rev, workdir=workdir)

    formatter = formatters.get(fmt)
    print(formatter(results))
    return 1 if results else 0
示例#13
0
def test_stylish_formatter(results):
    expected = """
a/b/c.txt
  1  error  oh no foo  (foo)
  4  error  oh no baz  (baz)

d/e/f.txt
  4:2  warning  oh no bar  bar-not-allowed (bar)

\u2716 3 problems (2 errors, 1 warning)
""".strip()

    fmt = formatters.get('stylish', disable_colors=True)
    assert expected == fmt(results)
示例#14
0
    def test_stylish_formatter(self):
        expected = """
a/b/c.txt
  1  error  oh no foo  (foo)
  4  error  oh no baz  (baz)

d/e/f.txt
  4:2  warning  oh no bar  bar-not-allowed (bar)

\u2716 3 problems (2 errors, 1 warning)
""".strip()

        fmt = formatters.get('stylish', disable_colors=True)
        self.assertEqual(expected, fmt(self.results))
示例#15
0
def run(paths,
        linters,
        formats,
        outgoing,
        workdir,
        edit,
        setup=False,
        list_linters=False,
        **lintargs):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_issues

    if list_linters:
        lint_paths = find_linters(linters)
        print("Available linters: {}".format(
            [os.path.splitext(os.path.basename(l))[0] for l in lint_paths]))
        return 0

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # Always run bootstrapping, but return early if --setup was passed in.
    ret = lint.setup()
    if setup:
        return ret

    # run all linters
    result = lint.roll(paths, outgoing=outgoing, workdir=workdir)

    if edit and result.issues:
        edit_issues(result)
        result = lint.roll(result.issues.keys())

    for formatter_name, path in formats:
        formatter = formatters.get(formatter_name)

        out = formatter(result)
        if sys.version_info[0] == 2:
            # Encode output with 'replace' to avoid UnicodeEncodeErrors on
            # environments that aren't using utf-8.
            out = formatter(result).encode(sys.stdout.encoding or 'ascii',
                                           'replace')

        if out:
            output_file = open(path, 'w') if path else sys.stdout
            print(out, file=output_file)

    return result.returncode
示例#16
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, rev=rev, workdir=workdir)

    formatter = formatters.get(fmt)

    # Encode output with 'replace' to avoid UnicodeEncodeErrors on
    # environments that aren't using utf-8.
    print(formatter(results, failed=lint.failed).encode(
        sys.stdout.encoding or 'ascii', 'replace'))
    return 1 if results or lint.failed else 0
示例#17
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, rev=rev, workdir=workdir)

    formatter = formatters.get(fmt)

    # Explicitly utf-8 encode the output as some of the formatters make
    # use of unicode characters. This will prevent a UnicodeEncodeError
    # on environments where utf-8 isn't the default
    print(formatter(results).encode('utf-8', 'replace'))
    return lint.return_code
示例#18
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths, rev=rev, workdir=workdir)

    formatter = formatters.get(fmt)

    # Explicitly utf-8 encode the output as some of the formatters make
    # use of unicode characters. This will prevent a UnicodeEncodeError
    # on environments where utf-8 isn't the default
    print(formatter(results).encode('utf-8', 'replace'))
    return lint.return_code
示例#19
0
    def lint(self, paths, linters=None, fmt='stylish', **lintargs):
        """Run linters."""
        from mozlint import LintRoller, formatters

        paths = paths or ['.']

        lint_files = self.find_linters(linters)

        lintargs['exclude'] = ['obj*']
        lint = LintRoller(**lintargs)
        lint.read(lint_files)

        # run all linters
        results = lint.roll(paths)

        formatter = formatters.get(fmt)
        print(formatter(results))
示例#20
0
    def lint(self, paths, linters=None, fmt="stylish", **lintargs):
        """Run linters."""
        from mozlint import LintRoller, formatters

        paths = paths or ["."]

        lint_files = self.find_linters(linters)

        lintargs["exclude"] = ["obj*"]
        lint = LintRoller(**lintargs)
        lint.read(lint_files)

        # run all linters
        results = lint.roll(paths)

        formatter = formatters.get(fmt)
        print(formatter(results))
示例#21
0
def run(paths,
        linters,
        fmt,
        outgoing,
        workdir,
        edit,
        list_linters=None,
        **lintargs):
    from mozlint import LintRoller, formatters

    if list_linters:
        lint_paths = find_linters(linters)
        print("Available linters: {}".format(
            [os.path.splitext(os.path.basename(l))[0] for l in lint_paths]))
        return 0
    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # Check if the path that is entered is a valid one.
    invalid_paths = [path for path in paths if not os.path.exists(path)]
    if invalid_paths:
        print("Error: The following paths do not exist:\n{}".format(
            "\n".join(invalid_paths)))
        return 1

    # run all linters
    results = lint.roll(paths, outgoing=outgoing, workdir=workdir)

    if edit:
        editor = os.environ['EDITOR']
        for path in results:
            subprocess.call([editor, path])
        return 1 if lint.failed else 0

    formatter = formatters.get(fmt)

    # Encode output with 'replace' to avoid UnicodeEncodeErrors on
    # environments that aren't using utf-8.
    out = formatter(results,
                    failed=lint.failed).encode(sys.stdout.encoding or 'ascii',
                                               'replace')
    if out:
        print(out)
    return 1 if results or lint.failed else 0
示例#22
0
def edit_issues(result):
    if not result.issues:
        return

    editor = get_editor()
    if not editor:
        print('warning: could not find a default editor')
        return

    name = os.path.basename(editor)
    if name in ('vim', 'nvim', 'gvim'):
        cmd = [
            editor,
            # need errorformat to match both Error and Warning, with or without a column
            '--cmd',
            'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %trror\\ -\\ %m',
            '--cmd',
            'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %tarning\\ -\\ %m',
            '--cmd',
            'set errorformat+=%f:\\ line\\ %l\\\\,\\ %trror\\ -\\ %m',
            '--cmd',
            'set errorformat+=%f:\\ line\\ %l\\\\,\\ %tarning\\ -\\ %m',
            # start with quickfix window opened
            '-c',
            'copen',
            # running with -q seems to open an empty buffer in addition to the
            # first file, this removes that empty buffer
            '-c',
            '1bd',
        ]

        with tempfile.NamedTemporaryFile() as fh:
            s = formatters.get('compact', summary=False)(result)
            fh.write(s)
            fh.flush()

            cmd.extend(['-q', fh.name])
            subprocess.call(cmd)

    else:
        for path, errors in result.issues.iteritems():
            subprocess.call([editor, path])
示例#23
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    # Calculate files from VCS
    vcfiles = VCFiles()
    if rev:
        paths.extend(vcfiles.by_rev(rev))
    if workdir:
        paths.extend(vcfiles.by_workdir())
    paths = paths or ['.']

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths)

    formatter = formatters.get(fmt)
    print(formatter(results))
    return 1 if results else 0
示例#24
0
def run(paths, linters, fmt, rev, workdir, **lintargs):
    from mozlint import LintRoller, formatters

    # Calculate files from VCS
    vcfiles = VCFiles()
    if rev:
        paths.extend(vcfiles.by_rev(rev))
    if workdir:
        paths.extend(vcfiles.by_workdir())
    paths = paths or ['.']

    lint = LintRoller(**lintargs)
    lint.read(find_linters(linters))

    # run all linters
    results = lint.roll(paths)

    formatter = formatters.get(fmt)
    print(formatter(results))
    return 1 if results else 0
示例#25
0
    def lint(self, paths, linters=None, fmt='stylish', **lintargs):
        """Run linters."""
        from mozlint import LintRoller, formatters

        paths = paths or ['.']

        lint_files = self.find_linters(linters)

        lintargs['exclude'] = ['obj*']
        lint = LintRoller(**lintargs)
        lint.read(lint_files)

        # run all linters
        results = lint.roll(paths)

        status = 0
        if results:
            status = 1

        formatter = formatters.get(fmt)
        print(formatter(results))
        return status
示例#26
0
def edit_results(results):
    if not results:
        return

    editor = get_editor()
    if not editor:
        print('warning: could not find a default editor')
        return

    name = os.path.basename(editor)
    if name in ('vim', 'nvim', 'gvim'):
        cmd = [
            editor,
            # need errorformat to match both Error and Warning, with or without a column
            '--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %trror\\ -\\ %m',
            '--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %tarning\\ -\\ %m',
            '--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ %trror\\ -\\ %m',
            '--cmd', 'set errorformat+=%f:\\ line\\ %l\\\\,\\ %tarning\\ -\\ %m',
            # start with quickfix window opened
            '-c', 'copen',
            # running with -q seems to open an empty buffer in addition to the
            # first file, this removes that empty buffer
            '-c', '1bd',
        ]

        with tempfile.NamedTemporaryFile() as fh:
            s = formatters.get('compact', summary=False)(results)
            fh.write(s)
            fh.flush()

            cmd.extend(['-q', fh.name])
            subprocess.call(cmd)

    else:
        for path, errors in results.iteritems():
            subprocess.call([editor, path])
示例#27
0
def test_formatters(result, name):
    opts = EXPECTED[name]
    fmt = formatters.get(name, **opts["kwargs"])
    # encoding to str bypasses a UnicodeEncodeError in pytest
    assert fmt(result).encode("utf-8") == opts["format"].encode("utf-8")
示例#28
0
def test_formatters(result, name):
    opts = EXPECTED[name]
    fmt = formatters.get(name, **opts['kwargs'])
    # encoding to str bypasses a UnicodeEncodeError in pytest
    assert fmt(result).encode('utf-8') == opts['format'].encode('utf-8')
示例#29
0
def run(
    paths,
    linters,
    formats,
    outgoing,
    workdir,
    rev,
    edit,
    setup=False,
    list_linters=False,
    num_procs=None,
    virtualenv_manager=None,
    **lintargs
):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_issues

    lintargs["config_paths"] = [
        os.path.join(lintargs["root"], p) for p in lintargs["config_paths"]
    ]

    if list_linters:
        lint_paths = find_linters(lintargs["config_paths"], linters)
        linters = [
            os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"]
        ]
        print("\n".join(sorted(linters)))
        return 0

    lint = LintRoller(**lintargs)
    linters_info = find_linters(lintargs["config_paths"], linters)

    result = None

    try:

        lint.read(linters_info["lint_paths"])

        # Always run bootstrapping, but return early if --setup was passed in.
        ret = lint.setup(virtualenv_manager=virtualenv_manager)
        if setup:
            return ret

        if linters_info["linters_not_found"] != []:
            raise NoValidLinter

        # run all linters
        result = lint.roll(
            paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs
        )
    except NoValidLinter as e:
        result = lint.result
        print(str(e))

    if edit and result.issues:
        edit_issues(result)
        result = lint.roll(result.issues.keys(), num_procs=num_procs)

    for every in linters_info["linters_not_found"]:
        result.failed_setup.add(every)

    for formatter_name, path in formats:
        formatter = formatters.get(formatter_name)

        out = formatter(result)
        if out:
            fh = open(path, "w") if path else sys.stdout

            if not path and fh.encoding == "ascii":
                # If sys.stdout.encoding is ascii, printing output will fail
                # due to the stylish formatter's use of unicode characters.
                # Ideally the user should fix their environment by setting
                # `LC_ALL=C.UTF-8` or similar. But this is a common enough
                # problem that we help them out a little here by manually
                # encoding and writing to the stdout buffer directly.
                out += "\n"
                fh.buffer.write(out.encode("utf-8", errors="replace"))
                fh.buffer.flush()
            else:
                print(out, file=fh)

    return result.returncode
示例#30
0
def test_formatters(results, name):
    opts = EXPECTED[name]
    fmt = formatters.get(name, **opts['kwargs'])
    assert fmt(results) == opts['format']
示例#31
0
文件: cli.py 项目: jt9897253/mozjs
def run(
    paths,
    linters,
    formats,
    outgoing,
    workdir,
    rev,
    edit,
    check_exclude_list,
    setup=False,
    list_linters=False,
    num_procs=None,
    virtualenv_manager=None,
    **lintargs
):
    from mozlint import LintRoller, formatters
    from mozlint.editor import edit_issues

    lintargs["config_paths"] = [
        os.path.join(lintargs["root"], p) for p in lintargs["config_paths"]
    ]

    # Always perform exhaustive linting for exclude list paths
    lintargs["use_filters"] = lintargs["use_filters"] and not check_exclude_list

    if list_linters:
        lint_paths = find_linters(lintargs["config_paths"], linters)
        linters = [
            os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"]
        ]
        print("\n".join(sorted(linters)))
        return 0

    lint = LintRoller(**lintargs)
    linters_info = find_linters(lintargs["config_paths"], linters)

    result = None

    try:

        lint.read(linters_info["lint_paths"])

        if check_exclude_list:
            if len(lint.linters) > 1:
                print("error: specify a single linter to check with `-l/--linter`")
                return 1
            paths = lint.linters[0]["local_exclude"]

        if (
            not linters
            and not paths
            and os.getcwd() == lint.root
            and not (outgoing or workdir)
        ):
            print(
                "warning: linting the entire repo takes a long time, using --outgoing and "
                "--workdir instead. If you want to lint the entire repo, run `./mach lint .`"
            )
            # Setting the default values
            outgoing = "default"
            workdir = "all"

        # Always run bootstrapping, but return early if --setup was passed in.
        ret = lint.setup(virtualenv_manager=virtualenv_manager)
        if setup:
            return ret

        if linters_info["linters_not_found"] != []:
            raise NoValidLinter

        # run all linters
        result = lint.roll(
            paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs
        )
    except NoValidLinter as e:
        result = lint.result
        print(str(e))

    if edit and result.issues:
        edit_issues(result)
        result = lint.roll(result.issues.keys(), num_procs=num_procs)

    for every in linters_info["linters_not_found"]:
        result.failed_setup.add(every)

    if check_exclude_list:
        # Get and display all those paths in the exclude list which are
        # now green and can be safely removed from the list
        out = get_exclude_list_output(result, paths)
        print(out, file=sys.stdout)
        return result.returncode

    for formatter_name, path in formats:
        formatter = formatters.get(formatter_name)

        out = formatter(result)
        if out:
            fh = open(path, "w") if path else sys.stdout

            if not path and fh.encoding == "ascii":
                # If sys.stdout.encoding is ascii, printing output will fail
                # due to the stylish formatter's use of unicode characters.
                # Ideally the user should fix their environment by setting
                # `LC_ALL=C.UTF-8` or similar. But this is a common enough
                # problem that we help them out a little here by manually
                # encoding and writing to the stdout buffer directly.
                out += "\n"
                fh.buffer.write(out.encode("utf-8", errors="replace"))
                fh.buffer.flush()
            else:
                print(out, file=fh)

    return result.returncode
示例#32
0
def test_formatters(results, name):
    opts = EXPECTED[name]
    fmt = formatters.get(name, **opts['kwargs'])
    assert fmt(results) == opts['format']