def do_import(conn, session, filename, dataset=None, project=None):
    """
    Import the new image to OMERO using the command line importer
    
    @param conn: The BlitzGateway connection
    @param session: A dictionary containing the session ID and hostname
    @param filename: The path of the image being imported
    @param dataset: The dataset into which the new image is being placed
    @param project: The project into which the dataset is being placed
    """
    user = conn.getUser()
    
    sessionId = session['ID']
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["sessions", "login", "-s", "localhost", "-k", "%s" % sessionId], strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(conn, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    import_args.extend(["-s","localhost","-u","%s"%user.getName()])
    
    # redirect both stderr and stdout to file
    errlog = output_dir + "/stderr.txt"
    import_args.extend(["---errs",errlog])
    outlog = output_dir + "/stdout.txt"
    import_args.extend(["---file",outlog])
    cli.invoke(import_args, strict=True)
    
    # use stdout to get the id of the new image
    newImg = get_new_image(conn)
    return newImg
Example #2
0
 def put(self, filename, name=None, dataset=None):
     """
     Import filename using OMERO CLI, optionally with a specified name
     to a specified dataset (dataset_id).
     Return : OMERO image Id
     """
     cli = omero.cli.CLI()
     cli.loadplugins()
     import_args = ["import"]
     import_args.extend(["-s", str(self._server)])
     import_args.extend(["-k", str(self._key)])
     if dataset is not None:
         import_args.extend(["-d", str(dataset)])
     if name is not None:
         import_args.extend(["-n", str(name)])
     clio = "cli.out"
     clie = "cli.err"
     import_args.extend(["---errs=" + clie, "---file=" + clio, "--"])
     import_args.append(filename)
     cli.invoke(import_args, strict=True)
     pix_id = int(open(clio, 'r').read().rstrip())
     im_id = self.conn.getQueryService().get("Pixels", pix_id).image.id.val
     os.remove(clio)
     os.remove(clie)
     return im_id
Example #3
0
def runAsScript():
    """
    The main entry point of the script, as called by the client via the
    scripting service, passing the required parameters.
    """

    client = scripts.client('CLI Test.py', "Test script/CLI interactions")

    try:
        conn = BlitzGateway(client_obj=client)

        cli = omero.cli.CLI()
        cli.loadplugins()

        cmd = []
        cmd.extend(["login"])
        cmd.extend(["-s", "localhost"])
        # cmd.extend(["-g", conn.getGroupFromContext().getName()])
        cmd.extend(["-p", "4064"])
        cmd.extend(["-k", conn._getSessionId()])

        cli.invoke(cmd, strict=True)

    finally:
        client.closeSession()
Example #4
0
def do_import(conn, session, filename, dataset=None, project=None):
    user = conn.getUser()
    group = conn.getGroupFromContext()
    print "Current group: ", group.getName()

    sessionId = session['ID']
    hostname = session['host']
    print 'session', session
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(
        ["sessions", "login", "-s", "localhost", "-k",
         "%s" % sessionId],
        strict=True)
    #     cli.invoke(["login", "%s@localhost" % user.getName(), "-w", "omero", "-C"], strict=True)
    #     cli.invoke(["sessions", "group", group.getName()], strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(conn, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    import_args.extend(["-s", "localhost", "-u", "%s" % user.getName()])
    # redirect both stderr and stdout to file
    errlog = output_dir + "/stderr.txt"
    import_args.extend(["---errs", errlog])
    outlog = output_dir + "/stdout.txt"
    import_args.extend(["---file", outlog])
    print import_args
    cli.invoke(import_args, strict=True)

    # use stdout to get the id of the new image
    newImg = get_new_image(conn)
    return newImg
def do_import(conn, session, filename, dataset=None, project=None):
    user = conn.getUser()
    group = conn.getGroupFromContext()
    print "Current group: ", group.getName()
    
    sessionId = session['ID']
    hostname = session['host']
    print 'session',session
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["sessions", "login", "-s", "localhost", "-k", "%s" % sessionId], strict=True)
#     cli.invoke(["login", "%s@localhost" % user.getName(), "-w", "omero", "-C"], strict=True)
#     cli.invoke(["sessions", "group", group.getName()], strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(conn, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    import_args.extend(["-s","localhost","-u","%s"%user.getName()])
    # redirect both stderr and stdout to file
    errlog = output_dir + "/stderr.txt"
    import_args.extend(["---errs",errlog])
    outlog = output_dir + "/stdout.txt"
    import_args.extend(["---file",outlog])
    print import_args
    cli.invoke(import_args, strict=True)
    
    # use stdout to get the id of the new image
    newImg = get_new_image(conn)
    return newImg
Example #6
0
def main(args):
    #filename=args[0];
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["login", "root@localhost", "-w", "omero", "-C"], strict=True)
    import_args = ["import"]
    dsId = create_containers(cli, "dataset with images", "project with a dataset")
    import_args.extend(["-d", str(dsId)])
    import_args.append(args[0])
    print import_args
    cli.invoke(import_args, strict=True)
def import_results(results, user, group, sid, conn):
    """
    Import new results, print errors, return number of attempted imports

    'results' is a list of json strings representing dicts with keys:
        inputs: list of dicts including, one per job input image including,
                inputID: input Image ID
                datasetID: dataset ID for this result (same as input image)
        fail: string with error message for failed processing job
        results: 1st path uploaded as image file, all others attached
    """
    for result in results:
        try:
            r = json.loads(result)
            # use imageID & datasetID of 1st input image
            image_id = r['inputs'][0]['imageID']
            dataset_id = r['inputs'][0]['datasetID']
            if 'fail' in r:
                print "ER decon failed for imageID=%s: %s" % \
                    (image_id, r['fail'])
            else:
                cli = omero.cli.CLI()
                cli.loadplugins()
                import_args = ["import", "-s", HOST, "-k", "%s" % sid]
                import_args += ["-d", str(dataset_id)]
                result_dir = os.path.split(r['results'][0])[0]
                plog = os.path.join(result_dir, "imports.log")
                perr = os.path.join(result_dir, "imports.err")
                result_image = r['results'][0]
                import_args += ["---errs", perr, "---file", plog, result_image]
                cli.invoke(import_args, strict=True)
                with open(plog, 'r') as flog:
                    pix = int(flog.readlines()[0].rstrip())  # Pixels ID
                iid = conn.getQueryService().get("Pixels", pix).image.id.val
                img = conn.getObject("Image", oid=iid)
                descrip = "ER decon result from Image ID: %s" % image_id
                img.setDescription(descrip)
                if len(r['results']) > 1:
                    # attach remaining results
                    for attachment in r['results'][1:]:
                        fann = conn.createFileAnnfromLocalFile(str(attachment))
                        img.linkAnnotation(fann)
                img.save()

        except Exception as e:
            print str(e)

    return len(results)
Example #8
0
def cliImport(client,
              ipath,
              destID,
              skip,
              depth,
              namespace,
              dataset=None,
              conn=None):
    # create import call string
    args = createArgumentList(ipath, destID, skip, depth)

    images_skipped = None
    images_imported = None

    try:
        with tempfile.NamedTemporaryFile(suffix=".stdout") as stdout:
            with tempfile.NamedTemporaryFile(suffix=".stderr") as stderr:
                cli = omero.cli.CLI()
                cli.loadplugins()
                #cli.set_client(client)
                cli.set_client(client.createClient(secure=True))
                args.extend(["--file", stdout.name])
                args.extend(["--errs", stderr.name])

                cli.invoke(args)
                images_skipped, images_imported = parseLogFile(stderr)
                print("Images imported: ", len(images_imported))
                print("Images skipped: ", len(images_skipped))

                if dataset:
                    #append log file
                    #link reportFile to object
                    ann = conn.createFileAnnfromLocalFile(stderr.name,
                                                          mimetype="text/csv",
                                                          ns=namespace +
                                                          "_log")
                    dataset.linkAnnotation(ann)

                return images_skipped, images_imported, stderr
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        print('ERROR at cli import: %s\n %s %s' %
              (str(e), exc_type, exc_tb.tb_lineno))
        return None, None, None
    finally:
        return images_skipped, images_imported, None
Example #9
0
def do_import(conn, session, filename, dataset=None, project=None):
    """
    Use the omero command line interface to import the image.
    Use the current session.

    :param conn:            the BlitzGateWay connection
    :param session:         dictionary containing the session ID and
                            hostname
    :param filename:        name of the file being imported to omero
    :param dataset:         the omero dataset where we are importing to
    :param project:         the omero project where we are importing to
    """
    user = conn.getUser()
    group = conn.getGroupFromContext()

    sessionId = session['ID']
    hostname = session['host']
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(
        ["sessions", "login", "-s", "localhost", "-k", "{}".format(sessionId)],
        strict=True)

    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(conn, dataset, project)
        import_args.extend(["-d", str(dsId)])

    import_args.append(filename)
    import_args.extend(["-s", "localhost", "-u", "{}".format(user.getName())])

    # redirect both stderr and stdout to file
    errlog = output_dir + "/stderr.txt"
    import_args.extend(["---errs", errlog])
    outlog = output_dir + "/stdout.txt"
    import_args.extend(["---file", outlog])
    cli.invoke(import_args, strict=True)

    # use stdout to get the id of the new image
    newImg = get_new_image(conn)
    return newImg
Example #10
0
def celery_import(conn, tempdir, filename, groupId, datasetId):
    conn.SERVICE_OPTS.setOmeroGroup(groupId)
    user = conn.getUser()
    #sessionId = session['ID']
    sessionId = conn.c.getSessionId()

    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["sessions", "login", "-s", "localhost", "-k", "%s" % sessionId], strict=True)
    cli.invoke(["sessions", "group", "%s" % groupId], strict=True)    
    import_args = ["import"]
    if datasetId is not None:
        import_args.extend(["-d", str(datasetId)])
    import_args.append(filename)
    import_args.extend(["-s","localhost","-u","%s"%user.getName()])
    
    # redirect both stderr and stdout to file
    errlog = tempdir + "/stderr.txt"
    import_args.extend(["---errs",errlog])
    outlog = tempdir + "/stdout.txt"
    import_args.extend(["---file",outlog])
    cli.invoke(import_args, strict=True)
    
    # use stdout to get the id of the new image
    newImg = get_new_image(conn,tempdir)
    empty_temp(tempdir)
    return newImg
Example #11
0
def do_import(user, group, filename):
    print user, group, filename
    print "-"*100
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["login", "%s@localhost" % user, "-w", "ome", "-C"], strict=True)
    cli.invoke(["sessions", "group", group], strict=True)
    cli.invoke(["import", filename], strict=True)
def do_import(conn, session, filename, dataset=None, project=None):
    """
    Import the new image to OMERO using the command line importer
    
    @param conn: The BlitzGateway connection
    @param session: A dictionary containing the session ID and hostname
    @param filename: The path of the image being imported
    @param dataset: The dataset into which the new image is being placed
    @param project: The project into which the dataset is being placed
    """
    user = conn.getUser()

    sessionId = session['ID']
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(
        ["sessions", "login", "-s", "localhost", "-k",
         "%s" % sessionId],
        strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(conn, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    import_args.extend(["-s", "localhost", "-u", "%s" % user.getName()])

    # redirect both stderr and stdout to file
    errlog = output_dir + "/stderr.txt"
    import_args.extend(["---errs", errlog])
    outlog = output_dir + "/stdout.txt"
    import_args.extend(["---file", outlog])
    cli.invoke(import_args, strict=True)

    # use stdout to get the id of the new image
    newImg = get_new_image(conn)
    return newImg
Example #13
0
def do_import(user, group, filename, dataset=None, project=None):
    print user, group, filename
    print "-"*100
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["login", "%s@localhost" % user, "-w", "ome", "-C"], strict=True)
    cli.invoke(["sessions", "group", group], strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(cli, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    print import_args
    cli.invoke(import_args, strict=True)
Example #14
0
def do_import(user, group, filename, dataset=None, project=None):
    print user, group, filename
    print "-"*100
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli.invoke(["login", "%s@localhost" % user, "-w", "ome", "-C"], strict=True)
    cli.invoke(["sessions", "group", group], strict=True)
    import_args = ["import"]
    if dataset is not None:
        dsId = create_containers(cli, dataset, project)
        import_args.extend(["-d", str(dsId)])
    import_args.append(filename)
    print import_args
    cli.invoke(import_args, strict=True)
Example #15
0
    def do_import(self, project_name, dataset_name, ext, image_dir, image_num):
        image_files = []
        p_id = -1
        d_id = -1

        if os.path.isfile(image_dir):  # target = file
            image_files.append(image_dir)
        elif os.path.isdir(image_dir):  # target = dir
            search_command = '*' + ext
            image_files = glob.glob(os.path.join(image_dir, search_command))
        else:
            print "Cannot find file or dir:", image_dir
            return -1

        if len(image_files) != int(image_num):
            raise Exception(
                "Number of images does not match: %s <> %s in %s" %
                (str(len(image_files)), str(int(image_num)), dataset_name))

        image_files.sort()  # ソート
        for image_file in image_files:
            if os.path.isfile(image_file):
                cli = omero.cli.CLI()
                cli.loadplugins()
                cli.invoke([
                    "login", "-s", self.HOST, "-p", self.PORT, "-u", self.USER,
                    "-w", self.PASSWORD, "-C"
                ],
                           strict=True)
                cli.invoke(["sessions", "group", self.GROUP], strict=True)
                try:
                    p_id, d_id = self.create_containers(
                        project_name, dataset_name)
                    title = os.path.basename(image_file)  # OMEROで見えるファイル名
                    cli.invoke(
                        ["import", "-d",
                         str(d_id), "-n", title, image_file],
                        strict=True)
                    print p_id, d_id
                except:
                    print "Import failed for %s : %s" % (image_file, str(
                        cli.rv))

                cli.close()

        return p_id, d_id
    def do_import(self, user, group, project, dataset, archive, filename=None):
        cli = omero.cli.CLI()
        cli.loadplugins()
        cli.invoke(["login", "%s@localhost" % user, "-w", "ome", "-C"], strict=True)
        cli.invoke(["sessions", "group", group], strict=True)
        import_args = ["import"]
        if archive:
            import_args.extend(["-a"])

        if project == self.screens:
            if dataset != self.orphans:
                targetId = self.create_screen(cli, dataset)
                import_args.extend(["-r", str(targetId)])
                output = "Importing plate(s) into Screen:" + dataset
            else:
                output = "Importing plate(s) as an orphan"
        else:
            if project == self.no_projs:
                targetId = self.create_containers(cli, None, dataset)
                import_args.extend(["-d", str(targetId)])
                output = "Importing image(s) into Dataset:" + dataset
            elif dataset == self.no_dats:
                self.create_containers(cli, project, None)
                targetId = None
            elif project != self.orphans and dataset != self.orphans:
                targetId = self.create_containers(cli, project, dataset)
                import_args.extend(["-d", str(targetId)])
                output = "Importing image(s) into Project/Dataset:" + project+"/"+dataset
            else:
                output = "Importing image(s) as orphan(s)"

        if filename is not None:
            print output,
            try:
                import_args.append(filename)
                print " ...using import args", import_args
                cli.invoke(import_args, strict=True)
            except:
                print "Import failed!"
        else:
            print "No import, just container creation."
Example #17
0
            continue
        rois = create_rois(im, seg_im)
        if len(rois) == 0:
            print("Image:%s - No ROIs found" % im.id)
        else:
            if DRYRUN:
                print("Image:%s - Found %s ROIs" % (im.id, len(rois)))
            else:
                save_rois(conn, im, rois)


class IDR0076Control(omero.cli.BaseControl):
    def _configure(self, parser):
        parser.add_login_arguments()
        parser.set_defaults(func=self.__call__)

    def __call__(self, args):
        c = self.ctx.conn(args)
        conn = BlitzGateway(client_obj=c)
        main(conn)


try:
    register("idr0076", IDR0076Control, "help")
except NameError:
    if __name__ == "__main__":
        cli = omero.cli.CLI()
        cli.loadplugins()
        cli.register("idr0076", IDR0076Control, "help")
        cli.invoke(sys.argv[1:])
  def send_child(self):
    """Send/export/upload processed image back into omero."""

    cli = omero.cli.CLI()
    cli.loadplugins()

    ## TODO replace with a property setter once it is implemented
    ##      https://trac.openmicroscopy.org.uk/ome/ticket/12388
    cli._client = self.client.createClient(secure = True)

    cmd = [
      "import",
      "--debug", "ERROR",
    ]
    if self.datasetID:
      cmd.extend(["-d", str(self.datasetID)])
    if self.child_name:
      cmd.extend(["-n", self.child_name])

    ## TODO experiment setting STDOUT into a variable rather than temporary
    ##      file such with StringIO

    ## The ID of exported image will be printed back to STDOUT. So we need
    ## to catch it in file, and read that file to get its ID. And yeah, this
    ## is a bit convoluted but it is the recommended method.
    cid = None
    with tempfile.NamedTemporaryFile(suffix=".stdout") as stdout:
      ## FIXME when stuff is printed to stderr, the user will get a file
      ##       to download with that text. Unfortunately, non-errors are
      ##       still being printed there. The filtering is broken in 5.0.1
      ##       but on future releases we may be able to simply not set
      ##       "---errs" option.
      ##       https://github.com/openmicroscopy/openmicroscopy/issues/2477
      cmd.extend([
        "---errs", os.devnull,
        "---file", stdout.name,
      ])
      cmd.append(self.fout.name)

      ## FIXME https://github.com/openmicroscopy/openmicroscopy/issues/2476
      STDERR = sys.stderr
      try:
        with open(os.devnull, 'w') as DEVNULL:
          sys.stderr = DEVNULL
          cli.invoke(cmd)
      finally:
        sys.stderr = STDERR
      ret_code = cli.rv

      if ret_code == 0:
        ## we only need to read one line or something is very wrong
        cid = int(stdout.readline())
        if not cid:
          raise Exception("unable to get exported image ID")
      else:
        ## I am not going to redirect stderr to a temp file, read it back
        ## in case of an error, and then print it to stderr myself so that
        ## the user gets a file to download with the errors. This is being
        ## fixed upstream already.
        ## https://github.com/openmicroscopy/openmicroscopy/issues/2477
        raise Exception("failed to import processed image into the database")

    self.child = self.conn.getObject("Image", cid)
    def importFile(self, fileName, exName):
        """
            Import file or directory using 'bin/omero importer'
            This method is solely responsible for logging the user in,
            attempting (possibly multiply) an import, logging and
            throwing an exception if necessary.
        """

        try:
            self.state.appropriateWait(self.throttleImport)  # See ticket:5739

            key = self.loginUser(exName)
            if not key:
                self.log.info("File not imported: %s", fileName)
                return
            self.log.info("Importing %s (session=%s)", fileName, key)

            imageId = []

            t = create_path("dropbox", "err")
            to = create_path("dropbox", "out")

            cli = omero.cli.CLI()
            cli.loadplugins()
            cmd = ["-s", self.host, "-p", str(self.port), "-k", key, "import"]
            cmd.extend(
                [str("---errs=%s" % t),
                    str("---file=%s" % to), "--", "--agent=dropbox"])
            cmd.extend(shlex.split(self.importArgs))
            cmd.append(fileName)
            logging.debug("cli.invoke(%s)" % cmd)
            cli.invoke(cmd)
            retCode = cli.rv

            if retCode == 0:
                self.log.info(
                    "Import of %s completed (session=%s)", fileName, key)
                if to.exists():
                    f = open(str(to), "r")
                    lines = f.readlines()
                    f.close()
                    if len(lines) > 0:
                        for line in lines:
                            imageId.append(line.strip())
                    else:
                        self.log.error("No lines in output file. No image ID.")
                else:
                    self.log.error("%s not found !" % to)

            else:
                self.log.error(
                    "Import of %s failed=%s (session=%s)",
                    fileName, str(retCode), key)
                self.log.error(
                    "***** start of output from importer-cli to stderr *****")
                if t.exists():
                    f = open(str(t), "r")
                    lines = f.readlines()
                    f.close()
                    for line in lines:
                        self.log.error(line.strip())
                else:
                    self.log.error("%s not found !" % t)
                self.log.error("***** end of output from importer-cli *****")
        finally:
            remove_path(t)
            remove_path(to)

        return imageId
Example #20
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import omero
import omero.cli

HOST = "yourhost"
USER = "******"
PASS = "******"

client = omero.client(HOST)
client.createSession(USER, PASS)

try:
    cli = omero.cli.CLI()
    cli.loadplugins()
    cli._client = client

    for x in sys.argv[1:]:
        cli.invoke([
            "hql", "-q", "--limit", "-1",
            ("SELECT i.id FROM Experimenter i "
             "WHERE i.omeName='%s'") % x
        ])

finally:
    client.__del__()
def imageUploadImportOmero(request, conn=None, **kwargs):
    try:
        fileId = request.POST.get("fileId", 0)
        datasetId = request.POST.get("datasetId", 0)
        path = ImportFile.getPathFromId(fileId)
        if path is None:
            return HttpResponse("FAIL:No path for temp file ID " + str(fileId))

        dataset = conn.getObject("Dataset", datasetId)
        if dataset is None:
            return HttpResponse("FAIL:No dataset with ID " + str(datasetId))

        txt = ""
        ret_code = -1

        with tempfile.NamedTemporaryFile(suffix=".stdout") as stdout:
            with tempfile.NamedTemporaryFile(suffix=".stderr") as stderr:
                cli = omero.cli.CLI()
                cli.loadplugins()
                cli._client = conn.c
                cli.invoke([
                    "import", "---errs", stderr.name, "---file", stdout.name,
                    "--no-upgrade-check", path, "-d", datasetId
                ])
                ret_code = cli.rv
                if ret_code == 0:
                    txt = "IM_" + stdout.readline().split(':')[1]
        # if there was an import error, we expect the file is not an image
        if ret_code != 0:
            newFileId = -1
            fileName = os.path.basename(path)
            fileSize = os.path.getsize(path)
            c = omero.client('localhost', 4064)
            jSes = c.joinSession(conn.getEventContext().sessionUuid)
            fileService = jSes.getReceptorLightFileManager()
            if fileService is not None:
                newFileId = fileService.createFile(fileName)
                if newFileId > 0:
                    newFileInfo = fileService.getFileInformation(newFileId)
                    if newFileInfo is not None:
                        ft = newFileInfo.getFileType()
                        ft.setValue(omero.rtypes.rstring("ReceptorLightFile"))
                        newFileInfo.setFileType(ft)
                        fSi = omero.rtypes.rint(int(fileSize))
                        newFileInfo.setSize(fSi)

                        fileService.setFileInformation(newFileId, newFileInfo)

                        dataPath = fileService.getDataDir()
                        targetPath = dataPath + '/RL_FILES/' + \
                            str(newFileId) + '_' + fileName
                        os.remove(targetPath)
                        shutil.move(path, targetPath)
                        txt = "FI_" + str(newFileId)
                    else:
                        txt = "FAIL:FileInfo was NULL."
                else:
                    txt = "FAIL:FIleID was -1."
            else:
                txt = "FAIL:FileServer was NULL."
        return HttpResponse(txt)
    except Exception as e:
        txt = "FAIL:" + str(e)
        return HttpResponse(txt)
    def send_child(self):
        """Send/export/upload processed image back into omero."""

        cli = omero.cli.CLI()
        cli.loadplugins()

        ## TODO replace with a property setter once it is implemented
        ##      https://trac.openmicroscopy.org.uk/ome/ticket/12388
        cli._client = self.client.createClient(secure=True)

        cmd = [
            "import",
            "--debug",
            "ERROR",
        ]
        if self.datasetID:
            cmd.extend(["-d", str(self.datasetID)])
        if self.child_name:
            cmd.extend(["-n", self.child_name])

        ## TODO experiment setting STDOUT into a variable rather than temporary
        ##      file such with StringIO

        ## The ID of exported image will be printed back to STDOUT. So we need
        ## to catch it in file, and read that file to get its ID. And yeah, this
        ## is a bit convoluted but it is the recommended method.
        cid = None
        with tempfile.NamedTemporaryFile(suffix=".stdout") as stdout:
            ## FIXME when stuff is printed to stderr, the user will get a file
            ##       to download with that text. Unfortunately, non-errors are
            ##       still being printed there. The filtering is broken in 5.0.1
            ##       but on future releases we may be able to simply not set
            ##       "---errs" option.
            ##       https://github.com/openmicroscopy/openmicroscopy/issues/2477
            cmd.extend([
                "---errs",
                os.devnull,
                "---file",
                stdout.name,
            ])
            cmd.append(self.fout.name)

            ## FIXME https://github.com/openmicroscopy/openmicroscopy/issues/2476
            STDERR = sys.stderr
            try:
                with open(os.devnull, 'w') as DEVNULL:
                    sys.stderr = DEVNULL
                    cli.invoke(cmd)
            finally:
                sys.stderr = STDERR
            ret_code = cli.rv

            if ret_code == 0:
                ## we only need to read one line or something is very wrong
                cid = int(stdout.readline())
                if not cid:
                    raise Exception("unable to get exported image ID")
            else:
                ## I am not going to redirect stderr to a temp file, read it back
                ## in case of an error, and then print it to stderr myself so that
                ## the user gets a file to download with the errors. This is being
                ## fixed upstream already.
                ## https://github.com/openmicroscopy/openmicroscopy/issues/2477
                raise Exception(
                    "failed to import processed image into the database")

        self.child = self.conn.getObject("Image", cid)
Example #23
0
 def query_remote(self, cli):
     # invoke login
     # cli.invoke(["login"])
     cli.invoke(["hql", "-q", "'select g.name from ExperimenterGroup g'"])
    def importFile(self, fileName, exName):
        """
            Import file or directory using 'bin/omero importer'
            This method is solely responsible for logging the user in,
            attempting (possibly multiply) an import, logging and
            throwing an exception if necessary.
        """

        key = self.loginUser(exName)
        if not key:
            self.log.info("File not imported: %s", fileName)
            return

        try:
            self.state.appropriateWait(self.throttleImport) # See ticket:5739
            self.log.info("Importing %s (session=%s)", fileName, key)

            imageId = []

            t = create_path("dropbox", "err")
            to = create_path("dropbox", "out")

            cli = omero.cli.CLI()
            cli.loadplugins()
            cmd = ["-s", self.host, "-p", str(self.port), "-k", key, "import"]
            cmd.extend([str("---errs=%s"%t), str("---file=%s"%to), "--", "--agent=dropbox"])
            cmd.extend(shlex.split(self.importArgs))
            cmd.append(fileName)
            logging.debug("cli.invoke(%s)" % cmd)
            cli.invoke(cmd)
            retCode = cli.rv

            if retCode == 0:
                self.log.info("Import of %s completed (session=%s)", fileName, key)
                if to.exists():
                    f = open(str(to),"r")
                    lines = f.readlines()
                    f.close()
                    if len(lines) > 0:
                        for line in lines:
                            imageId.append(line.strip())
                    else:
                        self.log.error("No lines in output file. No image ID.")
                else:
                    self.log.error("%s not found !" % to)

            else:
                self.log.error("Import of %s failed=%s (session=%s)", fileName, str(retCode), key)
                self.log.error("***** start of output from importer-cli to stderr *****")
                if t.exists():
                    f = open(str(t),"r")
                    lines = f.readlines()
                    f.close()
                    for line in lines:
                        self.log.error(line.strip())
                else:
                    self.log.error("%s not found !" % t)
                self.log.error("***** end of output from importer-cli *****")
        finally:
            remove_path(t)
            remove_path(to)

        return imageId
    del os.environ["ICE_CONFIG"]

    # Find image files
    uploaded_image_ids = []
    for image_id in ids:
        image = local_conn.getObject("Image", image_id)
        print image.getName()

        temp_file = NamedTemporaryFile().name
        try:
            # TODO haven't tested an image with multiple files.
            for f in image.getImportedImageFiles():
                file_loc = os.path.join(managed_dir, f.path, f.name)
                print "file location is: ", file_loc
                with open(temp_file, 'wr') as f, stdout_redirected(f):
                    cli.invoke(["import", file_loc])

                with open(temp_file, 'r') as f:
                    txt = f.readline()
                    print "text is ", txt
                    assert txt.startswith("Image:")
                    uploaded_image_ids.append(re.findall(r'\d+', txt)[0])

            print "ids are: ", uploaded_image_ids
        # See this for how to scrape image id from file:
        # https://github.com/openmicroscopy/openmicroscopy/blob/develop/components/
        # tools/OmeroPy/src/omero/testlib/__init__.py#L277
        except Exception as inst:
            # TODO handle errors.
            print type(inst)  # the exception instance
            print inst.args  # arguments stored in .args