Example #1
0
def get_jars():
    '''Get the final list of JAR files passed to javabridge'''
    imagej_path = get_path_to_jars()
    if hasattr(sys, 'frozen'):
        jar_files = [
            jar_filename for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")
        ]

        def sort_fn(a, b):
            aa, bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                      for x in a, b]
            return cmp(aa, bb)

        jar_files = sorted(jar_files, cmp=sort_fn)
    else:
        jar_files = get_cellprofiler_jars()
    jar_files = [os.path.join(imagej_path, f) for f in jar_files]
    class_path = javabridge.JARS + jar_files

    if os.environ.has_key("CLASSPATH"):
        class_path += os.environ["CLASSPATH"].split(os.pathsep)
        logging.debug("Adding Java class path from environment variable, "
                      "CLASSPATH"
                      "")
        logging.debug("    CLASSPATH=" + os.environ["CLASSPATH"])

    plugin_directory = cpprefs.get_ij_plugin_directory()
    if (plugin_directory is not None and os.path.isdir(plugin_directory)):
        logger.debug("Using %s as imagej plugin directory" % plugin_directory)
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path.append(plugin_directory)
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path.append(jarpath)
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: " +
                    plugin_directory)

    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from javabridge.locate import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib", "tools.jar")
            class_path.append(tools_jar)
        else:
            logger.warning("Failed to find tools.jar")
    return class_path
Example #2
0
def get_jars():
    '''Get the final list of JAR files passed to javabridge'''
    imagej_path = get_path_to_jars()
    if hasattr(sys, 'frozen'):
        jar_files = [
            jar_filename
            for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")]
        def sort_fn(a, b):
            aa,bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                     for x in a, b]
            return cmp(aa, bb)
        jar_files = sorted(jar_files, cmp = sort_fn)
    else:
        jar_files = get_cellprofiler_jars()
    jar_files = [os.path.join(imagej_path, f)  for f in jar_files]
    class_path = javabridge.JARS + jar_files
    
    if os.environ.has_key("CLASSPATH"):
        class_path += os.environ["CLASSPATH"].split(os.pathsep)
        logging.debug(
            "Adding Java class path from environment variable, ""CLASSPATH""")
        logging.debug("    CLASSPATH="+os.environ["CLASSPATH"])
        
    plugin_directory = cpprefs.get_ij_plugin_directory()
    if (plugin_directory is not None and 
        os.path.isdir(plugin_directory)):
        logger.debug("Using %s as imagej plugin directory" % plugin_directory)
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path.append(plugin_directory)
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path.append(jarpath)
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: "
                    + plugin_directory)
        
    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from javabridge.locate import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib","tools.jar")
            class_path.append(tools_jar)
        else:
            logger.warning("Failed to find tools.jar")
    return class_path
Example #3
0
def start_cellprofiler_jvm():
    '''Start the Java VM with arguments appropriate for CellProfiler'''
    global logger
    
    if hasattr(sys, 'frozen'):
        if sys.platform != 'darwin':
            root_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
            bioformats_path = os.path.join(root_path, 'bioformats')
        else:
            bioformats_path = os.path.abspath(os.path.split(__file__)[0])
            root_path = os.path.split(bioformats_path)[0]
        imagej_path = os.path.join(root_path, 'imagej','jars')
        def sort_fn(a, b):
            aa,bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                     for x in a, b]
            return cmp(aa, bb)
        jar_files = [
            jar_filename
            for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")]
        jar_files = sorted(jar_files, cmp = sort_fn)
    else:
        bioformats_path = os.path.abspath(os.path.split(__file__)[0])
        root_path = os.path.split(bioformats_path)[0]
        jar_files = get_cellprofiler_jars()
        imagej_path = os.path.join(root_path, 'imagej','jars')
    
    class_path = os.pathsep.join(
        [os.path.join(imagej_path, jar_file) for jar_file in jar_files])
    
    if os.environ.has_key("CLASSPATH"):
        class_path += os.pathsep + os.environ["CLASSPATH"]
        
    plugin_directory = get_ij_plugin_directory()
    logger.debug("Using %s as imagej plugin directory" % plugin_directory)
    if (plugin_directory is not None and 
        os.path.isdir(plugin_directory)):
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path += os.pathsep + plugin_directory
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path += os.pathsep + jarpath
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: " + plugin_directory)
        
    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from cellprofiler.utilities.setup import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib","tools.jar")
            class_path += os.pathsep + tools_jar
        else:
            logger.warning("Failed to find tools.jar")
    
    jvm_arg = [x.groups()[0] for x in [
        re.match('--jvm-heap-size=([0-9]+[gGkKmM])', y) for y in sys.argv]
               if x is not None]
    if len(jvm_arg) > 0:
        jvm_arg = jvm_arg[0]
    else:
        jvm_arg = "512m"
        
    args = [r"-Djava.class.path="+class_path,
            r"-Dloci.bioformats.loaded=true",
            #r"-verbose:class",
            #r"-verbose:jni",
            r"-Xmx%s" % jvm_arg]
    #
    # Get the log4j logger setup from a file in the bioformats directory
    # if such a file exists.
    #
    log4j_properties = os.path.join(bioformats_path, "log4j.properties")
    if os.path.exists(log4j_properties):
        log4j_properties = "file:/"+log4j_properties.replace(os.path.sep, "/")
        args += [r"-Dlog4j.configuration="+log4j_properties]
        init_logger = False
    else:
        init_logger = True
    
    if get_headless():
        # We're running silently, so don't change the Java preferences
        # The following definition uses a process-scope preferences factory
        args += [
            "-Djava.util.prefs.PreferencesFactory="
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]
    run_headless = (get_headless() and 
                    not os.environ.has_key("CELLPROFILER_USE_XVFB"))
    run_headless = False
        
    logger.debug("JVM arguments: " + " ".join(args))
    jutil.start_vm(args, run_headless)
    logger.debug("Java virtual machine started.")
    jutil.attach()
    try:
        jutil.static_call("loci/common/Location",
                          "cacheDirectoryListings",
                          "(Z)V", True)
    except:
        logger.warning("Bioformats version does not support directory cacheing")
    
    #
    # Start the log4j logger to avoid error messages.
    #
    if init_logger:
        try:
            jutil.static_call("org/apache/log4j/BasicConfigurator",
                              "configure", "()V")
            log4j_logger = jutil.static_call("org/apache/log4j/Logger",
                                             "getRootLogger",
                                             "()Lorg/apache/log4j/Logger;")
            warn_level = jutil.get_static_field("org/apache/log4j/Level","WARN",
                                                "Lorg/apache/log4j/Level;")
            jutil.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V", 
                       warn_level)
            del logger
            del warn_level
        except:
            logger.error("Failed to initialize log4j\n", exc_info=True)
    if not get_headless():
        jutil.activate_awt()
Example #4
0
def start_cellprofiler_jvm():
    '''Start the Java VM with arguments appropriate for CellProfiler'''
    global logger

    if hasattr(sys, 'frozen'):
        if sys.platform != 'darwin':
            root_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
            bioformats_path = os.path.join(root_path, 'bioformats')
        else:
            bioformats_path = os.path.abspath(os.path.split(__file__)[0])
            root_path = os.path.split(bioformats_path)[0]
        imagej_path = os.path.join(root_path, 'imagej', 'jars')

        def sort_fn(a, b):
            aa, bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                      for x in a, b]
            return cmp(aa, bb)

        jar_files = [
            jar_filename for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")
        ]
        jar_files = sorted(jar_files, cmp=sort_fn)
    else:
        bioformats_path = os.path.abspath(os.path.split(__file__)[0])
        root_path = os.path.split(bioformats_path)[0]
        jar_files = get_cellprofiler_jars()
        imagej_path = os.path.join(root_path, 'imagej', 'jars')

    class_path = os.pathsep.join(
        [os.path.join(imagej_path, jar_file) for jar_file in jar_files])

    if os.environ.has_key("CLASSPATH"):
        class_path += os.pathsep + os.environ["CLASSPATH"]

    if (get_ij_plugin_directory() is not None
            and os.path.isdir(get_ij_plugin_directory())):
        plugin_directory = get_ij_plugin_directory()
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path += os.pathsep + plugin_directory
        #
        # Add any .jar files in the directory
        #
        class_path += os.pathsep + os.pathsep.join([
            os.path.join(plugin_directory, jarfile)
            for jarfile in os.listdir(plugin_directory)
            if jarfile.lower().endswith(".jar")
        ])

    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from cellprofiler.utilities.setup import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib", "tools.jar")
            class_path += os.pathsep + tools_jar
        else:
            logger.warning("Failed to find tools.jar")

    jvm_arg = [
        x.groups()[0] for x in
        [re.match('--jvm-heap-size=([0-9]+[gGkKmM])', y) for y in sys.argv]
        if x is not None
    ]
    if len(jvm_arg) > 0:
        jvm_arg = jvm_arg[0]
    else:
        jvm_arg = "512m"

    args = [
        r"-Djava.class.path=" + class_path,
        r"-Dloci.bioformats.loaded=true",
        #r"-verbose:class",
        #r"-verbose:jni",
        r"-Xmx%s" % jvm_arg
    ]
    #
    # Get the log4j logger setup from a file in the bioformats directory
    # if such a file exists.
    #
    log4j_properties = os.path.join(bioformats_path, "log4j.properties")
    if os.path.exists(log4j_properties):
        log4j_properties = "file:/" + log4j_properties.replace(
            os.path.sep, "/")
        args += [r"-Dlog4j.configuration=" + log4j_properties]
        init_logger = False
    else:
        init_logger = True

    if get_headless():
        # We're running silently, so don't change the Java preferences
        # The following definition uses a process-scope preferences factory
        args += [
            "-Djava.util.prefs.PreferencesFactory="
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"
        ]
    run_headless = (get_headless()
                    and not os.environ.has_key("CELLPROFILER_USE_XVFB"))
    run_headless = False

    logger.debug("JVM arguments: " + " ".join(args))
    jutil.start_vm(args, run_headless)
    logger.debug("Java virtual machine started.")
    jutil.attach()
    try:
        jutil.static_call("loci/common/Location", "cacheDirectoryListings",
                          "(Z)V", True)
    except:
        logger.warning(
            "Bioformats version does not support directory cacheing")

    #
    # Start the log4j logger to avoid error messages.
    #
    if init_logger:
        try:
            jutil.static_call("org/apache/log4j/BasicConfigurator",
                              "configure", "()V")
            log4j_logger = jutil.static_call("org/apache/log4j/Logger",
                                             "getRootLogger",
                                             "()Lorg/apache/log4j/Logger;")
            warn_level = jutil.get_static_field("org/apache/log4j/Level",
                                                "WARN",
                                                "Lorg/apache/log4j/Level;")
            jutil.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V",
                       warn_level)
            del logger
            del warn_level
        except:
            logger.error("Failed to initialize log4j\n", exc_info=True)
    if not get_headless():
        jutil.activate_awt()
Example #5
0
def start_cellprofiler_jvm():
    '''Start the Java VM with arguments appropriate for CellProfiler'''
    global logger
    
    if hasattr(sys, 'frozen'):
        if sys.platform != 'darwin':
            root_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
            bioformats_path = os.path.join(root_path, 'bioformats')
        else:
            bioformats_path = os.path.abspath(os.path.split(__file__)[0])
            root_path = os.path.split(bioformats_path)[0]
        imagej_path = os.path.join(root_path, 'imagej','jars')
        def sort_fn(a, b):
            aa,bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                     for x in a, b]
            return cmp(aa, bb)
        jar_files = [
            jar_filename
            for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")]
        jar_files = sorted(jar_files, cmp = sort_fn)
    else:
        bioformats_path = os.path.abspath(os.path.split(__file__)[0])
        root_path = os.path.split(bioformats_path)[0]
        jar_files = get_cellprofiler_jars()
        imagej_path = os.path.join(root_path, 'imagej','jars')
    
    class_path = os.pathsep.join(
        [os.path.join(imagej_path, jar_file) for jar_file in jar_files])
    
    if os.environ.has_key("CLASSPATH"):
        class_path += os.pathsep + os.environ["CLASSPATH"]
        
    plugin_directory = get_ij_plugin_directory()
    logger.debug("Using %s as imagej plugin directory" % plugin_directory)
    if (plugin_directory is not None and 
        os.path.isdir(plugin_directory)):
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path += os.pathsep + plugin_directory
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path += os.pathsep + jarpath
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: " + plugin_directory)
        
    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from cellprofiler.utilities.setup import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib","tools.jar")
            class_path += os.pathsep + tools_jar
        else:
            logger.warning("Failed to find tools.jar")
    
    jvm_arg = jutil.get_jvm_heap_size_arg()
    if jvm_arg is None:
        jvm_arg = "512m"
        
    args = [r"-Djava.class.path="+class_path,
            r"-Dloci.bioformats.loaded=true",
            #r"-verbose:class",
            #r"-verbose:jni",
            r"-Xmx%s" % jvm_arg]
    #
    # Get the log4j logger setup from a file in the bioformats directory
    # if such a file exists.
    #
    log4j_properties = os.path.join(bioformats_path, "log4j.properties")
    if os.path.exists(log4j_properties):
        log4j_properties = "file:/"+log4j_properties.replace(os.path.sep, "/")
        args += [r"-Dlog4j.configuration="+log4j_properties]
        init_logger = False
    else:
        init_logger = True
        
    if plugin_directory is not None and os.path.isdir(plugin_directory):
        # For IJ1 compatibility
        args += [r"-Dplugins.dir=%s" % plugin_directory]
    
    # In headless mode, we have to avoid changing the Java preferences.
    # 
    # Aside from that, we need to prevent ImageJ from exiting and from
    # displaying the updater dialog - at least temporarily, we do that
    # through preferences. We use the HeadlessPreferencesFactory to
    # limit the scope of the changes to this process - otherwise we'd
    # turn off updating for the machine.
    #
    # TODO: ImageJ is implementing a pluggable mechanism to control the
    #       quit process. We can also contribute a pluggable mechanism
    #       that gives us more control over the updater.
    #
    args += [
        "-Djava.util.prefs.PreferencesFactory="
        "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]
    run_headless = (get_headless() and 
                    not os.environ.has_key("CELLPROFILER_USE_XVFB"))
    run_headless = False
        
    logger.debug("JVM arguments: " + " ".join(args))
    jutil.start_vm(args, run_headless)
    logger.debug("Java virtual machine started.")
    jutil.attach()
    try:
        jutil.static_call("loci/common/Location",
                          "cacheDirectoryListings",
                          "(Z)V", True)
    except:
        logger.warning("Bioformats version does not support directory cacheing")
    
    #
    # Start the log4j logger to avoid error messages.
    #
    if init_logger:
        try:
            jutil.static_call("org/apache/log4j/BasicConfigurator",
                              "configure", "()V")
            log4j_logger = jutil.static_call("org/apache/log4j/Logger",
                                             "getRootLogger",
                                             "()Lorg/apache/log4j/Logger;")
            warn_level = jutil.get_static_field("org/apache/log4j/Level","WARN",
                                                "Lorg/apache/log4j/Level;")
            jutil.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V", 
                       warn_level)
            del logger
            del warn_level
        except:
            logger.error("Failed to initialize log4j\n", exc_info=True)
    if not get_headless():
        jutil.activate_awt()
Example #6
0
def cp_start_vm():
    '''Start CellProfiler's JVM via Javabridge
    
    JVM parameters are harvested from preferences and
    the environment variables:
    
    CP_JDWP_PORT - port # for debugging Java within the JVM
    cpprefs.get_awt_headless() - controls java.awt.headless to prevent
        awt from being invoked
    '''
    
    args = ["-Dloci.bioformats.loaded=true",
            "-Dlogback.configurationFile=logback.xml",
            "-Djava.util.prefs.PreferencesFactory="+
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]

    imagej_path = get_path_to_jars()
    if hasattr(sys, 'frozen'):
        jar_files = [
            jar_filename
            for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")]
        def sort_fn(a, b):
            aa,bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                     for x in a, b]
            return cmp(aa, bb)
        jar_files = sorted(jar_files, cmp = sort_fn)
    else:
        jar_files = get_cellprofiler_jars()
    jar_files = [os.path.join(imagej_path, f)  for f in jar_files]
    class_path = javabridge.JARS + jar_files
    
    if os.environ.has_key("CLASSPATH"):
        class_path += os.environ["CLASSPATH"].split(os.pathsep)
        logging.debug(
            "Adding Java class path from environment variable, ""CLASSPATH""")
        logging.debug("    CLASSPATH="+os.environ["CLASSPATH"])
        
    plugin_directory = cpprefs.get_ij_plugin_directory()
    if (plugin_directory is not None and 
        os.path.isdir(plugin_directory)):
        logger.debug("Using %s as imagej plugin directory" % plugin_directory)
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path.append(plugin_directory)
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path.append(jarpath)
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: "
                    + plugin_directory)
        
    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from javabridge.locate import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib","tools.jar")
            class_path.append(tools_jar)
        else:
            logger.warning("Failed to find tools.jar")

    args += get_patcher_args(class_path)
    awt_headless = cpprefs.get_awt_headless()
    if awt_headless:
        logger.debug("JVM will be started with AWT in headless mode")
        args.append("-Djava.awt.headless=true")
        
    heap_size = str(cpprefs.get_jvm_heap_mb())+"m"
    if os.environ.has_key("CP_JDWP_PORT"):
        args.append(
            ("-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:%s"
             ",server=y,suspend=n") % os.environ["CP_JDWP_PORT"])

    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size = heap_size)
    #
    # Enable Bio-Formats directory cacheing
    #
    try:
        c_location = javabridge.JClassWrapper("loci.common.Location")
        c_location.cacheDirectoryListings(True)
        logger.debug("Enabled Bio-formats directory cacheing")
    except:
        logger.warning("Bioformats version does not support directory cacheing")
    #
    # Monkey-patch bioformats.formatreader.get_class_list to add
    # the classes we added to loci.formats.in
    #
    import bioformats.formatreader
    
    old_get_class_list = bioformats.formatreader.get_class_list
    
    def get_class_list():
        "Return a wrapped instance of loci.formats.ClassList"
        
        env = javabridge.get_env()
        class_list = old_get_class_list()
        rais_classname = 'loci/common/RandomAccessInputStream'
        #
        # Move any class to the back that thinks it can read garbage
        #
        fd, path = tempfile.mkstemp(suffix=".garbage")
        stream = None
        try:
            os.write(fd, "This is not an image file")
            os.fsync(fd)
            stream = javabridge.make_instance(
                rais_classname, '(Ljava/lang/String;)V', path)
            problem_classes = []
            for klass in env.get_object_array_elements(class_list.get_classes()):
                try:
                    instance =  javabridge.call(
                        klass, "newInstance", "()Ljava/lang/Object;")
                    can_read_garbage = javabridge.call(
                        instance, "isThisType",
                        "(L%s;)Z" % rais_classname, stream)
                    if can_read_garbage:
                        problem_classes.append(klass)
                        class_list.remove_class(klass)
                except:
                    logger.info("Caught exception in %s.isThisType",
                                javabridge.to_string(klass))
        finally:
            os.close(fd)
            javabridge.call(stream, "close", "()V")
            os.remove(path)
                    
        for classname in ("loci.formats.in.FlowSightReader", 
                          "loci.formats.in.IM3Reader"):
            klass = javabridge.class_for_name(classname)
            class_list.add_class(klass)
        for klass in problem_classes:
            class_list.add_class(klass)
        return class_list
    bioformats.formatreader.get_class_list = get_class_list
Example #7
0
def start_cellprofiler_jvm():
    '''Start the Java VM with arguments appropriate for CellProfiler'''
    global logger

    if hasattr(sys, 'frozen'):
        if sys.platform != 'darwin':
            root_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
            bioformats_path = os.path.join(root_path, 'bioformats')
        else:
            bioformats_path = os.path.abspath(os.path.split(__file__)[0])
            root_path = os.path.split(bioformats_path)[0]
        imagej_path = os.path.join(root_path, 'imagej', 'jars')

        def sort_fn(a, b):
            aa, bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                      for x in a, b]
            return cmp(aa, bb)

        jar_files = [
            jar_filename for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")
        ]
        jar_files = sorted(jar_files, cmp=sort_fn)
    else:
        bioformats_path = os.path.abspath(os.path.split(__file__)[0])
        root_path = os.path.split(bioformats_path)[0]
        jar_files = get_cellprofiler_jars()
        imagej_path = os.path.join(root_path, 'imagej', 'jars')

    class_path = os.pathsep.join(
        [os.path.join(imagej_path, jar_file) for jar_file in jar_files])

    if os.environ.has_key("CLASSPATH"):
        class_path += os.pathsep + os.environ["CLASSPATH"]

    plugin_directory = get_ij_plugin_directory()
    logger.debug("Using %s as imagej plugin directory" % plugin_directory)
    if (plugin_directory is not None and os.path.isdir(plugin_directory)):
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path += os.pathsep + plugin_directory
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path += os.pathsep + jarpath
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: " +
                    plugin_directory)

    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from cellprofiler.utilities.setup import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib", "tools.jar")
            class_path += os.pathsep + tools_jar
        else:
            logger.warning("Failed to find tools.jar")

    jvm_arg = jutil.get_jvm_heap_size_arg()
    if jvm_arg is None:
        jvm_arg = "512m"

    args = [
        r"-Djava.class.path=" + class_path,
        r"-Dloci.bioformats.loaded=true",
        #r"-verbose:class",
        #r"-verbose:jni",
        r"-Xmx%s" % jvm_arg
    ]
    #
    # Get the log4j logger setup from a file in the bioformats directory
    # if such a file exists.
    #
    log4j_properties = os.path.join(bioformats_path, "log4j.properties")
    if os.path.exists(log4j_properties):
        log4j_properties = "file:/" + log4j_properties.replace(
            os.path.sep, "/")
        args += [r"-Dlog4j.configuration=" + log4j_properties]
        init_logger = False
    else:
        init_logger = True

    if plugin_directory is not None and os.path.isdir(plugin_directory):
        # For IJ1 compatibility
        args += [r"-Dplugins.dir=%s" % plugin_directory]

    # In headless mode, we have to avoid changing the Java preferences.
    #
    # Aside from that, we need to prevent ImageJ from exiting and from
    # displaying the updater dialog - at least temporarily, we do that
    # through preferences. We use the HeadlessPreferencesFactory to
    # limit the scope of the changes to this process - otherwise we'd
    # turn off updating for the machine.
    #
    # TODO: ImageJ is implementing a pluggable mechanism to control the
    #       quit process. We can also contribute a pluggable mechanism
    #       that gives us more control over the updater.
    #
    args += [
        "-Djava.util.prefs.PreferencesFactory="
        "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"
    ]
    run_headless = (get_headless()
                    and not os.environ.has_key("CELLPROFILER_USE_XVFB"))
    run_headless = False

    logger.debug("JVM arguments: " + " ".join(args))
    jutil.start_vm(args, run_headless)
    logger.debug("Java virtual machine started.")
    jutil.attach()
    try:
        jutil.static_call("loci/common/Location", "cacheDirectoryListings",
                          "(Z)V", True)
    except:
        logger.warning(
            "Bioformats version does not support directory cacheing")

    #
    # Start the log4j logger to avoid error messages.
    #
    if init_logger:
        try:
            jutil.static_call("org/apache/log4j/BasicConfigurator",
                              "configure", "()V")
            log4j_logger = jutil.static_call("org/apache/log4j/Logger",
                                             "getRootLogger",
                                             "()Lorg/apache/log4j/Logger;")
            warn_level = jutil.get_static_field("org/apache/log4j/Level",
                                                "WARN",
                                                "Lorg/apache/log4j/Level;")
            jutil.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V",
                       warn_level)
            del logger
            del warn_level
        except:
            logger.error("Failed to initialize log4j\n", exc_info=True)
    if not get_headless():
        jutil.activate_awt()
Example #8
0
    import cellprofiler.utilities.version
    f = open("cellprofiler/frozen_version.py", "w")
    f.write("# MACHINE_GENERATED\nversion_string = '%s'" %
            cellprofiler.utilities.version.version_string)
    f.close()

APPNAME = 'CellProfiler'
APP = ['CellProfiler.py']
icon_src_path = os.path.join('.', 'cellprofiler', 'icons')
DATA_FILES = [('cellprofiler/icons',
               glob.glob(os.path.join(icon_src_path, '*.png')) +
               [os.path.join(icon_src_path, "icon_copyrights.txt")])]
from javabridge import JARS
imagej_path = os.path.abspath(os.path.join(".", "imagej", "jars"))
jars = JARS + [
    os.path.join(imagej_path, jar) for jar in get_cellprofiler_jars()
]
DATA_FILES.append(('imagej/jars', jars))
OPTIONS = {
    'argv_emulation':
    True,
    'packages': ['cellprofiler', 'contrib', 'imagej', 'javabridge'],
    'includes': [
        'numpy', 'scipy', 'sklearn', 'sklearn.utils.sparsetools.*', 'wx',
        'matplotlib', 'email.iterators', 'smtplib', 'zmq', 'javabridge',
        'bioformats', 'sqlite3', 'libtiff', 'wx.lib.intctrl',
        'libtiff.' + tiff_h_name, 'xml.dom.minidom', 'h5py', 'h5py.defs',
        'h5py.utils', 'h5py._proxy', 'readline'
    ],
    'excludes': ['pylab', 'Tkinter', 'Cython', 'scipy.weave', 'virtualenv'],
    'resources': ['CellProfilerIcon.png'],
Example #9
0
def cp_start_vm():
    '''Start CellProfiler's JVM via Javabridge
    
    JVM parameters are harvested from preferences and
    the environment variables:
    
    CP_JDWP_PORT - port # for debugging Java within the JVM
    cpprefs.get_awt_headless() - controls java.awt.headless to prevent
        awt from being invoked
    '''

    args = [
        "-Dloci.bioformats.loaded=true",
        "-Dlogback.configurationFile=logback.xml",
        "-Djava.util.prefs.PreferencesFactory=" +
        "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"
    ]

    imagej_path = get_path_to_jars()
    if hasattr(sys, 'frozen'):
        jar_files = [
            jar_filename for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")
        ]

        def sort_fn(a, b):
            aa, bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                      for x in a, b]
            return cmp(aa, bb)

        jar_files = sorted(jar_files, cmp=sort_fn)
    else:
        jar_files = get_cellprofiler_jars()
    jar_files = [os.path.join(imagej_path, f) for f in jar_files]
    class_path = javabridge.JARS + jar_files

    if os.environ.has_key("CLASSPATH"):
        class_path += os.environ["CLASSPATH"].split(os.pathsep)
        logging.debug("Adding Java class path from environment variable, "
                      "CLASSPATH"
                      "")
        logging.debug("    CLASSPATH=" + os.environ["CLASSPATH"])

    plugin_directory = cpprefs.get_ij_plugin_directory()
    if (plugin_directory is not None and os.path.isdir(plugin_directory)):
        logger.debug("Using %s as imagej plugin directory" % plugin_directory)
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path.append(plugin_directory)
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path.append(jarpath)
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: " +
                    plugin_directory)

    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from javabridge.locate import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib", "tools.jar")
            class_path.append(tools_jar)
        else:
            logger.warning("Failed to find tools.jar")

    args += get_patcher_args(class_path)
    awt_headless = cpprefs.get_awt_headless()
    if awt_headless:
        logger.debug("JVM will be started with AWT in headless mode")
        args.append("-Djava.awt.headless=true")

    heap_size = str(cpprefs.get_jvm_heap_mb()) + "m"
    if os.environ.has_key("CP_JDWP_PORT"):
        args.append(("-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:%s"
                     ",server=y,suspend=n") % os.environ["CP_JDWP_PORT"])

    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size=heap_size)
    #
    # Enable Bio-Formats directory cacheing
    #
    try:
        c_location = javabridge.JClassWrapper("loci.common.Location")
        c_location.cacheDirectoryListings(True)
        logger.debug("Enabled Bio-formats directory cacheing")
    except:
        logger.warning(
            "Bioformats version does not support directory cacheing")
Example #10
0
if sys.platform == "darwin":
    import cellprofiler.utilities.version
    f = open("cellprofiler/frozen_version.py", "w")
    f.write("# MACHINE_GENERATED\nversion_string = '%s'" % cellprofiler.utilities.version.version_string)
    f.close()

APPNAME = 'CellProfiler'
APP = ['CellProfiler.py']
icon_src_path = os.path.join('.', 'cellprofiler', 'icons')
DATA_FILES = [('cellprofiler/icons', 
               glob.glob(os.path.join(icon_src_path, '*.png'))+
               [os.path.join(icon_src_path, "icon_copyrights.txt")])]
from javabridge import JARS
imagej_path = os.path.abspath(os.path.join(".", "imagej", "jars"))
jars = JARS + [os.path.join(imagej_path, jar) for jar in get_cellprofiler_jars()]
jars.append(os.path.join(
    imagej_path, "cellprofiler-java-dependencies-classpath.txt"))
DATA_FILES.append(('imagej/jars', jars))
OPTIONS = {'argv_emulation': True,
           'packages': ['cellprofiler', 'contrib', 'imagej', 'javabridge'],
           'includes': ['numpy', 'scipy', 'sklearn', 'sklearn.utils.sparsetools.*',
                        'wx', 'matplotlib','email.iterators', 'smtplib', 'zmq',
                        'javabridge', 'bioformats', 
                        'sqlite3', 'libtiff', 'wx.lib.intctrl', 'libtiff.'+tiff_h_name,
                        'xml.dom.minidom', 'h5py', 'h5py.defs', 'h5py.utils', 'h5py._proxy', 'readline'],
           'excludes': ['pylab', 'Tkinter', 'Cython', 'scipy.weave',
                        'virtualenv'],
           'resources': ['CellProfilerIcon.png'],
           'iconfile' : 'CellProfilerIcon.icns',
           'frameworks' : [tiff_dylib, jpeg_dylib],
Example #11
0
def cp_start_vm():
    '''Start CellProfiler's JVM via Javabridge
    
    JVM parameters are harvested from preferences and
    the environment variables:
    
    CP_JDWP_PORT - port # for debugging Java within the JVM
    cpprefs.get_awt_headless() - controls java.awt.headless to prevent
        awt from being invoked
    '''
    
    args = ["-Dloci.bioformats.loaded=true",
            "-Dlogback.configurationFile=logback.xml",
            "-Djava.util.prefs.PreferencesFactory="+
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]

    imagej_path = get_path_to_jars()
    if hasattr(sys, 'frozen'):
        jar_files = [
            jar_filename
            for jar_filename in os.listdir(imagej_path)
            if jar_filename.lower().endswith(".jar")]
        def sort_fn(a, b):
            aa,bb = [(0 if x.startswith("cellprofiler-java") else 1, x)
                     for x in a, b]
            return cmp(aa, bb)
        jar_files = sorted(jar_files, cmp = sort_fn)
    else:
        jar_files = get_cellprofiler_jars()
    jar_files = [os.path.join(imagej_path, f)  for f in jar_files]
    class_path = javabridge.JARS + jar_files
    
    if os.environ.has_key("CLASSPATH"):
        class_path += os.environ["CLASSPATH"].split(os.pathsep)
        logging.debug(
            "Adding Java class path from environment variable, ""CLASSPATH""")
        logging.debug("    CLASSPATH="+os.environ["CLASSPATH"])
        
    plugin_directory = cpprefs.get_ij_plugin_directory()
    if (plugin_directory is not None and 
        os.path.isdir(plugin_directory)):
        logger.debug("Using %s as imagej plugin directory" % plugin_directory)
        #
        # Add the plugin directory to pick up .class files in a directory
        # hierarchy.
        #
        class_path.append(plugin_directory)
        logger.debug("Adding %s to class path" % plugin_directory)
        #
        # Add any .jar files in the directory
        #
        for jarfile in os.listdir(plugin_directory):
            jarpath = os.path.join(plugin_directory, jarfile)
            if jarfile.lower().endswith(".jar"):
                logger.debug("Adding %s to class path" % jarpath)
                class_path.append(jarpath)
            else:
                logger.debug("Skipping %s" % jarpath)
    else:
        logger.info("Plugin directory doesn't point to valid folder: "
                    + plugin_directory)
        
    if sys.platform.startswith("win") and not hasattr(sys, 'frozen'):
        # Have to find tools.jar
        from javabridge.locate import find_jdk
        jdk_path = find_jdk()
        if jdk_path is not None:
            tools_jar = os.path.join(jdk_path, "lib","tools.jar")
            class_path.append(tools_jar)
        else:
            logger.warning("Failed to find tools.jar")

    args += get_patcher_args(class_path)
    awt_headless = cpprefs.get_awt_headless()
    if awt_headless:
        logger.debug("JVM will be started with AWT in headless mode")
        args.append("-Djava.awt.headless=true")
        
    heap_size = str(cpprefs.get_jvm_heap_mb())+"m"
    if os.environ.has_key("CP_JDWP_PORT"):
        args.append(
            ("-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:%s"
             ",server=y,suspend=n") % os.environ["CP_JDWP_PORT"])

    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size = heap_size)
    #
    # Enable Bio-Formats directory cacheing
    #
    try:
        c_location = javabridge.JClassWrapper("loci.common.Location")
        c_location.cacheDirectoryListings(True)
        logger.debug("Enabled Bio-formats directory cacheing")
    except:
        logger.warning("Bioformats version does not support directory cacheing")