Skip to content

Commit 2b39d40

Browse files
authored
ci: support dir path in fortran style script (#1666)
1 parent af7e80b commit 2b39d40

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

.github/common/update_fortran_style.py

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import string
33
from itertools import repeat
4+
from multiprocessing import cpu_count, Pool
45
from pathlib import Path
56

67
from fprettify.fparse_utils import InputStream
@@ -15,7 +16,7 @@ def join_comments(comments) -> str:
1516
class Rules:
1617
@staticmethod
1718
def separate_lines(path):
18-
"""Variables defined on separate lines"""
19+
"""Define dummy arguments, local variables, and procedure declarations on separate lines."""
1920

2021
flines = []
2122
with open(path, "r") as f:
@@ -63,7 +64,7 @@ def separate_lines(path):
6364

6465
@staticmethod
6566
def trailing_returns(path):
66-
"""Remove return statements at the end of routines"""
67+
"""Remove return statements (and corresponding comments) at the end of routines."""
6768

6869
flines = []
6970
with open(path, "r") as f:
@@ -96,9 +97,10 @@ def trailing_returns(path):
9697
@staticmethod
9798
def cleanup_comments(path):
9899
"""
99-
Remove comments on lines with only whitespace, remove '--' from the beginnings
100-
of comments, make sure comment spacing is consistent (one space after '!'),
101-
remove horizontal dividers consisting of '-' or '*', remove 'SPECIFICATION'
100+
Remove empty comments (containing only '!', or '!' followed by some number of '-' or '='),
101+
remove double dashes from beginnings of comments (e.g., '! -- comment' becomes '! comment'),
102+
remove 'SPECIFICATION' comment lines, and make internal comment spacing consistent (one space
103+
after '!' before text begins).
102104
"""
103105

104106
flines = []
@@ -170,26 +172,42 @@ def reformat(
170172
action="store_true",
171173
default=True,
172174
required=False,
173-
help="Define dummy arguments and local variables on separate lines.",
175+
help="Define dummy arguments, local variables, and procedure declarations on separate lines.",
174176
)
175177
parser.add_argument(
176178
"--trailing-returns",
177179
action="store_true",
178180
default=True,
179181
required=False,
180-
help="Remove return statements at the end of routines.",
182+
help="Remove return statements (and corresponding comments) at the end of routines.",
181183
)
182184
parser.add_argument(
183185
"--cleanup-comments",
184186
action="store_true",
185187
default=True,
186188
required=False,
187-
help="Remove empty comments (containing only '!', or '!' followed by some number of '-' or '='), remove double dashes from beginnings of comments (e.g., '! -- comment' becomes '! comment'), and make internal comment spacing consistent (one space after '!' before text begins).",
189+
help="Remove empty comments (containing only '!', or '!' followed by some number of '-' or '='), remove double dashes from beginnings of comments (e.g., '! -- comment' becomes '! comment'), remove 'SPECIFICATION' comment lines, and make internal comment spacing consistent (one space after '!' before text begins).",
188190
)
189191
args = parser.parse_args()
190-
reformat(
191-
path=Path(args.path).expanduser().absolute(),
192-
separate_lines=args.separate_lines,
193-
trailing_returns=args.trailing_returns,
194-
cleanup_comments=args.cleanup_comments,
195-
)
192+
193+
# parse path
194+
path = Path(args.path).expanduser().absolute()
195+
assert path.exists(), f"Path not found: {path}"
196+
197+
# parse rules
198+
sl = args.separate_lines
199+
tr = args.trailing_returns
200+
cc = args.cleanup_comments
201+
202+
# reformat
203+
if path.is_file():
204+
reformat(
205+
path=Path(args.path).expanduser().absolute(),
206+
separate_lines=sl,
207+
trailing_returns=tr,
208+
cleanup_comments=cc,
209+
)
210+
else:
211+
with Pool(cpu_count()) as pool:
212+
files = [p for p in path.rglob("*.f90") if p.is_file()]
213+
pool.starmap(reformat, [(f, sl, tr, cc) for f in files])

0 commit comments

Comments
 (0)