Skip to content
Snippets Groups Projects
Commit 988bc7c3 authored by Greg Wilson's avatar Greg Wilson
Browse files

Tidying up utilities

parent 55cb0fa5
Branches
Tags
No related merge requests found
......@@ -38,7 +38,7 @@ clean :
## workshop-check : check workshop homepage.
workshop-check :
bin/workshop_check.py index.html
@bin/workshop_check.py .
## ----------------------------------------
## Commands specific to lesson websites.
......@@ -67,7 +67,7 @@ HTML_FILES = \
## lesson-check : validate lesson Markdown.
lesson-check :
bin/lesson_check.py -s . -p bin/markdown-ast.rb
@bin/lesson_check.py -s . -p bin/markdown-ast.rb
unittest :
python bin/test_lesson_check.py
......
......@@ -119,11 +119,13 @@ def parse_args():
def check_config(args):
"""Check configuration file."""
config_file = os.path.join(args.source_dir, '_config.yml')
with open(config_file, 'r') as reader:
config = yaml.load(reader)
args.reporter.check_field(config_file, 'configuration', config, 'kind', 'lesson')
try:
config_file = os.path.join(args.source_dir, '_config.yml')
with open(config_file, 'r') as reader:
config = yaml.load(reader)
args.reporter.check_field(config_file, 'configuration', config, 'kind', 'lesson')
except FileNotFoundError as e:
args.reporter.add('_config.yml', 'Missing required file')
def read_all_markdown(args, source_dir):
......@@ -176,11 +178,10 @@ def check_fileset(source_dir, reporter, filenames_present):
"""Are all required files present? Are extraneous files present?"""
# Check files with predictable names.
required = [p[1].replace('%', source_dir) for p in REQUIRED_FILES]
missing = set(required) - set(filenames_present)
for m in missing:
reporter.add(None, 'Missing required file {0}', m)
reporter.add(m, 'Missing required file')
# Check episode files' names.
seen = []
......@@ -191,7 +192,7 @@ def check_fileset(source_dir, reporter, filenames_present):
if m and m.group(1):
seen.append(m.group(1))
else:
reporter.add(None, 'Episode {0} has badly-formatted filename', filename)
reporter.add(filename, 'Episode has badly-formatted filename')
# Check episode filename numbering.
reporter.check(len(seen) == len(set(seen)),
......
......@@ -3,9 +3,10 @@ import unittest
import lesson_check
import util
class TestFileList(unittest.TestCase):
def setUp(self):
self.reporter = util.Reporter(None) ## TODO: refactor reporter class.
self.reporter = util.Reporter(None)
def test_file_list_has_expected_entries(self):
# For first pass, simply assume that all required files are present
......@@ -15,5 +16,6 @@ class TestFileList(unittest.TestCase):
lesson_check.check_fileset('', self.reporter, all_filenames)
self.assertEqual(len(self.reporter.messages), 0)
if __name__ == "__main__":
unittest.main()
......@@ -47,5 +47,5 @@ class Reporter(object):
if not self.messages:
return
for m in self.messages:
for m in sorted(self.messages):
print(m, file=stream)
......@@ -28,7 +28,7 @@ console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# TODO: these regexp patterns need comments inside
# FIXME: these regexp patterns need comments inside
EMAIL_PATTERN = r'[^@]+@[^@]+\.[^@]+'
HUMANTIME_PATTERN = r'((0?[1-9]|1[0-2]):[0-5]\d(am|pm)(-|to)(0?[1-9]|1[0-2]):[0-5]\d(am|pm))|((0?\d|1\d|2[0-3]):[0-5]\d(-|to)(0?\d|1\d|2[0-3]):[0-5]\d)'
EVENTBRITE_PATTERN = r'\d{9,10}'
......@@ -400,12 +400,15 @@ def check_file(filename, data, errors):
def check_config(filename, errors):
'''Check YAML configuration file.'''
with open(filename, 'r') as reader:
config = yaml.load(reader)
try:
with open(filename, 'r') as reader:
config = yaml.load(reader)
if config['kind'] != 'workshop':
msg = 'Not configured as a workshop: found "{0}" instead'.format(config['kind'])
add_error(msg, errors)
if config['kind'] != 'workshop':
msg = 'Not configured as a workshop: found "{0}" instead'.format(config['kind'])
add_error(msg, errors)
except FileNotFoundError as e:
add_error('_config.yml not found', errors)
def main():
......@@ -415,15 +418,19 @@ def main():
print(USAGE, file=sys.stderr)
sys.exit(1)
index_file = sys.argv[1]
config_file = os.path.join(os.path.dirname(index_file), '_config.yml')
root_dir = sys.argv[1]
index_file = os.path.join(root_dir, 'index.html')
config_file = os.path.join(root_dir, '_config.yml')
logger.info('Testing "{0}" and "{1}"'.format(index_file, config_file))
errors = []
check_config(config_file, errors)
with open(index_file) as reader:
data = reader.read()
check_file(index_file, data, errors)
try:
with open(index_file) as reader:
data = reader.read()
check_file(index_file, data, errors)
except FileNotFoundError as e:
add_error('index.html not found', errors)
if errors:
for m in errors:
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment