-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_util.py
188 lines (146 loc) · 4.9 KB
/
setup_util.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import pathlib
import shutil
from urllib import request
class Directory:
"""Container for common filesystem directory-based operations."""
@staticmethod
def exists(a_path: str | pathlib.Path) -> bool | None:
"""Checks if the given directory exists.
Args:
a_path: The path to the directory.
Returns:
A boolean indicating whether the directory exists or not or None if the argument is illegal.
"""
# <editor-fold desc="Checks">
if a_path is None or a_path == "":
return False
# </editor-fold>
return pathlib.Path(a_path).exists()
@staticmethod
def create_directory(a_path: str | pathlib.Path) -> pathlib.Path | None:
"""Creates a directory and all its subdirectories.
Args:
a_path: The path to the directory to be deleted.
Returns:
The path that was created or None if the directory could not be deleted or the argument was illegal.
Notes:
This static method always deletes the directory if it already exists.
"""
# <editor-fold desc="Checks">
if a_path is None or a_path == "":
return None
# </editor-fold>
tmp_path: pathlib.Path = pathlib.Path(a_path)
if not Directory.purge(tmp_path):
return None
tmp_path.mkdir(parents=True)
return tmp_path
@staticmethod
def copy_directory(source_directory_name: str | pathlib.Path,
destination_directory_name: str | pathlib.Path) -> bool:
"""Copies a directory and all its contents to a new location.
Args:
source_directory_name: The path to the directory to be copied.
destination_directory_name: The path to the destination directory.
Returns:
A boolean indicating the success of the operation.
"""
# <editor-fold desc="Checks">
if source_directory_name is None or source_directory_name == "":
return False
if destination_directory_name is None or destination_directory_name == "":
return False
# </editor-fold>
try:
tmp_source_dir = pathlib.Path(source_directory_name)
tmp_destination_dir = pathlib.Path(destination_directory_name)
if not Directory.purge(tmp_destination_dir):
return False
if tmp_destination_dir.exists():
tmp_destination_dir.mkdir(parents=True)
shutil.copytree(tmp_source_dir, tmp_destination_dir)
return True
except Exception as e:
return False
@staticmethod
def purge(a_path: str | pathlib.Path) -> bool:
"""Deletes a directory and all its contents.
This function can also be used if the directory might not exist because
it checks the existence before purging.
Args:
a_path: The path to the directory to be deleted.
Returns:
A boolean indicating the success of the operation.
"""
# <editor-fold desc="Checks">
if a_path is None or a_path == "":
return False
# </editor-fold>
try:
tmp_path = pathlib.Path(a_path)
if tmp_path.exists():
shutil.rmtree(tmp_path)
except Exception as e:
return False
return True
class File:
"""Container for common filesystem file-based operations."""
@staticmethod
def copy(a_source_file_name: str | pathlib.Path, a_dest_file_name: str | pathlib.Path, overwrite: bool = False) -> bool:
"""Copies a file to a new location.
Args:
a_source_file_name: The path of the file to copy.
a_dest_file_name: The destination path of the file to copy.
overwrite: A boolean indicating whether the file should be overwritten.
Returns:
A boolean indicating the success of the operation.
"""
# <editor-fold desc="Checks">
if a_source_file_name is None or a_source_file_name == "":
return False
if a_dest_file_name is None or a_dest_file_name == "":
return False
if overwrite is None:
return False
# </editor-fold>
try:
if overwrite:
if pathlib.Path(a_dest_file_name).exists():
os.remove(a_dest_file_name)
shutil.copy(a_source_file_name, pathlib.Path(a_dest_file_name))
except Exception as e:
return False
return True
@staticmethod
def delete(a_filepath: str | pathlib.Path) -> bool:
"""Deletes a file.
Args:
a_filepath: The path of the file to delete.
Returns:
A boolean indicating the success of the operation.
"""
# <editor-fold desc="Checks">
if a_filepath is None or a_filepath == "":
return False
# </editor-fold>
try:
os.remove(a_filepath)
except Exception as e:
print(e)
return False
return True
def download_file(an_url: str, a_filepath: str) -> bool:
"""Downloads a single file of the given URL.
Args:
an_url: The URL to download.
a_filepath: The path to the file to download.
Returns:
True if the download was successful, False otherwise.
"""
try:
request.urlretrieve(an_url, a_filepath)
return True
except Exception as e:
print(e)
return False