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
-
10
1
import argparse
11
- import os
2
+ from pathlib import Path
12
3
import re
13
4
import subprocess
14
5
import sys
15
6
16
- script_path = os . path . dirname ( os . path . abspath ( __file__ ) )
7
+ script_path = Path ( __file__ ). parent . resolve ( )
17
8
ignore_filename = ".astyleignore"
18
9
def_filename = ".astylerc"
19
10
astyle_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 ()
22
13
else :
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 )
27
18
git_branch = "remotes/origin/main"
28
19
20
+ script_version = "1.0.0"
29
21
astyle_major = 3
30
22
astyle_minor = 1
31
- astyle_path = ""
23
+ astyle_path = Path ()
24
+ astyle_cmd = "astyle"
32
25
if 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" ):
37
28
print ("Platform unknown." )
38
29
sys .exit (1 )
39
30
43
34
44
35
45
36
# 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 ():
48
39
print (msg )
49
40
sys .exit (1 )
50
41
@@ -53,7 +44,7 @@ def checkPath(path, msg):
53
44
def checkAstyle ():
54
45
try :
55
46
output = subprocess .check_output (
56
- [os . path . join ( astyle_path , astyle_cmd ) , "--version" ],
47
+ [astyle_path , "--version" ],
57
48
stderr = subprocess .STDOUT ,
58
49
)
59
50
@@ -64,11 +55,8 @@ def checkAstyle():
64
55
if major >= astyle_major and minor >= astyle_minor :
65
56
print (output .decode ("utf-8" ).rstrip ())
66
57
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 } ." )
72
60
sys .exit (1 )
73
61
else :
74
62
raise subprocess .CalledProcessError (1 , "No version found" )
@@ -91,16 +79,21 @@ def gitdiff_files():
91
79
print (e .output )
92
80
sys .exit (1 )
93
81
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 )
96
88
97
- cmd = []
98
- cmd .append ("git" )
89
+ cmd = ["git" ]
99
90
cmd .append ("diff" )
100
91
cmd .append ("--name-only" )
101
92
cmd .append ("--diff-filter=d" )
102
93
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 } " )
104
97
105
98
proc = subprocess .Popen (cmd , stdout = subprocess .PIPE )
106
99
while True :
@@ -109,18 +102,22 @@ def gitdiff_files():
109
102
break
110
103
if line :
111
104
if line .endswith ((".h" , ".c" , ".hpp" , ".cpp" )):
112
- source_list .append (os . path . join ( gitroot , line .rstrip () ))
105
+ source_list .append (gitroot / line .rstrip ())
113
106
if proc .poll () != 0 :
114
107
sys .exit (1 )
115
108
source_list .sort ()
116
109
117
110
118
111
# Find all files in source root path
119
112
def 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 )
124
121
source_list .sort ()
125
122
126
123
@@ -132,22 +129,17 @@ def manage_exclude_list():
132
129
exclude_list .append (line .rstrip ())
133
130
if exclude_list :
134
131
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
140
133
for s in reversed (source_list ):
141
- if exclude_pattern . search ( s ):
134
+ if s . is_relative_to ( exclude_path ):
142
135
source_list .remove (s )
143
136
144
137
145
138
# Launch Astyle on all source files
146
139
def astyle ():
147
- cmd = []
148
- cmd .append (os .path .join (astyle_path , astyle_cmd ))
140
+ cmd = [astyle_path ]
149
141
cmd .append ("-n" )
150
- cmd .append ("--options=" + def_path )
142
+ cmd .append (f "--options={ def_path } " )
151
143
cmd .append ("dummy_file" )
152
144
153
145
stddout_name = astyle_out_path
@@ -161,21 +153,19 @@ def astyle():
161
153
162
154
163
155
# 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." )
167
157
168
158
parser .add_argument (
169
159
"-d" ,
170
160
"--definition" ,
171
161
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 } " ,
173
163
)
174
164
g0 = parser .add_mutually_exclusive_group ()
175
165
g0 .add_argument (
176
166
"-g" ,
177
167
"--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}" ,
179
169
action = "store_true" ,
180
170
)
181
171
g0 .add_argument (
@@ -188,7 +178,7 @@ def astyle():
188
178
"-i" ,
189
179
"--ignore" ,
190
180
metavar = "<ignore file>" ,
191
- help = "File containing path to ignore. Default: " + ignore_path ,
181
+ help = "File containing path to ignore. Default: { ignore_path}" ,
192
182
)
193
183
parser .add_argument (
194
184
"-p" , "--path" , metavar = "<astyle install path>" , help = "Astyle installation path"
@@ -197,7 +187,7 @@ def astyle():
197
187
"-r" ,
198
188
"--root" ,
199
189
metavar = "<source root path>" ,
200
- help = "Source root path to use. Default: " + src_path ,
190
+ help = "Source root path to use. Default: { src_path}" ,
201
191
)
202
192
args = parser .parse_args ()
203
193
@@ -209,27 +199,28 @@ def main():
209
199
global astyle_path
210
200
global git_branch
211
201
202
+ print (f"Script { Path (__file__ ).name } version { script_version } " )
212
203
if args .root :
213
- src_path = os . path . realpath (args .root )
204
+ src_path = Path (args .root ). resolve ( )
214
205
215
206
if args .definition :
216
- def_path = os . path . realpath (args .definition )
207
+ def_path = Path (args .definition ). resolve ( )
217
208
218
209
if args .ignore :
219
- ignore_path = os . path . realpath (args .ignore )
210
+ ignore_path = Path (args .ignore ). resolve ( )
220
211
221
212
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 )
226
217
227
218
checkPath (src_path , "Source root path does not exist!" )
228
219
checkPath (def_path , "Code style definition file does not exist!" )
229
220
checkPath (ignore_path , "Ignore file does not exist!" )
230
221
checkAstyle ()
231
222
try :
232
- os . remove ( astyle_out_path )
223
+ astyle_out_path . unlink ( )
233
224
except OSError :
234
225
pass
235
226
0 commit comments