summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAurélien Bompard <aurelien@bompard.org>2013-06-06 21:59:37 +0200
committerAurélien Bompard <aurelien@bompard.org>2013-06-06 21:59:37 +0200
commit00033eefbb3f0f38711fd9788d9d062f6f78ec7f (patch)
treecfb16ab86a50328f9cee86e9b699b9234cafd43c
parented64d1d1db150ae99618b69fd008dbc82a92d2cd (diff)
downloadkittystore-00033eefbb3f0f38711fd9788d9d062f6f78ec7f.tar.gz
kittystore-00033eefbb3f0f38711fd9788d9d062f6f78ec7f.tar.xz
kittystore-00033eefbb3f0f38711fd9788d9d062f6f78ec7f.zip
Add index support to the import script
-rw-r--r--kittystore/import.py17
-rw-r--r--kittystore/scripts.py63
2 files changed, 55 insertions, 25 deletions
diff --git a/kittystore/import.py b/kittystore/import.py
index 34efa22..c296c42 100644
--- a/kittystore/import.py
+++ b/kittystore/import.py
@@ -33,7 +33,7 @@ from optparse import OptionParser
from random import randint
from email.utils import unquote
-from kittystore import get_store
+from kittystore.scripts import get_store_from_options, StoreFromOptionsError
PREFIX_RE = re.compile("^\[([\w\s_-]+)\] ")
@@ -225,6 +225,12 @@ def parse_args():
parser.add_option("-s", "--store", help="the URL to the store database")
parser.add_option("-l", "--list-name", help="the fully-qualified list "
"name (including the '@' symbol and the domain name")
+ parser.add_option("-i", "--search-index", metavar="PATH",
+ help="the path to the search index")
+ parser.add_option("--settings",
+ help="the Python path to a Django settings module")
+ parser.add_option("--pythonpath",
+ help="a directory to add to the Python path")
parser.add_option("-v", "--verbose", action="store_true",
help="show more output")
parser.add_option("-d", "--debug", action="store_true",
@@ -248,13 +254,16 @@ def parse_args():
for mbfile in args:
if not os.path.exists(mbfile):
parser.error("No such mbox file: %s" % mbfile)
- return opts, args
+ try:
+ store = get_store_from_options(opts)
+ except StoreFromOptionsError, e:
+ parser.error(e.args[0])
+ return store, opts, args
def main():
- opts, args = parse_args()
+ store, opts, args = parse_args()
print 'Importing messages from %s to database...' % opts.list_name
- store = get_store(opts.store, debug=opts.debug)
mlist = DummyMailingList(opts.list_name)
importer = DbImporter(mlist, store, opts)
for mbfile in args:
diff --git a/kittystore/scripts.py b/kittystore/scripts.py
index fa40d20..19fac23 100644
--- a/kittystore/scripts.py
+++ b/kittystore/scripts.py
@@ -33,22 +33,18 @@ from kittystore import get_store
#
-# Manual database update
+# Helpers
#
+class StoreFromOptionsError(Exception): pass
-def updatedb():
- parser = OptionParser(usage="%prog -s store_url")
- parser.add_option("-s", "--store", metavar="URL",
- help="the URL to the store database")
- parser.add_option("-i", "--search-index", metavar="PATH",
- help="the path to the search index")
- parser.add_option("--settings",
- help="the Python path to a settings module")
- parser.add_option("--pythonpath",
- help="a directory to add to the Python path")
- parser.add_option("-d", "--debug", action="store_true",
- help="show SQL queries")
- opts, args = parser.parse_args()
+def get_store_from_options(opts):
+ """
+ Returns a Store instance from an options object. Known options are;
+ - "store": the store URL
+ - "search_index": the search index path
+ - "settings": the Django settings module
+ - "pythonpath": an additional Python path to import the Django settings
+ """
django_settings = None
if opts.settings is not None:
if opts.pythonpath is not None:
@@ -56,23 +52,48 @@ def updatedb():
try:
django_settings = importlib.import_module(opts.settings)
except ImportError as e:
- parser.error("could not import settings '%s' (Is it on "
- "sys.path?): %s" % (opts.settings, e))
+ raise StoreFromOptionsError(
+ "could not import settings '%s' (Is it on "
+ "sys.path?): %s" % (opts.settings, e))
if opts.store is not None:
store_url = opts.store
elif getattr(django_settings, "KITTYSTORE_URL", None) is not None:
store_url = django_settings.KITTYSTORE_URL
else:
- parser.error("you must either specify a store URL (eg: "
- "sqlite:///kittystore.sqlite) or a Django configuration "
- "module (Python path to the settings module)")
+ raise StoreFromOptionsError(
+ "you must either specify a store URL (eg: "
+ "sqlite:///kittystore.sqlite) or a Django configuration "
+ "module (Python path to the settings module)")
if opts.search_index is None:
opts.search_index = getattr(django_settings, "KITTYSTORE_SEARCH_INDEX", None)
if args:
- parser.error("no arguments allowed.")
+ raise StoreFromOptionsError("no arguments allowed.")
+ return get_store(store_url, search=opts.search_index, debug=opts.debug)
+
+
+#
+# Manual database update
+#
+
+def updatedb():
+ parser = OptionParser(usage="%prog -s store_url")
+ parser.add_option("-s", "--store", metavar="URL",
+ help="the URL to the store database")
+ parser.add_option("-i", "--search-index", metavar="PATH",
+ help="the path to the search index")
+ parser.add_option("--settings",
+ help="the Python path to a Django settings module")
+ parser.add_option("--pythonpath",
+ help="a directory to add to the Python path")
+ parser.add_option("-d", "--debug", action="store_true",
+ help="show SQL queries")
+ opts, args = parser.parse_args()
print 'Upgrading the database schema and populating ' \
'the search index if necessary...'
- store = get_store(store_url, search=opts.search_index, debug=opts.debug)
+ try:
+ store = get_store_from_options(opts)
+ except StoreFromOptionsError, e:
+ parser.error(e.args[0])
version = list(store.db.execute(
"SELECT patch.version FROM patch "
"ORDER BY version DESC LIMIT 1"