Skip to content

Commit

Permalink
Merge pull request #13 from zerwes/issue12
Browse files Browse the repository at this point in the history
skip inline var blocks
closes #12
  • Loading branch information
zerwes authored Nov 21, 2022
2 parents 904ecbc + 2255033 commit f9dd692
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 59 deletions.
5 changes: 5 additions & 0 deletions example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
- name: ansible fqcn test playbook
hosts: localhost
gather_facts: true
vars:
xyz: abc
num: 1
deploy: true
domain: example.com
tasks:
- name: test task 1
command: cat dog
Expand Down
5 changes: 5 additions & 0 deletions exampleconverted.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
- name: ansible fqcn test playbook
hosts: localhost
gather_facts: true
vars:
xyz: abc
num: 1
deploy: true
domain: example.com
tasks:
- name: test task 1
# possible ambiguous replacement: command : ansible.builtin.command | community.ciscosmb.command | community.routeros.command
Expand Down
152 changes: 93 additions & 59 deletions fqcn-fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def checkignoreregex(checkline):
return True
return False

def checkstartexcludeblock(checkline):
"""check if line starts a exclude block"""
for exre in _general_exclude_blocks_regex:
if exre.match(checkline):
return True
return False

class Dumper(yaml.Dumper): # pylint: disable=too-many-ancestors
"""https://github.com/yaml/pyyaml/issues/234"""
def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyword-arg-before-vararg,unused-argument
Expand Down Expand Up @@ -83,6 +90,11 @@ def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyw
re.compile(r'\s*-\srole:\s*\w+'),
]

# list of starting regex to exclude complete subsequent blocks
_general_exclude_blocks_regex = [
re.compile(r'\s*-*\s*vars:$'),
]

required_fqcnconverter_file_version = '0.1.2'

argparser = argparse.ArgumentParser(description=__doc__)
Expand Down Expand Up @@ -320,79 +332,101 @@ def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyw
startingwhitespaces4comments = 0
in_task = False
in_task_done = False
in_ignore_block = False
startingwhitespaces_ignore_block = None
fqcnregex = _fqcnregex
for line in fi:
if args.debug:
debugmsg(
'STARTLINE : line: %s in_task: %s in_task_done: %s\n' %
'\n\n\nSTARTLINE : line: %s in_task: %s in_task_done: %s\n' %
(line, in_task, in_task_done,)
)
if args.printdiff:
originallines.append(line)
nline = line
taskmatch = _taskstartregex.match(line)
if taskmatch:
in_task_done = False
in_task = False
if in_task_done and not taskmatch:
if checkstartexcludeblock(line):
in_ignore_block = True
startingwhitespaces_ignore_block = re.match(r'\s*-?\s*', line).group()
if args.debug:
debugmsg('SKIPLINE! %s\n' % (in_task_done and not in_task))
print('.', file=sys.stderr, end='', flush=True)
else:
if not in_task:
if args.debug:
debugmsg('TASKMATCH : line: %s taskmatch: %s\n' % (line, taskmatch,))
if taskmatch:
in_task = True
in_task_done = False
fqcnregex = _fqcnregex
startingwhitespaces = r'\s*-?\s+'
startingwhitespacesaftertask = len(taskmatch.group('white'))
if args.debug:
debugmsg('line: %s taskmatch: %s' % (line, taskmatch,))
debugmsg(
'startingwhitespaces "%s" startingwhitespacesaftertask "%s"' %
(startingwhitespaces, startingwhitespacesaftertask,)
)
fqcnmatch = fqcnregex.match(line)
if args.debug:
debugmsg('FQCNMATCH : line: %s fqcnmatch: %s\n' % (line, fqcnmatch,))
debugmsg('fqcnregex: %s' % fqcnregex)
if fqcnmatch and not checkignoreregex(line):
in_task_done = True
in_task = False
fqcnmodule = fqcnmatch.group('module')
nline = re.sub(
'^(%s)%s:' % (startingwhitespaces, fqcnmodule),
'\\1%s:' % fqcndict[fqcnmodule][0],
line
debugmsg(
'start exclude block : startingwhitespaces_ignore_block : "%s"' %
startingwhitespaces_ignore_block
)
if fqcnmodule == fqcndict[fqcnmodule][0]:
print('.', file=sys.stderr, end='', flush=True)
else:
print('*', file=sys.stderr, end='', flush=True)
if len(fqcndict[fqcnmodule]) > 1:
wtxt = ('possible ambiguous replacement: %s : %s' %
(fqcnmodule, ' | '.join(fqcndict[fqcnmodule])))
warnings.append(wtxt)
if args.writewarnings:
if args.writefiles:
print('%s# %s' % (' '*startingwhitespaces4comments, wtxt))
if args.printdiff:
changedlines.append(
'%s# %s\n' % (' '*startingwhitespaces4comments, wtxt)
)
elif in_ignore_block:
if re.match(r'%s\w+' % startingwhitespaces_ignore_block, line):
in_ignore_block = False
startingwhitespaces_ignore_block = None
if args.debug:
debugmsg('end exclude block!')
else:
if args.debug:
debugmsg('... in exclude block ... ignore line')
if not in_ignore_block:
taskmatch = _taskstartregex.match(line)
if taskmatch:
if args.debug:
debugmsg('taskmatch: %s' % taskmatch)
in_task_done = False
in_task = False
if in_task_done and not taskmatch:
if args.debug:
debugmsg('SKIPLINE! %s\n' % (in_task_done and not in_task))
print('.', file=sys.stderr, end='', flush=True)
if startingwhitespacesaftertask > 0:
startingwhitespaces = ' ' * startingwhitespacesaftertask
startingwhitespaces4comments = startingwhitespacesaftertask
startingwhitespacesaftertask = 0
fqcnregex = re.compile('^%s(?P<module>%s):' %
(startingwhitespaces, '|'.join(fqcndict.keys()))
)
else:
if not in_task:
if args.debug:
debugmsg('set STARTINGWHITESPACES to "%s"' % startingwhitespaces)
debugmsg('TASKMATCH : line: %s taskmatch: %s\n' % (line, taskmatch,))
if taskmatch:
in_task = True
in_task_done = False
fqcnregex = _fqcnregex
startingwhitespaces = r'\s*-?\s+'
startingwhitespacesaftertask = len(taskmatch.group('white'))
if args.debug:
debugmsg('line: %s taskmatch: %s' % (line, taskmatch,))
debugmsg(
'startingwhitespaces "%s" startingwhitespacesaftertask "%s"' %
(startingwhitespaces, startingwhitespacesaftertask,)
)
fqcnmatch = fqcnregex.match(line)
if args.debug:
debugmsg('FQCNMATCH : line: %s fqcnmatch: %s\n' % (line, fqcnmatch,))
debugmsg('fqcnregex: %s' % fqcnregex)
if fqcnmatch and not checkignoreregex(line):
in_task_done = True
in_task = False
fqcnmodule = fqcnmatch.group('module')
nline = re.sub(
'^(%s)%s:' % (startingwhitespaces, fqcnmodule),
'\\1%s:' % fqcndict[fqcnmodule][0],
line
)
if fqcnmodule == fqcndict[fqcnmodule][0]:
print('.', file=sys.stderr, end='', flush=True)
else:
print('*', file=sys.stderr, end='', flush=True)
if len(fqcndict[fqcnmodule]) > 1:
wtxt = ('possible ambiguous replacement: %s : %s' %
(fqcnmodule, ' | '.join(fqcndict[fqcnmodule])))
warnings.append(wtxt)
if args.writewarnings:
if args.writefiles:
print('%s# %s' % (' '*startingwhitespaces4comments, wtxt))
if args.printdiff:
changedlines.append(
'%s# %s\n' % (' '*startingwhitespaces4comments, wtxt)
)
else:
print('.', file=sys.stderr, end='', flush=True)
if startingwhitespacesaftertask > 0:
startingwhitespaces = ' ' * startingwhitespacesaftertask
startingwhitespaces4comments = startingwhitespacesaftertask
startingwhitespacesaftertask = 0
fqcnregex = re.compile('^%s(?P<module>%s):' %
(startingwhitespaces, '|'.join(fqcndict.keys()))
)
if args.debug:
debugmsg('set STARTINGWHITESPACES to "%s"' % startingwhitespaces)

if args.writefiles:
print(nline, end='')
Expand Down

0 comments on commit f9dd692

Please sign in to comment.