Skip to content

Instantly share code, notes, and snippets.

@bdowling
Created July 27, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdowling/56fa6d173a825923fe439022aac69480 to your computer and use it in GitHub Desktop.
Save bdowling/56fa6d173a825923fe439022aac69480 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
import argparse
import csv
import re
import os
import sys
def warn(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def load_indexdata(index):
"""Load data from index file."""
index_data = []
headers = []
with open(index) as indexfs:
line = indexfs.readline()
while re.match('^\s*$', line) or line.startswith('#') or line.startswith('Template,'):
headers.append(line)
line = indexfs.readline()
# unslurp the last line that wasn't a header
indexfs.seek(indexfs.tell()-len(line))
indexbuffer = [ line for line in indexfs if not re.match('^\s*$', line) ]
noncsvlines = [ line for line in indexbuffer if len(re.findall(',', line))<3 ]
if len(noncsvlines):
warn("WARNING Non-CSV lines will be lost in sort:\n\n{}\n".format(''.join(noncsvlines)))
sys.exit(1)
data = csv.reader(indexbuffer)
for row in data:
if len(row) > 2:
index_data.append(row)
else:
warn("Unparsed row detected, will be lost in sort")
sys.exit(1)
return index_data, headers
def check_template_names(indexfile):
index, headers = load_indexdata(indexfile)
osname = []
short = []
for row in index:
template = re.sub('\.template$', '', row[0].strip())
tprefix = '_'.join(template.split('_')[:2])
cmd = ' '.join(template.split('_')[2:])
cmd_full = re.sub(r'\[\[|\]\]','', row[3].strip())
os = row[2].strip()
if (os != tprefix):
osname.append("Template prefix '{}' does not match os '{}'".format(tprefix, os))
if (cmd != cmd_full):
short.append("%30s vs %-30s" % ( cmd, cmd_full ) )
print("OS Name differs:")
print('\n'.join(osname))
print("\n")
print("Commands differ from template:")
print('\n'.join(short))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='''
Check the ntc-templates template names in index and commands they match with"
''')
parser.add_argument("--index", type=str, help="ntc-templates index file to sort", default='./templates/index')
args = parser.parse_args()
check_template_names(args.index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment