File: fix_formatting.py

package info (click to toggle)
widelands 2%3A1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684,084 kB
  • sloc: cpp: 196,737; ansic: 19,395; python: 8,515; sh: 1,734; xml: 700; makefile: 46; lisp: 25
file content (164 lines) | stat: -rwxr-xr-x 5,602 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""The code base had inconsistent usage of tabs/spaces for indenting in Lua.

files. Spaces were more prominent - and I prefer them over tabs. So I wrote
this small script to fix leading tabs in Lua files to spaces.

It also saves files in unix file endings ("\r\n") and strips empty lines at the
end of files and whitespace characters at the end of lines.

After fixing the Lua tabs, this script also executes clang-format over the src
directory and pyformat over the utils directory.

"""

import argparse
import os
import re
import sys
from subprocess import check_call, CalledProcessError
from file_utils import read_text_file, write_text_file, find_files

import autoflake
import autopep8
import unify

LEADING_TABS = re.compile(r'^\s*\t+\s*')
SPACES_PER_TAB = 3


def parse_args():
    p = argparse.ArgumentParser(
        description='Fix common whitespace errors in Lua files, run clang-format'
        ' over the code base and pyformat over the utils directory.'
        ' Recurses over all relevant files.')

    p.add_argument('-c', '--c++', action='store_true',
                   help='Format C++ files only')
    p.add_argument('-l', '--lua', action='store_true',
                   help='Format Lua files only')
    p.add_argument('-p', '--python', action='store_true',
                   help='Format Python files only')
    p.add_argument('-d', '--dir', action='store',
                   help='Format the given directory and its subdirectories only')

    return vars(p.parse_args())

def pyformatters():
    """Return list of code formatters."""
    yield lambda code: autoflake.fix_code(
        code,
        remove_all_unused_imports=True,
        remove_unused_variables=True)

    yield autopep8.fix_code
    yield unify.format_code

def pyformat_file(filename):
    """Run format_code() on a file.

    Return True if the new formatting differs from the original.
    Based on pyformat https://github.com/myint/pyformat @ 7293e4d

    """
    encoding = autopep8.detect_encoding(filename)
    with autopep8.open_with_encoding(filename,
                                     encoding=encoding) as input_file:
        source = input_file.read()

    if not source:
        return False

    for fix in pyformatters():
        formatted_source = fix(source)

    if source != formatted_source:
        with autopep8.open_with_encoding(filename, mode='w',
                                         encoding=encoding) as output_file:
            output_file.write(formatted_source)
        return True

    return False


def main():
    args = parse_args()
    format_cplusplus = args['c++'] or not (args['lua'] or args['python'])
    format_lua = args['lua'] or not (args['c++'] or args['python'])
    format_python = args['python'] or not (args['c++'] or args['lua'])

    if not os.path.isdir('src') or not os.path.isdir('utils'):
        print('CWD is not the root of the repository.')
        return 1

    if format_cplusplus:
        directory = args['dir']
        if not directory:
            directory = './src'
        sys.stdout.write('\nFormatting C++ in directory: ' + directory + ' ')
        for filename in find_files(directory, ['.cc', '.h']):
            if 'third_party' in filename:
                continue
            sys.stdout.write('.')
            sys.stdout.flush()
            check_call(['clang-format', '-i', filename])
            check_call(['git', 'add', '--renormalize', filename])
        print(' done.')

    if format_lua:
        directories = set()
        if args['dir']:
            directories.add(args['dir'])
        else:
            directories = {'./data', './test'}
        for directory in directories:
            sys.stdout.write(
                '\nFixing Lua tabs in directory: ' + directory + ' ')
            for filename in find_files(directory, ['.lua']):
                sys.stdout.write('.')
                sys.stdout.flush()
                lines = read_text_file(filename).strip().split('\n')
                new_lines = []
                for line in lines:
                    m = LEADING_TABS.match(line)
                    if m is not None:
                        line = line[m.start():m.end()].expandtabs(
                            SPACES_PER_TAB) + line[m.end():]
                    new_lines.append(line.rstrip() + '\n')
                write_text_file(filename, ''.join(new_lines))
                check_call(['git', 'add', '--renormalize', filename])
            print(' done.')

    if format_python:
        directories = set()
        if args['dir']:
            directories.add(args['dir'])
        else:
            directories = {'./utils', './cmake/codecheck'}
        for directory in directories:
            sys.stdout.write(
                '\nFormatting Python scripts in directory: ' + directory + ' ')
            python_files = find_files(directory, ['.py'])
            try:
                check_call(['docformatter', '-i', *python_files])
            except CalledProcessError as e:
                # Return code 3 means there are changes. That's OK for us, next
                # step will pick them up
                if e.returncode != 3 :
                    raise
            for filename in find_files(directory, ['.py']):
                sys.stdout.write('.')
                sys.stdout.flush()
                pyformat_file(filename)
                check_call(['git', 'add', '--renormalize', filename])
            print(' done.')

    print('Formatting finished.')
    return 0


if __name__ == '__main__':
    sys.exit(main())