示例#1
0
    def testUserdirEnvironmentDefault(self, monkeypatch):
        monkeypatch.delenv('OMERO_USERDIR', raising=False)

        assert get_omero_userdir().basename() == 'omero'

        c = get_omero_user_cache_dir()
        assert c.basename() == omero_version.split('.')[0]
        assert 'OMERO.py' in str(c)
示例#2
0
    def testUserdirEnvironmentAppdir(self, monkeypatch):
        monkeypatch.setenv('OMERO_USERDIR', '')

        d = get_omero_userdir()
        assert d.basename() == omero_version.split('.')[0]
        assert 'OMERO.py' in str(d)

        c = get_omero_user_cache_dir()
        assert c.basename() == omero_version.split('.')[0]
        assert 'OMERO.py' in str(c)
示例#3
0
 def __init__(self, dir=None):
     """
     """
     self.logger = logging.getLogger(make_logname(self))
     if dir is None:
         self.dir = get_omero_userdir() / "sessions"
     else:
         self.dir = path(dir)
     if not self.dir.exists():
         self.dir.makedirs()
     try:
         self.dir.chmod(0700)
     except:
         print "WARN: failed to chmod %s" % self.dir
示例#4
0
 def __init__(self, dir=None):
     """
     """
     self.logger = logging.getLogger(make_logname(self))
     if dir is None:
         self.dir = old_div(get_omero_userdir(), "sessions")
     else:
         self.dir = path(dir)
     if not self.dir.exists():
         self.dir.makedirs()
     try:
         self.dir.chmod(0o700)
     except:
         print("WARN: failed to chmod %s" % self.dir)
示例#5
0
 def open_config(self, args):
     if args.source:
         cfg_xml = path(args.source)
         if not cfg_xml.exists():
             self.ctx.die(124, "File not found: %s" % args.source)
     else:
         grid_dir = self.ctx.dir / "etc" / "grid"
         if grid_dir.exists():
             cfg_xml = grid_dir / "config.xml"
         else:
             usr_xml = get_omero_userdir() / "config.xml"
             self.ctx.err("%s not found; using %s" % (grid_dir, usr_xml))
             cfg_xml = usr_xml
     try:
         return ConfigXml(str(cfg_xml))
     except portalocker.LockException:
         self.ctx.die(112, "Could not acquire lock on %s" % cfg_xml)
     except Exception, e:
         self.ctx.die(113, str(e))
示例#6
0
 def open_config(self, args):
     if args.source:
         cfg_xml = path(args.source)
         if not cfg_xml.exists():
             self.ctx.die(124, "File not found: %s" % args.source)
     else:
         grid_dir = self.ctx.dir / "etc" / "grid"
         if grid_dir.exists():
             cfg_xml = grid_dir / "config.xml"
         else:
             usr_xml = get_omero_userdir() / "config.xml"
             self.ctx.err("%s not found; using %s" % (grid_dir, usr_xml))
             cfg_xml = usr_xml
     try:
         return ConfigXml(str(cfg_xml))
     except portalocker.LockException:
         self.ctx.die(112, "Could not acquire lock on %s" % cfg_xml)
     except Exception, e:
         self.ctx.die(113, str(e))
示例#7
0
    def tmpdir(self):
        """
        Returns a platform-specific user-writable temporary directory

        First, the value of "OMERO_TMPDIR" is attempted (if available),
        then user's home directory, then the global temp director.

        Typical errors for any of the possible temp locations are:
         * non-existence
         * inability to lock

        See: https://trac.openmicroscopy.org/ome/ticket/1653
        """
        locktest = None

        # Read temporary  files directory from deprecated OMERO_TEMPDIR envvar
        custom_tmpdir_deprecated = os.environ.get('OMERO_TEMPDIR', None)
        if 'OMERO_TEMPDIR' in os.environ:
            import warnings
            warnings.warn(
                "OMERO_TEMPDIR is deprecated. Use OMERO_TMPDIR instead.",
                DeprecationWarning)

        # Read temporary files directory from OMERO_TMPDIR envvar
        default_tmpdir = None
        if custom_tmpdir_deprecated:
            default_tmpdir = path(custom_tmpdir_deprecated) / "omero" / "tmp"
        custom_tmpdir = os.environ.get('OMERO_TMPDIR', default_tmpdir)

        # List target base directories by order of precedence
        targets = []
        if custom_tmpdir:
            targets.append(path(custom_tmpdir))
        targets.append(old_div(get_omero_userdir(), "tmp"))
        targets.append(path(tempfile.gettempdir()) / "omero" / "tmp")

        # Handles existing files named tmp
        # See https://trac.openmicroscopy.org/ome/ticket/2805
        for target in targets:
            if target.exists() and not target.isdir():
                self.logger.debug("%s exists and is not a directory" % target)
                targets.remove(target)

        name = None
        choice = None
        locktest = None

        for target in targets:

            if choice is not None:
                break
            try:
                try:
                    self.create(target)
                    name = self.mkstemp(
                        prefix=".lock_test", suffix=".tmp", dir=target)
                    locktest = open(name, "a+")
                    portalocker.lock(
                        locktest, portalocker.LOCK_EX | portalocker.LOCK_NB)
                    locktest.close()
                    locktest = None
                    choice = target
                    self.logger.debug("Chose global tmpdir: %s", choice)
                finally:
                    if locktest is not None:
                        try:
                            locktest.close()
                        except:
                            self.logger.warn(
                                "Failed to close locktest: %s",
                                name, exc_info=True)

                    if name is not None:
                        try:
                            os.remove(name)
                        except:
                            self.logger.debug("Failed os.remove(%s)", name)

            except Exception as e:
                if "Operation not permitted" in str(e) or \
                   "Operation not supported" in str(e):

                    # This is the issue described in ticket:1653
                    # To prevent printing the warning, we just continue
                    # here.
                    self.logger.debug("%s does not support locking.", target)
                else:
                    self.logger.warn("Invalid tmp dir: %s" %
                                     target, exc_info=True)

        if choice is None:
            raise Exception("Could not find lockable tmp dir")

        return choice
    def tmpdir(self):
        """
        Returns a platform-specific user-writable temporary directory

        First, the value of "OMERO_TMPDIR" is attempted (if available),
        then user's home directory, then the global temp director.

        Typical errors for any of the possible temp locations are:
         * non-existence
         * inability to lock

        See: http://trac.openmicroscopy.org.uk/ome/ticket/1653
        """
        locktest = None

        # Read temporary  files directory from deprecated OMERO_TEMPDIR envvar
        custom_tmpdir_deprecated = os.environ.get('OMERO_TEMPDIR', None)
        if 'OMERO_TEMPDIR' in os.environ:
            import warnings
            warnings.warn(
                "OMERO_TEMPDIR is deprecated. Use OMERO_TMPDIR instead.",
                DeprecationWarning)

        # Read temporary files directory from OMERO_TMPDIR envvar
        default_tmpdir = None
        if custom_tmpdir_deprecated:
            default_tmpdir = path(custom_tmpdir_deprecated) / "omero" / "tmp"
        custom_tmpdir = os.environ.get('OMERO_TMPDIR', default_tmpdir)

        # List target base directories by order of precedence
        targets = []
        if custom_tmpdir:
            targets.append(path(custom_tmpdir))
        targets.append(get_omero_userdir() / "tmp")
        targets.append(path(tempfile.gettempdir()) / "omero" / "tmp")

        # Handles existing files named tmp
        # See https://trac.openmicroscopy.org.uk/ome/ticket/2805
        for target in targets:
            if target.exists() and not target.isdir():
                self.logger.debug("%s exists and is not a directory" % target)
                targets.remove(target)

        name = None
        choice = None
        locktest = None

        for target in targets:

            if choice is not None:
                break
            try:
                try:
                    self.create(target)
                    name = self.mkstemp(
                        prefix=".lock_test", suffix=".tmp", dir=target)
                    locktest = open(name, "a+")
                    portalocker.lock(
                        locktest, portalocker.LOCK_EX | portalocker.LOCK_NB)
                    locktest.close()
                    locktest = None
                    choice = target
                    self.logger.debug("Chose global tmpdir: %s", choice)
                finally:
                    if locktest is not None:
                        try:
                            locktest.close()
                        except:
                            self.logger.warn(
                                "Failed to close locktest: %s",
                                name, exc_info=True)

                    if name is not None:
                        try:
                            os.remove(name)
                        except:
                            self.logger.debug("Failed os.remove(%s)", name)

            except Exception, e:
                if "Operation not permitted" in str(e) or \
                   "Operation not supported" in str(e):

                    # This is the issue described in ticket:1653
                    # To prevent printing the warning, we just continue
                    # here.
                    self.logger.debug("%s does not support locking.", target)
                else:
                    self.logger.warn("Invalid tmp dir: %s" %
                                     target, exc_info=True)
示例#9
0
    def testUserdirEnvironmentSet(self, monkeypatch, tmpdir):
        monkeypatch.setenv('OMERO_USERDIR', str(tmpdir))

        assert get_omero_userdir() == tmpdir

        assert get_omero_user_cache_dir() == tmpdir / "cache"