Skip to content

Commit 85c8d73

Browse files
committed
tmp 2
1 parent 5568d38 commit 85c8d73

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

lint_rules/lint_rules/ifs_arpege_coding_standards.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'MissingKindSpecifierRealLiterals'
3636
]
3737

38+
jprd_files = []
3839

3940
class FindFloatLiterals(ExpressionFinder):
4041
"""
@@ -170,17 +171,41 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
170171
...
171172
"""
172173
# sourcefile = subroutine.source.file
173-
print(f"fix_subroutine: subroutine: {subroutine} | subroutine.source: {subroutine.source} | subroutine.source.file: {subroutine.source.file}")
174+
print(f"fix_subroutine: subroutine: {subroutine} | subroutine.source: {subroutine.source} | subroutine.source.file: {subroutine.source.file} | {str(sourcefile.path)}")
175+
orig_file = str(sourcefile.path).replace('source/ifs-source/', '')
176+
subdir = orig_file.split('/')[0]
177+
if orig_file in jprd_files:
178+
with open(f"files_to_commit_{subdir}_jprd.txt", "a") as myfile:
179+
myfile.write(f"{orig_file}\n")
180+
kind_spec = 'JPRD'
181+
else:
182+
with open(f"files_to_commit_{subdir}_jprm.txt", "a") as myfile:
183+
myfile.write(f"{orig_file}\n")
184+
kind_spec = 'JPRM'
174185
original_content = read_file(str(sourcefile.path))
175186
content = original_content
176187
literal_nodes = FindFloatLiterals(with_ir_node=True).visit(subroutine.body)
177188
content_new = None
189+
imports = FindNodes(ir.Import).visit(subroutine.spec)
190+
imp_map = {}
191+
parkind1_available = False
192+
substitutions = 0
193+
for imp in imports:
194+
if imp.module.lower() == 'parkind1':
195+
parkind1_available = True
196+
imp_map[imp] = imp.clone(symbols=imp.symbols + (imp.symbols[0].clone(name='JPRQ'),))
197+
# print(f"imp_map: {imp_map}")
198+
if not parkind1_available:
199+
with open(f"files_skipped_{subdir}.txt", "a") as myfile:
200+
myfile.write(f"{orig_file} since parkind1 not avail\n")
201+
print(f"no parkind1 in {str(sourcefile.path)} avail, thus early exit ...")
202+
return
178203
for node, literals in literal_nodes:
179204
# print(f"node.source: {node.source.string} | {type(node.source.string)}")
180205
literal_map = {}
181206
for literal in literals:
182207
if literal.kind is None and 'e' not in str(literal.value).lower() and 'd' not in str(literal.value).lower():
183-
literal_map[literal] = FloatLiteral(value=literal.value, kind='JPRB')
208+
literal_map[literal] = FloatLiteral(value=literal.value, kind=kind_spec)
184209
if literal_map:
185210
# fixed_node = SubstituteExpressions(literal_map).visit(node)
186211
fixed_node = node.source.string
@@ -190,7 +215,7 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
190215
# else:
191216
# comment = None
192217
for key in literal_map:
193-
fixed_node = re.sub(rf'{re.escape(str(key))}',
218+
fixed_node = re.sub(rf'{re.escape(str(key))}(?!(_{kind_spec}|_JP|[0-9]|[eEdD]))',
194219
str(literal_map[key]), fixed_node, flags = re.S)
195220
# indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
196221
# fixed_node_str = fgen(fixed_node, depth=indent)
@@ -199,12 +224,35 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
199224
fixed_node_str = str(fixed_node)
200225
# with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
201226
# f.write(fixed_node_str)
202-
content_new = re.sub(rf'{re.escape(node.source.string)}',
227+
# content_new = re.sub(rf'{re.escape(node.source.string)}(?!_JPRB)(?<!JPRB)$',
228+
content_new, subs = re.subn(rf'{re.escape(node.source.string)}(?!(_{kind_spec}|_JP|[0-9]|[eEdD]))',
203229
fixed_node_str, content, flags = re.S)
230+
substitutions += subs
204231
content = content_new
205232
# with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
206233
# f.write(content_new)
207-
if content_new is not None:
234+
if content_new is not None and subs > 0:
235+
_subroutine = content_new.lower().find(f'subroutine {subroutine.name.lower()}')
236+
_function = content_new.lower().find(f'function {subroutine.name.lower()}')
237+
if _subroutine != -1:
238+
index = _subroutine
239+
if _function != -1:
240+
index = _function
241+
if index != -1:
242+
for node in imp_map:
243+
fixed_node = node.source.string
244+
# fixed_node = re.sub(rf'{re.escape(str(key))}',
245+
# str(imp_map[node]), fixed_node, flags = re.S)
246+
# fixed_node_str = f'{str(fixed_node)}, JPRQ'
247+
fixed_node_str = str(fixed_node)
248+
if kind_spec not in fixed_node_str.upper():
249+
fixed_node_str = f'{str(fixed_node)}, {kind_spec}'
250+
print(f"fixed_node_str: {fixed_node_str}")
251+
content_new = re.sub(rf'{re.escape(node.source.string)}',
252+
fixed_node_str, content[index:], flags = re.S, count=1)
253+
# content[index::] = content_new
254+
# content = content[:index-1] + content_new
255+
content_new = content[:index] + content_new
208256
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
209257
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
210258
diff_str = '\n'.join(list(diff))

loki/lint/linter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def fix(self, sourcefile, file_report, backup_suffix=None, overwrite_config=None
226226
sourcefile = Fixer.fix(sourcefile, file_report.fixable_reports, config)
227227

228228
# Create the the source string for the output
229-
sourcefile.write(conservative=True)
229+
# sourcefile.write(conservative=True)
230230

231231

232232
class LinterTransformation(Transformation):
@@ -291,7 +291,15 @@ def check_and_fix_file(path, linter, fix=False, backup_suffix=None):
291291
if fix:
292292
linter.fix(source, report, backup_suffix=backup_suffix)
293293
except Exception as exc: # pylint: disable=broad-except
294-
linter.reporter.add_file_error(path, type(exc), str(exc))
294+
print(f"exc: {exc}")
295+
orig_file = str(path).replace('source/ifs-source/', '')
296+
subdir = orig_file.split('/')[0]
297+
with open(f"files_exception_{subdir}.txt", "a") as myfile:
298+
myfile.write(f"{orig_file}: {exc}\n")
299+
try:
300+
linter.reporter.add_file_error(path, type(exc), str(exc))
301+
except Exception as e:
302+
print(f" ... e: {e}")
295303
if loki_config['debug']:
296304
raise exc
297305
return False

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ license = {text = "Apache-2.0"}
1919
dynamic = ["version", "readme"]
2020
dependencies = [
2121
"numpy<1.24", # essential for tests, loop transformations and other dependencies
22-
"pymbolic>=2022.1", # essential for expression tree
22+
"pymbolic==2022.2", # essential for expression tree
2323
"PyYAML", # essential for loki-lint
2424
"pcpp", # essential for preprocessing
2525
"more-itertools", # essential for SCC transformation

0 commit comments

Comments
 (0)