Example #1
0
    def test_readonly(self):
        filename = os.path.join(self.dir, "foo.txt")

        with open(filename, "wb") as fobj:
            fobj.write(b"nope")

        dir_mode = os.stat(self.dir).st_mode
        file_mode = os.stat(filename).st_mode
        # setting directory permissions doesn't work under Windows, so make
        # the file read only, so the rename fails. On the other hand marking
        # the file read only doesn't make rename fail on unix, so make the
        # directory read only as well.
        os.chmod(filename, stat.S_IREAD)
        os.chmod(self.dir, stat.S_IREAD)
        try:
            with self.assertRaises(OSError):
                with atomic_save(filename, "wb") as fobj:
                    fobj.write(b"foo")
        finally:
            # restore permissions
            os.chmod(self.dir, dir_mode)
            os.chmod(filename, file_mode)

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"nope")

        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])
Example #2
0
    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb") as fileobj:
                if PY2:
                    self._config.write(fileobj)
                else:
                    temp = StringIO()
                    self._config.write(temp)
                    data = temp.getvalue().encode("utf-8", "surrogateescape")
                    fileobj.write(data)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)
    def test_readonly(self):
        filename = os.path.join(self.dir, "foo.txt")

        with open(filename, "wb") as fobj:
            fobj.write(b"nope")

        dir_mode = os.stat(self.dir).st_mode
        file_mode = os.stat(filename).st_mode
        # setting directory permissions doesn't work under Windows, so make
        # the file read only, so the rename fails. On the other hand marking
        # the file read only doesn't make rename fail on unix, so make the
        # directory read only as well.
        os.chmod(filename, stat.S_IREAD)
        os.chmod(self.dir, stat.S_IREAD)
        try:
            with self.assertRaises(OSError):
                with atomic_save(filename, "wb") as fobj:
                    fobj.write(b"foo")
        finally:
            # restore permissions
            os.chmod(self.dir, dir_mode)
            os.chmod(filename, file_mode)

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"nope")

        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])
Example #4
0
    def test_non_exist(self):
        filename = os.path.join(self.dir, "foo.txt")

        with atomic_save(filename, "wb") as fobj:
            fobj.write(b"foo")
            temp_name = fobj.name

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"foo")

        self.assertFalse(os.path.exists(temp_name))
        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])
    def test_non_exist(self):
        filename = os.path.join(self.dir, "foo.txt")

        with atomic_save(filename, "wb") as fobj:
            fobj.write(b"foo")
            temp_name = fobj.name

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"foo")

        self.assertFalse(os.path.exists(temp_name))
        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])
Example #6
0
def dump_items(filename, items):
    """Pickle items to disk.

    Doesn't handle exceptions.
    """

    dirname = os.path.dirname(filename)
    mkdir(dirname)

    with atomic_save(filename, "wb") as fileobj:
        # While protocol 2 is usually faster it uses __setitem__
        # for unpickle and we override it to clear the sort cache.
        # This roundtrip makes it much slower, so we use protocol 1
        # unpickle numbers (py2.7):
        #   2: 0.66s / 2 + __set_item__: 1.18s / 1 + __set_item__: 0.72s
        # see: http://bugs.python.org/issue826897
        pickle.dump(items, fileobj, 1)
Example #7
0
def dump_items(filename, items):
    """Pickle items to disk.

    Doesn't handle exceptions.
    """

    dirname = os.path.dirname(filename)
    mkdir(dirname)

    with atomic_save(filename, "wb") as fileobj:
        # While protocol 2 is usually faster it uses __setitem__
        # for unpickle and we override it to clear the sort cache.
        # This roundtrip makes it much slower, so we use protocol 1
        # unpickle numbers (py2.7):
        #   2: 0.66s / 2 + __set_item__: 1.18s / 1 + __set_item__: 0.72s
        # see: http://bugs.python.org/issue826897
        pickle.dump(items, fileobj, 1)
Example #8
0
    def save(self, filename=None):
        """Save the library to the given filename, or the default if `None`"""

        if filename is None:
            filename = self.filename

        print_d("Saving contents to %r." % filename, self)

        try:
            dirname = os.path.dirname(filename)
            mkdir(dirname)
            with atomic_save(filename, "wb") as fileobj:
                fileobj.write(dump_audio_files(self.get_content()))
                # unhandled SerializationError, shouldn't happen -> better
                # not replace the library file with nothing
        except EnvironmentError:
            print_w("Couldn't save library to path: %r" % filename)
        else:
            self.dirty = False
Example #9
0
    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb" if PY2 else "w") as fileobj:
                self._config.write(fileobj)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)
Example #10
0
    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb" if PY2 else "w") as fileobj:
                self._config.write(fileobj)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)
Example #11
0
    def save(self, filename=None):
        """Save the library to the given filename, or the default if `None`"""

        if filename is None:
            filename = self.filename

        print_d("Saving contents to %r." % filename, self)

        try:
            dirname = os.path.dirname(filename)
            mkdir(dirname)
            with atomic_save(filename, "wb") as fileobj:
                fileobj.write(dump_audio_files(self.get_content()))
        except SerializationError:
            # Can happen when we try to pickle while the library is being
            # modified, like in the periodic 15min save.
            # Ignore, as it should try again later or on program exit.
            util.print_exc()
        except EnvironmentError:
            print_w("Couldn't save library to path: %r" % filename)
        else:
            self.dirty = False
Example #12
0
    def save(self, filename=None):
        """Save the library to the given filename, or the default if `None`"""

        if filename is None:
            filename = self.filename

        print_d("Saving contents to %r." % filename, self)

        try:
            dirname = os.path.dirname(filename)
            mkdir(dirname)
            with atomic_save(filename, "wb") as fileobj:
                fileobj.write(dump_audio_files(self.get_content()))
        except SerializationError:
            # Can happen when we try to pickle while the library is being
            # modified, like in the periodic 15min save.
            # Ignore, as it should try again later or on program exit.
            util.print_exc()
        except EnvironmentError:
            print_w("Couldn't save library to path: %r" % filename)
        else:
            self.dirty = False