1- # -*- coding: utf-8 -*-
2-
3- # File name : astyle.py
4- # Author : Frederic PILLON <frederic.pillon@st.com>
5- # Created : 11/16/2018
6- # Python Version :
7- # Requirements : Artistic Style Version 3.1
8- # Description : Launch astyle on found source files
9-
101import argparse
11- import os
2+ from pathlib import Path
123import re
134import subprocess
145import sys
156
16- script_path = os . path . dirname ( os . path . abspath ( __file__ ) )
7+ script_path = Path ( __file__ ). parent . resolve ( )
178ignore_filename = ".astyleignore"
189def_filename = ".astylerc"
1910astyle_out_filename = "astyle.out"
20- if os . getcwd () != script_path :
21- src_path = os . getcwd ()
11+ if Path . cwd () != script_path :
12+ src_path = Path . cwd ()
2213else :
23- src_path = os . path . realpath ( os . path . join ( ".." , ".." ))
24- ignore_path = os . path . join ( script_path , ignore_filename )
25- def_path = os . path . join ( script_path , def_filename )
26- astyle_out_path = astyle_out_filename
14+ src_path = script_path . parent . parent
15+ ignore_path = script_path / ignore_filename
16+ def_path = script_path / def_filename
17+ astyle_out_path = Path ( astyle_out_filename )
2718git_branch = "remotes/origin/main"
2819
20+ script_version = "1.0.0"
2921astyle_major = 3
3022astyle_minor = 1
31- astyle_path = ""
23+ astyle_path = Path ()
24+ astyle_cmd = "astyle"
3225if sys .platform .startswith ("win32" ):
33- astyle_cmd = "astyle.exe"
34- elif sys .platform .startswith ("linux" ) or sys .platform .startswith ("darwin" ):
35- astyle_cmd = "astyle"
36- else :
26+ astyle_cmd += ".exe"
27+ elif not sys .platform .startswith ("linux" ) and not sys .platform .startswith ("darwin" ):
3728 print ("Platform unknown." )
3829 sys .exit (1 )
3930
4334
4435
4536# Check if path exists
46- def checkPath (path , msg ):
47- if not os . path . exists (path ):
37+ def checkPath (p , msg ):
38+ if not p . exists ():
4839 print (msg )
4940 sys .exit (1 )
5041
@@ -53,7 +44,7 @@ def checkPath(path, msg):
5344def checkAstyle ():
5445 try :
5546 output = subprocess .check_output (
56- [os . path . join ( astyle_path , astyle_cmd ) , "--version" ],
47+ [astyle_path , "--version" ],
5748 stderr = subprocess .STDOUT ,
5849 )
5950
@@ -64,11 +55,8 @@ def checkAstyle():
6455 if major >= astyle_major and minor >= astyle_minor :
6556 print (output .decode ("utf-8" ).rstrip ())
6657 else :
67- print (
68- "Required Astyle version {}.{} (Current: {}.{})" .format (
69- astyle_major , astyle_minor , major , minor
70- )
71- )
58+ print (f"Astyle minimum version required { astyle_major } .{ astyle_minor } ." )
59+ print (f"Current is { major } .{ minor } ." )
7260 sys .exit (1 )
7361 else :
7462 raise subprocess .CalledProcessError (1 , "No version found" )
@@ -91,16 +79,21 @@ def gitdiff_files():
9179 print (e .output )
9280 sys .exit (1 )
9381
94- gitroot = output .decode ("utf-8" ).rstrip ()
95- rel_src = re .sub ("^[/\\ ]+]" , "" , re .sub (gitroot , "" , src_path ))
82+ gitroot = Path (output .decode ("utf-8" ).rstrip ())
83+ try :
84+ rel_src = src_path .relative_to (gitroot )
85+ except ValueError :
86+ print (f"{ src_path } not in git repository." )
87+ sys .exit (1 )
9688
97- cmd = []
98- cmd .append ("git" )
89+ cmd = ["git" ]
9990 cmd .append ("diff" )
10091 cmd .append ("--name-only" )
10192 cmd .append ("--diff-filter=d" )
10293 cmd .append (git_branch )
103- cmd .append ("--relative=" + rel_src )
94+ # Relative only if source root path specified
95+ if args .root :
96+ cmd .append (f"--relative={ rel_src } " )
10497
10598 proc = subprocess .Popen (cmd , stdout = subprocess .PIPE )
10699 while True :
@@ -109,18 +102,22 @@ def gitdiff_files():
109102 break
110103 if line :
111104 if line .endswith ((".h" , ".c" , ".hpp" , ".cpp" )):
112- source_list .append (os . path . join ( gitroot , line .rstrip () ))
105+ source_list .append (gitroot / line .rstrip ())
113106 if proc .poll () != 0 :
114107 sys .exit (1 )
115108 source_list .sort ()
116109
117110
118111# Find all files in source root path
119112def find_files ():
120- for root , dirs , files in os .walk (src_path , followlinks = True ):
121- for f in files :
122- if f .endswith ((".h" , ".c" , ".hpp" , ".cpp" )):
123- source_list .append (os .path .join (root , f ))
113+ for spath_object in src_path .glob ("**/*" ):
114+ if spath_object .is_file () and spath_object .suffix in [
115+ ".h" ,
116+ ".c" ,
117+ ".hpp" ,
118+ ".cpp" ,
119+ ]:
120+ source_list .append (spath_object )
124121 source_list .sort ()
125122
126123
@@ -132,22 +129,17 @@ def manage_exclude_list():
132129 exclude_list .append (line .rstrip ())
133130 if exclude_list :
134131 for pattern in exclude_list :
135- if sys .platform .startswith ("win32" ):
136- winpattern = os .path .join (src_path , pattern .replace ("/" ,"\\ " )) .replace ("\\ " ,"\\ \\ " )
137- exclude_pattern = re .compile (winpattern + ".*" )
138- else :
139- exclude_pattern = re .compile (os .path .join (src_path , pattern ) + ".*" )
132+ exclude_path = src_path / pattern
140133 for s in reversed (source_list ):
141- if exclude_pattern . search ( s ):
134+ if s . is_relative_to ( exclude_path ):
142135 source_list .remove (s )
143136
144137
145138# Launch Astyle on all source files
146139def astyle ():
147- cmd = []
148- cmd .append (os .path .join (astyle_path , astyle_cmd ))
140+ cmd = [astyle_path ]
149141 cmd .append ("-n" )
150- cmd .append ("--options=" + def_path )
142+ cmd .append (f "--options={ def_path } " )
151143 cmd .append ("dummy_file" )
152144
153145 stddout_name = astyle_out_path
@@ -161,21 +153,19 @@ def astyle():
161153
162154
163155# Parser
164- parser = argparse .ArgumentParser (
165- description = "Launch astyle on source files found at specified root path."
166- )
156+ parser = argparse .ArgumentParser (description = "Launch astyle on source files." )
167157
168158parser .add_argument (
169159 "-d" ,
170160 "--definition" ,
171161 metavar = "<code style definition file>" ,
172- help = "Code style definition file for Astyle. Default: " + def_path ,
162+ help = f "Code style definition file for Astyle. Default: { def_path } " ,
173163)
174164g0 = parser .add_mutually_exclusive_group ()
175165g0 .add_argument (
176166 "-g" ,
177167 "--gitdiff" ,
178- help = "Use changes files from git default branch. Default: " + git_branch ,
168+ help = "Use changes files from git default branch. Default: { git_branch}" ,
179169 action = "store_true" ,
180170)
181171g0 .add_argument (
@@ -188,7 +178,7 @@ def astyle():
188178 "-i" ,
189179 "--ignore" ,
190180 metavar = "<ignore file>" ,
191- help = "File containing path to ignore. Default: " + ignore_path ,
181+ help = "File containing path to ignore. Default: { ignore_path}" ,
192182)
193183parser .add_argument (
194184 "-p" , "--path" , metavar = "<astyle install path>" , help = "Astyle installation path"
@@ -197,7 +187,7 @@ def astyle():
197187 "-r" ,
198188 "--root" ,
199189 metavar = "<source root path>" ,
200- help = "Source root path to use. Default: " + src_path ,
190+ help = "Source root path to use. Default: { src_path}" ,
201191)
202192args = parser .parse_args ()
203193
@@ -209,27 +199,28 @@ def main():
209199 global astyle_path
210200 global git_branch
211201
202+ print (f"Script { Path (__file__ ).name } version { script_version } " )
212203 if args .root :
213- src_path = os . path . realpath (args .root )
204+ src_path = Path (args .root ). resolve ( )
214205
215206 if args .definition :
216- def_path = os . path . realpath (args .definition )
207+ def_path = Path (args .definition ). resolve ( )
217208
218209 if args .ignore :
219- ignore_path = os . path . realpath (args .ignore )
210+ ignore_path = Path (args .ignore ). resolve ( )
220211
221212 if args .path :
222- astyle_path = os . path . realpath (args .path )
223- checkPath (
224- os . path . join ( astyle_path , astyle_cmd ), "Could not find Astyle binary!"
225- )
213+ astyle_path = Path (args .path ). resolve () / astyle_cmd
214+ checkPath (astyle_path , "Could not find Astyle binary!" )
215+ else :
216+ astyle_path = Path ( astyle_cmd )
226217
227218 checkPath (src_path , "Source root path does not exist!" )
228219 checkPath (def_path , "Code style definition file does not exist!" )
229220 checkPath (ignore_path , "Ignore file does not exist!" )
230221 checkAstyle ()
231222 try :
232- os . remove ( astyle_out_path )
223+ astyle_out_path . unlink ( )
233224 except OSError :
234225 pass
235226
0 commit comments