diff --git a/pywb/manager/manager.py b/pywb/manager/manager.py index cb42df0e..dfcdb502 100644 --- a/pywb/manager/manager.py +++ b/pywb/manager/manager.py @@ -4,6 +4,7 @@ import sys import logging import heapq import yaml +import re from distutils.util import strtobool from pkg_resources import resource_string @@ -33,10 +34,15 @@ directory structure expected by pywb DEF_INDEX_FILE = 'index.cdxj' AUTO_INDEX_FILE = 'autoindex.cdxj' + COLL_RX = re.compile('^[\w][-\w]*$') + def __init__(self, coll_name, colls_dir='collections', must_exist=True): self.default_config = load_yaml_config(DEFAULT_CONFIG) - self.colls_dir = colls_dir + if coll_name and not self.COLL_RX.match(coll_name): + raise ValueError('Invalid Collection Name: ' + coll_name) + + self.colls_dir = os.path.join(os.getcwd(), colls_dir) self._set_coll_dirs(coll_name) @@ -75,11 +81,11 @@ directory structure expected by pywb if not os.path.isdir(dirname): os.mkdir(dirname) - logging.info('Created Dir: ' + dirname) + logging.info('Created Directory: ' + dirname) def add_collection(self): os.makedirs(self.curr_coll_dir) - logging.info('Created directory: ' + self.curr_coll_dir) + logging.info('Created Directory: ' + self.curr_coll_dir) self._create_dir(self.archive_dir) self._create_dir(self.indexes_dir) @@ -429,7 +435,7 @@ Create manage file based web archive collections # Add default template def do_add_template(r): - m = CollectionsManager(r.coll_name) + m = CollectionsManager(r.coll_name, must_exist=False) if r.add: m.add_template(r.add, r.force) elif r.remove: diff --git a/tests/test_auto_colls.py b/tests/test_auto_colls.py index 4b70a315..46de3823 100644 --- a/tests/test_auto_colls.py +++ b/tests/test_auto_colls.py @@ -507,7 +507,7 @@ class TestManagedColls(object): main(['autoindex', 'auto']) thread.join() - + # assert file was update assert os.path.getmtime(index_file) > mtime @@ -552,6 +552,15 @@ class TestManagedColls(object): with raises(IOError): main(['index', 'test', invalid_warc]) + def test_err_invalid_name(self): + """ Invalid collection name + """ + with raises(ValueError): + main(['init', '../abc%']) + + with raises(ValueError): + main(['init', '45^23']) + def test_err_missing_dirs(self): """ Test various errors with missing warcs dir, missing cdx dir, non dir cdx file, and missing collections root