2015-11-25 21:00:33 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-03-13 17:09:03 +00:00
|
|
|
"""Checks that the peering details in this repo conform to a few basic rules."""
|
2015-11-25 21:00:33 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
REQUIRED_FIELDS = ['publicKey', 'password', 'contact']
|
2017-01-20 01:43:25 +00:00
|
|
|
RECOMMENDED_FIELDS = ['gpg', 'login']
|
2015-11-25 21:00:33 +00:00
|
|
|
|
2015-11-25 21:17:57 +00:00
|
|
|
RED = '\x1b[01;31m'
|
|
|
|
GREEN = '\x1b[01;32m'
|
2015-11-25 22:57:26 +00:00
|
|
|
YELLOW = '\x1b[01;33m'
|
2015-11-25 21:17:57 +00:00
|
|
|
END = '\x1b[0m'
|
2015-11-25 21:00:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate(path):
|
2016-03-13 17:09:03 +00:00
|
|
|
"""Test a single set of peering creds."""
|
2015-11-25 21:00:33 +00:00
|
|
|
print("Validating %s" % path)
|
|
|
|
try:
|
|
|
|
creds = open(path).read()
|
2016-03-13 17:46:55 +00:00
|
|
|
peers = json.loads(creds)
|
|
|
|
# Check formatting
|
|
|
|
pretty = json.dumps(peers, sort_keys=True, indent=4, separators=(',', ':'))
|
|
|
|
pretty = "%s\n" % pretty
|
|
|
|
formatting = True
|
|
|
|
if pretty != creds:
|
|
|
|
if "--clean" in sys.argv:
|
|
|
|
with open(path, 'w') as outfile:
|
|
|
|
outfile.write(pretty)
|
|
|
|
print(" %sJSON in %s has been fixed.%s" % (YELLOW, path, END))
|
|
|
|
else:
|
2016-07-17 02:56:38 +00:00
|
|
|
print(" %sJSON in %s is NOT properly formatted! This is really easy to fix!"
|
|
|
|
" just run tests.py --clean.%s" % (YELLOW, path, END))
|
2016-03-13 17:46:55 +00:00
|
|
|
formatting = False
|
2015-11-25 21:00:33 +00:00
|
|
|
hosts = peers.keys()
|
|
|
|
for host in hosts:
|
|
|
|
for field in REQUIRED_FIELDS:
|
2016-03-13 17:09:03 +00:00
|
|
|
if field not in peers[host]:
|
2015-11-25 22:57:26 +00:00
|
|
|
print(" %sHost %s is missing the required field %s%s" % (RED, host,
|
|
|
|
field, END))
|
2015-11-25 21:00:33 +00:00
|
|
|
return False
|
2015-11-25 22:57:26 +00:00
|
|
|
for field in RECOMMENDED_FIELDS:
|
2016-03-13 17:09:03 +00:00
|
|
|
if field not in peers[host]:
|
2015-11-25 22:57:26 +00:00
|
|
|
print(" %sHost %s is missing the recommended field %s%s" % (YELLOW, host,
|
|
|
|
field, END))
|
2016-03-13 17:51:08 +00:00
|
|
|
if formatting:
|
|
|
|
return True
|
|
|
|
else:
|
2016-03-13 17:46:55 +00:00
|
|
|
return False
|
2015-11-25 21:00:33 +00:00
|
|
|
except ValueError:
|
|
|
|
print(" %sInvalid JSON!%s" % (RED, END))
|
|
|
|
return False
|
|
|
|
|
2015-11-26 03:09:59 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
success = True
|
2016-03-13 17:46:55 +00:00
|
|
|
for directory, subdirs, files in os.walk('.'):
|
|
|
|
if len(files) > 0:
|
|
|
|
if directory != '.' and not directory.startswith('./.git'):
|
2015-11-26 03:09:59 +00:00
|
|
|
for f in files:
|
2016-03-13 17:46:55 +00:00
|
|
|
result = validate("%s/%s" % (directory, f))
|
|
|
|
if not result:
|
|
|
|
success = False
|
2015-11-26 03:09:59 +00:00
|
|
|
if not success:
|
|
|
|
sys.exit(1)
|