Commit fa895cc4 authored by Stefan Wolfsheimer's avatar Stefan Wolfsheimer
Browse files

refactored putfile and getfile function

parent 9be8335a
......@@ -94,7 +94,9 @@ def getAllCommunities(args):
def collectPIDsForCollection(collectionPath, configuration):
PIDobjectsString = '['
irodsu = IRODSUtils(configuration.irods_home_dir, logger, configuration.irods_debug)
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
irods_env=configuration.irodsenv)
rc, res = irodsu.deepListDir(collectionPath)
if not res:
return None
......@@ -140,7 +142,9 @@ 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)
......@@ -162,7 +166,9 @@ 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')
if users_metadata:
......@@ -231,7 +237,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("--irodenv", 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')
......
......@@ -23,6 +23,7 @@ class Configuration():
self.dryrun = dryrun
self.logger = logger
self.access_token = ""
self.irodsenv = None
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG, \
'ERROR': logging.ERROR, 'WARNING': logging.WARNING, \
'CRITICAL': logging.CRITICAL}
......@@ -102,4 +103,3 @@ class Configuration():
else:
self.logger.error('missing section %s: with parameter %s' % (section, option))
return None
\ No newline at end of file
#!/usr/bin/env python
# -*- python -*-
import json
import logging
import os
import io
import subprocess
from os.path import expanduser
from irods.session import iRODSSession
import irods.keywords as kw
class IRODS(object):
"""
Wrapper class for iRODS session with cleanup on context exit
"""
def __init__(self,
irods_config_file,
irods_auth_file):
self.irods_config_file = irods_config_file
self.irods_auth_file = irods_auth_file
def __enter__(self):
auth_file = self.irods_auth_file
env_file = self.irods_config_file
self.session = iRODSSession(irods_env_file=env_file,
irods_authentication_file=auth_file)
# self.session.connection_timeout = self.connection_timeout
return self.session
def __exit__(self, exc_type, exc_value, traceback):
self.session.cleanup()
##############################################################################
# iRODS Admin Utility Class #
##############################################################################
class IRODSUtils():
"""
class IRODSUtils(object):
"""
utility for irods management
"""
def __init__(self, home_dir='/', logger_parent=None, debug=False):
def __init__(self, home_dir='/', logger_parent=None, debug=False,
irods_env=None):
"""initialize the object"""
self.user = None
if logger_parent:
if irods_env is None:
self.irods_env = expanduser("~/.irods/irods_environment.json")
else:
self.irods_env = irods_env
self.irods_auth = os.path.join(os.path.dirname(self.irods_env),
".irodsA")
self.user = None
if logger_parent:
self.logger = logger_parent
else:
else:
logger_name = "IrodsUtils"
self.logger = logging.getLogger(logger_name)
self.irods_home_dir = home_dir
......@@ -30,31 +60,37 @@ class IRODSUtils():
else:
self.logger.setLevel(logging.INFO)
def getFile(self, path, resource=None):
"""get file content"""
cmdList = ["iget"]
if resource is not None:
cmdList += ['-R', resource]
cmdList += [path, '-']
try:
(rc, out) = self.execute_icommand(cmdList)
return out
except:
return None
BLOCK_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE
if resource is None:
options = {}
else:
options = {kw.DEST_RESC_NAME_KW: resource}
with IRODS(irods_config_file=self.irods_env,
irods_auth_file=self.irods_auth) as session:
obj = session.data_objects.get(path)
result = ''
with obj.open('r', **options) as f:
while True:
chunk = f.read(BLOCK_SIZE)
if chunk:
result += chunk
else:
break
return result
def putFile(self, source, destination, resource=None):
"""put the file into the destination collection"""
cmdList = ["iput"]
if resource is not None:
cmdList += ['-R', resource]
cmdList += ["-f", source, destination]
(rc, out) = self.execute_icommand(cmdList)
return out
if resource is None:
options = {}
else:
options = {kw.DEST_RESC_NAME_KW: resource}
with IRODS(irods_config_file=self.irods_env,
irods_auth_file=self.irods_auth) as session:
session.data_objects.put(source, destination, **options)
def getMetadata(self, path, key, option=''):
"""get file metadata"""
......
......@@ -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
from os.path import dirname
from os.path import abspath
from os.path import join
import sys
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)
session.collections.create(self.coll_path)
print(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"
print(remote_file)
irodsu = IRODSUtils(irods_env=ENV)
irodsu.putFile(testfile, remote_file)
content = irodsu.getFile(remote_file)
self.assertEqual(content,
testfile_content)
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