예제 #1
0
def cformat(cli):
    """Format C code according to QMK's style.
    """
    # Empty array for files
    files = []
    # Core directories for formatting
    core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core', 'platforms']
    ignores = [
        'tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios'
    ]
    # Find the list of files to format
    if cli.args.files:
        files.extend(normpath(file) for file in cli.args.files)
    # If -a is specified
    elif cli.args.all_files:
        all_files = c_source_files(core_dirs)
        # The following statement checks each file to see if the file path is in the ignored directories.
        files.extend(file for file in all_files
                     if not any(i in str(file) for i in ignores))
    # No files specified & no -a flag
    else:
        base_args = ['git', 'diff', '--name-only', cli.args.base_branch]
        out = subprocess.run(base_args + core_dirs,
                             check=True,
                             stdout=subprocess.PIPE)
        changed_files = filter(None, out.stdout.decode('UTF-8').split('\n'))
        filtered_files = [
            normpath(file) for file in changed_files
            if not any(i in file for i in ignores)
        ]
        files.extend(file for file in filtered_files
                     if file.exists() and file.suffix in ['.c', '.h', '.cpp'])

    # Run clang-format on the files we've found
    cformat_run(files, cli.args.all_files)
예제 #2
0
def cformat(cli):
    """Format C code according to QMK's style.
    """
    # Find the list of files to format
    if cli.args.files:
        files = list(filter_files(cli.args.files))

        if not files:
            cli.log.error('No C files in filelist: %s',
                          ', '.join(map(str, cli.args.files)))
            exit(0)

        if cli.args.all_files:
            cli.log.warning('Filenames passed with -a, only formatting: %s',
                            ','.join(map(str, files)))

    elif cli.args.all_files:
        all_files = c_source_files(core_dirs)
        # The following statement checks each file to see if the file path is in the ignored directories.
        files = [
            file for file in all_files
            if not any(i in str(file) for i in ignored)
        ]

    else:
        git_diff_cmd = [
            'git', 'diff', '--name-only', cli.args.base_branch, *core_dirs
        ]
        git_diff = cli.run(git_diff_cmd)

        if git_diff.returncode != 0:
            cli.log.error("Error running %s", git_diff_cmd)
            print(git_diff.stderr)
            return git_diff.returncode

        files = []

        for file in git_diff.stdout.strip().split('\n'):
            if not any([file.startswith(ignore) for ignore in ignored]):
                if path.exists(file) and file.split(
                        '.')[-1] in c_file_suffixes:
                    files.append(file)

    # Sanity check
    if not files:
        cli.log.error(
            'No changed files detected. Use "qmk cformat -a" to format all files'
        )
        return False

    # Run clang-format on the files we've found
    if cli.args.dry_run:
        return not find_diffs(files)
    else:
        return cformat_run(files)
예제 #3
0
파일: c.py 프로젝트: zsa/qmk_firmware
def format_c(cli):
    """Format C code according to QMK's style.
    """
    # Find the list of files to format
    if cli.args.files:
        files = list(filter_files(cli.args.files, cli.args.core_only))

        if not files:
            cli.log.error('No C files in filelist: %s',
                          ', '.join(map(str, cli.args.files)))
            exit(0)

        if cli.args.all_files:
            cli.log.warning('Filenames passed with -a, only formatting: %s',
                            ','.join(map(str, files)))

    elif cli.args.all_files:
        all_files = c_source_files(core_dirs)
        files = list(filter_files(all_files, True))

    else:
        git_diff_cmd = [
            'git', 'diff', '--name-only', cli.args.base_branch, *core_dirs
        ]
        git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)

        if git_diff.returncode != 0:
            cli.log.error("Error running %s", git_diff_cmd)
            print(git_diff.stderr)
            return git_diff.returncode

        changed_files = git_diff.stdout.strip().split('\n')
        files = list(filter_files(changed_files, True))

    # Sanity check
    if not files:
        cli.log.error(
            'No changed files detected. Use "qmk format-c -a" to format all core files'
        )
        return False

    # Run clang-format on the files we've found
    if cli.args.dry_run:
        return not find_diffs(files)
    else:
        return cformat_run(files)