This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
382 lines (309 loc) · 15.2 KB
/
functions.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import contextlib
import subprocess
import sys
from pathlib import Path
from threading import Thread
from time import sleep
from urllib.request import urlopen, urlretrieve
#######################################################################################
# PATHLIB FUNCTIONS #
#######################################################################################
# unlink all files in a directory and remove the directory
def rmdir(rm_dir: str, keep_dir: bool = True) -> None:
def unlink_files(path_to_rm: Path) -> None:
try:
for file in path_to_rm.iterdir():
if file.is_file():
file.unlink()
else:
unlink_files(path_to_rm)
except FileNotFoundError:
print(f"Couldn't remove non existent directory: {path_to_rm}, ignoring")
# convert string to Path object
rm_dir_as_path = Path(rm_dir)
try:
unlink_files(rm_dir_as_path)
except RecursionError: # python doesn't work for folders with a lot of subfolders
print(f"Failed to remove {rm_dir} with python, using bash")
bash(f"rm -rf {rm_dir_as_path.absolute().as_posix()}/*")
# Remove emtpy directory
if not keep_dir:
try:
rm_dir_as_path.rmdir()
except FileNotFoundError: # Directory doesn't exist, because bash was used
return
# remove a single file
def rmfile(file: str, force: bool = False) -> None:
if force: # for symbolic links
Path(file).unlink(missing_ok=True)
file_as_path = Path(file)
with contextlib.suppress(FileNotFoundError):
file_as_path.unlink()
# make directory
def mkdir(mk_dir: str, create_parents: bool = False) -> None:
mk_dir_as_path = Path(mk_dir)
if not mk_dir_as_path.exists():
mk_dir_as_path.mkdir(parents=create_parents)
def path_exists(path_str: str) -> bool:
return Path(path_str).exists()
def get_full_path(path_str: str) -> str:
return Path(path_str).absolute().as_posix()
# recursively copy files from a dir into another dir
def cpdir(src_as_str: str, dst_as_string: str) -> None: # dst_dir must be a full path, including the new dir name
def copy_files(src: Path, dst: Path) -> None:
# create dst dir if it doesn't exist
if verbose:
print(f"Copying {src} to {dst}")
mkdir(dst.absolute().as_posix(), create_parents=True)
for src_file in src.iterdir():
if src_file.is_file():
dst_file = dst.joinpath(src_file.stem + src_file.suffix)
dst_file.write_bytes(src_file.read_bytes())
elif src_file.is_dir():
if src_file.exists():
new_dst = dst.joinpath(src_file.stem + src_file.suffix)
copy_files(src_file, new_dst)
else:
raise FileNotFoundError(f"No such file or directory: {src_file.absolute().as_posix()}")
src_as_path = Path(src_as_str)
dst_as_path = Path(dst_as_string)
if src_as_path.exists():
if not dst_as_path.exists():
mkdir(dst_as_string)
# TODO: Fix python copy dir
'''
try:
copy_files(src_as_path, dst_as_path)
except RecursionError:
print("\033[93m" + f"Failed to copy {root_src} to {root_dst}, using bash" + "\033[0m")
bash(f"cp -rp {src_as_path.absolute().as_posix()} {dst_as_path.absolute().as_posix()}")
'''
bash(f"cp -rp {src_as_path.absolute().as_posix()}/* {dst_as_path.absolute().as_posix()}")
else:
raise FileNotFoundError(f"No such directory: {src_as_path.absolute().as_posix()}")
def cpfile(src_as_str: str, dst_as_str: str) -> None: # "/etc/resolv.conf", "/var/some_config/resolv.conf"
src_as_path = Path(src_as_str)
dst_as_path = Path(dst_as_str)
if verbose:
print(f"Copying {src_as_path.absolute().as_posix()} to {dst_as_path.absolute().as_posix()}")
if src_as_path.exists():
dst_as_path.write_bytes(src_as_path.read_bytes())
else:
raise FileNotFoundError(f"No such file: {src_as_path.absolute().as_posix()}")
#######################################################################################
# BASH FUNCTIONS #
#######################################################################################
# return the output of a command
def bash(command: str) -> str:
output = subprocess.check_output(command, shell=True, text=True).strip()
if verbose:
print(output, flush=True)
return output
def chroot(command: str) -> str:
return bash(f'chroot /mnt/depthboot /bin/bash -c "{command}"')
#######################################################################################
# MISC STUFF #
#######################################################################################
def set_verbose(new_state: bool) -> None:
global verbose
verbose = new_state
def prevent_idle() -> None:
Thread(target=__prevent_idle, daemon=True).start()
def __prevent_idle():
bash('systemd-inhibit /bin/bash -c "sleep 14400" --what="idle"') # sleep indefinitely, thereby preventing idle
print_error("Been copying for 4 HOURS?!?!? Please create an issue")
#######################################################################################
# PACKAGE MANAGER PROGRESS MONITOR FUNCTIONS #
#######################################################################################
# TO AVOID ISSUES: Sync all repos before calling package manager functions
# The functions below, will start a thread to monitor the progress of their respective package managers
def track_apt(path_to_log: str) -> None:
Thread(target=_track_apt, args=(path_to_log,), daemon=True).start()
def track_dnf(path_to_log) -> None:
Thread(target=_track_dnf, args=(path_to_log,), daemon=True).start()
def track_pacman(path_to_log) -> None:
# The actual start of this function is at the bottom
def _track_pacman() -> None:
# As funny as it may sound in python, this function is optimized for performance, due to the huge amount of
# disk I/O Therefore some functions could be shorter, but that might increase the already relatively huge load.
# wait for install to start
while not path_exists(path_to_log):
sleep(0.1)
# wait for total package amount to appear in log
stop = False
while not stop:
sleep(1)
with open(path_to_log, "r") as file:
log = file.readlines()
for line in log:
if "Old Version New Version Net Change Download Size" in line:
total_packages = int(line.strip().split(" ")[1][1:-1])
stop = True
break
# wait and find line where packages start to download
# Pacman might be resolving dependencies, so we need to wait for that to finish
stop = False
while not stop:
sleep(1)
with open(path_to_log, "r") as file:
log = file.readlines()
for line in log:
if ":: Retrieving packages..." in line:
download_start_index = log.index(line) + 1
stop = True
break
# Print download progress
stop = False
downloaded_functions = []
while not stop:
sleep(1)
with open(path_to_log, "r") as file:
log = file.readlines()
for line in log[download_start_index:]: # check lines after the start index to increase performance
if ":: Processing package changes..." in line: # pacman is preparing to install packages
install_start_index = log.index(line) + 1
stop = True
break
package = line.strip()[:-15]
if package not in downloaded_functions:
print(f"Downloading {package}, ({len(downloaded_functions)}/{total_packages})", end="\r",
flush=True)
downloaded_functions.append(package)
# Print install progress
stop = False
installed_packages = []
while not stop:
sleep(1)
with open(path_to_log, "r") as file:
log = file.readlines()
for line in log[
install_start_index:]: # only check lines after the install start to increase performance
if ":: Running post-transaction hooks..." in line: # pacman is preparing to run post install hooks
post_install_start_index = log.index(line) + 1
stop = True
break
if "installing " in line:
package = line.strip()[11:-3]
if package not in installed_packages:
print(f"Installing package {package}, ({len(installed_packages)}/{total_packages})",
end="\r",
flush=True)
installed_packages.append(package)
# Monitor postinstall hooks
# Don't print the full output, as it might include "scary"-ish messages
stop = False
while not stop:
sleep(1)
with open(path_to_log, "r") as file:
log = file.readlines()
for line in log[post_install_start_index:]:
# pacman has no final success message, so we have to manually check if the install is finished
if not line.startswith("("): # if the line doesn't start with a number, it's not relevant for us
continue
temp_line = line.strip().split(" ")[0]
# check if this is the last line by comparing the numbers inside the brackets
if temp_line[1:-1].split("/")[0] == temp_line[1:-1].split("/")[1]:
print("Installation finished", flush=True)
stop = True
break
print(f"Running postinstall hooks: {temp_line}", end="\r", flush=True)
installed_packages.append(package)
Thread(target=_track_pacman, daemon=True).start()
# Track progress of apt/apt-get
def _track_apt(path_to_log) -> None:
pass
# Track progress of dnf
def _track_dnf(path_to_log) -> None:
pass
#######################################################################################
# FILE PROGRESS MONITOR FUNCTIONS #
#######################################################################################
def extract_file(file: str, dest: str) -> None:
"""
Extract a compressed file using tar and use pv to show progress if pv is installed.
:param file: A string representing the full path to the compressed file to be extracted.
:param dest: A string representing the full destination directory where the extracted files will be extracted to.
:return: None
"""
if no_extract_progress: # for non-interactive shells only
if file.endswith(".gz"):
# --warning=no-unknown-keyword is to supress a warning about unknown headers in the arch rootfs
bash(f"tar xfpz {file} --warning=no-unknown-keyword -C {dest}")
elif file.endswith(".xz"):
bash(f"tar xfpJ {file} -C {dest}")
return
if file.endswith(".gz"):
# --warning=no-unknown-keyword is to supress a warning about unknown headers in the arch rootfs
bash(f"pv {file} | tar xfpz - --warning=no-unknown-keyword -C {dest}")
elif file.endswith(".xz"):
bash(f"pv {file} | tar xfpJ - -C {dest}")
def download_file(url: str, path: str) -> None:
# start monitor in a separate thread
if no_download_progress: # for non-interactive shells only
# start download
urlretrieve(url=url, filename=path)
return
# get total file size from server
total_file_size = int(urlopen(url).headers["Content-Length"])
Thread(target=_print_download_progress, args=(Path(path), total_file_size,), daemon=True).start()
# start download
urlretrieve(url=url, filename=path)
# stop monitor
open(".stop_download_progress", "a").close()
print("\n", end="")
def _print_download_progress(file_path: Path, total_size) -> None:
while True:
if path_exists(".stop_download_progress"):
rmfile(".stop_download_progress")
return
try:
print("\rDownloading: " + "%.0f" % int(file_path.stat().st_size / 1048576) + "mb / "
+ "%.0f" % (total_size / 1048576) + "mb", end="", flush=True)
except FileNotFoundError:
sleep(0.5) # in case download hasn't started yet
#######################################################################################
# PRINT FUNCTIONS #
#######################################################################################
# tree implementation in python
# Credit: https://stackoverflow.com/a/59109706
def create_tree(dir_str: str) -> str:
# TODO: sort alphabetically
def tree(dir_path: Path, prefix: str = ''):
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
dir_path.iterdir()
contents = list(dir_path.iterdir())
# contents each get pointers that are ├── with a final └── :
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, path in zip(pointers, contents):
yield prefix + pointer + path.name
if path.is_dir(): # extend the prefix and recurse:
extension = branch if pointer == tee else space
# i.e. space because last, └── , above so no more |
yield from tree(path, prefix=prefix + extension)
final_tree = dir_str + "\n"
for line in tree(Path(dir_str)):
final_tree += line + "\n"
return final_tree
def print_warning(message: str) -> None:
print("\033[93m" + message + "\033[0m", flush=True)
def print_error(message: str) -> None:
print("\033[91m" + message + "\033[0m", flush=True)
def print_status(message: str) -> None:
print("\033[94m" + message + "\033[0m", flush=True)
def print_question(message: str) -> None:
print("\033[92m" + message + "\033[0m", flush=True)
def print_header(message: str) -> None:
print("\033[95m" + message + "\033[0m", flush=True)
verbose = False
# on import check if pv is installed and set global variable
try:
bash("which pv > /dev/null 2>&1") # suppress all output to avoid scaring the user (pv is not a hard dependency)
no_extract_progress = False
except subprocess.CalledProcessError:
no_extract_progress = True
no_download_progress = not sys.stdout.isatty() # disable download progress if terminal is not interactive