1- #!/usr/ bin/ env python3
1+ #!/ usr / bin / env python3
22
3- # SPDX- FileCopyrightText: 2024 Vector Informatik GmbH
3+ #SPDX - FileCopyrightText : 2024 Vector Informatik GmbH
44#
5- # SPDX- License- Identifier: MIT
5+ #SPDX - License - Identifier : MIT
66from pathlib import Path
77from shutil import which
88
99import argparse
1010import os
1111import re
12+ import requests
1213import subprocess
1314import sys
1415
@@ -34,6 +35,39 @@ def die(status, fmt, *args):
3435CLANG_VERSION = "18"
3536CLANG_FORMAT = "clang-format-" + CLANG_VERSION
3637
38+ def get_pr_files (pr : str , repo : str ):
39+
40+ url = 'https://api.github.com/repos/' + repo + '/pulls/' + pr + '/files'
41+
42+ log ("Checking at {}" .format (url ))
43+
44+ r = requests .get (url )
45+
46+ max_page = 1
47+ if 'link' in r .headers :
48+ last_page_link_header = r .headers ["link" ].split (',' )[- 1 ].split (';' )[0 ]
49+ page_reg = re .compile ('.+page=([0-9]+)>' )
50+ page = re .match (page_reg , last_page_link_header ).group (1 )
51+
52+ if page .isnumeric ():
53+ max_page = int (page )
54+
55+ files = []
56+ for fileObject in r .json ():
57+
58+ files .append (fileObject ["filename" ])
59+
60+ if max_page > 1 :
61+ print (f"Need to check { max_page } pages" )
62+ for page in range (2 , max_page + 1 ):
63+ page_url = url + f'?page={ page } '
64+ r = requests .get (page_url )
65+ for fileObject in r .json ():
66+ files .append (fileObject ["filename" ])
67+
68+ print (f"Found { len (files )} files" )
69+ return files
70+
3771def main ():
3872 if which (CLANG_FORMAT ) is None :
3973 warn ("No {} found!" , CLANG_FORMAT )
@@ -44,14 +78,14 @@ def main():
4478 description = "Run clang tidy on select source files" )
4579
4680 parser .add_argument (
47- '--git_commit ' , help = "The Git commit to be reformatted" , type = str )
81+ '--pr ' , help = "The Github PR to be reformatted" , type = str )
4882 parser .add_argument (
49- '--dry_run ' , help = "Only produce warnings " , action = 'store_true' )
83+ '--repo ' , help = "The Github repo to be reformatted " , type = str )
5084 parser .add_argument (
51- '--werror ' , help = "Treat clang-format warnings as errors " , action = 'store_true' )
85+ '--dryrun ' , help = "Only produce warnings" , action = 'store_true' )
5286 args = parser .parse_args ()
5387
54- # Check for supported clang- format version
88+ # Check for supported clang - format version
5589 format_version = subprocess .run ([CLANG_FORMAT , '--version' ], capture_output = True , encoding = 'utf-8' )
5690
5791 version_reg = re .compile ('^.* clang-format version (\d+)\.(\d+)\.(\d+).*' )
@@ -74,37 +108,43 @@ def main():
74108 totalFiles = 0
75109 totalWarnings = 0
76110
77- # Check the Formatting!
111+ # Check the Formatting !
78112 files = []
79113
80- if args .git_commit is not None and args .git_commit != "" :
81- commit_files = subprocess .run (['git' , 'diff-tree' , '--no-commit-id' , '--name-only' , '-r' , args .git_commit ],
82- capture_output = True , encoding = 'utf8' )
83- files = commit_files .stdout .splitlines ()
114+ if args .pr is not None and args .repo is not None :
115+ files = get_pr_files (args .pr , args .repo )
84116 else :
85117 for directory in dirs :
86118 rootPath = Path ("./" + directory )
87119 for ext in fileExtensions :
88120 files = files + (sorted (rootPath .rglob (ext )))
89121
90- dry_run = ''
91- if args .dry_run :
92- dry_run = "--dry-run"
93- print (dry_run )
122+ clang_format_cmd = [CLANG_FORMAT ]
123+ if args .dryrun :
124+ clang_format_cmd .append ('--Werror' )
125+ clang_format_cmd .append ('--dry-run' )
126+
127+ clang_format_cmd = clang_format_cmd + ['-i' , '--style=file' ]
128+ print (clang_format_cmd )
94129 for file in files :
95- totalFiles = totalFiles + 1
96- formatResult = subprocess .run ([CLANG_FORMAT , '--Werror' , dry_run , '-i' , '--style=file' , file ], capture_output = True , encoding = 'utf-8' )
97- if formatResult .returncode != 0 :
98- formattingCorrect = False
99- totalWarnings = totalWarnings + 1
100- warn ("File not formatted correctly: {}" , file )
130+
131+ if any (p in file for p in dirs ):
132+
133+ totalFiles = totalFiles + 1
134+ formatResult = subprocess .run (clang_format_cmd + [file ], capture_output = True , encoding = 'utf-8' )
135+ if formatResult .returncode != 0 :
136+ formattingCorrect = False
137+ totalWarnings = totalWarnings + 1
138+ warn ("File not formatted correctly: {}" , file )
139+ else :
140+ print (f"Skip: { file } " )
101141
102142 info ("{} files checked, {} produced a warning!" , totalFiles , totalWarnings )
103143 if formattingCorrect is False :
104- # Only warn for now
144+ # Only warn for now
105145 warn ("Formatting for one or more SilKit source code files not correct.!" )
106146 warn ("Please format your source code properly using the SilKit .clang-format config file!" )
107- ret_code = 64 if args .werror else 0
147+ ret_code = 64 if args .dryrun else 0
108148 exit (ret_code )
109149
110150 info ("All source code files properly formatted!" )
0 commit comments