|
25 | 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26 | 26 | # THE SOFTWARE. |
27 | 27 |
|
28 | | -# This is based on tools/codeformat.py from the main micropython/micropython |
29 | | -# repository but without support for .c/.h files. |
| 28 | +# This is just a wrapper around running ruff format, so that code formatting can be |
| 29 | +# invoked in the same way as in the main repo. |
30 | 30 |
|
31 | | -import argparse |
32 | | -import glob |
33 | | -import itertools |
34 | 31 | import os |
35 | | -import re |
36 | 32 | import subprocess |
37 | 33 |
|
38 | | -# Relative to top-level repo dir. |
39 | | -PATHS = [ |
40 | | - "**/*.py", |
41 | | -] |
42 | | - |
43 | | -EXCLUSIONS = [] |
44 | | - |
45 | 34 | # Path to repo top-level dir. |
46 | 35 | TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
47 | 36 |
|
48 | | -PY_EXTS = (".py",) |
49 | | - |
50 | | - |
51 | | -def list_files(paths, exclusions=None, prefix=""): |
52 | | - files = set() |
53 | | - for pattern in paths: |
54 | | - files.update(glob.glob(os.path.join(prefix, pattern), recursive=True)) |
55 | | - for pattern in exclusions or []: |
56 | | - files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern))) |
57 | | - return sorted(files) |
58 | | - |
59 | 37 |
|
60 | 38 | def main(): |
61 | | - cmd_parser = argparse.ArgumentParser(description="Auto-format Python files.") |
62 | | - cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output") |
63 | | - cmd_parser.add_argument( |
64 | | - "-f", |
65 | | - action="store_true", |
66 | | - help="Filter files provided on the command line against the default list of files to check.", |
67 | | - ) |
68 | | - cmd_parser.add_argument("files", nargs="*", help="Run on specific globs") |
69 | | - args = cmd_parser.parse_args() |
70 | | - |
71 | | - # Expand the globs passed on the command line, or use the default globs above. |
72 | | - files = [] |
73 | | - if args.files: |
74 | | - files = list_files(args.files) |
75 | | - if args.f: |
76 | | - # Filter against the default list of files. This is a little fiddly |
77 | | - # because we need to apply both the inclusion globs given in PATHS |
78 | | - # as well as the EXCLUSIONS, and use absolute paths |
79 | | - files = {os.path.abspath(f) for f in files} |
80 | | - all_files = set(list_files(PATHS, EXCLUSIONS, TOP)) |
81 | | - if args.v: # In verbose mode, log any files we're skipping |
82 | | - for f in files - all_files: |
83 | | - print("Not checking: {}".format(f)) |
84 | | - files = list(files & all_files) |
85 | | - else: |
86 | | - files = list_files(PATHS, EXCLUSIONS, TOP) |
87 | | - |
88 | | - # Extract files matching a specific language. |
89 | | - def lang_files(exts): |
90 | | - for file in files: |
91 | | - if os.path.splitext(file)[1].lower() in exts: |
92 | | - yield file |
93 | | - |
94 | | - # Run tool on N files at a time (to avoid making the command line too long). |
95 | | - def batch(cmd, files, N=200): |
96 | | - while True: |
97 | | - file_args = list(itertools.islice(files, N)) |
98 | | - if not file_args: |
99 | | - break |
100 | | - subprocess.check_call(cmd + file_args) |
101 | | - |
102 | | - # Format Python files with black. |
103 | | - command = ["black", "--fast", "--line-length=99"] |
104 | | - if args.v: |
105 | | - command.append("-v") |
106 | | - else: |
107 | | - command.append("-q") |
108 | | - batch(command, lang_files(PY_EXTS)) |
| 39 | + command = ["ruff", "format", "."] |
| 40 | + subprocess.check_call(command, cwd=TOP) |
109 | 41 |
|
110 | 42 |
|
111 | 43 | if __name__ == "__main__": |
|
0 commit comments