Update validate-line-endings.py to not depend on pip packages (#41022)
* Update validate-line-endings.py to not depend on pip packages * Apply PR feedback * Apply PR feedback
Blue committed
Jul 8, 2026 at 13:25 UTC
30ce51a30062189c37e5ddac6ec410e07315e5d2
2 files changed
+14
-16
.pipelines/build-stage.yml
-3
@@ -104,9 +104,6 @@ stages:
104
pool: ${{ parameters.pool }}
105
106
steps:
107
- - script: pip install --user -r tools/devops/requirements.txt --break-system-packages
108
- displayName: Install dependencies
109
-
107
- script: python tools/devops/validate-localization.py
108
displayName: Validate localization resources
109
tools/devops/validate-line-endings.py
+14
-13
@@ -1,6 +1,6 @@
1
-import click
1
+import argparse
2
import os.path
3
-from git import Repo
3
+import subprocess
4
5
EXTENSIONS = ['.c', '.cpp', '.h', '.hpp', '.idl', '.resw']
6
@@ -16,14 +16,10 @@ def to_crlf(content: bytes) -> bytes:
16
# Normalize every line ending (CRLF, lone CR, lone LF) to a single CRLF.
17
return content.replace(b'\r\n', b'\n').replace(b'\r', b'\n').replace(b'\n', b'\r\n')
18
19
-@click.command()
20
-@click.argument('path', required=True, type=click.Path(exists=True))
21
-@click.option('--fix', is_flag=True, help='Convert mismatching files to CRLF line endings.')
19
def main(path: str, fix: bool):
23
- repo = Repo(path, search_parent_directories=True)
24
-
25
- tracked = repo.git.ls_files('-z').split('\0')
26
- source_files = [os.path.join(repo.working_tree_dir, e) for e in tracked if e and is_source_file(e)]
20
+ tracked = subprocess.run(
21
+ ['git', '-C', path, 'ls-files', '-z'], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').split('\0')
22
+ source_files = [os.path.join(path, e) for e in tracked if e and is_source_file(e)]
23
24
mismatches = []
25
for e in source_files:
@@ -39,15 +35,20 @@ def main(path: str, fix: bool):
35
fd.write(to_crlf(content))
36
37
if not mismatches:
42
- click.secho('All files use CRLF line endings', fg='green')
38
+ print(f'All {len(source_files)} files use CRLF line endings')
39
return
40
41
listed = '\n'.join(mismatches)
42
if fix:
47
- click.secho(f'Converted {len(mismatches)} files to CRLF:\n{listed}', fg='yellow')
43
+ print(f'Converted {len(mismatches)} files to CRLF:\n{listed}')
44
else:
49
- click.secho(f'{len(mismatches)} files have non-CRLF line endings:\n{listed}', fg='red')
45
+ print(f'{len(mismatches)} files have non-CRLF line endings:\n{listed}')
46
raise SystemExit(1)
47
48
if __name__ == '__main__':
53
- main()
49
+ parser = argparse.ArgumentParser(description='Validate that source files use CRLF line endings.')
50
+ parser.add_argument('path', help='Path to validate (must be inside the repo).')
51
+ parser.add_argument('--fix', action='store_true', help='Convert mismatching files to CRLF line endings.')
52
+ args = parser.parse_args()
53
+
54
+ main(args.path, args.fix)