1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 06:59:52 +01:00

manager: validate name on collection init: must start with wordchar and can contain wordchar or -

This commit is contained in:
Ilya Kreymer 2015-04-03 01:18:35 -07:00
parent 134b90eca5
commit a34607764e
2 changed files with 20 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import sys
import logging import logging
import heapq import heapq
import yaml import yaml
import re
from distutils.util import strtobool from distutils.util import strtobool
from pkg_resources import resource_string from pkg_resources import resource_string
@ -33,10 +34,15 @@ directory structure expected by pywb
DEF_INDEX_FILE = 'index.cdxj' DEF_INDEX_FILE = 'index.cdxj'
AUTO_INDEX_FILE = 'autoindex.cdxj' AUTO_INDEX_FILE = 'autoindex.cdxj'
COLL_RX = re.compile('^[\w][-\w]*$')
def __init__(self, coll_name, colls_dir='collections', must_exist=True): def __init__(self, coll_name, colls_dir='collections', must_exist=True):
self.default_config = load_yaml_config(DEFAULT_CONFIG) 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) self._set_coll_dirs(coll_name)
@ -75,11 +81,11 @@ directory structure expected by pywb
if not os.path.isdir(dirname): if not os.path.isdir(dirname):
os.mkdir(dirname) os.mkdir(dirname)
logging.info('Created Dir: ' + dirname) logging.info('Created Directory: ' + dirname)
def add_collection(self): def add_collection(self):
os.makedirs(self.curr_coll_dir) 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.archive_dir)
self._create_dir(self.indexes_dir) self._create_dir(self.indexes_dir)
@ -429,7 +435,7 @@ Create manage file based web archive collections
# Add default template # Add default template
def do_add_template(r): def do_add_template(r):
m = CollectionsManager(r.coll_name) m = CollectionsManager(r.coll_name, must_exist=False)
if r.add: if r.add:
m.add_template(r.add, r.force) m.add_template(r.add, r.force)
elif r.remove: elif r.remove:

View File

@ -507,7 +507,7 @@ class TestManagedColls(object):
main(['autoindex', 'auto']) main(['autoindex', 'auto'])
thread.join() thread.join()
# assert file was update # assert file was update
assert os.path.getmtime(index_file) > mtime assert os.path.getmtime(index_file) > mtime
@ -552,6 +552,15 @@ class TestManagedColls(object):
with raises(IOError): with raises(IOError):
main(['index', 'test', invalid_warc]) 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): def test_err_missing_dirs(self):
""" Test various errors with missing warcs dir, """ Test various errors with missing warcs dir,
missing cdx dir, non dir cdx file, and missing collections root missing cdx dir, non dir cdx file, and missing collections root