Skip to content

Commit 7e12c80

Browse files
authored
Merge pull request #1876 from fpistm/astyle_review
ci: review
2 parents 4ba69b8 + c9ff285 commit 7e12c80

File tree

4 files changed

+104
-86
lines changed

4 files changed

+104
-86
lines changed

.github/workflows/CodeSpell.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
steps:
1616
- name: Checkout
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@main
1818

1919
# See: https://github.com/codespell-project/actions-codespell/blob/master/README.md
2020
- name: Spell check

.github/workflows/Continuous-Integration.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,6 @@ on:
2727
# Allows you to run this workflow manually from the Actions tab
2828
workflow_dispatch:
2929
jobs:
30-
astyle_check:
31-
runs-on: ubuntu-latest
32-
name: AStyle check
33-
steps:
34-
# First of all, clone the repo using the checkout action.
35-
- name: Checkout
36-
uses: actions/checkout@main
37-
38-
- name: Astyle check
39-
id: Astyle
40-
uses: stm32duino/actions/astyle-check@main
41-
with:
42-
astyle-definition: 'CI/astyle/.astylerc'
43-
ignore-path-list: 'CI/astyle/.astyleignore'
44-
45-
# Use the output from the `Astyle` step
46-
- name: Astyle Errors
47-
if: failure()
48-
run: |
49-
cat ${{ steps.Astyle.outputs.astyle-result }}
50-
exit 1
5130
core_build:
5231
runs-on: ubuntu-latest
5332
name: Core compilation

.github/workflows/astyle.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Check code formatting with astyle
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths-ignore:
7+
- '*.json'
8+
- '**.md'
9+
- keywords.txt
10+
- CI/**
11+
- '!CI/astyle/.astyleignore'
12+
- '!CI/astyle/.astylerc'
13+
- '!CI/astyle/astyle.py'
14+
- tools/**
15+
pull_request:
16+
paths-ignore:
17+
- '*.json'
18+
- '**.md'
19+
- keywords.txt
20+
- CI/**
21+
- '!CI/astyle/.astyleignore'
22+
- '!CI/astyle/.astylerc'
23+
- '!CI/astyle/astyle.py'
24+
- tools/**
25+
# Allows you to run this workflow manually from the Actions tab
26+
workflow_dispatch:
27+
jobs:
28+
astyle_check:
29+
runs-on: ubuntu-latest
30+
name: Check for astyle errors
31+
steps:
32+
# First of all, clone the repo using the checkout action.
33+
- name: Checkout
34+
uses: actions/checkout@main
35+
36+
- name: Astyle check
37+
id: Astyle
38+
uses: stm32duino/actions/astyle-check@main
39+
with:
40+
astyle-definition: 'CI/astyle/.astylerc'
41+
ignore-path-list: 'CI/astyle/.astyleignore'
42+
43+
# Use the output from the `Astyle` step
44+
- name: Astyle Errors
45+
if: failure()
46+
run: |
47+
cat ${{ steps.Astyle.outputs.astyle-result }}
48+
exit 1

CI/astyle/astyle.py

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
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-
101
import argparse
11-
import os
2+
from pathlib import Path
123
import re
134
import subprocess
145
import sys
156

16-
script_path = os.path.dirname(os.path.abspath(__file__))
7+
script_path = Path(__file__).parent.resolve()
178
ignore_filename = ".astyleignore"
189
def_filename = ".astylerc"
1910
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()
2213
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)
2718
git_branch = "remotes/origin/main"
2819

20+
script_version = "1.0.0"
2921
astyle_major = 3
3022
astyle_minor = 1
31-
astyle_path = ""
23+
astyle_path = Path()
24+
astyle_cmd = "astyle"
3225
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"):
3728
print("Platform unknown.")
3829
sys.exit(1)
3930

@@ -43,8 +34,8 @@
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):
5344
def 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
119112
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)
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
146139
def 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

168158
parser.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
)
174164
g0 = parser.add_mutually_exclusive_group()
175165
g0.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
)
181171
g0.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
)
193183
parser.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
)
202192
args = 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

Comments
 (0)