<commit_before>import os
import subprocess

from . import base
from . import filetype
from . import status


class CompileCheck(base.PerFileCheck):
    COMPILECOMMAND = []
    ONLY_IF_OLDFILE_COMPILES = True

    def checkOldFile(self, changedFile):
        with base.TempDir() as dirname:
            tempfilename = os.path.join(
                dirname,
                os.path.basename(changedFile.filename))
            with open(tempfilename, "w") as f:
                f.write("\n".join(changedFile.oldlines))
            cmd = list(self.COMPILECOMMAND)
            cmd.append(tempfilename)
            try:
                subprocess.check_output(cmd, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError:
                return False
            return True

    def checkFile(self, changedFile):
        if changedFile.status != status.ADDED:
            if (self.ONLY_IF_OLDFILE_COMPILES and
                    not self.checkOldFile(changedFile)):
                # nothing to check, old file didn't compile
                return []

        cmd = list(self.COMPILECOMMAND)
        cmd.append(changedFile.filename)
        try:
            subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as calledprocesserror:
            return [base.CheckError(changedFile, self.__class__,
                                    calledprocesserror.output)]
        except OSError as e:
            error = (
                "Trying to execute:\n%s\n. This failed (%s), possibly "
                "executable is not installed on your system." % (
                    repr(cmd)[1:-1], str(e)))
            return [base.CheckError(changedFile, self.__class__, error)]
        return []


class PythonCompileCheck(CompileCheck):
    INTERESTED_IN_FILETYPES = [filetype.PYTHON]
    COMPILECOMMAND = ['python', '-m' 'py_compile']


class Pep8Check(CompileCheck):
    INTERESTED_IN_FILETYPES = [filetype.PYTHON]
    COMPILECOMMAND = ['flake8']

    def check_file_get_error_numbers(self, filename):
        cmd = list(self.COMPILECOMMAND) + [filename]<commit_msg>bugfix copy setup.cfg file before pep8 comparing<commit_after>import os
import subprocess

from . import base
from . import filetype
from . import status


class CompileCheck(base.PerFileCheck):
    COMPILECOMMAND = []
    ONLY_IF_OLDFILE_COMPILES = True

    def prepareOldFileDir(self, dirname):
        return dirname

    def checkOldFile(self, changedFile):
        with base.TempDir() as dirname:
            dirname = self.prepareOldFileDir(dirname)
            tempfilename = os.path.join(
                dirname,
                os.path.basename(changedFile.filename))
            with open(tempfilename, "w") as f:
                f.write("\n".join(changedFile.oldlines))
            cmd = list(self.COMPILECOMMAND)
            cmd.append(tempfilename)
            try:
                subprocess.check_output(cmd, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError:
                return False
            return True

    def checkFile(self, changedFile):
        if changedFile.status != status.ADDED:
            if (self.ONLY_IF_OLDFILE_COMPILES and
                    not self.checkOldFile(changedFile)):
                # nothing to check, old file didn't compile
                return []

        cmd = list(self.COMPILECOMMAND)
        cmd.append(changedFile.filename)
        try:
            subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as calledprocesserror:
            return [base.CheckError(changedFile, self.__class__,
                                    calledprocesserror.output)]
        except OSError as e:
            error = (
                "Trying to execute:\n%s\n. This failed (%s), possibly "
                "executable is not installed on your system." % (
                    repr(cmd)[1:-1], str(e)))
            return [base.CheckError(changedFile, self.__class__, error)]
        return []


class PythonCompileCheck(CompileCheck):
    INTERESTED_IN_FILETYPES = [filetype.PYTHON]
    COMPILECOMMAND = ['python', '-m' 'py_compile']


class Pep8Check(CompileCheck):
    INTERESTED_IN_FILETYPES = [filetype.PYTHON]
    COMPILECOMMAND = ['flake8']

    def prepareOldFileDir(self, dirname):
        if os.exists("setup.cfg"):
            subprocess.check_call([
                "cp", "setup.cfg", dirname])
        return dirname

    def check_file_get_error_numbers(self, filename):
        cmd = list(self.COMPILECOMMAND) + [filename]
{'fraction_non_alphanumeric': 0.09112709832134293, 'fraction_numerical': 0.001962066710268149, 'mean_word_length': 2.151098901098901, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '869356', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1271, 'n_words': 275}