|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
1 |
|
"""Create archive file. |
|
3
|
|
|
""" |
|
4
|
1 |
|
import os |
|
5
|
1 |
|
import re |
|
6
|
1 |
|
import zipfile |
|
7
|
|
|
|
|
8
|
1 |
|
from .core import Common |
|
9
|
1 |
|
from .exceptions import ZipFileExistError |
|
10
|
1 |
|
from .utils.logging import get_logger |
|
11
|
|
|
|
|
12
|
1 |
|
_logger = get_logger("MakeArchive") |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
1 |
|
class MakeArchive(Common): |
|
16
|
|
|
"""Make archive file. |
|
17
|
|
|
|
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
1 |
|
def __init__(self, extension, directory_path=None): |
|
21
|
|
|
"""Constructor |
|
22
|
|
|
|
|
23
|
|
|
Args: |
|
24
|
|
|
extension (str): Target file extension. |
|
25
|
|
|
directory_path (str): Target directory path. |
|
26
|
|
|
""" |
|
27
|
1 |
|
super().__init__() |
|
28
|
1 |
|
self.__extension = self._convert_extension_with_dot(extension) |
|
29
|
1 |
|
self.__regex_ext = re.compile(self.__extension) |
|
30
|
1 |
|
if directory_path is not None: |
|
31
|
1 |
|
self.__directory_path = directory_path |
|
32
|
|
|
else: |
|
33
|
1 |
|
self.__directory_path = os.getcwd() |
|
34
|
1 |
|
_logger.debug("Current Directory: {cwd}".format(cwd=self.__directory_path)) |
|
35
|
1 |
|
os.chdir(self.__directory_path) |
|
36
|
|
|
|
|
37
|
1 |
|
def make_zip(self, filename, remove_flag=False, overwrite=False): |
|
38
|
|
|
"""Make zip file for adding specify extension files. |
|
39
|
|
|
|
|
40
|
|
|
Make zip file for files which you choose extension. |
|
41
|
|
|
|
|
42
|
|
|
Args: |
|
43
|
|
|
filename (str): Zip file name |
|
44
|
|
|
remove_flag (bool): If true, remove original files |
|
45
|
|
|
overwrite (bool): If true, overwrite zip file even if exist same name file |
|
46
|
|
|
|
|
47
|
|
|
Returns: |
|
48
|
|
|
bool: If success, return true. |
|
49
|
|
|
|
|
50
|
|
|
""" |
|
51
|
1 |
|
count = 0 |
|
52
|
1 |
|
files = self._make_file_list(self.__directory_path) |
|
53
|
1 |
|
if overwrite: |
|
54
|
1 |
|
file_mode = "w" |
|
55
|
|
|
else: |
|
56
|
1 |
|
file_mode = "x" |
|
57
|
1 |
|
for file in files: |
|
58
|
1 |
|
if not self.__regex_ext.search(file): |
|
59
|
1 |
|
_logger.debug( |
|
60
|
|
|
"Skip(No target extension): {filename}".format(filename=file) |
|
61
|
|
|
) |
|
62
|
|
|
else: |
|
63
|
1 |
|
try: |
|
64
|
1 |
|
with zipfile.ZipFile( |
|
65
|
|
|
filename, file_mode, zipfile.ZIP_DEFLATED |
|
66
|
|
|
) as zip_file: |
|
67
|
1 |
|
zip_file.write(file) |
|
68
|
1 |
|
count += 1 |
|
69
|
1 |
|
_logger.debug( |
|
70
|
|
|
"Add Zipfile n files: {count}. Filename: " |
|
71
|
|
|
"{filename}".format(count=count, filename=file) |
|
72
|
|
|
) |
|
73
|
1 |
|
file_mode = "a" |
|
74
|
1 |
|
except FileExistsError as file_exists_error: |
|
75
|
1 |
|
raise ZipFileExistError() from file_exists_error |
|
76
|
|
|
|
|
77
|
1 |
|
if remove_flag: |
|
78
|
1 |
|
self._remove_file(file, assume_yes=True) |
|
79
|
1 |
|
_logger.debug("File removed: {filename}".format(filename=file)) |
|
80
|
1 |
|
_logger.info("-" * 55) |
|
81
|
1 |
|
_logger.info("Finished. Zipfile: {filename}".format(filename=filename)) |
|
82
|
|
|
return True |
|
83
|
|
|
|