File: utils.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (38 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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