Ejemplo n.º 1
0
    def testUuid4(self):
        """
        Tests that on Mac platforms after 10.5 our use of omero_ext.uuid
        ensures that the broken functions _uuid_generate_{random,time}
        are nulled. In newer versions of Python, this will be the case
        as soon as the uuid moduly is loaded and so we fake their being
        non-null by setting the value to DUMMY and reloading omero_ext.uuid.

        On non Mac systems, this test does more or less nothing.

        See http://trac.openmicroscopy.org.uk/ome/ticket/3774
        """
        import sys

        if sys.platform == "darwin":
            import os

            if int(os.uname()[2].split(".")[0]) >= 9:
                # uuid.__uuid__ is the original module used.
                U = uuid.__uuid__
                U._uuid_generate_random = U._uuid_generate_time = DUMMY
                reload(uuid)
                self.assertEquals(None, U._uuid_generate_random)
                self.assertEquals(None, U._uuid_generate_time)
        self.assertTrue(uuid.uuid4())
Ejemplo n.º 2
0
 def create_obj(self, ctx, name):
     id = None
     id_path = ctx.dir / ("%s.id" % name)
     prop = self.props.get(name)
     # Do nothing if not in props
     if prop is None:
         return None
     # If an integer, use that as an id
     try:
         id = int(prop)
         log.debug("Using specified %s:%s" % (name, id))
     except:
         # Otherwise, create/re-use
         if prop == "":
             try:
                 id = int(id_path.lines()[0])
             except exceptions.Exception, e:
                 log.debug("No %s.id: %s", name, e)
                 prop = str(uuid.uuid4())
         # Now, if there's still no id, create one
         if id is not None:
             log.debug("Using reload %s:%s" % (name, id))
         else:
             kls = getattr(omero.model, "%sI" % name)
             obj = kls()
             obj.name = omero.rtypes.rstring(prop)
             obj = ctx.update_service().saveAndReturnObject(obj)
             id = obj.id.val
             log.debug("Created obj %s:%s" % (name, id))
Ejemplo n.º 3
0
 def __init__(self, adapter_or_client, handle, poll=True):
     self.event = omero.util.concurrency.get_event(name="DeleteCallbackI")
     self.result = None
     self.poll = poll
     self.handle = handle
     self.adapter = adapter_or_client
     self.id = Ice.Identity(str(uuid.uuid4()), "DeleteHandleCallback")
     if not isinstance(self.adapter, Ice.ObjectAdapter):
         self.adapter = self.adapter.adapter
Ejemplo n.º 4
0
 def __init__(self, adapter_or_client, handle, poll = True):
     self.event = omero.util.concurrency.get_event(name="DeleteCallbackI")
     self.result = None
     self.poll = poll
     self.handle = handle
     self.adapter = adapter_or_client
     self.id = Ice.Identity(str(uuid.uuid4()), "DeleteHandleCallback")
     if not isinstance(self.adapter, Ice.ObjectAdapter):
         self.adapter = self.adapter.adapter
Ejemplo n.º 5
0
def internal_service_factory(communicator,
                             user="******",
                             group=None,
                             retries=6,
                             interval=10,
                             client_uuid=None,
                             stop_event=None):
    """
    Try to return a ServiceFactory from the grid.

    Try a number of times then give up and raise the
    last exception returned. This method will only
    work internally to the grid, i.e. behind the Glacier2
    firewall. It is intended for internal servers to
    be able to create sessions for accessing the database. ::

        communicator := Ice.Communicator used to find the registry
        user         := Username which should have a session created
        group        := Group into which the session should be logged
        retries      := Number of session creation retries before throwing
        interval     := Seconds between retries
        client_uuid  := Uuid of the client which should be used
    """
    log = logging.getLogger("omero.utils")
    if stop_event == None:
        stop_event = omero.util.concurrency.get_event(
            name="internal_service_factory")

    tryCount = 0
    excpt = None
    query = communicator.stringToProxy("IceGrid/Query")
    query = IceGrid.QueryPrx.checkedCast(query)

    implicit_ctx = communicator.getImplicitContext()
    implicit_ctx.put(omero.constants.AGENT, "Python service")
    if client_uuid is not None:
        implicit_ctx.put(omero.constants.CLIENTUUID, client_uuid)
    else:
        if not implicit_ctx.containsKey(omero.constants.CLIENTUUID):
            client_uuid = str(uuid.uuid4())
            implicit_ctx.put(omero.constants.CLIENTUUID, client_uuid)

    while tryCount < retries:
        if stop_event.isSet():  # Something is shutting down, exit.
            return None
        try:
            blitz = query.findAllObjectsByType("::Glacier2::SessionManager")[0]
            blitz = Glacier2.SessionManagerPrx.checkedCast(blitz)
            sf = blitz.create(user, None)
            # Group currently unused.
            return omero.api.ServiceFactoryPrx.checkedCast(sf)
        except Exception, e:
            tryCount += 1
            log.info("Failed to get session on attempt %s", str(tryCount))
            excpt = e
            stop_event.wait(interval)
Ejemplo n.º 6
0
    def assertCanEdit(self, blitzObject, expected=True,
            sudo_needed=False, exc_info=False):
        """ Checks the canEdit() method AND actual behavior (ability to edit) """

        self.assertEqual(blitzObject.canEdit(), expected, "Unexpected result of canEdit(). Expected: %s" % expected)
        # Now test if we can actually Edit and 'Hard link' the object
        nameEdited = False
        # for saves, omero group must *not* be -1
        origGroup = self.gateway.SERVICE_OPTS.getOmeroGroup()   # need to switch back to this after edits
        gid = blitzObject.details.group.id.val
        self.gateway.SERVICE_OPTS.setOmeroGroup(gid)
        try:
            blitzObject.setName("new name: %s" % _uuid.uuid4())
            blitzObject.save()
            nameEdited = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                nameEdited = True # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()


        objectUsed = False
        try:
            obj = blitzObject._obj
            if isinstance(obj, omero.model.Image):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.DatasetImageLinkI()
                link.setParent(ds)
                link.setChild(obj)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            elif isinstance(obj, omero.model.Project):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.ProjectDatasetLinkI()
                link.setParent(obj)
                link.setChild(ds)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            else:
                raise Exception("Unknown type: %s" % blitzObject)
            objectUsed = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                objectUsed = True # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()

        self.gateway.SERVICE_OPTS.setOmeroGroup(origGroup)  # revert back
        self.assertEqual(blitzObject.canEdit(), expected, "Unexpected result of canEdit(). Expected: %s" % expected)
        self.assertEqual(nameEdited, expected, "Unexpected ability to Edit. Expected: %s" % expected)
        self.assertEqual(objectUsed|sudo_needed, expected, "Unexpected ability to Use. Expected: %s" % expected)
Ejemplo n.º 7
0
def uploadScript(scriptService, scriptPath):
    _uuid = str(uuid.uuid4())

    file = open(scriptPath)
    scriptText = file.read()
    file.close()
    try:
        scriptId = scriptService.uploadOfficialScript("/%s/%s" % (_uuid, scriptPath), scriptText)
    except ApiUsageException:
        raise # The next line will never be run!
        scriptId = editScript(scriptService, scriptPath)
    return scriptId
Ejemplo n.º 8
0
 def __init__(self, adapter_or_client, process, poll = True):
     self.event = omero.util.concurrency.get_event(name="ProcessCallbackI")
     self.result = None
     self.poll = poll
     self.process = process
     self.adapter = adapter_or_client
     self.id = Ice.Identity(str(uuid.uuid4()), "ProcessCallback")
     if not isinstance(self.adapter, Ice.ObjectAdapter):
         self.adapter = self.adapter.adapter
     self.prx = self.adapter.add(self, self.id) # OK ADAPTER USAGE
     self.prx = omero.grid.ProcessCallbackPrx.uncheckedCast(self.prx)
     process.registerCallback(self.prx)
Ejemplo n.º 9
0
    def __init__(self, adapter_or_client, process, poll = True, category=None):
        self.event = omero.util.concurrency.get_event(name="ProcessCallbackI")
        self.result = None
        self.poll = poll
        self.process = process
        self.adapter, self.category = \
                adapter_and_category(adapter_or_client, category)

        self.id = Ice.Identity(str(uuid.uuid4()), self.category)
        self.prx = self.adapter.add(self, self.id) # OK ADAPTER USAGE
        self.prx = omero.grid.ProcessCallbackPrx.uncheckedCast(self.prx)
        process.registerCallback(self.prx)
Ejemplo n.º 10
0
    def __init__(self, adapter_or_client, process, poll=True, category=None):
        self.event = omero.util.concurrency.get_event(name="ProcessCallbackI")
        self.result = None
        self.poll = poll
        self.process = process
        self.adapter, self.category = \
                adapter_and_category(adapter_or_client, category)

        self.id = Ice.Identity(str(uuid.uuid4()), self.category)
        self.prx = self.adapter.add(self, self.id)  # OK ADAPTER USAGE
        self.prx = omero.grid.ProcessCallbackPrx.uncheckedCast(self.prx)
        process.registerCallback(self.prx)
Ejemplo n.º 11
0
def uploadScript(scriptService, scriptPath):
    _uuid = str(uuid.uuid4())

    file = open(scriptPath)
    scriptText = file.read()
    file.close()
    try:
        scriptId = scriptService.uploadOfficialScript(
            "/%s/%s" % (_uuid, scriptPath), scriptText)
    except ApiUsageException:
        raise  # The next line will never be run!
        scriptId = editScript(scriptService, scriptPath)
    return scriptId
Ejemplo n.º 12
0
    def test5975(self):
        """
        Runs various tests which try to force the stored user name
        to be a session uuid (which should never happen)
        """
        cli = MyCLI()
        key = str(uuid.uuid4())
        key_login = "******" % key
        cli.creates_client(sess=key, new=True)
        cli.invoke(key_login)
        self.assert5975(key, cli)

        cli.invoke("s logout")
        self.assert5975(key, cli)
Ejemplo n.º 13
0
    def test5975(self):
        """
        Runs various tests which try to force the stored user name
        to be a session uuid (which should never happen)
        """
        cli = MyCLI()
        key = str(uuid.uuid4())
        key_login = "******" % key
        cli.creates_client(sess=key, new=True)
        cli.invoke(key_login)
        self.assert5975(key, cli)

        cli.invoke("s logout")
        self.assert5975(key, cli)
Ejemplo n.º 14
0
def internal_service_factory(communicator, user="******", group=None, retries=6, interval=10, client_uuid=None, stop_event = None):
    """
    Try to return a ServiceFactory from the grid.

    Try a number of times then give up and raise the
    last exception returned. This method will only
    work internally to the grid, i.e. behind the Glacier2
    firewall. It is intended for internal servers to
    be able to create sessions for accessing the database. ::
        communicator := Ice.Communicator used to find the registry
        user         := Username which should have a session created
        group        := Group into which the session should be logged
        retries      := Number of session creation retries before throwing
        interval     := Seconds between retries
        client_uuid  := Uuid of the client which should be used
    """
    log = logging.getLogger("omero.utils")
    if stop_event == None:
        stop_event = omero.util.concurrency.get_event(name="internal_service_factory")

    tryCount = 0
    excpt = None
    query = communicator.stringToProxy("IceGrid/Query")
    query = IceGrid.QueryPrx.checkedCast(query)

    import omero_Constants_ice
    implicit_ctx = communicator.getImplicitContext()
    implicit_ctx.put(omero.constants.AGENT, "Python service")
    if client_uuid is not None:
        implicit_ctx.put(omero.constants.CLIENTUUID, client_uuid)
    else:
        if not implicit_ctx.containsKey(omero.constants.CLIENTUUID):
            client_uuid = str(uuid.uuid4())
            implicit_ctx.put(omero.constants.CLIENTUUID, client_uuid)

    while tryCount < retries:
        if stop_event.isSet(): # Something is shutting down, exit.
            return None
        try:
            blitz = query.findAllObjectsByType("::Glacier2::SessionManager")[0]
            blitz = Glacier2.SessionManagerPrx.checkedCast(blitz)
            sf = blitz.create(user, None)
            # Group currently unused.
            return omero.api.ServiceFactoryPrx.checkedCast(sf)
        except Exception, e:
            tryCount += 1
            log.info("Failed to get session on attempt %s", str(tryCount))
            excpt = e
            stop_event.wait(interval)
Ejemplo n.º 15
0
    def assertCanEdit(self, blitzObject, expected=True,
            sudo_needed=False, exc_info=False):
        """ Checks the canEdit() method AND actual behavior (ability to edit) """

        nameEdited = False
        try:
            blitzObject.setName("new name: %s" % _uuid.uuid4())
            blitzObject.save()
            nameEdited = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                nameEdited = True # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()


        objectUsed = False
        try:
            obj = blitzObject._obj
            if isinstance(obj, omero.model.Image):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.DatasetImageLinkI()
                link.setParent(ds)
                link.setChild(obj)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            elif isinstance(obj, omero.model.Project):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.ProjectDatasetLinkI()
                link.setParent(obj)
                link.setChild(ds)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            else:
                raise Exception("Unknown type: %s" % blitzObject)
            objectUsed = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                objectUsed = True # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()

        self.assertEqual(blitzObject.canEdit(), expected, "Unexpected result of canEdit(). Expected: %s" % expected)
        self.assertEqual(nameEdited, expected, "Unexpected ability to Edit. Expected: %s" % expected)
        self.assertEqual(objectUsed|sudo_needed, expected, "Unexpected ability to Use. Expected: %s" % expected)
Ejemplo n.º 16
0
    def testCopiedSessionWorks(self):
        """
        Found by Colin while using a session key from
        a non-CLI-source.
        """
        cli = MyCLI()

        MOCKKEY = "MOCKKEY%s" % uuid.uuid4()

        key_login = "******" % MOCKKEY

        # Try with session when it's still available
        cli.creates_client(sess=MOCKKEY, new=True)
        cli.invoke(key_login)
        cli._client = None  # Forcing new instance
Ejemplo n.º 17
0
    def testCopiedSessionWorks(self):
        """
        Found by Colin while using a session key from
        a non-CLI-source.
        """
        cli = MyCLI()

        MOCKKEY = "MOCKKEY%s" % uuid.uuid4()

        key_login = "******" % MOCKKEY

        # Try with session when it's still available
        cli.creates_client(sess=MOCKKEY, new=True)
        cli.invoke(key_login)
        cli._client = None # Forcing new instance
Ejemplo n.º 18
0
    def testProcessCallback(self):

        callback = CallbackI()

        id = self.client.getCommunicator().stringToIdentity(str(uuid.uuid4()))
        cb = self.client.getAdapter().add(callback, id)
        cb = omero.grid.ProcessCallbackPrx.uncheckedCast(cb)
        p = self._getProcessor()
        params = p.params()
        assert params.stdoutFormat

        process = p.execute(rmap({}))
        process.registerCallback(cb)
        self.assertSuccess(p, process)

        assert len(callback.finish) > 0
Ejemplo n.º 19
0
def usermode_processor(client, serverid = "UsermodeProcessor",\
                       cfg = None, accepts_list = None, stop_event = None,\
                       omero_home = path.getcwd()):
    """
    Creates and activates a usermode processor for the given client.
    It is the responsibility of the client to call "cleanup()" on
    the ProcessorI implementation which is returned.

    cfg is the path to an --Ice.Config-valid file or files. If none
    is given, the value of ICE_CONFIG will be taken from the environment
    if available. Otherwise, all properties will be taken from the client
    instance.

    accepts_list is the list of IObject instances which will be passed to
    omero.api.IScripts.validateScript. If none is given, only the current
    Experimenter's own object will be passed.

    stop_event is an threading.Event. One will be acquired from
    omero.util.concurrency.get_event if none is provided.
    """

    if cfg is None:
        cfg = os.environ.get("ICE_CONFIG")

    if accepts_list is None:
        uid = client.sf.getAdminService().getEventContext().userId
        accepts_list = [omero.model.ExperimenterI(uid, False)]

    if stop_event is None:
        stop_event = omero.util.concurrency.get_event(name="UsermodeProcessor")

    id = Ice.Identity()
    id.name = "%s-%s" % (serverid, uuid.uuid4())
    id.category = client.getCategory()

    ctx = omero.util.ServerContext(serverid, client.ic, stop_event)
    impl = omero.processor.ProcessorI(ctx,
                                      use_session=client.sf,
                                      accepts_list=accepts_list,
                                      cfg=cfg,
                                      omero_home=omero_home,
                                      category=id.category)
    ctx.add_servant(client.adapter, impl, ice_identity=id)
    return impl
Ejemplo n.º 20
0
def usermode_processor(client, serverid = "UsermodeProcessor",\
                       cfg = None, accepts_list = None, stop_event = None,\
                       omero_home = path.getcwd()):
    """
    Creates and activates a usermode processor for the given client.
    It is the responsibility of the client to call "cleanup()" on
    the ProcessorI implementation which is returned.

    cfg is the path to an --Ice.Config-valid file or files. If none
    is given, the value of ICE_CONFIG will be taken from the environment
    if available. Otherwise, all properties will be taken from the client
    instance.

    accepts_list is the list of IObject instances which will be passed to
    omero.api.IScripts.validateScript. If none is given, only the current
    Experimenter's own object will be passed.

    stop_event is an threading.Event. One will be acquired from
    omero.util.concurrency.get_event if none is provided.
    """

    if cfg is None:
        cfg = os.environ.get("ICE_CONFIG")

    if accepts_list is None:
        uid = client.sf.getAdminService().getEventContext().userId
        accepts_list = [omero.model.ExperimenterI(uid, False)]

    if stop_event is None:
        stop_event = omero.util.concurrency.get_event(name="UsermodeProcessor")

    id = Ice.Identity()
    id.name = "%s-%s" % (serverid, uuid.uuid4())
    id.category = client.getCategory()

    ctx = omero.util.ServerContext(serverid, client.ic, stop_event)
    impl = omero.processor.ProcessorI(ctx,
        use_session=client.sf, accepts_list=accepts_list, cfg=cfg,
        omero_home = omero_home, category=id.category)
    ctx.add_servant(client.adapter, impl, ice_identity=id)
    return impl
Ejemplo n.º 21
0
    def __init__(self, adapter_or_client, handle, category=None):

        if adapter_or_client is None:
            raise omero.ClientError("Null client")

        if handle is None:
            raise omero.ClientError("Null handle")

        self.event = omero.util.concurrency.get_event(name="CmdCallbackI")
        self.state = (None, None)  # (Response, Status)
        self.handle = handle
        self.adapter, self.category = \
                adapter_and_category(adapter_or_client, category)

        self.id = Ice.Identity(str(uuid.uuid4()), self.category)
        self.prx = self.adapter.add(self, self.id) # OK ADAPTER USAGE
        self.prx = omero.cmd.CmdCallbackPrx.uncheckedCast(self.prx)
        handle.addCallback(self.prx)

        # Now check just in case the process exited VERY quickly
        self.poll()
Ejemplo n.º 22
0
    def __init__(self, adapter_or_client, handle, category=None):

        if adapter_or_client is None:
            raise omero.ClientError("Null client")

        if handle is None:
            raise omero.ClientError("Null handle")

        self.event = omero.util.concurrency.get_event(name="CmdCallbackI")
        self.state = (None, None)  # (Response, Status)
        self.handle = handle
        self.adapter, self.category = \
                adapter_and_category(adapter_or_client, category)

        self.id = Ice.Identity(str(uuid.uuid4()), self.category)
        self.prx = self.adapter.add(self, self.id)  # OK ADAPTER USAGE
        self.prx = omero.cmd.CmdCallbackPrx.uncheckedCast(self.prx)
        handle.addCallback(self.prx)

        # Now check just in case the process exited VERY quickly
        self.poll()
Ejemplo n.º 23
0
    def testUuid4(self):
        """
        Tests that on Mac platforms after 10.5 our use of omero_ext.uuid
        ensures that the broken functions _uuid_generate_{random,time}
        are nulled. In newer versions of Python, this will be the case
        as soon as the uuid moduly is loaded and so we fake their being
        non-null by setting the value to DUMMY and reloading omero_ext.uuid.

        On non Mac systems, this test does more or less nothing.

        See http://trac.openmicroscopy.org.uk/ome/ticket/3774
        """
        import sys
        if sys.platform == 'darwin':
            import os
            if int(os.uname()[2].split('.')[0]) >= 9:
                # uuid.__uuid__ is the original module used.
                U = uuid.__uuid__
                U._uuid_generate_random = U._uuid_generate_time = DUMMY
                reload(uuid)
                self.assertEquals(None, U._uuid_generate_random)
                self.assertEquals(None, U._uuid_generate_time)
        self.assertTrue(uuid.uuid4())
Ejemplo n.º 24
0
 def uuid(self):
     import omero_ext.uuid as _uuid  # see ticket:3774
     return str(_uuid.uuid4())
## To see the actions of the usermode
## processor, enable logging here.
## ----------------------------------
# omero.util.configure_logging()

client = omero.client()
try:
    sf = client.createSession()
    scriptService = sf.getScriptService()

    # Store the script on the server. A uuid
    # is appended to the name since only one
    # script of each name is allowed.
    id = scriptService.uploadScript(\
        "/examples/NativeWrapper-%s.py" % uuid.uuid4(), NATIVE_WRAPPER)

    # Now we create a processor for handling the
    # script we just created since it is not an
    # official script. If we had used
    # scriptService.uploadOfficialScript, or if
    # the script was already installed in the server
    # we would not need the ump.
    ump = omero.processor.usermode_processor(client)
    try:
        # Sending this file itself as the target
        # but it could of course be any value you'd
        # like.
        target = os.path.join(os.getcwd(), __file__)
        target = omero.rtypes.rstring(target)
Ejemplo n.º 26
0
 def beforeMethod(self):
     self.uuid = str(uuid.uuid4())
     self.dir = create_path(folder=True) / self.uuid
     self.dir.makedirs()
     self.sim = Simulator(self.dir)
     self.driver = Driver(self.sim)
Ejemplo n.º 27
0
    def process(self, client, session, job, current, params, properties = None, iskill = True):
        """
        session: session uuid, used primarily if client is None
        client: an omero.client object which should be attached to a session
        """

        if properties is None: properties = {}

        if not session or not job or not job.id:
            raise omero.ApiUsageException("No null arguments")

        file, handle = self.lookup(job)

        try:
            if not file:
                raise omero.ApiUsageException(\
                    None, None, "Job should have one executable file attached.")

            sf = self.internal_session()
            if params:
                self.logger.debug("Checking params for job %s" % job.id.val)
                svc = sf.getSessionService()
                inputs = svc.getInputs(session)
                errors = omero.scripts.validate_inputs(params, inputs, svc, session)
                if errors:
                    errors = "Invalid parameters:\n%s" % errors
                    raise omero.ValidationException(None, None, errors)

            properties["omero.job"] = str(job.id.val)
            properties["omero.user"] = session
            properties["omero.pass"] = session
            properties["Ice.Default.Router"] = client.getProperty("Ice.Default.Router")

            process = ProcessI(self.ctx, sys.executable, properties, params, iskill, omero_home = self.omero_home)
            self.resources.add(process)

            # client.download(file, str(process.script_path))
            scriptText = sf.getScriptService().getScriptText(file.id.val)
            process.script_path.write_bytes(scriptText)

            self.logger.info("Downloaded file: %s" % file.id.val)
            s = client.sha1(str(process.script_path))
            if not s == file.sha1.val:
                msg = "Sha1s don't match! expected %s, found %s" % (file.sha1.val, s)
                self.logger.error(msg)
                process.cleanup()
                raise omero.InternalException(None, None, msg)
            else:
                process.activate()
                handle.setStatus("Running")

            id = None
            if self.category:
                id = Ice.Identity()
                id.name = "Process-%s" % uuid.uuid4()
                id.category = self.category
            prx = self.ctx.add_servant(current, process, ice_identity=id)
            return omero.grid.ProcessPrx.uncheckedCast(prx), process

        finally:
            handle.close()
Ejemplo n.º 28
0
    def _initData(self, id):
        """
        Initializes the current client via an Ice.InitializationData
        instance. This is called by all of the constructors, but may
        also be called on createSession(name, pass) if a previous
        call to closeSession() has nulled the Ice.Communicator.
        """

        if not id:
            raise omero.ClientError("No initialization data provided.");

        # Strictly necessary for this class to work
        id.properties.setProperty("Ice.ImplicitContext", "Shared")
        id.properties.setProperty("Ice.ACM.Client", "0")
        id.properties.setProperty("Ice.RetryIntervals", "-1")
        id.properties.setProperty("Ice.Default.EndpointSelection", "Ordered")
        id.properties.setProperty("Ice.Default.PreferSecure", "1")
        id.properties.setProperty("Ice.Plugin.IceSSL" , "IceSSL:createIceSSL")
        id.properties.setProperty("IceSSL.Ciphers" , "ADH")
        id.properties.setProperty("IceSSL.VerifyPeer" , "0")

        # Setting MessageSizeMax
        messageSize = id.properties.getProperty("Ice.MessageSizeMax")
        if not messageSize or len(messageSize) == 0:
            id.properties.setProperty("Ice.MessageSizeMax", str(omero.constants.MESSAGESIZEMAX))

        # Setting ConnectTimeout
        self.parseAndSetInt(id, "Ice.Override.ConnectTimeout",\
                           omero.constants.CONNECTTIMEOUT)

        # Endpoints set to tcp if not present
        endpoints = id.properties.getProperty("omero.ClientCallback.Endpoints")
        if not endpoints or len(endpoints) == 0:
            id.properties.setProperty("omero.ClientCallback.Endpoints", "tcp")

        # ThreadPool to 5 if not present
        threadpool = id.properties.getProperty("omero.ClientCallback.ThreadPool.Size")
        if not threadpool or len(threadpool) == 0:
            id.properties.setProperty("omero.ClientCallback.ThreadPool.Size", str(omero.constants.CLIENTTHREADPOOLSIZE))

        # Port, setting to default if not present
        port = self.parseAndSetInt(id, "omero.port",\
                                  omero.constants.GLACIER2PORT)

        # Default Router, set a default and then replace
        router = id.properties.getProperty("Ice.Default.Router")
        if not router or len(router) == 0:
            router = str(omero.constants.DEFAULTROUTER)
        host = id.properties.getPropertyWithDefault("omero.host", """<"omero.host" not set>""")
        router = router.replace("@omero.port@", str(port))
        router = router.replace("@omero.host@", str(host))
        id.properties.setProperty("Ice.Default.Router", router)

        # Dump properties
        dump = id.properties.getProperty("omero.dump")
        if len(dump) > 0:
            m = self.getPropertyMap(id.properties)
            keys = list(m.keys())
            keys.sort()
            for key in keys:
                print "%s=%s" % (key, m[key])

        self.__lock.acquire()
        try:
            if self.__ic:
                raise omero.ClientError("Client already initialized")

            try:
                self.__ic = Ice.initialize(id)
            except Ice.EndpointParseException:
                msg = "No host specified. "
                msg += "Use omero.client(HOSTNAME), ICE_CONFIG, or similar."
                raise omero.ClientError(msg)

            if not self.__ic:
                raise omero.ClientError("Improper initialization")

            # Register Object Factory
            self.of = ObjectFactory()
            self.of.registerObjectFactory(self.__ic)
            for of in omero.rtypes.ObjectFactories.values():
                of.register(self.__ic)

            # Define our unique identifier (used during close/detach)
            self.__uuid = str(uuid.uuid4())
            ctx = self.__ic.getImplicitContext()
            if not ctx:
                raise omero.ClientError("Ice.ImplicitContext not set to Shared")
            ctx.put(omero.constants.CLIENTUUID, self.__uuid)

            # ticket:2951 - sending user group
            group = id.properties.getPropertyWithDefault("omero.group", "")
            if group:
                ctx.put("omero.group", group)

            # Register the default client callback
            self.__oa = self.__ic.createObjectAdapter("omero.ClientCallback")
            cb = BaseClient.CallbackI(self.__ic, self.__oa)
            self.__oa.add(cb, self.__ic.stringToIdentity("ClientCallback/%s" % self.__uuid))
            self.__oa.activate()
        finally:
            self.__lock.release()
Ejemplo n.º 29
0
 def beforeMethod(self):
     self.uuid = str(uuid.uuid4())
     self.dir = create_path(folder=True) / self.uuid
     self.dir.makedirs()
     self.sim = Simulator(self.dir)
     self.driver = Driver(self.sim)
Ejemplo n.º 30
0
    def _initData(self, id):
        """
        Initializes the current client via an Ice.InitializationData
        instance. This is called by all of the constructors, but may
        also be called on createSession(name, pass) if a previous
        call to closeSession() has nulled the Ice.Communicator.
        """

        if not id:
            raise omero.ClientError("No initialization data provided.")

        # Strictly necessary for this class to work
        id.properties.setProperty("Ice.ImplicitContext", "Shared")
        id.properties.setProperty("Ice.ACM.Client", "0")
        id.properties.setProperty("Ice.CacheMessageBuffers", "0")
        id.properties.setProperty("Ice.RetryIntervals", "-1")
        id.properties.setProperty("Ice.Default.EndpointSelection", "Ordered")
        id.properties.setProperty("Ice.Default.PreferSecure", "1")
        id.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL:createIceSSL")
        id.properties.setProperty("IceSSL.Ciphers", "ADH")
        id.properties.setProperty("IceSSL.Protocols", "tls1")
        id.properties.setProperty("IceSSL.VerifyPeer", "0")

        # Setting block size
        blockSize = id.properties.getProperty("omero.block_size")
        if not blockSize or len(blockSize) == 0:
            id.properties.setProperty("omero.block_size",
                                      str(omero.constants.DEFAULTBLOCKSIZE))

        # Set the default encoding if this is Ice 3.5 or later
        # and none is set.
        if Ice.intVersion() >= 30500:
            if not id.properties.getProperty("Ice.Default.EncodingVersion"):
                id.properties.setProperty("Ice.Default.EncodingVersion", "1.0")

        # Setting MessageSizeMax
        messageSize = id.properties.getProperty("Ice.MessageSizeMax")
        if not messageSize or len(messageSize) == 0:
            id.properties.setProperty("Ice.MessageSizeMax",
                                      str(omero.constants.MESSAGESIZEMAX))

        # Setting ConnectTimeout
        self.parseAndSetInt(id, "Ice.Override.ConnectTimeout",\
                           omero.constants.CONNECTTIMEOUT)

        # Set large thread pool max values for all communicators
        for x in ("Client", "Server"):
            sizemax = id.properties.getProperty("Ice.ThreadPool.%s.SizeMax" %
                                                x)
            if not sizemax or len(sizemax) == 0:
                id.properties.setProperty("Ice.ThreadPool.%s.SizeMax" % x,
                                          "50")

        # Port, setting to default if not present
        port = self.parseAndSetInt(id, "omero.port",\
                                  omero.constants.GLACIER2PORT)

        # Default Router, set a default and then replace
        router = id.properties.getProperty("Ice.Default.Router")
        if not router or len(router) == 0:
            router = str(omero.constants.DEFAULTROUTER)
        host = id.properties.getPropertyWithDefault(
            "omero.host", """<"omero.host" not set>""")
        router = router.replace("@omero.port@", str(port))
        router = router.replace("@omero.host@", str(host))
        id.properties.setProperty("Ice.Default.Router", router)

        # Dump properties
        dump = id.properties.getProperty("omero.dump")
        if len(dump) > 0:
            m = self.getPropertyMap(id.properties)
            keys = list(m.keys())
            keys.sort()
            for key in keys:
                print "%s=%s" % (key, m[key])

        self.__lock.acquire()
        try:
            if self.__ic:
                raise omero.ClientError("Client already initialized")

            try:
                self.__ic = Ice.initialize(id)
            except Ice.EndpointParseException:
                msg = "No host specified. "
                msg += "Use omero.client(HOSTNAME), ICE_CONFIG, or similar."
                raise omero.ClientError(msg)

            if not self.__ic:
                raise omero.ClientError("Improper initialization")

            # Register Object Factory
            import ObjectFactoryRegistrar as ofr
            ofr.registerObjectFactory(self.__ic, self)

            for of in omero.rtypes.ObjectFactories.values():
                of.register(self.__ic)

            # Define our unique identifier (used during close/detach)
            self.__uuid = str(uuid.uuid4())
            ctx = self.__ic.getImplicitContext()
            if not ctx:
                raise omero.ClientError(
                    "Ice.ImplicitContext not set to Shared")
            ctx.put(omero.constants.CLIENTUUID, self.__uuid)

            # ticket:2951 - sending user group
            group = id.properties.getPropertyWithDefault("omero.group", "")
            if group:
                ctx.put("omero.group", group)

        finally:
            self.__lock.release()
Ejemplo n.º 31
0
    def process(self,
                client,
                session,
                job,
                current,
                params,
                properties=None,
                iskill=True):
        """
        session: session uuid, used primarily if client is None
        client: an omero.client object which should be attached to a session
        """

        if properties is None: properties = {}

        if not session or not job or not job.id:
            raise omero.ApiUsageException("No null arguments")

        file, handle = self.lookup(job)

        try:
            if not file:
                raise omero.ApiUsageException(\
                    None, None, "Job should have one executable file attached.")

            sf = self.internal_session()
            if params:
                self.logger.debug("Checking params for job %s" % job.id.val)
                svc = sf.getSessionService()
                inputs = svc.getInputs(session)
                errors = omero.scripts.validate_inputs(params, inputs, svc,
                                                       session)
                if errors:
                    errors = "Invalid parameters:\n%s" % errors
                    raise omero.ValidationException(None, None, errors)

            properties["omero.job"] = str(job.id.val)
            properties["omero.user"] = session
            properties["omero.pass"] = session
            properties["Ice.Default.Router"] = client.getProperty(
                "Ice.Default.Router")

            launcher, ProcessClass = self.find_launcher(current)
            process = ProcessClass(self.ctx,
                                   launcher,
                                   properties,
                                   params,
                                   iskill,
                                   omero_home=self.omero_home)
            self.resources.add(process)

            # client.download(file, str(process.script_path))
            scriptText = sf.getScriptService().getScriptText(file.id.val)
            process.script_path.write_bytes(scriptText)

            self.logger.info("Downloaded file: %s" % file.id.val)
            s = client.sha1(str(process.script_path))
            if not s == file.hash.val:
                msg = "Sha1s don't match! expected %s, found %s" % (
                    file.hash.val, s)
                self.logger.error(msg)
                process.cleanup()
                raise omero.InternalException(None, None, msg)
            else:
                process.activate()
                handle.setStatus("Running")

            id = None
            if self.category:
                id = Ice.Identity()
                id.name = "Process-%s" % uuid.uuid4()
                id.category = self.category
            prx = self.ctx.add_servant(current, process, ice_identity=id)
            return omero.grid.ProcessPrx.uncheckedCast(prx), process

        finally:
            handle.close()
Ejemplo n.º 32
0
    def assertCanEdit(self,
                      blitzObject,
                      expected=True,
                      sudo_needed=False,
                      exc_info=False):
        """ Checks the canEdit() method AND actual behavior (ability to edit) """

        self.assertEqual(
            blitzObject.canEdit(), expected,
            "Unexpected result of canEdit(). Expected: %s" % expected)
        # Now test if we can actually Edit and 'Hard link' the object
        nameEdited = False
        # for saves, omero group must *not* be -1
        origGroup = self.gateway.SERVICE_OPTS.getOmeroGroup(
        )  # need to switch back to this after edits
        gid = blitzObject.details.group.id.val
        self.gateway.SERVICE_OPTS.setOmeroGroup(gid)
        try:
            blitzObject.setName("new name: %s" % _uuid.uuid4())
            blitzObject.save()
            nameEdited = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                nameEdited = True  # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()

        objectUsed = False
        try:
            obj = blitzObject._obj
            if isinstance(obj, omero.model.Image):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.DatasetImageLinkI()
                link.setParent(ds)
                link.setChild(obj)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            elif isinstance(obj, omero.model.Project):
                ds = omero.model.DatasetI()
                ds.setName(omero.rtypes.rstring("assertCanEdit"))
                link = omero.model.ProjectDatasetLinkI()
                link.setParent(obj)
                link.setChild(ds)
                update = self.gateway.getUpdateService()
                rv = update.saveObject(link, self.gateway.SERVICE_OPTS)
            else:
                raise Exception("Unknown type: %s" % blitzObject)
            objectUsed = True
        except omero.ReadOnlyGroupSecurityViolation:
            if sudo_needed:
                objectUsed = True  # assume ok
        except omero.SecurityViolation:
            if exc_info:
                traceback.print_exc()

        self.gateway.SERVICE_OPTS.setOmeroGroup(origGroup)  # revert back
        self.assertEqual(
            blitzObject.canEdit(), expected,
            "Unexpected result of canEdit(). Expected: %s" % expected)
        self.assertEqual(nameEdited, expected,
                         "Unexpected ability to Edit. Expected: %s" % expected)
        self.assertEqual(objectUsed | sudo_needed, expected,
                         "Unexpected ability to Use. Expected: %s" % expected)
Ejemplo n.º 33
0
 def __init__(self):
     self.db_uuid = str(uuid.uuid4())
     self.return_values = []
Ejemplo n.º 34
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

SCRIPT = """if True:
    import omero
    import omero.scripts as s
    s.client("name")"""

import omero, omero.scripts
import time
import omero_ext.uuid as uuid # see ticket:3774

launched = time.time()
client = omero.client()
try:
    sf = client.createSession()
    scriptService = sf.getScriptService()
    id = scriptService.uploadOfficialScript(\
        "/examples/%s.py" % uuid.uuid4(), SCRIPT)
    proc = scriptService.runScript(id, None, None)
    cb = omero.scripts.ProcessCallbackI(client, proc)
    launched = time.time()
    while cb.block(500) is None:
        if 10.0 < (time.time() - launched):
            raise StopIteration("Too long!")
finally:
    print "Finished in (s): %s" % (time.time() - launched)
    client.closeSession();
Ejemplo n.º 35
0
    def _initData(self, id):
        """
        Initializes the current client via an Ice.InitializationData
        instance. This is called by all of the constructors, but may
        also be called on createSession(name, pass) if a previous
        call to closeSession() has nulled the Ice.Communicator.
        """

        if not id:
            raise omero.ClientError("No initialization data provided.")

        # Strictly necessary for this class to work
        id.properties.setProperty("Ice.ImplicitContext", "Shared")
        id.properties.setProperty("Ice.ACM.Client", "0")
        id.properties.setProperty("Ice.CacheMessageBuffers", "0")
        id.properties.setProperty("Ice.RetryIntervals", "-1")
        id.properties.setProperty("Ice.Default.EndpointSelection", "Ordered")
        id.properties.setProperty("Ice.Default.PreferSecure", "1")
        id.properties.setProperty("Ice.Plugin.IceSSL" , "IceSSL:createIceSSL")
        id.properties.setProperty("IceSSL.Ciphers" , "ADH")
        id.properties.setProperty("IceSSL.Protocols" , "tls1")
        id.properties.setProperty("IceSSL.VerifyPeer" , "0")

        # Setting block size
        blockSize = id.properties.getProperty("omero.block_size")
        if not blockSize or len(blockSize) == 0:
            id.properties.setProperty("omero.block_size", str(omero.constants.DEFAULTBLOCKSIZE))

        # Set the default encoding if this is Ice 3.5 or later
        # and none is set.
        if Ice.intVersion() >= 30500:
            if not id.properties.getProperty("Ice.Default.EncodingVersion"):
                id.properties.setProperty("Ice.Default.EncodingVersion", "1.0")

        # Setting MessageSizeMax
        messageSize = id.properties.getProperty("Ice.MessageSizeMax")
        if not messageSize or len(messageSize) == 0:
            id.properties.setProperty("Ice.MessageSizeMax", str(omero.constants.MESSAGESIZEMAX))

        # Setting ConnectTimeout
        self.parseAndSetInt(id, "Ice.Override.ConnectTimeout",\
                           omero.constants.CONNECTTIMEOUT)

        # Set large thread pool max values for all communicators
        for x in ("Client", "Server"):
            sizemax = id.properties.getProperty("Ice.ThreadPool.%s.SizeMax" % x)
            if not sizemax or len(sizemax) == 0:
                id.properties.setProperty("Ice.ThreadPool.%s.SizeMax" % x, "50")

        # Port, setting to default if not present
        port = self.parseAndSetInt(id, "omero.port",\
                                  omero.constants.GLACIER2PORT)

        # Default Router, set a default and then replace
        router = id.properties.getProperty("Ice.Default.Router")
        if not router or len(router) == 0:
            router = str(omero.constants.DEFAULTROUTER)
        host = id.properties.getPropertyWithDefault("omero.host", """<"omero.host" not set>""")
        router = router.replace("@omero.port@", str(port))
        router = router.replace("@omero.host@", str(host))
        id.properties.setProperty("Ice.Default.Router", router)

        # Dump properties
        dump = id.properties.getProperty("omero.dump")
        if len(dump) > 0:
            m = self.getPropertyMap(id.properties)
            keys = list(m.keys())
            keys.sort()
            for key in keys:
                print "%s=%s" % (key, m[key])

        self.__lock.acquire()
        try:
            if self.__ic:
                raise omero.ClientError("Client already initialized")

            try:
                self.__ic = Ice.initialize(id)
            except Ice.EndpointParseException:
                msg = "No host specified. "
                msg += "Use omero.client(HOSTNAME), ICE_CONFIG, or similar."
                raise omero.ClientError(msg)

            if not self.__ic:
                raise omero.ClientError("Improper initialization")

            # Register Object Factory
            import ObjectFactoryRegistrar as ofr
            ofr.registerObjectFactory(self.__ic, self)

            for of in omero.rtypes.ObjectFactories.values():
                of.register(self.__ic)

            # Define our unique identifier (used during close/detach)
            self.__uuid = str(uuid.uuid4())
            ctx = self.__ic.getImplicitContext()
            if not ctx:
                raise omero.ClientError("Ice.ImplicitContext not set to Shared")
            ctx.put(omero.constants.CLIENTUUID, self.__uuid)

            # ticket:2951 - sending user group
            group = id.properties.getPropertyWithDefault("omero.group", "")
            if group:
                ctx.put("omero.group", group)

        finally:
            self.__lock.release()
Ejemplo n.º 36
0
 def uuid(self):
     import omero_ext.uuid as _uuid # see ticket:3774
     return str(_uuid.uuid4())
Ejemplo n.º 37
0
 def repouuid(self):
     """
     Returns a string similar to that written by
     RandomAccessFile.writeUTF() in Java
     """
     return "XX%s" % uuid.uuid4()
Ejemplo n.º 38
0
 def repouuid(self):
     """
     Returns a string similar to that written by
     RandomAccessFile.writeUTF() in Java
     """
     return "XX%s" % uuid.uuid4()
Ejemplo n.º 39
0
## To see the actions of the usermode
## processor, enable logging here.
## ----------------------------------
# omero.util.configure_logging()

client = omero.client()
try:
    sf = client.createSession()
    scriptService = sf.getScriptService()

    # Store the script on the server. A uuid
    # is appended to the name since only one
    # script of each name is allowed.
    id = scriptService.uploadScript(\
        "/examples/NativeWrapper-%s.py" % uuid.uuid4(), NATIVE_WRAPPER)

    # Now we create a processor for handling the
    # script we just created since it is not an
    # official script. If we had used
    # scriptService.uploadOfficialScript, or if
    # the script was already installed in the server
    # we would not need the ump.
    ump = omero.processor.usermode_processor(client)
    try:
        # Sending this file itself as the target
        # but it could of course be any value you'd
        # like.
        target = os.path.join(os.getcwd(), __file__)
        target = omero.rtypes.rstring(target)
Ejemplo n.º 40
0
 def __init__(self):
     self.db_uuid = str(uuid.uuid4())
     self.return_values = []