Test Failed
Pull Request — master (#9)
by Stephen
06:04
created

dirutility.open.open.tartempdir()   A

Complexity

Conditions 5

Size

Total Lines 36
Code Lines 14

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nop 5
dl 36
loc 36
rs 9.2333
c 0
b 0
f 0
1
import os
2
import tempfile
3
from contextlib import contextmanager
4
from typing import Dict, Union, TextIO
5
6
from dirutility.error import InvalidAbsoluteDirectoryError
7
from dirutility.open.clazz import TempDir, TarFile, ZipFile
8
9
10 View Code Duplication
@contextmanager
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
def tempdir(absdirpath: str, prefix='', suffix='') -> TempDir:
12
    """
13
    Create and return a temporary directory.  This has the same
14
    behavior as mkdtemp but can be used as a context manager.  For
15
    example:
16
17
        with tempdir('/home/dirutility/temp', prefix='prefix_', suffix='_suffix') as tmpdir:
18
            ...
19
20
    Upon exiting the context, the directory and everything contained
21
    in it are removed.
22
23
    :param absdirpath:
24
    :param prefix:
25
    :param suffix:
26
    :return:
27
    :rtype: TempDir
28
    """
29
    with tempfile.TemporaryDirectory(prefix=prefix, suffix=suffix, dir=absdirpath) as tmpdir_path:
30
        if os.path.isabs(absdirpath) and os.path.isdir(absdirpath):
31
            dir_obj = TempDir(tmpdir_path)
32
            yield dir_obj
33
        else:
34
            raise InvalidAbsoluteDirectoryError(absdirpath)
35
36
37 View Code Duplication
@contextmanager
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
def tartempdir(
39
        absfilepath: str, tempdir: TempDir, mode: str = 'w:gz', temp: bool = True, force: bool = True) -> TarFile:
40
    """
41
    Create and return a tarfile directory.  This has the same
42
    behavior as mkdtemp then create tarfile from temp dir but can be used as a context manager.  For
43
    example:
44
45
        with tardir('/home/dirutility/temp/bac.tar', 'haha.tar') as tardir:
46
            ...
47
48
    Upon exiting the context, the directory and everything contained
49
    in it are removed.
50
51
    :param absfilepath: archive file create abs path
52
    :param tempdir: temporary directory object
53
    :param mode: tarfile mode
54
    :param temp: temporary file
55
    :param force: force create file
56
    :return:
57
    :rtype: TarFile
58
    """
59
    file_create_by_me = False
60
    try:
61
        if force:
62
            yield _create_archive(absfilepath, tempdir, mode)
63
            file_create_by_me = True
64
        elif not os.path.exists(absfilepath):
65
            yield _create_archive(absfilepath, tempdir, mode)
66
        else:
67
            raise FileExistsError(absfilepath)
68
    finally:
69
        if temp and file_create_by_me:
70
            os.remove(absfilepath)
71
        else:
72
            pass
73
74
75
def _create_archive(absfilepath, tempdir, mode) -> TarFile:
76
    """
77
78
    :param absfilepath:
79
    :param tempdir:
80
    :param mode:
81
    :return:
82
    """
83
    if mode.endswith('zip'):
84
        return ZipFile(absfilepath, tempdir, mode)
85
    else:
86
        return TarFile(absfilepath, tempdir, mode)
87
88
89 View Code Duplication
@contextmanager
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
90
def tardir(
91
        absfilepath: str, mode: str = 'w:gz', temp: bool = True, force: bool = True, withdir: bool = False,
92
        **paths: Dict[str, Union[dict, str, TextIO, None]]) -> TarFile:
93
    """
94
    Create and return a tarfile directory.  This has the same
95
    behavior as mkdtemp then create tarfile from temp dir but can be used as a context manager.  For
96
    example:
97
98
        with tardir('/home/dirutility/temp/bac.tar', 'haha.tar') as tardir:
99
            ...
100
101
    Upon exiting the context, the directory and everything contained
102
    in it are removed.
103
104
    :param absfilepath: archive file create abs path
105
    :param mode: tarfile mode
106
    :param temp: temporary file
107
    :param force: force create file
108
    :param withdir: if or not with dir in archive file
109
    :param paths: directory paths objects
110
    :return:
111
    :rtype: TarFile
112
    """
113
    absdirpath = absfilepath.rsplit('/', 1)[0]
114
    with tempdir(absdirpath) as temp_obj:
115
        temp_obj.create_structure(**paths)
116
        with tartempdir(absfilepath, temp_obj, mode=mode, temp=temp, force=force) as tar_obj:
117
            tar_obj.create_archive(withdir=withdir)
118
            yield tar_obj
119