aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-10-05 15:02:11 +0500
committerEgor Tensin <egor@tensin.name>2026-10-05 15:02:11 +0500
commit19c7e97db9a14de64171784d749360d490e32e69 (patch)
treeed7a8bc1ebfded86ca2d11eaebb777fd1dbeb5dd
parentproject.ctest_driver: refactoring (diff)
downloadcmake-common-19c7e97db9a14de64171784d749360d490e32e69.tar.gz
cmake-common-19c7e97db9a14de64171784d749360d490e32e69.zip
project.format: refactoring
-rw-r--r--project/ctest_driver.py7
-rw-r--r--project/format.py36
-rw-r--r--project/utils.py5
3 files changed, 21 insertions, 27 deletions
diff --git a/project/ctest_driver.py b/project/ctest_driver.py
index 5e952a8..90362d7 100644
--- a/project/ctest_driver.py
+++ b/project/ctest_driver.py
@@ -23,11 +23,6 @@ import sys
from project import utils
-def read_file(path):
- with open(path, mode="r") as fd:
- return fd.read()
-
-
def _run_is_valid_code(actual, expected):
if actual in expected:
return True
@@ -104,7 +99,7 @@ def action_run(args):
def action_grep(args):
- contents = read_file(args.path)
+ contents = utils.read_file(args.path)
return _match_output(contents, args.pass_regexes, args.fail_regexes)
diff --git a/project/format.py b/project/format.py
index e1897db..cfd8d98 100644
--- a/project/format.py
+++ b/project/format.py
@@ -15,12 +15,7 @@ import difflib
import os
import sys
-from project.utils import cd, normalize_path, run_capture, setup_logging
-
-
-def read_file(path):
- with open(path) as file:
- return file.read()
+from project import utils
class ClangFormat:
@@ -37,11 +32,11 @@ class ClangFormat:
return cmd_line
def format_in_place(self, paths):
- run_capture(self._get_command_line(paths, in_place=True))
+ utils.run_capture(self._get_command_line(paths, in_place=True))
@staticmethod
def _show_diff(path, formatted):
- original = read_file(path)
+ original = utils.read_file(path)
if original and not formatted:
# Assuming this file is ignored in .clang-format-ignore.
return True
@@ -68,14 +63,14 @@ class ClangFormat:
def show_diff(self, paths):
clean = True
for path in paths:
- formatted = run_capture(self._get_command_line([path]))
+ formatted = utils.run_capture(self._get_command_line([path]))
clean = self._show_diff(path, formatted) and clean
return clean
def git_root_dir():
cmd_line = ["git", "rev-parse", "--show-toplevel"]
- root_dir = run_capture(cmd_line)
+ root_dir = utils.run_capture(cmd_line)
if root_dir[-1] != "\n":
raise RuntimeError("git rev-parse --show-toplevel should append a newline?")
return root_dir[:-1]
@@ -83,13 +78,13 @@ def git_root_dir():
def list_git_files():
cmd_line = ["git", "ls-tree", "-r", "-z", "--name-only", "HEAD"]
- repo_files = run_capture(cmd_line)
+ repo_files = utils.run_capture(cmd_line)
repo_files = repo_files.split("\0")
return repo_files
def list_all_files():
- return (normalize_path(path) for path in list_git_files())
+ return (utils.normalize_path(path) for path in list_git_files())
def excluded(path, exclude):
@@ -121,17 +116,16 @@ DEFAULT_VERSION = "clang-format"
DEFAULT_STYLE = "file"
-def process_cpp_files(
+def do_format(
version=DEFAULT_VERSION, style=DEFAULT_STYLE, in_place=False, exclude=None
):
clang_format = ClangFormat(version, style)
- with cd(git_root_dir()):
+ with utils.cd(git_root_dir()):
cpp_files = filter_files(list_cpp_files(), exclude)
if in_place:
clang_format.format_in_place(cpp_files)
- else:
- if not clang_format.show_diff(cpp_files):
- sys.exit(1)
+ return 0
+ return 0 if clang_format.show_diff(cpp_files) else 1
def parse_args(argv=None):
@@ -162,7 +156,7 @@ def parse_args(argv=None):
"-e",
"--exclude",
nargs="*",
- type=normalize_path,
+ type=utils.normalize_path,
help="files or directories to exclude",
)
@@ -171,9 +165,9 @@ def parse_args(argv=None):
def main(argv=None):
args = parse_args(argv)
- with setup_logging():
- process_cpp_files(**vars(args))
+ with utils.setup_logging():
+ return do_format(**vars(args))
if __name__ == "__main__":
- main()
+ sys.exit(main())
diff --git a/project/utils.py b/project/utils.py
index 57f5d85..87ac8d1 100644
--- a/project/utils.py
+++ b/project/utils.py
@@ -17,6 +17,11 @@ import time
import project.os
+def read_file(path):
+ with open(path, encoding="utf-8") as fd:
+ return fd.read()
+
+
def normalize_path(s):
return os.path.abspath(os.path.normpath(s))