Changeset View
Changeset View
Standalone View
Standalone View
utils_maintenance/clang_format_paths.py
| Show All 25 Lines | extensions_only_retab = ( | ||||
| ".sh", | ".sh", | ||||
| ) | ) | ||||
| ignore_files = { | ignore_files = { | ||||
| "intern/cycles/render/sobol.cpp", # Too heavy for clang-format | "intern/cycles/render/sobol.cpp", # Too heavy for clang-format | ||||
| } | } | ||||
| def compute_paths(paths): | def compute_paths(paths, use_default_paths): | ||||
| # Optionally pass in files to operate on. | # Optionally pass in files to operate on. | ||||
| if not paths: | if use_default_paths: | ||||
| paths = ( | paths = ( | ||||
| "intern/atomic", | "intern", | ||||
| "intern/audaspace", | |||||
| "intern/clog", | |||||
| "intern/cycles", | |||||
| "intern/dualcon", | |||||
| "intern/eigen", | |||||
| "intern/ffmpeg", | |||||
| "intern/ghost", | |||||
| "intern/glew-mx", | |||||
| "intern/guardedalloc", | |||||
| "intern/iksolver", | |||||
| "intern/locale", | |||||
| "intern/memutil", | |||||
| "intern/mikktspace", | |||||
| "intern/opencolorio", | |||||
| "intern/opensubdiv", | |||||
| "intern/openvdb", | |||||
| "intern/rigidbody", | |||||
| "intern/string", | |||||
| "intern/utfconv", | |||||
campbellbarton: I left out this part as it causes `intern/itasc` to be formatted.
If this is applied we should… | |||||
Done Inline ActionsNow the command --changed-only will error out, since "intern/string" doesn't exist which git diff will look for. ankitm: Now the command `--changed-only` will error out, since `"intern/string"` doesn't exist which… | |||||
| "source", | "source", | ||||
| "tests/gtests", | "tests/gtests", | ||||
| ) | ) | ||||
| if os.sep != "/": | if os.sep != "/": | ||||
| paths = [f.replace("/", os.sep) for f in paths] | paths = [f.replace("/", os.sep) for f in paths] | ||||
| return paths | return paths | ||||
| def source_files_from_git(paths): | def source_files_from_git(paths, changed_only): | ||||
| if changed_only: | |||||
| cmd = ("git", "diff", "HEAD", "--name-only", "-z", "--", *paths) | |||||
| else: | |||||
| cmd = ("git", "ls-tree", "-r", "HEAD", *paths, "--name-only", "-z") | cmd = ("git", "ls-tree", "-r", "HEAD", *paths, "--name-only", "-z") | ||||
| files = subprocess.check_output(cmd).split(b'\0') | files = subprocess.check_output(cmd).split(b'\0') | ||||
| return [f.decode('ascii') for f in files] | return [f.decode('ascii') for f in files] | ||||
| def convert_tabs_to_spaces(files): | def convert_tabs_to_spaces(files): | ||||
| for f in files: | for f in files: | ||||
| print("TabExpand", f) | print("TabExpand", f) | ||||
| with open(f, 'r', encoding="utf-8") as fh: | with open(f, 'r', encoding="utf-8") as fh: | ||||
| ▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | parser.add_argument( | ||||
| dest="expand_tabs", | dest="expand_tabs", | ||||
| default=False, | default=False, | ||||
| action='store_true', | action='store_true', | ||||
| help="Run a pre-pass that expands tabs " | help="Run a pre-pass that expands tabs " | ||||
| "(default=False)", | "(default=False)", | ||||
| required=False, | required=False, | ||||
| ) | ) | ||||
| parser.add_argument( | parser.add_argument( | ||||
| "--changed-only", | |||||
| dest="changed_only", | |||||
| default=False, | |||||
| action='store_true', | |||||
| help="Format only edited files, including the staged ones. " | |||||
| "Using this with \"paths\" will pick the edited files lying on those paths. " | |||||
| "(default=False)", | |||||
| required=False | |||||
| ) | |||||
| parser.add_argument( | |||||
| "paths", | "paths", | ||||
| nargs=argparse.REMAINDER, | nargs=argparse.REMAINDER, | ||||
| help="All trailing arguments are treated as paths." | help="All trailing arguments are treated as paths." | ||||
| ) | ) | ||||
| return parser | return parser | ||||
| Show All 13 Lines | if version > VERSION_MAX_RECOMMENDED: | ||||
| print( | print( | ||||
| "You may want to install clang-format-%d.%d, " | "You may want to install clang-format-%d.%d, " | ||||
| "or use the precompiled libs repository." % | "or use the precompiled libs repository." % | ||||
| (version[0], version[1]), | (version[0], version[1]), | ||||
| ) | ) | ||||
| args = argparse_create().parse_args() | args = argparse_create().parse_args() | ||||
| use_default_paths = not bool(args.paths) | use_default_paths = not (bool(args.paths) or bool(args.changed_only)) | ||||
| paths = compute_paths(args.paths) | paths = compute_paths(args.paths, use_default_paths) | ||||
| print("Operating on:") | print("Operating on%s" %(" only edited files under" if args.changed_only else "")) | ||||
Not Done Inline ActionsIn this case adding the strings is simpler. campbellbarton: In this case adding the strings is simpler. | |||||
| for p in paths: | for p in paths: | ||||
| print(" ", p) | print(" ", p) | ||||
| files = [ | files = [ | ||||
| f for f in source_files_from_git(paths) | f for f in source_files_from_git(paths, args.changed_only) | ||||
| if f.endswith(extensions) | if f.endswith(extensions) | ||||
| if f not in ignore_files | if f not in ignore_files | ||||
| ] | ] | ||||
| # Always operate on all cmake files (when expanding tabs and no paths given). | # Always operate on all cmake files (when expanding tabs and no paths given). | ||||
| files_retab = [ | files_retab = [ | ||||
| f for f in source_files_from_git((".",) if use_default_paths else paths) | f for f in source_files_from_git( | ||||
| (".",) if use_default_paths else paths, | |||||
| args.changed_only | |||||
| ) | |||||
| if f.endswith(extensions_only_retab) | if f.endswith(extensions_only_retab) | ||||
| if f not in ignore_files | if f not in ignore_files | ||||
| ] | ] | ||||
| if args.expand_tabs: | if args.expand_tabs: | ||||
| convert_tabs_to_spaces(files + files_retab) | convert_tabs_to_spaces(files + files_retab) | ||||
| clang_format(files) | clang_format(files) | ||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||
| main() | main() | ||||
I left out this part as it causes intern/itasc to be formatted.
If this is applied we should format all of intern as well.
Noting against this, just that it's outside the scope for this patch.