Example #1
0
def test_add_publication(data, user, pubname):
    conf = getConfig(confSection=user)
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)
    pubdata = data['publications'][pubname]
    publication = client.new("publication")
    initobj(publication, pubdata)
    query = "Investigation [name='%s']" % pubdata['investigation']
    publication.investigation = client.assertedSearch(query)[0]
    publication.create()
Example #2
0
def test_add_relateddatafile(data, user, rdfname):
    conf = getConfig(confSection=user)
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)
    rdfdata = data['related_datafiles'][rdfname]
    rdf = client.new("relatedDatafile")
    initobj(rdf, rdfdata)
    rdf.sourceDatafile = get_datafile(client, rdfdata['source'])
    rdf.destDatafile = create_datafile(client, data, rdfdata['dest'])
    rdf.create()
 def connect(self, ):
     """Connects to an ICAT server. Please do read_icat_config() before."""
     self.client = icat.Client(self.options['url'], )  # **self.options.client_kwargs)
     try:
         credentials = {'username': self.options['username'], 'password': self.options['password']}
         self.client.login(self.options['auth'], credentials)
     except icat.ICATSessionError as e:
         self.logger.error(e)
         self.logger.error("hint: edit %s" % self.options.configFile)
         sys.exit()
Example #4
0
def client(setupicat, request):
    conf = getConfig(ids="mandatory")
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)

    def cleanup():
        query = "SELECT df FROM Datafile df WHERE df.location IS NOT NULL"
        client.deleteData(client.search(query))

    request.addfinalizer(cleanup)
    return client
def test_get_icat_version():
    """Query the version from the test ICAT server.

    This implicitly tests that the test ICAT server is properly
    configured and that we can connect to it.
    """

    conf = getConfig(needlogin=False)
    client = icat.Client(conf.url, **conf.client_kwargs)
    # python-icat supports ICAT server 4.2 or newer.  But actually, we
    # just want to check that client.apiversion is set and supports
    # comparison with version strings.
    assert client.apiversion >= '4.2'
    print("\nConnect to %s\nICAT version %s\n" % (conf.url, client.apiversion))
 def downloadWorker(conf, client_kwargs):
     client = icat.Client(conf.url, **client_kwargs)
     client.login(conf.auth, conf.credentials)
     while True:
         dataset = dsQueue.get()
         if dataset is None:
             break
         try:
             dataset.verify(client)
             statitem = dataset.download(client)
             resultQueue.put(statitem)
         except Exception as err:
             resultQueue.put(err)
         dsQueue.task_done()
     client.logout()
Example #7
0
def test_add_study(data, user, studyname):
    pytest.skip("Study disabled, see Issue icatproject/icat.server#155")
    conf = getConfig(confSection=user)
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)
    studydata = data['studies'][studyname]
    study = client.new("study")
    initobj(study, studydata)
    query = "User [name='%s']" % studydata['user']
    study.user = client.assertedSearch(query)[0]
    for invname in studydata['investigations']:
        query = "Investigation [name='%s']" % invname
        si = client.new("studyInvestigation")
        si.investigation = client.assertedSearch(query)[0]
        study.studyInvestigations.append(si)
    study.create()
def test_get_ids_version():
    """Query the version from the test IDS server.

    This implicitly tests that the test ICAT and IDS servers are
    properly configured and that we can connect to them.
    """

    conf = getConfig(needlogin=False, ids="mandatory")
    client = icat.Client(conf.url, **conf.client_kwargs)
    # python-icat supports all publicly released IDS server version,
    # e.g. 1.0.0 or newer.  But actually, we just want to check that
    # client.apiversion is set and supports comparison with version
    # strings.
    assert client.ids.apiversion >= '1.0.0'
    print("\nConnect to %s\nIDS version %s\n" 
          % (conf.idsurl, client.ids.apiversion))
Example #9
0
 def uploadWorker(conf, client_kwargs):
     # Note: I'm not sure whether Suds is thread safe.  Therefore
     # use a separate local client object in each thread.
     client = icat.Client(conf.url, **client_kwargs)
     client.login(conf.auth, conf.credentials)
     while True:
         dataset = dsQueue.get()
         if dataset is None:
             break
         try:
             statitem = dataset.uploadFiles(client)
             resultQueue.put(statitem)
         except Exception as err:
             resultQueue.put(err)
         dsQueue.task_done()
     client.logout()
Example #10
0
 def __init__(self, **kwargs):
     if 'URL' not in kwargs:
         kwargs['URL'] = ICAT['URL']
     if 'AUTH' not in kwargs:
         kwargs['AUTH'] = ICAT['AUTH']
     if 'USER' not in kwargs:
         kwargs['USER'] = ICAT['USER']
     if 'PASSWORD' not in kwargs:
         kwargs['PASSWORD'] = ICAT['PASSWORD']
     if 'SESSION' not in kwargs:
         kwargs['SESSION'] = {
             'username': kwargs['USER'],
             'password': kwargs['PASSWORD']
         }
     logger.debug("Logging in to ICAT at %s" % kwargs['URL'])
     self.client = icat.Client(url=kwargs['URL'])
     self.sessionId = self.client.login(kwargs['AUTH'], kwargs['SESSION'])
Example #11
0
def test_equality_client(client):
    """Test that objects that belong to different clients are never equal.

    There used to be a bug such that the client was not taken into
    account, fixed in c9a1be6.
    """
    # Get a second client that is connected to the same server.
    conf = getConfig(needlogin=False)
    client2 = icat.Client(conf.url, **conf.client_kwargs)
    u1 = client.new("user", id=728, name="u_a")
    u2 = client2.new("user", id=728, name="u_a")
    # u1 and u2 have all attributes, including the id the same.
    assert u1.id == u2.id
    assert u1.name == u2.name
    # But they belong to different client instances and thus, they are
    # still not equal.
    assert u1 != u2
    assert not (u1 == u2)
Example #12
0
 def upload(conf, client_kwargs, dataset):
     try:
         # Note: I'm not sure whether Suds is thread safe.  Therefore
         # use a separate local client object in each thread.
         client = icat.Client(conf.url, **client_kwargs)
         client.login(conf.auth, conf.credentials)
         name = dataset.name
         datafileFormat = dataset.getDatafileFormat(client)
         f = Datafile(dataset.datasetdir, "upload.dat", 32)
         while True:
             # Get the dataset object from ICAT, continue to retry
             # while it does not exist yet.
             try:
                 ds = dataset.search(client)
                 log.info("Upload: dataset %s found.", name)
                 break
             except icat.SearchAssertionError:
                 log.info("Upload: dataset %s not found (yet).", name)
                 time.sleep(0.2)
                 continue
         selection = DataSelection([ds])
         datafile = client.new("datafile",
                               name=f.fname,
                               dataset=ds,
                               datafileFormat=datafileFormat)
         while True:
             # Do the upload.  This may (or even should) fail due to
             # the dataset not online.  The error triggers a restore,
             # so we continue to retry until the restore has been
             # completed.
             try:
                 df = client.putData(f.path, datafile)
                 log.info("Upload to dataset %s succeeded.", name)
                 break
             except icat.IDSDataNotOnlineError:
                 status = client.ids.getStatus(selection)
                 log.info("Upload: dataset %s is %s.", name, status)
                 time.sleep(0.2)
                 continue
         resultQueue.put(f)
         client.logout()
     except Exception as err:
         resultQueue.put(err)
Example #13
0
def test_login(user):
    """Login to the ICAT server.
    """

    conf = getConfig(confSection=user)
    client = icat.Client(conf.url, **conf.client_kwargs)
    sessionId = client.login(conf.auth, conf.credentials)
    assert sessionId
    assert sessionId == client.sessionId
    username = client.getUserName()
    assert username == user
    print("\nLogged in as %s to %s." % (user, conf.url))
    client.logout()
    assert client.sessionId is None

    # Verify that the logout was effective, e.g. that the sessionId is
    # invalidated.
    with tmpSessionId(client, sessionId):
        with pytest.raises(icat.exception.ICATSessionError):
            username = client.getUserName()
Example #14
0
def client():
    conf = getConfig(needlogin=False)
    client = icat.Client(conf.url, **conf.client_kwargs)
    return client
Example #15
0
 def __init__(self, credentials=None):
     if not credentials:
         credentials = ICAT_CREDENTIALS
     super(ICATClient, self).__init__(credentials)  # pylint:disable=super-with-arguments
     self.client = icat.Client(self.credentials.host)
Example #16
0
def get_icat_version():
    conf = getConfig(needlogin=False)
    client = icat.Client(conf.url, **conf.client_kwargs)
    return client.apiversion
Example #17
0
def client(conf):
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)
    return client
 def __init__(self, credentials):
     super(ICATClient, self).__init__(credentials)
     self.client = icat.Client(self.credentials.host)
Example #19
0
def client(setupicat):
    conf = getConfig()
    client = icat.Client(conf.url, **conf.client_kwargs)
    client.login(conf.auth, conf.credentials)
    return client
Example #20
0
 def __init__(self):
     self.client = icat.Client(ICAT_SETTINGS['URL'])
     self.client_login()
Example #21
0
#! /usr/bin/python

from __future__ import print_function
import sys
import logging
import icat
import icat.config

logging.basicConfig(level=logging.INFO)
#logging.getLogger('suds.client').setLevel(logging.DEBUG)

conf = icat.config.Config(needlogin=False, ids="optional").getconfig()

client = icat.Client(conf.url, **conf.client_kwargs)
print("Python %s\n" % (sys.version))
print("python-icat version %s (%s)\n" % (icat.__version__, icat.__revision__))
print("Connect to %s\nICAT version %s\n" % (conf.url, client.apiversion))
if client.ids:
    print("Connect to %s\nIDS version %s\n" %
          (conf.idsurl, client.ids.apiversion))
def checker():
    conf = getConfig(needlogin=False)
    client = icat.Client(conf.url, **conf.client_kwargs)
    return ICATChecker(client)