def run_external_checker(self):
        # Check the first two lines of the file, and scan for certain
        # specific keywords indicating possible special treatment for
        # this file.
        python_fragment_p = False
        with open(self.filename) as f:
            for lineno in (1, 2):
                line = f.readline()
                if line and re.match('^# No_Style_Check$', line) is not None:
                    # ??? VERBOSE...
                    return
                elif line and re.match('^# Style_Check:Python_Fragment',
                                       line, re.I) is not None:
                    python_fragment_p = True

        try:
            p = Run(['pycodestyle',
                     '--config=' + os.path.join(get_prefix_dir(),
                                                'etc', 'pycodestyle.cfg'),
                     self.filename])
            if p.status != 0 or p.out:
                return p.out
        except OSError as e:
            return 'Failed to run pycodestyle: %s' % e

        if not python_fragment_p and self.__run_pyflakes():
            try:
                p = Run(['pyflakes', self.filename])
                if p.status != 0 or p.out:
                    return p.out
            except OSError as e:
                return 'Failed to run pyflakes: %s' % e
Beispiel #2
0
    def run_external_checker(self):
        # Check the first two lines of the file, and scan for certain
        # specific keywords indicating possible special treatment for
        # this file.
        python_fragment_p = False
        with open(self.filename) as f:
            for _unused_lineno in (1, 2):
                line = f.readline()
                if line and re.match("^# No_Style_Check$", line) is not None:
                    # ??? VERBOSE...
                    return
                elif (line and re.match("^# Style_Check:Python_Fragment", line,
                                        re.I) is not None):
                    python_fragment_p = True

        extend_ignore = list(EXTEND_IGNORE_LIST_ALL)
        if python_fragment_p or self.__incomplete_python_file_p():
            extend_ignore.extend(EXTEND_IGNORE_LIST_INCOMPLETE_PYTHON)

        p = Run([
            sys.executable,
            "-m",
            "flake8",
            f"--max-line-length={MAX_LINE_LENGTH}",
            "--extend-ignore={}".format(",".join(extend_ignore)),
            self.filename,
        ])
        if p.status != 0 or p.out:
            return p.out
Beispiel #3
0
def get_file_type(filename):
    """Run the "file" command on filename and return its output.

    :param filename: The name of the file on which to run the "file"
        command.
    :type filename: str
    """
    try:
        p = Run(["file", filename])
        if p.status != 0:
            raise FileCheckerError(
                "%s returned nonzero (%d):" %
                (p.command_line_image(), p.status), p.out)
    except OSError as e:
        raise FileCheckerError("Failed to run `file %s': %s" % (filename, e))

    return p.out
Beispiel #4
0
 def run_external_checker(self):
     try:
         p = Run(["/bin/sh", "-n", self.filename])
         if p.status != 0:
             return p.out
     except OSError as e:  # pragma: no cover (see below)
         # Can only really happen if sh is not installed on the host
         # machine. Near-zero probability, but we keep this handler
         # so as to generate an error message rather than a traceback.
         return "Failed to run sh: %s" % e
Beispiel #5
0
 def run_external_checker(self):
     try:
         p = Run(['/usr/bin/perl', '-c', self.filename])
         if p.status != 0:
             return p.out
     except OSError as e:  # pragma: no cover (see below)
         # Can only really happen if perl is not installed on the host
         # machine. Fairly low probability, but we keep this handler
         # so as to generate an error message rather than a traceback.
         return 'Failed to run perl: %s' % e
Beispiel #6
0
    def run_external_checker(self):
        env = {
            # This sets the Java max heap size manually. By default, Java
            # allocates a value that causes a heap memory error on kwai.
            # For N909-043.
            "JAVA_ARGS": "-Xmx1024m"
        }

        # Create a properties, file, as we as can't pass -D arguments to
        # checkstyle without modifying it.
        (prop_fd, prop_filename) = mkstemp("tmp-style_checker-")

        try:
            with closing(os.fdopen(prop_fd, "w")) as f:
                f.write("basedir=%s\n" %
                        os.path.dirname(os.path.abspath(self.filename)))

            p = Run(
                [
                    "checkstyle",
                    "-p",
                    prop_filename,
                    "-c",
                    self.style_filename,
                    "-f",
                    "plain",
                    self.filename,
                ],
                env=env,
                ignore_environ=False,
            )
            if p.status != 0:
                # Return the program's output minus a few useless lines.
                out = "\n".join([
                    line for line in p.out.splitlines()
                    if not re.match("(Starting audit|Audit done)", line)
                ])
                return out
        except OSError as e:
            return "Failed to run checkstyle: %s" % e

        finally:
            os.unlink(prop_filename)
Beispiel #7
0
    def run_external_checker(self):
        # For these kinds of files, the external check should be
        # disabled if there is a "No_Style_Check" comment starting
        # either the first or the second line.
        with open(self.filename) as f:
            for lineno in (1, 2):
                line = f.readline()
                if line and re.match('^// No_Style_Check$', line) is not None:
                    # ??? VERBOSE...
                    return
        jslint_cmd = ['gjslint']
        if not any(x in self.config.module_name
                   for x in self.MODULES_WITH_JSDOC):
            jslint_cmd.append('--nojsdoc')
        jslint_cmd.append(self.filename)

        try:
            p = Run(jslint_cmd)
            if p.status != 0:
                return p.out
        except OSError as e:
            return 'Failed to run gjslint: %s' % e
Beispiel #8
0
    def run_external_checker(self):
        env = {
            # This sets the Java max heap size manually. By default, Java
            # allocates a value that causes a heap memory error on kwai.
            # For N909-043.
            'JAVA_ARGS': '-Xmx1024m'
        }

        # Create a properties, file, as we as can't pass -D arguments to
        # checkstyle without modifying it.
        (prop_fd, prop_filename) = mkstemp('tmp-style_checker-')

        try:
            os.write(
                prop_fd, 'basedir=%s\n' %
                os.path.dirname(os.path.abspath(self.filename)))
            os.close(prop_fd)

            p = Run([
                'checkstyle', '-p', prop_filename, '-c', self.style_filename,
                '-f', 'plain', self.filename
            ],
                    env=env,
                    ignore_environ=False)
            if p.status != 0:
                # Return the program's output minus a few useless lines.
                out = '\n'.join([
                    line for line in p.out.splitlines()
                    if not re.match('(Starting audit|Audit done)', line)
                ])
                return out
        except OSError as e:
            return 'Failed to run checkstyle: %s' % e

        finally:
            os.unlink(prop_filename)
Beispiel #9
0
    def run_external_checker(self):
        file_type = self.file_type

        cmd = [
            'gcc',
            '-c',
            '-gnats',
            '-gnatm20',
            # Set "-x ada" so that we can style check Ada sources even
            # if their extension is not automatically identified by GCC.
            '-x',
            'ada',
        ]

        # Base options: use GNAT style

        if file_type in (RT_SPEC, RT_BODY):
            # Set GNAT mode for runtime files only (changes legality and
            # semantics, and sets more restrictive style rules).
            # Note: This also enables language extensions.
            cmd.append('-gnatg')
        else:
            # Enable GNAT style checks, GNAT warnings, and treat warnings
            # and style messages as errors.
            cmd.extend(['-gnatyg', '-gnatw.g', '-gnatwe'])

        if 'gnatx' in self.config.style_checks_options:
            cmd.append('-gnatX')

        # Set language version: by default enable Ada 2012, except for
        # compiler units (which must be Ada 95 for bootstrap). Can be
        # overridden using flags gnat95 and gnat05. (Note that flag
        # "gnat12" has no effect since this is now the default).

        if file_type in (RT_SPEC, RT_BODY):
            # Language version already set by -gnatg for runtime units
            pass
        elif 'gnatx' in self.config.style_checks_options:
            # Language version already set by -gnatX
            pass
        elif file_type == COMPILER_CORE or \
                'gnat95' in self.config.style_checks_options:
            cmd.append('-gnat95')
        elif 'gnat05' in self.config.style_checks_options:
            cmd.append('-gnat05')
        else:
            cmd.append('-gnat12')

        # Set preprocessing data file.
        cmd.append('-gnatep=' + self.config.ada_preprocessing_filename)

        cmd.append(self.filename)

        # Run GCC with some some critical environment variables unset.
        BANNED_ENV_VARS = ('GCC_EXEC_PREFIX', 'GCC_INSTALL_DIRECTORY',
                           'GCC_INCLUDE_DIR', 'GCC_ROOT', 'GNAT_ROOT',
                           'BINUTILS_ROOT')
        gcc_env = {
            var_name: value
            for var_name, value in os.environ.items()
            if var_name not in BANNED_ENV_VARS
        }

        try:
            log_info('Running: %s' % command_line_image(cmd), 2)
            p = Run(cmd, env=gcc_env)
            if p.status != 0 or p.out:
                return p.out
        except OSError as e:
            return 'Failed to run %s: %s' % (cmd[0], e)
Beispiel #10
0
    def run_external_checker(self):
        file_type = self.file_type

        cmd = [
            "gcc",
            "-c",
            "-gnats",
            "-gnatm20",
            # Set "-x ada" so that we can style check Ada sources even
            # if their extension is not automatically identified by GCC.
            "-x",
            "ada",
        ]

        # Base options: use GNAT style

        if file_type in (RT_SPEC, RT_BODY):
            # Set GNAT mode for runtime files only (changes legality and
            # semantics, and sets more restrictive style rules).
            #
            # Note: -gnatg normally also enables language extensions
            # (i.e. -gnatX), but it only does so when it recognizes
            # the unit's filename. Since we also store implementation
            # variations of the same runtime unit inside files whose name
            # do not always match the normal filename naming scheme
            # (e.g. "a-except__zfp.adb"), we force -gnatX to ensure
            # extensions are consistently used across all GNAT runtime
            # sources.
            #
            # Note also that the compiler hardcodes the language version
            # it uses when compiling the runtime units, so there is no point
            # in trying to force a different version with a command-line
            # switch, the compiler just ignores them (see T618-047 for
            # confirmation of that).
            cmd.extend(["-gnatg", "-gnatX"])
        else:
            # Enable GNAT style checks, GNAT warnings, and treat warnings
            # and style messages as errors.
            cmd.extend(["-gnatyg", "-gnatw.g", "-gnatwe"])

        # The "gnat" repository needs specific treatment because we want
        # to allow building GNAT without requiring too recent a compiler.

        if file_type in (RT_SPEC, RT_BODY):
            # Language version already set by -gnatg for runtime units
            pass
        elif file_type == COMPILER_CORE:
            cmd.append("-gnat12")

        # For all other repositories, allow Ada 2012 by default, except
        # explicity overriden by the repository.

        elif "gnatx" in self.config.style_checks_options:
            cmd.append("-gnatX")
        elif "gnat95" in self.config.style_checks_options:
            cmd.append("-gnat95")
        elif "gnat05" in self.config.style_checks_options:
            cmd.append("-gnat05")
        elif "gnat12" in self.config.style_checks_options:
            cmd.append("-gnat12")
        elif "gnat2020" in self.config.style_checks_options:
            cmd.append("-gnat2020")
        else:
            cmd.append("-gnat2020")

        # Set preprocessing data file.
        cmd.append("-gnatep=" + self.config.ada_preprocessing_filename)

        cmd.append(self.filename)

        # Run GCC with some some critical environment variables unset.
        BANNED_ENV_VARS = (
            "GCC_EXEC_PREFIX",
            "GCC_INSTALL_DIRECTORY",
            "GCC_INCLUDE_DIR",
            "GCC_ROOT",
            "GNAT_ROOT",
            "BINUTILS_ROOT",
        )
        gcc_env = {
            var_name: value
            for var_name, value in os.environ.items()
            if var_name not in BANNED_ENV_VARS
        }

        try:
            log_info("Running: %s" % command_line_image(cmd), 2)
            p = Run(cmd, env=gcc_env)
            if p.status != 0 or p.out:
                return p.out
        except OSError as e:
            return "Failed to run %s: %s" % (cmd[0], e)