Exemplo n.º 1
0
    def run_flake8_and_mark(self, path, config):
        """Run flak8 and edit and fix errors

        Args:
            path (str): The path to target for the fix
            config (str): The path to a config to be passed to flake8

        Returns:
            str: the message stating what was performed
        """

        self.find_options(config)
        args = [
            'flake8',
            path,
            "--format=json",
            "--select=M5"  # only covers case of no mark present
        ]
        if config:
            args.append("--config={}".format(config))

        with self.patch_sys_argv(args), self.captured_stdout() as stdout:
            try:
                flake8_main()
            except SystemExit:
                pass  # This is raised by flake8

        flake8_output = json.loads(stdout.getvalue())
        self._backup_whole_path(path)
        return self.fix_it(flake8_output)
Exemplo n.º 2
0
def run_flake8(file_contents, extra_args=None):
    with open(str(TMP_DIR / "example.py"), 'w') as tempf:
        tempf.write(dedent(file_contents).strip() + '\n')

    orig_dir = os.getcwd()
    os.chdir(str(TMP_DIR))
    orig_args = sys.argv
    try:
        # Can't pass args to flake8 but can set to sys.argv
        sys.argv = [
            'flake8',
            '--jobs',
            '1',
            '--exit-zero',
            'example.py',
        ]
        if extra_args:
            sys.argv.extend(extra_args)

        # Run it
        with captured_stdout() as stdout:
            flake8_main()
        out = stdout.getvalue().strip()
        lines = out.split('\n')
        if lines[-1] == '':
            lines = lines[:-1]
        return lines
    finally:
        sys.argv = orig_args
        os.chdir(orig_dir)
    def run_flake8(self, extra_args=None):
        args = [
            'flake8',
            '--jobs', '1',
            '--exit-zero',
            '.',
        ]
        if extra_args:
            args.extend(extra_args)

        with self.tmpdir.as_cwd(), patch_sys_argv(args), captured_stdout() as stdout:
            flake8_main()
        full_output = stdout.getvalue()
        return Flake8Result(full_output)
Exemplo n.º 4
0
def run_flake8(paths):
    if flake8_main is None:
        return 0

    print('Running flake8 code linting')
    exit_code = 0  # flake8 < 3 path - 3+ always raises SystemExit
    try:
        original_argv = sys.argv
        sys.argv = ['flake8'] + paths
        flake8_main()
    except SystemExit as e:
        exit_code = e.code
    finally:
        sys.argv = original_argv

    print('flake8 failed' if exit_code else 'flake8 passed')
    return exit_code
Exemplo n.º 5
0
    def run_flake8(self, extra_args=None):
        args = [
            'flake8',
            '--jobs', '1',
            '--config', 'setup.cfg',
            '.',
        ]
        if extra_args:
            args.extend(extra_args)

        exit_code = 0  # In case --exit-zero has been passed
        with self.tmpdir.as_cwd(), patch_sys_argv(args), captured_stdout() as stdout:
            try:
                flake8_main()
            except SystemExit as exc:
                exit_code = exc.code
        full_output = stdout.getvalue()
        return Flake8Result(full_output, exit_code)
Exemplo n.º 6
0
def run_flake8(paths):
    if flake8_main is None:
        return 0

    print("Running flake8 code linting")
    try:
        original_argv = sys.argv
        sys.argv = ["flake8"] + paths
        flake8_main()
    except SystemExit as e:  # Always raised
        exit_code = e.code
        sys.argv = original_argv

    if exit_code:
        print("flake8 failed")
    else:
        print("flake8 passed")
    return int(exit_code)
Exemplo n.º 7
0
def main(argv):
    code = DRIVER_TEMPLATE.substitute(
        module="module",
        map_fn="map_fn",
        reduce_fn="reduce_fn",
        combine_fn="combine_fn",
        combiner_wp="None",
    )
    fd = None
    try:
        fd, fn = tempfile.mkstemp(suffix=".py", text=True)
        os.write(fd, code.encode("utf-8"))
    finally:
        if fd is not None:
            os.close(fd)
    flake8_argv = [fn] + [_ for _ in argv if _.startswith("-")]
    try:
        flake8_main(flake8_argv)
    finally:
        os.remove(fn)
Exemplo n.º 8
0
def run_flake8(paths):
    if flake8_main is None:
        return 0

    print('Running flake8 code linting')
    exit_code = 0   # flake8 < 3 path - 3+ always raises SystemExit
    try:
        original_argv = sys.argv
        sys.argv = ['flake8'] + paths
        flake8_main()
    except SystemExit as e:
        exit_code = e.code
    finally:
        sys.argv = original_argv

    if exit_code:
        print('flake8 failed')
    else:
        print('flake8 passed')
    return int(exit_code)
Exemplo n.º 9
0
def main(argv):
    code = DRIVER_TEMPLATE.substitute(
        module="module",
        map_fn="map_fn",
        reduce_fn="reduce_fn",
        combine_fn="combine_fn",
        combiner_wp="None",
    )
    fd = None
    try:
        fd, fn = tempfile.mkstemp(suffix=".py", text=True)
        os.write(fd, code.encode("utf-8"))
    finally:
        if fd is not None:
            os.close(fd)
    flake8_argv = [fn] + [_ for _ in argv if _.startswith("-")]
    try:
        flake8_main(flake8_argv)
    finally:
        os.remove(fn)
Exemplo n.º 10
0
def flake8_code(code):
    tmpdir = mkdtemp()
    with open(os.path.join(tmpdir, 'example.py'), 'w') as tempf:
        tempf.write(code)

    orig_dir = os.getcwd()
    os.chdir(tmpdir)
    orig_args = sys.argv

    try:
        sys.argv = ['flake8', '--jobs', '1', '--exit-zero', 'example.py']
        with captured_stdout() as stdout:
            flake8_main()
        out = stdout.getvalue().strip()
        lines = out.split('\n')
        if lines[-1] == '':
            lines = lines[:-1]
        return lines
    finally:
        sys.argv = orig_args
        os.chdir(orig_dir)
        shutil.rmtree(tmpdir)
Exemplo n.º 11
0
def main():
    patch_flake8()

    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    return flake8_main()
#!/usr/bin/env python3
# flake8: noqa
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import pytest
import sys
from flake8.main.cli import main as flake8_main
from mypy.main import main as mypy_main
DIR_ROOT = os.path.abspath(os.path.dirname(sys.argv[0]))
sys.path.append(os.path.join(DIR_ROOT))

if __name__ == '__main__':
    print("### Checking PEP-8 code adherence ###")
    try:
        flake8_main()
    except SystemExit as err:
        success_flake8 = (not err.code)

    print("### Verifying type annotations and type coherence ###")
    try:
        # This requires packages and modules to be well-defined (i.e., have __init__.py)
        # Which is a useful way to keep type-checking out of messy experimental folders
        # with an opt-in mechanism
        mypy_main(
            None,
            stdout=sys.stdout,
            stderr=sys.stderr,
            args=[
                "--ignore-missing-imports", "--strict-optional",
                "--incremental", "-p", "eai_graph_tools"