コード例 #1
0
ファイル: test_utils.py プロジェクト: cguwilliams/HmyApp
    def test_abort(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        with atomic_file(path) as fp:
            fp.write(r_content[:2])
            temp_name = fp._name
            fp.abort()

        # Then
        self.assertFalse(os.path.exists(path))
        self.assertFalse(os.path.exists(temp_name))
コード例 #2
0
ファイル: test_utils.py プロジェクト: pombreda/enstaller
    def test_abort(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        with atomic_file(path) as fp:
            fp.write(r_content[:2])
            temp_name = fp._name
            fp.abort()

        # Then
        self.assertFalse(os.path.exists(path))
        self.assertFalse(os.path.exists(temp_name))
コード例 #3
0
ファイル: test_utils.py プロジェクト: pombreda/enstaller
    def test_simple(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        with atomic_file(path) as fp:
            fp.write(r_content)

        # Then
        self.assertTrue(os.path.exists(path))
        with open(path, "rb") as fp:
            content = fp.read()
        self.assertEqual(content, r_content)
コード例 #4
0
ファイル: test_utils.py プロジェクト: cguwilliams/HmyApp
    def test_simple(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        with atomic_file(path) as fp:
            fp.write(r_content)

        # Then
        self.assertTrue(os.path.exists(path))
        with open(path, "rb") as fp:
            content = fp.read()
        self.assertEqual(content, r_content)
コード例 #5
0
ファイル: test_utils.py プロジェクト: pombreda/enstaller
    def test_failure(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        try:
            with atomic_file(path) as fp:
                fp.write(r_content[:2])
                raise KeyboardInterrupt()
        except BaseException:
            pass

        # Then
        self.assertFalse(os.path.exists(path))
コード例 #6
0
ファイル: test_utils.py プロジェクト: cguwilliams/HmyApp
    def test_failure(self):
        # Given
        r_content = b"some data"
        path = os.path.join(self.tempdir, "some_data.bin")

        # When
        try:
            with atomic_file(path) as fp:
                fp.write(r_content[:2])
                raise KeyboardInterrupt()
        except BaseException:
            pass

        # Then
        self.assertFalse(os.path.exists(path))
コード例 #7
0
ファイル: session.py プロジェクト: enthought/enstaller
    def download(self, url, target=None):
        """ Safely download the content at the give url.

        Safely here is understood as not leaving stalled content if the
        download fails or is canceled. It uses streaming as so not to use too
        much memory.
        """
        resp = self.fetch(url)

        if target is None:
            target = os.path.basename(urlparse(url).path)

        with atomic_file(target) as fp:
            for chunk in resp.iter_content(1024):
                fp.write(chunk)

        return target
コード例 #8
0
    def download(self, url, target=None):
        """ Safely download the content at the give url.

        Safely here is understood as not leaving stalled content if the
        download fails or is canceled. It uses streaming as so not to use too
        much memory.
        """
        resp = self._raw.get(url, stream=True)
        resp.raise_for_status()

        if target is None:
            target = os.path.basename(urllib.parse.urlparse(url).path)

        with atomic_file(target) as fp:
            for chunk in resp.iter_content(1024):
                fp.write(chunk)

        return target
コード例 #9
0
ファイル: fetch_utils.py プロジェクト: cguwilliams/HmyApp
def checked_content(filename, expected_md5):
    """
    A simple context manager ensure data written to filename match the given
    md5.

    Parameters
    ----------
    filename : str
        The path to write to
    expected_checksum : str
        The expected checksum

    Returns
    -------
    fp : MD5File
        A file-like MD5File instance.

    Example
    -------
    A simple example::

        with checked_content("foo.bin", expected_md5) as fp:
            fp.write(data)
        # An InvalidChecksum will be raised if the checksum does not match
        # expected_md5

    The checksum may be disabled by setting up abort to fp::

        with checked_content("foo.bin", expected_md5) as fp:
            fp.write(data)
            fp.abort = True
            # no checksum is getting validated
    """
    with atomic_file(filename) as target:
        checked_target = MD5File(target)
        yield checked_target

        if checked_target.is_aborted:
            target.abort()
            return
        else:
            if expected_md5 != checked_target.checksum:
                raise InvalidChecksum(filename, expected_md5,
                                      checked_target.checksum)
コード例 #10
0
ファイル: test_utils.py プロジェクト: pombreda/enstaller
    def test_exists_with_failure(self):
        # Given
        r_content = b"nono"
        path = os.path.join(self.tempdir, "some_data.bin")
        with open(path, "wb") as fp:
            fp.write(r_content)

        # When
        try:
            with atomic_file(path) as fp:
                fp.write(b"some data")
                raise ValueError("some random failure")
        except BaseException:
            pass

        # Then
        self.assertTrue(os.path.exists(path))
        with open(path, "rb") as fp:
            content = fp.read()
        self.assertEqual(content, r_content)
コード例 #11
0
ファイル: test_utils.py プロジェクト: cguwilliams/HmyApp
    def test_exists_with_failure(self):
        # Given
        r_content = b"nono"
        path = os.path.join(self.tempdir, "some_data.bin")
        with open(path, "wb") as fp:
            fp.write(r_content)

        # When
        try:
            with atomic_file(path) as fp:
                fp.write(b"some data")
                raise ValueError("some random failure")
        except BaseException:
            pass

        # Then
        self.assertTrue(os.path.exists(path))
        with open(path, "rb") as fp:
            content = fp.read()
        self.assertEqual(content, r_content)