Browse Source

Set up bootstrap script and reuse in test fixtures.

Stefano Cossu 6 years ago
parent
commit
eeee476291
4 changed files with 58 additions and 16 deletions
  1. 3 10
      conftest.py
  2. 2 0
      data/bootstrap/simple_layout.nq
  3. 1 1
      doc/notes/TODO
  4. 52 5
      util/bootstrap.py

+ 3 - 10
conftest.py

@@ -10,9 +10,8 @@ from PIL import Image
 
 from lakesuperior.app import create_app
 from lakesuperior.config_parser import config
-from lakesuperior.store_layouts.rdf.graph_store_connector import \
-        GraphStoreConnector
 from util.generators import random_image
+from util.bootstrap import bootstrap_db, bootstrap_binary_store
 
 
 @pytest.fixture(scope='module')
@@ -27,14 +26,8 @@ def db(app):
     '''
     Set up and tear down test triplestore.
     '''
-    dbconf = app.config['store']['ldp_rs']
-    db = GraphStoreConnector(
-            query_ep=dbconf['webroot'] + dbconf['query_ep'],
-            update_ep=dbconf['webroot'] + dbconf['update_ep'],
-            autocommit=True)
-
-    db.ds.default_context.parse(source='data/bootstrap/simple_layout.nq',
-            format='nquads')
+    db = bootstrap_db(app)
+    bootstrap_binary_store(app)
 
     yield db
 

+ 2 - 0
data/bootstrap/simple_layout.nq

@@ -1,3 +1,5 @@
+# Bootstrap data set for default RDF layout.
+#
 # This needs to be in N-Quads format because of
 # https://github.com/RDFLib/rdflib/issues/436
 

+ 1 - 1
doc/notes/TODO

@@ -23,7 +23,7 @@
   - [W] Toolbox
   - [ ] Storage layer (RDF + file)
   - [ ] LDP layer
-- [W] Bootstrap
+- [D] Bootstrap
 - [ ] Optimize queries
 - [ ] Messaging SPI
 - [ ] Hook up Hyrax

+ 52 - 5
util/bootstrap.py

@@ -1,16 +1,63 @@
 #!/usr/bin/env python
 
+import os
+import shutil
+import sys
+sys.path.append('.')
+
+from lakesuperior.app import create_app
 from lakesuperior.config_parser import config
+from lakesuperior.store_layouts.rdf.graph_store_connector import \
+        GraphStoreConnector
+from lakesuperior.model.ldpr import Ldpr
 
 # This script will parse configuration files and initialize a filesystem and
 # triplestore with an empty FCREPO repository.
+# It is used in test suites and on a first run.
 #
 # Additional, scaffolding files may be parsed to create initial contents.
 
-# @TODO
 
-# Initialize temporary folders.
-tmp_path = config['application']['store']['ldp_nr']['path'] + '/tmp'
-if not os.path.exists(tmp_path):
-    os.makedirs(tmp_path)
+def bootstrap_db(app):
+    '''
+    Initialize RDF store.
+    '''
+    dbconf = app.config['store']['ldp_rs']
+    print('Resetting RDF store to base data set: {}'.format(dbconf['webroot']))
+    db = GraphStoreConnector(
+            query_ep=dbconf['webroot'] + dbconf['query_ep'],
+            update_ep=dbconf['webroot'] + dbconf['update_ep'],
+            autocommit=True)
+
+    # @TODO Make configurable.
+    db.ds.default_context.parse(source='data/bootstrap/simple_layout.nq',
+            format='nquads')
+
+    return db
+
+
+def bootstrap_binary_store(app):
+    '''
+    Initialize binary file store.
+    '''
+    root_path = app.config['store']['ldp_nr']['path']
+    print('Removing binary store path: {}'.format(root_path))
+    try:
+        shutil.rmtree(root_path)
+    except FileNotFoundError:
+        pass
+    os.makedirs(root_path + '/tmp')
+
+
+if __name__=='__main__':
+    sys.stdout.write(
+            'This operation will WIPE ALL YOUR DATA. Are you sure? '
+            '(Please type `yes` to continue) > ')
+    choice = input().lower()
+    if choice != 'yes':
+        print('Aborting.')
+        sys.exit()
 
+    app = create_app(config['application'], config['logging'])
+    bootstrap_db(app)
+    bootstrap_binary_store(app)