Skip to content

Commit

Permalink
Merge pull request #300 from COSIMA/update_db_script
Browse files Browse the repository at this point in the history
Add a script to update a database
  • Loading branch information
micaeljtoliveira authored Jun 30, 2022
2 parents a657fba + eea4f03 commit 187d97a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
31 changes: 31 additions & 0 deletions cosima_cookbook/database_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import argparse
import pathlib

import cosima_cookbook as cc


def main(argv=None):
parser = argparse.ArgumentParser(description="Update COSIMA cookbook database.")
parser.add_argument(
"dirs", type=pathlib.Path, nargs="+", help="Directories to index."
)
parser.add_argument(
"-db",
"--database",
dest="db",
action="store",
default="cosima_master.db",
help="Database to update.",
)
args = parser.parse_args(argv)

print(cc)

print("Establishing a DB connection to: {}".format(args.db))
session = cc.database.create_session(args.db, timeout=30)

for dir in args.dirs:
print("Indexing: {}".format(dir))
cc.database.build_index(
dir, session, prune="delete", force=False, followsymlinks=True, nfiles=1000
)
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
'ipywidgets',
'lxml',
],

entry_points={
'console_scripts': [
'cosima_cookbook-update_db = cosima_cookbook.database_update:main',
]
},
extras_require = {
'build': ['distributed', 'pytest']
}
Expand Down
Binary file added test/data/update/experiment_a/test1.nc
Binary file not shown.
Binary file added test/data/update/experiment_b/test2.nc
Binary file not shown.
2 changes: 1 addition & 1 deletion test/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_find_files():

# Test works with alternative suffix
files = database.find_files("test/", "*.py")
assert len(files) == 8
assert len(files) == 9

for f in files:
assert Path(f).suffix == ".py"
Expand Down
13 changes: 13 additions & 0 deletions test/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import shlex
from cosima_cookbook import database_update


def test_database_update(tmpdir):

args = shlex.split(
"-db {db} test/data/update/experiment_a test/data/update/experiment_b".format(
db=tmpdir.join("test.db")
)
)

database_update.main(args)

0 comments on commit 187d97a

Please sign in to comment.