Beispiel #1
0
def get_py_files_in_pr(repo, pr_number, exclude=[]):
    files = get_files_involved_in_pr(repo, pr_number)
    for file in list(files.keys()):
        if file[-3:] != ".py" or utils.filename_match(file, exclude):
            del files[file]

    return files
Beispiel #2
0
def get_py_files_in_pr(repo, pr_number, exclude=None):
    if exclude is None:
        exclude = []
    files = get_files_involved_in_pr(repo, pr_number)
    for diff_file in list(files.keys()):
        if diff_file[-3:] != ".py" or utils.filename_match(diff_file, exclude):
            del files[diff_file]

    return files
Beispiel #3
0
def autopep8ify(ghrequest, config):
    # Run pycodestyle
    r = utils.query_request(ghrequest.diff_url)

    ## All the python files with additions
    patch = unidiff.PatchSet(r.content.splitlines(), encoding=r.encoding)

    # A dictionary with filename paired with list of new line numbers
    py_files = {}

    linter = config["scanner"]["linter"]
    files_to_exclude = config[linter]["exclude"]

    for patchset in patch:
        if patchset.target_file[-3:] == '.py':
            py_file = patchset.target_file[1:]
            if utils.filename_match(py_file, files_to_exclude):
                continue
            py_files[py_file] = []
            for hunk in patchset:
                for line in hunk.target_lines():
                    if line.is_added:
                        py_files[py_file].append(line.target_line_no)

    # Ignore errors and warnings specified in the config file
    to_ignore = ",".join(config["pycodestyle"]["ignore"])
    arg_to_ignore = ""
    if len(to_ignore) > 0:
        arg_to_ignore = "--ignore " + to_ignore

    for py_file in py_files:
        filename = py_file[1:]
        query = f"https://raw.githubusercontent.com/{ghrequest.repository}/{ghrequest.sha}/{py_file}"
        r = utils.query_request(query)
        with open("file_to_fix.py", 'w+', encoding=r.encoding) as file_to_fix:
            file_to_fix.write(r.text)

        cmd = f'autopep8 file_to_fix.py {arg_to_ignore}'
        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        stdout, _ = proc.communicate()
        ghrequest.results[filename] = stdout.decode(r.encoding)

        os.remove("file_to_fix.py")