Unverified Commit 00709e2f authored by Julia Kaufhold's avatar Julia Kaufhold Committed by GitHub
Browse files

Merge pull request #129 from stefan-wolfsheimer/b2share

B2share
parents feb9ed1e 3580b4db
......@@ -14,11 +14,12 @@ from b2shareclient import B2shareClient
from configuration import Configuration
from manifest import IRODSUtils
logger = logging.getLogger('B2shareClientCLI')
def draft(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Start creating draft ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -74,7 +75,8 @@ def getCommunityIDByName(configuration, community_name):
return community_id
def getAllCommunities(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Start get all communities ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -94,8 +96,10 @@ def getAllCommunities(args):
def collectPIDsForCollection(collectionPath, configuration):
PIDobjectsString = '['
irodsu = IRODSUtils(configuration.irods_home_dir, logger, configuration.irods_debug)
rc, res = irodsu.deepListDir(collectionPath)
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
irods_env=configuration.irodsenv)
res = irodsu.deepListDir(collectionPath)
if not res:
return None
filePathsMap = None
......@@ -130,7 +134,8 @@ def collectFilePathsFromTree(filesTree):
return filePaths
def addMetadata(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Adding metadata ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -140,14 +145,17 @@ def addMetadata(args):
configuration.access_token = accessToken
irodsu = IRODSUtils(configuration.irods_home_dir, logger, configuration.irods_debug)
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
irods_env=configuration.irodsenv)
metadata_file = irodsu.getFile(args.metadata)
b2shcl = B2shareClient(configuration)
b2shcl.addB2shareMetadata(args.record_id, metadata_file)
logger.info("Added metadata")
def publish(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Publishing ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -162,9 +170,11 @@ def publish(args):
#get access_token from users metadata in iRODS
def getAccessTokenWithConfigs(configuration, args):
irodsu = IRODSUtils(configuration.irods_home_dir, logger, configuration.irods_debug)
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
irods_env=configuration.irodsenv)
if irodsu:
users_metadata = irodsu.getMetadata(args.userName, "access_token", '-u')
users_metadata = irodsu.getUserMetadata(args.userName, "access_token")
if users_metadata:
return users_metadata[0]
else:
......@@ -173,7 +183,8 @@ def getAccessTokenWithConfigs(configuration, args):
return None
def getCommunitySchema(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Get Community Schema ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -193,7 +204,8 @@ def getCommunitySchema(args):
return schema
def getDraftByID(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("Get draft by ID ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -210,7 +222,8 @@ def getDraftByID(args):
return draft
def deleteDraft(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun, logger)
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
configuration.parseConf()
logger.info("DELETING DRAFT: " + args.draft_to_delete_id)
accessToken = getAccessTokenWithConfigs(configuration, args)
......@@ -231,7 +244,7 @@ if __name__ == "__main__":
parser.add_argument("-d", "--dryrun", action="store_true",
help="run without performing any real change")
parser.add_argument("-u", "--userName", help="iRODS user name")
parser.add_argument("--irodsenv", help="Path to irods configuration")
subparsers = parser.add_subparsers(help='sub-command help', dest='subcmd')
parser_draft = subparsers.add_parser('draft', help='create a draft record in B2Share')
......
......@@ -17,12 +17,14 @@ class Configuration():
irods_home_dir = ""
irods_debug = ""
def __init__(self, conffile, debug, dryrun, logger):
def __init__(self, conffile, debug, dryrun, logger,
irodsenv=None):
self.conffile = conffile
self.debug = debug
self.dryrun = dryrun
self.logger = logger
self.access_token = ""
self.irodsenv = irodsenv
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG, \
'ERROR': logging.ERROR, 'WARNING': logging.WARNING, \
'CRITICAL': logging.CRITICAL}
......@@ -102,4 +104,3 @@ class Configuration():
else:
self.logger.error('missing section %s: with parameter %s' % (section, option))
return None
\ No newline at end of file
This diff is collapsed.
......@@ -30,6 +30,7 @@ class argsMock():
draft_to_delete_id = "someID123"
debug = True
dryrun = False
irodsenv = None
class B2shareClientCLITest(unittest.TestCase):
def setUp(self):
......
......@@ -2,12 +2,17 @@
# -*- coding: utf-8 -*-
import unittest
import os
from os.path import dirname
from os.path import abspath
import logging
import sys
logger = logging.getLogger('ConfigurationTest')
sys.path.append("../../cmd")
sys.path.insert(0,
os.path.join(dirname(dirname(dirname(dirname(abspath(__file__))))),
"cmd"))
from configuration import Configuration
class ConfigurationTest(unittest.TestCase):
......@@ -118,4 +123,4 @@ class ConfigurationTest(unittest.TestCase):
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
unittest.main()
import unittest
import os
import sys
from os.path import dirname
from os.path import abspath
from os.path import join
from irods.models import DataObject
from irods.models import Collection
from irods.meta import iRODSMeta
sys.path.insert(0,
join(dirname(dirname(dirname(dirname(abspath(__file__))))),
"cmd"))
from manifest.irodsUtility import IRODS # noqa: E402
from manifest.irodsUtility import IRODSUtils as IRODSUtils # noqa: E402
ENV = None
class TestIRODSUtils(unittest.TestCase):
# functional test
# in order to execute the test we need a valid connection to
# an iRODS instance
# configured in ~/.irods/irods_enviornment.json
# getMetadata(file, PID)
# getMetadata(user, access_token, -u)
# getFile
# deepListDir
def setUp(self):
irodsu = IRODSUtils(irods_env=ENV)
with IRODS(irods_config_file=irodsu.irods_env,
irods_auth_file=irodsu.irods_auth) as session:
self.coll_path = '/{}/home/{}/test_dir'.format(session.zone,
session.username)
self.irods_username = session.username
session.collections.create(self.coll_path)
session.cleanup()
def tearDown(self):
'''Remove test data and close connections
'''
irodsu = IRODSUtils(irods_env=ENV)
with IRODS(irods_config_file=irodsu.irods_env,
irods_auth_file=irodsu.irods_auth) as session:
coll = session.collections.get(self.coll_path)
coll.remove(recurse=True, force=True)
session.cleanup()
def test_put_get(self):
testfile = os.path.join(dirname(abspath(__file__)),
"testfile.txt")
with open(testfile, 'r') as fp:
testfile_content = fp.read()
remote_file = self.coll_path + "/TESTFILE"
irodsu = IRODSUtils(irods_env=ENV)
irodsu.putFile(testfile, remote_file)
content = irodsu.getFile(remote_file)
self.assertEqual(content,
testfile_content)
def test_get_metadata(self):
testfile = os.path.join(dirname(abspath(__file__)),
"testfile.txt")
remote_file = self.coll_path + "/TESTFILE"
irodsu = IRODSUtils(irods_env=ENV)
irodsu.putFile(testfile, remote_file)
with IRODS(irods_config_file=irodsu.irods_env,
irods_auth_file=irodsu.irods_auth) as session:
session.metadata.add(DataObject,
remote_file,
iRODSMeta("attr1", "value1"))
session.metadata.add(Collection,
self.coll_path,
iRODSMeta("attr2", "value2"))
self.assertEqual(irodsu.getMetadata(remote_file, "attr1"),
["value1"])
self.assertEqual(irodsu.getMetadata(self.coll_path, "attr2"),
["value2"])
self.assertEqual(irodsu.getAllMetadata(remote_file).get('attr1'),
'value1')
self.assertEqual(irodsu.getAllMetadata(self.coll_path).get('attr2'),
'value2')
def test_deep_list_dir(self):
testfile = os.path.join(dirname(abspath(__file__)),
"testfile.txt")
a = '{}/a'.format(self.coll_path)
ab = '{}/a/b'.format(self.coll_path)
abc = '{}/a/b/c'.format(self.coll_path)
abd = '{}/a/b/d'.format(self.coll_path)
irodsu = IRODSUtils(irods_env=ENV)
with IRODS(irods_config_file=irodsu.irods_env,
irods_auth_file=irodsu.irods_auth) as session:
session.collections.create(a)
session.collections.create(ab)
session.collections.create(abc)
session.collections.create(abd)
session.cleanup()
irodsu = IRODSUtils(irods_env=ENV)
irodsu.putFile(testfile, a + "/TESTFILE")
irodsu.putFile(testfile, ab + "/TESTFILE")
irodsu.putFile(testfile, abc + "/TESTFILE")
irodsu.putFile(testfile, abd + "/TESTFILE")
result = irodsu.deepListDir(a, False)
self.assertEqual(result, {'a': {'__files__': ['TESTFILE'],
'b': {'__files__': ['TESTFILE'],
'c': {'__files__':
['TESTFILE']},
'd': {'__files__':
['TESTFILE']}}}})
result = irodsu.listDir(a, False)
self.assertEqual(result, {'a': {'__files__': ['TESTFILE'],
'b': {}}})
def test_get_owner(self):
testfile = os.path.join(dirname(abspath(__file__)),
"testfile.txt")
remote_file = self.coll_path + "/TESTFILE"
irodsu = IRODSUtils(irods_env=ENV)
irodsu.putFile(testfile, remote_file)
owner = self.irods_username
self.assertEqual(irodsu.getOwners(remote_file), [owner])
self.assertEqual(irodsu.getOwners(self.coll_path), [owner])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment