Beispiel #1
0
    def set(self, key, value, timeout=None, mgmt_element=False):
        # Management elements have no timeout
        if mgmt_element:
            timeout = 0

        # Don't prune on management element update, to avoid loop
        else:
            self._prune()

        timeout = self._normalize_timeout(timeout)
        filename = self._get_filename(key)
        try:
            fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
                                       dir=self._path)
            with os.fdopen(fd, 'wb') as f:
                pickle.dump(timeout, f, 1)
                pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
            rename(tmp, filename)
            os.chmod(filename, self._mode)
        except (IOError, OSError):
            return False
        else:
            # Management elements should not count towards threshold
            if not mgmt_element:
                self._update_count(delta=1)
            return True
Beispiel #2
0
    def set(self, key, value, timeout=None, mgmt_element=False):
        # Management elements have no timeout
        if mgmt_element:
            timeout = 0

        # Don't prune on management element update, to avoid loop
        else:
            self._prune()

        timeout = self._normalize_timeout(timeout)
        filename = self._get_filename(key)
        try:
            fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
                                       dir=self._path)
            with os.fdopen(fd, 'wb') as f:
                pickle.dump(timeout, f, 1)
                pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
            rename(tmp, filename)
            os.chmod(filename, self._mode)
        except (IOError, OSError):
            return False
        else:
            # Management elements should not count towards threshold
            if not mgmt_element:
                self._update_count(delta=1)
            return True
Beispiel #3
0
def atomic_open(filename, mode="r"):
    if "r" not in mode:
        fd, tmp_filename = tempfile.mkstemp(
            dir=os.path.dirname(filename), prefix=".__atomic-write"
        )
        os.chmod(tmp_filename, 0o644)
        f = os.fdopen(fd, mode)
    else:
        f = open(filename, mode)
        tmp_filename = None
    try:
        yield f
    except Exception:
        f.close()
        exc_type, exc_value, tb = sys.exc_info()
        if tmp_filename is not None:
            try:
                os.remove(tmp_filename)
            except OSError:
                pass
        reraise(exc_type, exc_value, tb)
    else:
        f.close()
        if tmp_filename is not None:
            rename(tmp_filename, filename)
Beispiel #4
0
 def commit(self):
     """Commits the artifact changes."""
     if self._new_artifact_file is not None:
         rename(self._new_artifact_file, self.dst_filename)
         self._new_artifact_file = None
     if self._update_con is not None:
         self._update_con.commit()
         self._update_con.close()
         self._update_con = None
Beispiel #5
0
 def commit(self):
     """Commits the artifact changes."""
     if self._new_artifact_file is not None:
         rename(self._new_artifact_file, self.dst_filename)
         self._new_artifact_file = None
     if self._update_con is not None:
         self._update_con.commit()
         self._update_con.close()
         self._update_con = None
Beispiel #6
0
 def save(self, session):
     fn = self.get_session_filename(session.sid)
     fd, tmp = tempfile.mkstemp(suffix=_fs_payment_suffix, dir=self.path)
     f = os.fdopen(fd, 'wb')
     try:
         dump(dict(session), f, HIGHEST_PROTOCOL)
     finally:
         f.close()
     try:
         rename(tmp, fn)
         os.chmod(fn, self.mode)
     except (IOError, OSError):
         pass
Beispiel #7
0
 def save(self, session):
     fn = self.get_session_filename(session.sid)
     fd, tmp = tempfile.mkstemp(suffix=_fs_transaction_suffix, dir=self.path)
     f = os.fdopen(fd, "wb")
     try:
         dump(dict(session), f, HIGHEST_PROTOCOL)
     finally:
         f.close()
     try:
         rename(tmp, fn)
         os.chmod(fn, self.mode)
     except (IOError, OSError):
         pass
Beispiel #8
0
 def set(self, key, value, timeout=None):
     timeout = self._normalize_timeout(timeout)
     filename = self._get_filename(key)
     self._prune()
     try:
         fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
                                    dir=self._path)
         with os.fdopen(fd, 'wb') as f:
             pickle.dump(timeout, f, 1)
             pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
         rename(tmp, filename)
         os.chmod(filename, self._mode)
     except (IOError, OSError):
         return False
     else:
         return True
Beispiel #9
0
 def set(self, key, value, timeout=None):
     timeout = self._normalize_timeout(timeout)
     filename = self._get_filename(key)
     self._prune()
     try:
         fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
                                    dir=self._path)
         with os.fdopen(fd, 'wb') as f:
             pickle.dump(timeout, f, 1)
             pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
         rename(tmp, filename)
         os.chmod(filename, self._mode)
     except (IOError, OSError):
         return False
     else:
         return True
Beispiel #10
0
    def save(self, session):
        fn = self.get_session_filename(session.sid)
        fd, tmp = tempfile.mkstemp(suffix=_fs_transaction_suffix,
                                   dir=self.path)
        f = os.fdopen(fd, "wb")

        try:
            pickle.dump(dict(session), f, pickle.HIGHEST_PROTOCOL)
        finally:
            f.close()

        try:
            rename(tmp, fn)
            os.chmod(fn, self.mode)
        except (IOError, OSError):  # noqa: B014
            pass
Beispiel #11
0
 def set(self, key, value, timeout=None):
     if timeout is None:
         timeout = self.default_timeout
     filename = self._get_filename(key)
     self._prune()
     try:
         fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix, dir=self._path)
         f = os.fdopen(fd, "wb")
         try:
             pickle.dump(int(time() + timeout), f, 1)
             pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
         finally:
             f.close()
         rename(tmp, filename)
         os.chmod(filename, self._mode)
     except (IOError, OSError):
         pass
Beispiel #12
0
 def set(self, key, value, timeout=None):
     if timeout is None:
         timeout = self.default_timeout
     filename = self._get_filename(key)
     self._prune()
     try:
         fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
                                    dir=self._path)
         f = os.fdopen(fd, 'wb')
         try:
             pickle.dump(int(time() + timeout), f, 1)
             pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
         finally:
             f.close()
         rename(tmp, filename)
         os.chmod(filename, self._mode)
     except (IOError, OSError):
         pass
Beispiel #13
0
    def commit(self):
        """Commits the artifact changes."""
        con = None
        try:
            for op in self._pending_update_ops:
                if con is None:
                    con = self.build_state.connect_to_database()
                op(con)

            if self._new_artifact_file is not None:
                rename(self._new_artifact_file, self.dst_filename)
                self._new_artifact_file = None

            if con is not None:
                con.commit()
                con.close()
                con = None
        finally:
            if con is not None:
                con.rollback()
                con.close()
Beispiel #14
0
def atomic_open(filename, mode="r"):
    if "r" not in mode:
        fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename), prefix=".__atomic-write")
        os.chmod(tmp_filename, 0644)
        f = os.fdopen(fd, mode)
    else:
        f = open(filename, mode)
        tmp_filename = None
    try:
        yield f
    except:
        f.close()
        exc_type, exc_value, tb = sys.exc_info()
        if tmp_filename is not None:
            try:
                os.remove(tmp_filename)
            except OSError:
                pass
        raise exc_type, exc_value, tb
    else:
        f.close()
        if tmp_filename is not None:
            rename(tmp_filename, filename)
Beispiel #15
0
    def _commit(self):
        con = None
        try:
            for op in self._pending_update_ops:
                if con is None:
                    con = self.build_state.connect_to_database()
                op(con)

            if self._new_artifact_file is not None:
                rename(self._new_artifact_file, self.dst_filename)
                self._new_artifact_file = None

            if con is not None:
                con.commit()
                con.close()
                con = None

            self.build_state.updated_artifacts.append(self)
            self.build_state.builder.failure_controller.clear_failure(self.artifact_name)
        finally:
            if con is not None:
                con.rollback()
                con.close()
Beispiel #16
0
    def _commit(self):
        con = None
        try:
            for op in self._pending_update_ops:
                if con is None:
                    con = self.build_state.connect_to_database()
                op(con)

            if self._new_artifact_file is not None:
                rename(self._new_artifact_file, self.dst_filename)
                self._new_artifact_file = None

            if con is not None:
                con.commit()
                con.close()
                con = None

            self.build_state.updated_artifacts.append(self)
            self.build_state.builder.failure_controller.clear_failure(
                self.artifact_name)
        finally:
            if con is not None:
                con.rollback()
                con.close()
Beispiel #17
0
def atomic_open(filename, mode='r'):
    if 'r' not in mode:
        fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename),
                                            prefix='.__atomic-write')
        os.chmod(tmp_filename, 0o644)
        f = os.fdopen(fd, mode)
    else:
        f = open(filename, mode)
        tmp_filename = None
    try:
        yield f
    except:  # pylint: disable=bare-except
        f.close()
        exc_type, exc_value, tb = sys.exc_info()
        if tmp_filename is not None:
            try:
                os.remove(tmp_filename)
            except OSError:
                pass
        reraise(exc_type, exc_value, tb)
    else:
        f.close()
        if tmp_filename is not None:
            rename(tmp_filename, filename)
Beispiel #18
0
def atomic_open(filename, mode='r'):
    if 'r' not in mode:
        fd, tmp_filename = tempfile.mkstemp(
            dir=os.path.dirname(filename), prefix='.__atomic-write')
        os.chmod(tmp_filename, 0o644)
        f = os.fdopen(fd, mode)
    else:
        f = open(filename, mode)
        tmp_filename = None
    try:
        yield f
    except:  # pylint: disable=bare-except
        f.close()
        exc_type, exc_value, tb = sys.exc_info()
        if tmp_filename is not None:
            try:
                os.remove(tmp_filename)
            except OSError:
                pass
        reraise(exc_type, exc_value, tb)
    else:
        f.close()
        if tmp_filename is not None:
            rename(tmp_filename, filename)