Skip to content

Commit

Permalink
Merge pull request #3728 from pmatos/PythonIgnore
Browse files Browse the repository at this point in the history
Ignore python files for clang-format
  • Loading branch information
Sonicadvance1 authored Jun 19, 2024
2 parents 1971404 + 88b01a0 commit 053620f
Showing 1 changed file with 4 additions and 72 deletions.
76 changes: 4 additions & 72 deletions External/code-format-helper/code-format-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
check the coding style before submitting it. The canonical source of this script
is in the LLVM source tree under llvm/utils/git.
For C/C++ code it uses clang-format and for Python code it uses darker (which
in turn invokes black).
For C/C++ code it uses clang-format.
You can learn more about the LLVM coding style on llvm.org:
https://llvm.org/docs/CodingStandards.html
Expand All @@ -31,8 +30,8 @@
ln -s $(pwd)/llvm/utils/git/code-format-helper.py .git/hooks/pre-commit
You can control the exact path to clang-format or darker with the following
environment variables: $CLANG_FORMAT_PATH and $DARKER_FORMAT_PATH.
You can control the exact path to clang-format with the following
environment variable: $CLANG_FORMAT_PATH.
"""


Expand Down Expand Up @@ -245,74 +244,7 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
else:
return None


class DarkerFormatHelper(FormatHelper):
name = "darker"
friendly_name = "Python code formatter"

@property
def instructions(self) -> str:
return " ".join(self.darker_cmd)

def filter_changed_files(self, changed_files: List[str]) -> List[str]:
filtered_files = []
for path in changed_files:
name, ext = os.path.splitext(path)
if ext == ".py":
filtered_files.append(path)

return filtered_files

@property
def darker_fmt_path(self) -> str:
if "DARKER_FORMAT_PATH" in os.environ:
return os.environ["DARKER_FORMAT_PATH"]
return "darker"

def has_tool(self) -> bool:
cmd = [self.darker_fmt_path, "--version"]
proc = None
try:
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except:
return False
return proc.returncode == 0

def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str]:
py_files = self.filter_changed_files(changed_files)
if not py_files:
return None
darker_cmd = [
self.darker_fmt_path,
"--check",
"--diff",
]
if args.start_rev and args.end_rev:
darker_cmd += ["-r", f"{args.start_rev}...{args.end_rev}"]
darker_cmd += py_files
if args.verbose:
print(f"Running: {' '.join(darker_cmd)}")
self.darker_cmd = darker_cmd
proc = subprocess.run(
darker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if args.verbose:
sys.stdout.write(proc.stderr.decode("utf-8"))

if proc.returncode != 0:
# formatting needed, or the command otherwise failed
if args.verbose:
print(f"error: {self.name} exited with code {proc.returncode}")
# Print the diff in the log so that it is viewable there
print(proc.stdout.decode("utf-8"))
return proc.stdout.decode("utf-8")
else:
sys.stdout.write(proc.stdout.decode("utf-8"))
return None


ALL_FORMATTERS = (DarkerFormatHelper(), ClangFormatHelper())

ALL_FORMATTERS = [ClangFormatHelper()]

def hook_main():
# fill out args
Expand Down

0 comments on commit 053620f

Please sign in to comment.