diff --git a/tools/utils_maintenance/autopep8_format_paths.py b/tools/utils_maintenance/autopep8_format_paths.py index 42e319ca209..18cfd83e14a 100755 --- a/tools/utils_maintenance/autopep8_format_paths.py +++ b/tools/utils_maintenance/autopep8_format_paths.py @@ -30,6 +30,14 @@ from typing import ( VERSION_MIN = (1, 6, 0) VERSION_MAX_RECOMMENDED = (1, 6, 0) AUTOPEP8_FORMAT_CMD = "autopep8" +AUTOPEP8_FORMAT_DEFAULT_ARGS = ( + # Operate on all directories recursively. + "--recursive", + # Update the files in-place. + "--in-place", + # Auto-detect the number of jobs to use. + "--jobs=0", +) BASE_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..")) os.chdir(BASE_DIR) @@ -119,13 +127,9 @@ def autopep8_ensure_version(autopep8_format_cmd_argument: str) -> Optional[Tuple def autopep8_format(files: List[str]) -> bytes: cmd = [ AUTOPEP8_FORMAT_CMD, - # Operate on all directories recursively. - "--recursive", - # Update the files in-place. - "--in-place", - # Auto-detect the number of jobs to use. - "--jobs=0", - ] + files + *AUTOPEP8_FORMAT_DEFAULT_ARGS, + *files + ] # Support executing from the module directory because Blender does not distribute the command. if cmd[0].endswith(".py"): @@ -134,6 +138,23 @@ def autopep8_format(files: List[str]) -> bytes: return subprocess.check_output(cmd, stderr=subprocess.STDOUT) +def autopep8_format_no_subprocess(files: List[str]) -> None: + cmd = [ + AUTOPEP8_FORMAT_CMD, + *AUTOPEP8_FORMAT_DEFAULT_ARGS, + *files + ] + + if not os.path.exists(AUTOPEP8_FORMAT_CMD): + print("Unable to find:", AUTOPEP8_FORMAT_CMD) + return + + sys.argv[:] = cmd + global_namespace = {"__file__": AUTOPEP8_FORMAT_CMD, "__name__": "__main__"} + with open(AUTOPEP8_FORMAT_CMD, encoding="utf-8") as file_handle: + exec(compile(file_handle.read(), AUTOPEP8_FORMAT_CMD, 'exec'), global_namespace) + + def argparse_create() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( @@ -154,6 +175,17 @@ def argparse_create() -> argparse.ArgumentParser: ), required=False, ) + parser.add_argument( + "--no-subprocess", + dest="no_subprocess", + default=False, + action='store_true', + help=( + "Don't use a sub-process, load autopep8 into this instance of Python. " + "Works around 8191 argument length limit on WIN32." + ), + required=False, + ) parser.add_argument( "--autopep8-command", dest="autopep8_command", @@ -214,6 +246,10 @@ def main() -> None: if not files: return + if args.no_subprocess: + autopep8_format_no_subprocess(files) + return + autopep8_format(files)