Esempio n. 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
Esempio n. 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
Esempio n. 3
0
 def start_ij(self):
     try:
         self.client_socket.close()
         self.server_socket.close()
     except: pass
     self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.server_socket.bind(("", 0))
     _, self.port = self.server_socket.getsockname()
     self.server_socket.listen(5)
     self.server_socket.listen(5)
     print "ImageJ bridge TCPServer waiting for client on port", self.port
     from cellprofiler.preferences import get_ij_plugin_directory
     plugin_dir =  get_ij_plugin_directory()
     if plugin_dir is not None:
         command = 'java -Xmx512m -Dplugins.dir=%s TCPClient %s' % (
             plugin_dir, self.port)
     else:
         command = 'java -Xmx512m TCPClient %s' % self.port
     self.ijproc = Popen(shlex.split(command),
                         stdin=None, stdout=None, stderr=None)
     self.client_socket, address = self.server_socket.accept()
     print "ImageJ bridge got a connection from", address      
Esempio n. 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"]
        
    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()
Esempio n. 5
0
    def start_workers(cls, num=None):
        if cls.workers:
            return

        try:
            num = multiprocessing.cpu_count() if num is None else num
        except NotImplementedError:
            num = 4

        cls.work_announce_address = get_announcer_address()
        logger.info("Starting workers on address %s" % cls.work_announce_address)
        if 'CP_DEBUG_WORKER' in os.environ:
            if os.environ['CP_DEBUG_WORKER'] == 'NOT_INPROC':
                return
            from cellprofiler.analysis_worker import \
                 AnalysisWorker, NOTIFY_ADDR, NOTIFY_STOP, CancelledException
            
            class WorkerRunner(threading.Thread):
                def __init__(self, work_announce_address):
                    threading.Thread.__init__(self)
                    self.work_announce_address = work_announce_address
                    self.notify_socket = zmq.Context.instance().socket(zmq.PUB)
                    self.notify_socket.bind(NOTIFY_ADDR)
                
                def run(self):
                    with AnalysisWorker(self.work_announce_address) as aw:
                        try:
                            aw.run()
                        except CancelledException:
                            logger.info("Exiting debug worker thread")
                
                def wait(self):
                    self.notify_socket.send(NOTIFY_STOP)
                    self.join()
                
            thread = WorkerRunner(cls.work_announce_address)
            thread.setDaemon(True)
            thread.start()
            cls.workers.append(thread)
            return
        
        close_fds = False
        # start workers
        for idx in range(num):
            if sys.platform == 'darwin':
                close_all_on_exec()
            
            # stdin for the subprocesses serves as a deadman's switch.  When
            # closed, the subprocess exits.
            if hasattr(sys, 'frozen'):
                if sys.platform == 'darwin':
                    # sys.argv[0] points at 
                    # CellProfiler.app/Contents/Resources/CellProfiler.py
                    # We want
                    # CellProfiler.app/Contents/MacOS/CellProfiler
                    #
                    contents_resources_dir = os.path.split(sys.argv[0])[0]
                    contents_dir = os.path.split(contents_resources_dir)[0]
                    cp_executable = os.path.join(contents_dir, "MacOS", "CellProfiler")
                    assert os.path.isfile(cp_executable), \
                           "Did not find CellProfiler in its expected place: %s" % cp_executable
                    assert os.access(cp_executable, os.EX_OK), \
                           "%s is not executable" % cp_executable
                    args = ["arch", "-x86_64", "-i386", cp_executable, 
                            "--work-announce", cls.work_announce_address,
                            "--plugins-directory", cpprefs.get_plugin_directory(),
                            "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()]
                else:
                    aw_path = os.path.join(
                        os.path.split(
                            os.path.abspath(sys.argv[0]))[0],
                                           "analysis_worker")
                    args = [aw_path,
                            '--work-announce',
                            cls.work_announce_address,
                            "--plugins-directory", cpprefs.get_plugin_directory(),
                            "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()]
                    
                worker = subprocess.Popen(args,
                                          env=find_worker_env(),
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          close_fds = close_fds)
            else:
                worker = subprocess.Popen(
                    [find_python(),
                     '-u',  # unbuffered
                     find_analysis_worker_source(),
                     '--work-announce',
                     cls.work_announce_address,
                     "--plugins-directory", cpprefs.get_plugin_directory(),
                     "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()],
                    env=find_worker_env(),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    close_fds = close_fds)

            def run_logger(workR, widx):
                while(True):
                    try:
                        line = workR.stdout.readline()
                        if not line:
                            break
                        logger.info("Worker %d: %s", widx, line.rstrip())
                    except:
                        break
            start_daemon_thread(target=run_logger, args=(worker, idx,), name='worker stdout logger')

            cls.workers += [worker]
            cls.deadman_switches += [worker.stdin]  # closing stdin will kill subprocess
Esempio n. 6
0
    def start_workers(cls, num=None):
        if cls.workers:
            return

        try:
            num = multiprocessing.cpu_count() if num is None else num
        except NotImplementedError:
            num = 4

        cls.work_announce_address = get_announcer_address()
        logger.info("Starting workers on address %s" % cls.work_announce_address)
        if 'CP_DEBUG_WORKER' in os.environ:
            if os.environ['CP_DEBUG_WORKER'] == 'NOT_INPROC':
                return
            from cellprofiler.analysis_worker import \
                 AnalysisWorker, NOTIFY_ADDR, NOTIFY_STOP, CancelledException
            
            class WorkerRunner(threading.Thread):
                def __init__(self, work_announce_address):
                    threading.Thread.__init__(self)
                    self.work_announce_address = work_announce_address
                    self.notify_socket = zmq.Context.instance().socket(zmq.PUB)
                    self.notify_socket.bind(NOTIFY_ADDR)
                
                def run(self):
                    with AnalysisWorker(self.work_announce_address) as aw:
                        try:
                            aw.run()
                        except CancelledException:
                            logger.info("Exiting debug worker thread")
                
                def wait(self):
                    self.notify_socket.send(NOTIFY_STOP)
                    self.join()
                
            thread = WorkerRunner(cls.work_announce_address)
            thread.setDaemon(True)
            thread.start()
            cls.workers.append(thread)
            return
        
        close_fds = False
        # start workers
        for idx in range(num):
            if sys.platform == 'darwin':
                close_all_on_exec()
            
            # stdin for the subprocesses serves as a deadman's switch.  When
            # closed, the subprocess exits.
            if hasattr(sys, 'frozen'):
                if sys.platform == 'darwin':
                    # sys.argv[0] points at 
                    # CellProfiler.app/Contents/Resources/CellProfiler.py
                    # We want
                    # CellProfiler.app/Contents/MacOS/CellProfiler
                    #
                    contents_resources_dir = os.path.split(sys.argv[0])[0]
                    contents_dir = os.path.split(contents_resources_dir)[0]
                    cp_executable = os.path.join(contents_dir, "MacOS", "CellProfiler")
                    assert os.path.isfile(cp_executable), \
                           "Did not find CellProfiler in its expected place: %s" % cp_executable
                    assert os.access(cp_executable, os.EX_OK), \
                           "%s is not executable" % cp_executable
                    args = ["arch", "-x86_64", cp_executable, 
                            "--work-announce", cls.work_announce_address,
                            "--plugins-directory", cpprefs.get_plugin_directory(),
                            "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()]
                else:
                    aw_path = os.path.join(
                        os.path.split(
                            os.path.abspath(sys.argv[0]))[0],
                                           "analysis_worker")
                    args = [aw_path,
                            '--work-announce',
                            cls.work_announce_address,
                            "--plugins-directory", cpprefs.get_plugin_directory(),
                            "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()]
                    
                worker = subprocess.Popen(args,
                                          env=find_worker_env(),
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          close_fds = close_fds)
            else:
                worker = subprocess.Popen(
                    [find_python(),
                     '-u',  # unbuffered
                     find_analysis_worker_source(),
                     '--work-announce',
                     cls.work_announce_address,
                     "--plugins-directory", cpprefs.get_plugin_directory(),
                     "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()],
                    env=find_worker_env(),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    close_fds = close_fds)

            def run_logger(workR, widx):
                while(True):
                    try:
                        line = workR.stdout.readline()
                        if not line:
                            break
                        logger.info("Worker %d: %s", widx, line.rstrip())
                    except:
                        break
            start_daemon_thread(target=run_logger, args=(worker, idx,), name='worker stdout logger')

            cls.workers += [worker]
            cls.deadman_switches += [worker.stdin]  # closing stdin will kill subprocess
Esempio n. 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"]

    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()
Esempio n. 8
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()
Esempio n. 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")
    #
    # 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
Esempio n. 10
0
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"-Djava.ext.dirs=%s"%__path,
          r"-Dloci.bioformats.loaded=true",
          #r"-verbose:class",
          #r"-verbose:jni",
          r"-Xmx%s" % jvm_arg]
if get_ij_plugin_directory() is not None:
    __args.append("-Dplugins.dir="+get_ij_plugin_directory())

#
# Get the log4j logger setup from a file in the bioformats directory
# if such a file exists.
#
__log4j_properties = os.path.join(__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() and not os.environ.has_key("CELLPROFILER_USE_XVFB"))
Esempio n. 11
0
    def start_workers(cls, num=None):
        if cls.workers:
            return

        try:
            num = multiprocessing.cpu_count() if num is None else num
        except NotImplementedError:
            num = 4

        cls.work_announce_address = get_announcer_address()
        logger.info("Starting workers on address %s" % cls.work_announce_address)
        if 'CP_DEBUG_WORKER' in os.environ:
            if os.environ['CP_DEBUG_WORKER'] == 'NOT_INPROC':
                return
            from cellprofiler.worker import \
                AnalysisWorker, NOTIFY_ADDR, NOTIFY_STOP
            from cellprofiler.pipeline import CancelledException

            class WorkerRunner(threading.Thread):
                def __init__(self, work_announce_address):
                    threading.Thread.__init__(self)
                    self.work_announce_address = work_announce_address
                    self.notify_socket = zmq.Context.instance().socket(zmq.PUB)
                    self.notify_socket.bind(NOTIFY_ADDR)

                def run(self):
                    with AnalysisWorker(self.work_announce_address) as aw:
                        try:
                            aw.run()
                        except CancelledException:
                            logger.info("Exiting debug worker thread")

                def wait(self):
                    self.notify_socket.send(NOTIFY_STOP)
                    self.join()

            thread = WorkerRunner(cls.work_announce_address)
            thread.setDaemon(True)
            thread.start()
            cls.workers.append(thread)
            return

        close_fds = False
        # start workers
        for idx in range(num):
            if sys.platform == 'darwin':
                close_all_on_exec()

            aw_args = ["--work-announce", cls.work_announce_address,
                       "--plugins-directory", cpprefs.get_plugin_directory(),
                       "--ij-plugins-directory", cpprefs.get_ij_plugin_directory()]
            jvm_arg = "%dm" % cpprefs.get_jvm_heap_mb()
            aw_args.append("--jvm-heap-size=%s" % jvm_arg)
            # stdin for the subprocesses serves as a deadman's switch.  When
            # closed, the subprocess exits.
            if hasattr(sys, 'frozen'):
                if sys.platform == 'darwin':
                    executable = os.path.join(
                            os.path.dirname(sys.executable), "CellProfiler")
                    args = ([executable] + aw_args)
                elif sys.platform.startswith('linux'):
                    aw_path = os.path.join(os.path.dirname(__file__),
                                           "worker.py")
                    args = [sys.executable, aw_path] + aw_args
                else:
                    args = [sys.executable] + aw_args

                worker = subprocess.Popen(args,
                                          env=find_worker_env(idx),
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          close_fds=close_fds)
            else:
                worker = subprocess.Popen(
                        [find_python(),
                         '-u',  # unbuffered
                         find_analysis_worker_source()] + aw_args,
                        env=find_worker_env(idx),
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT,
                        close_fds=close_fds)

            def run_logger(workR, widx):
                while True:
                    try:
                        line = workR.stdout.readline()
                        if not line:
                            break
                        logger.info("Worker %d: %s", widx, line.rstrip())
                    except:
                        break

            start_daemon_thread(target=run_logger, args=(worker, idx,), name='worker stdout logger')

            cls.workers += [worker]
            cls.deadman_switches += [worker.stdin]  # closing stdin will kill subprocess
Esempio n. 12
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()
Esempio n. 13
0
def start_cellprofiler_jvm():
    '''Start the Java VM with arguments appropriate for CellProfiler'''
    global USE_IJ2
    global logger
    
    if hasattr(sys, 'frozen') and sys.platform != 'darwin':
        root_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    else:
        root_path = os.path.abspath(os.path.split(__file__)[0])
        root_path = os.path.split(root_path)[0]
    path = os.path.join(root_path, 'bioformats')
    imagej_path = os.path.join(root_path, 'imagej')
    loci_jar = os.path.join(path, "loci_tools.jar")
    ij2_jar = os.path.join(imagej_path, "imagej-2.0-SNAPSHOT-all.jar")
    ij_jar = os.path.join(imagej_path, "ij.jar")
    imglib_jar = os.path.join(imagej_path, "imglib.jar")
    javacl_jar = os.path.join(imagej_path, "javacl-1.0-beta-4-shaded.jar")
    USE_IJ2 = get_ij_version() == IJ_2
    if os.path.exists(ij2_jar) and USE_IJ2:
        class_path = os.pathsep.join((loci_jar, ij2_jar))
        USE_IJ2 = True
    else:
        USE_IJ2 = False
        class_path = os.pathsep.join((loci_jar, ij_jar, imglib_jar, 
                                      javacl_jar))
    if os.environ.has_key("CLASSPATH"):
        class_path += os.pathsep + os.environ["CLASSPATH"]
        
    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]
    if get_ij_plugin_directory() is not None:
        args.append("-Dplugins.dir="+get_ij_plugin_directory())
    
    #
    # Get the log4j logger setup from a file in the bioformats directory
    # if such a file exists.
    #
    log4j_properties = os.path.join(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
        
    run_headless = (get_headless() and 
                    not os.environ.has_key("CELLPROFILER_USE_XVFB"))
        
    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 run_headless:
        jutil.activate_awt()
Esempio n. 14
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")
Esempio n. 15
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")
        
Esempio n. 16
0
    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"-Djava.ext.dirs=%s"%__path,
    r"-Dloci.bioformats.loaded=true",
    #r"-verbose:class",
    #r"-verbose:jni",
    r"-Xmx%s" % jvm_arg
]
if get_ij_plugin_directory() is not None:
    __args.append("-Dplugins.dir=" + get_ij_plugin_directory())

#
# Get the log4j logger setup from a file in the bioformats directory
# if such a file exists.
#
__log4j_properties = os.path.join(__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