Skip to content

Instantly share code, notes, and snippets.

@junaruga
Last active February 6, 2017 12:23
Show Gist options
  • Select an option

  • Save junaruga/b9270cdefad7a53d3e6aacfd4f7ccd74 to your computer and use it in GitHub Desktop.

Select an option

Save junaruga/b9270cdefad7a53d3e6aacfd4f7ccd74 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
def initilize_scl_dict():
return {
'line_count': 0,
'found_set_e': False,
'status': True,
'hear_doc': {
'in': False,
'word': None,
}
}
def is_ignored_line(line):
return re.match(r'^#', line) or re.match(r'^\s*$', line)
def analyze(scl_dict):
if 'line_count' not in scl_dict:
raise ValueError('scl_dict is invalid.')
if 'found_set_e' not in scl_dict:
raise ValueError('scl_dict is invalid.')
status = True
if scl_dict['line_count'] > 1 and not scl_dict['found_set_e']:
status = False
return status
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {0} <spec_file>".format(sys.argv[0]))
sys.exit(1)
spec_file = sys.argv[1]
results = []
with open(spec_file) as f:
is_in_scl = False
scl_dict = None
for line in f:
line = line.rstrip()
m_here_doc_start = re.match(r'^.*<<-?\s*(\w+)\s*$', line)
if is_ignored_line(line):
continue
if re.match(r'^%{\?scl:scl enable', line):
# scl macro start line
is_in_scl = True
scl_dict = initilize_scl_dict()
elif re.match(r'^%{\?scl:[\w"\']+}', line):
# scl macro end line
is_in_scl = False
scl_dict['status'] = analyze(scl_dict)
results.append(scl_dict)
elif is_in_scl:
if re.match(r'^set -e$', line):
scl_dict['found_set_e'] = True
elif re.match(r'^.*\\\s*$', line):
# Skip back slash in the end of line.
pass
elif m_here_doc_start:
# here document start line.
scl_dict['hear_doc']['in'] = True
scl_dict['hear_doc']['word'] = m_here_doc_start.group(1)
elif (scl_dict['hear_doc']['in'] and
line == scl_dict['hear_doc']['word']):
# here document end line.
scl_dict['hear_doc']['in'] = False
scl_dict['line_count'] += 1
elif scl_dict['hear_doc']['in']:
# Skip here document line except the end line.
pass
else:
scl_dict['line_count'] += 1
for scl_dict in results:
status_str = 'OK' if scl_dict['status'] else 'NG'
print('{}:[{}][{}]'.format(spec_file, status_str,
scl_dict['line_count']))
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment