Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate TempDir #434

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
* Mark `TempDir` as deprecated in favor of `tempfile.TemporaryDirectory`

### Infrastructure
* Fix readthedocs build by updating to v2 configuration schema
* Fix spellcheck erroring out on LICENSE file
Expand Down
1 change: 0 additions & 1 deletion b2sdk/_v3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
hex_sha1_of_stream,
hex_sha1_of_bytes,
hex_sha1_of_file,
TempDir,
IncrementalHexDigester,
)

Expand Down
22 changes: 1 addition & 21 deletions b2sdk/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import os
import platform
import re
import shutil
import tempfile
import time
from dataclasses import dataclass, field
from decimal import Decimal
from itertools import chain
from typing import Any, Iterator, List, NewType, Optional, Tuple, TypeVar
from typing import Any, Iterator, NewType, TypeVar
from urllib.parse import quote, unquote_plus

from logfury.v1 import DefaultTraceAbstractMeta, DefaultTraceMeta, limit_trace_arguments, disable_trace, trace_call
Expand Down Expand Up @@ -328,24 +326,6 @@ def fix_windows_path_limit(path):
return path


class TempDir:
"""
Context manager that creates and destroys a temporary directory.
"""

def __enter__(self):
"""
Return the unicode path to the temp dir.
"""
dirpath_bytes = tempfile.mkdtemp()
self.dirpath = str(dirpath_bytes.replace('\\', '\\\\'))
return self.dirpath

def __exit__(self, exc_type, exc_val, exc_tb):
shutil.rmtree(self.dirpath)
return None # do not hide exception


def _pick_scale_and_suffix(x):
# suffixes for different scales
suffixes = ' kMGTP'
Expand Down
3 changes: 2 additions & 1 deletion b2sdk/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
from .sync import B2SyncPath
from .transfer import DownloadManager, UploadManager

# version & version utils
# utils

from .version_utils import rename_argument, rename_function
from .utils import TempDir

# raw_simulator

Expand Down
38 changes: 38 additions & 0 deletions b2sdk/v2/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
######################################################################
#
# File: b2sdk/v2/utils.py
#
# Copyright 2022 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################

from __future__ import annotations

import shutil
import tempfile
import warnings


class TempDir:
"""
Context manager that creates and destroys a temporary directory.
"""

def __enter__(self):
"""
Return the unicode path to the temp dir.
"""
warnings.warn(
'TempDir is deprecated. Use tempfile.TemporaryDirectory or pytest tmp_path fixture instead.',
DeprecationWarning,
stacklevel=2,
)
dirpath_bytes = tempfile.mkdtemp()
self.dirpath = str(dirpath_bytes.replace('\\', '\\\\'))
return self.dirpath

def __exit__(self, exc_type, exc_val, exc_tb):
shutil.rmtree(self.dirpath)
return None # do not hide exception
7 changes: 4 additions & 3 deletions test/integration/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import gzip
import io
import pathlib
import tempfile
from pprint import pprint
from unittest import mock

from b2sdk.utils import Sha1HexDigest
from b2sdk.v2 import *

from .base import IntegrationTestBase
from .fixtures import * # pyflakes: disable
from .fixtures import * # noqa: F401, F403
from .helpers import authorize


Expand Down Expand Up @@ -62,7 +63,7 @@ def test_large_file(self):
def _file_helper(self, bucket, sha1_sum=None,
bytes_to_write: int | None = None) -> tuple[DownloadVersion, Sha1HexDigest]:
bytes_to_write = bytes_to_write or int(self.info.get_absolute_minimum_part_size()) * 2 + 1
with TempDir() as temp_dir:
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir = pathlib.Path(temp_dir)
source_small_file = pathlib.Path(temp_dir) / 'source_small_file'
with open(source_small_file, 'wb') as small_file:
Expand Down Expand Up @@ -95,7 +96,7 @@ def test_small_unverified(self):

def test_gzip(self):
bucket = self.create_bucket()
with TempDir() as temp_dir:
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir = pathlib.Path(temp_dir)
source_file = temp_dir / 'compressed_file.gz'
downloaded_uncompressed_file = temp_dir / 'downloaded_uncompressed_file'
Expand Down
6 changes: 3 additions & 3 deletions test/unit/account_info/test_account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
import unittest.mock as mock
from abc import ABCMeta, abstractmethod

import pytest
from apiver_deps import (
ALL_CAPABILITIES,
B2_ACCOUNT_INFO_ENV_VAR,
XDG_CONFIG_HOME_ENV_VAR,
AbstractAccountInfo,
InMemoryAccountInfo,
SqliteAccountInfo,
TempDir,
UploadUrlPool,
)
from apiver_deps_exception import CorruptAccountInfo, MissingAccountData

from .fixtures import *
from .fixtures import * # noqa: F401, F403


class WindowsSafeTempDir(TempDir):
class WindowsSafeTempDir(tempfile.TemporaryDirectory):
def __exit__(self, exc_type, exc_val, exc_tb):
try:
super().__exit__(exc_type, exc_val, exc_tb)
Expand Down
Loading