-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscelleanous.py
74 lines (61 loc) · 1.87 KB
/
miscelleanous.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
"""
Miscelleanous functions that are used in the project.
"""
from __future__ import annotations
import os
import subprocess as sp
class bcolors:
"""
Class for colorizing the output.
"""
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def create_directory(name: str) -> bool:
"""
Creates a directory with the given name if it does not exist already.
"""
if not os.path.exists(name):
os.mkdir(name)
exist = False
else:
exist = True
# check if the new directory exists and raise an error and stop execution if not
try:
if not os.path.exists(name):
raise FileNotFoundError(f"Directory {name} does not exist.")
except FileNotFoundError as e:
print(f"Error: {e}")
raise SystemExit(1) from e
return exist
# define a function which goes back to the original working directory if it is called
def chdir(dirname: str) -> None:
"""
Change the active directory.
"""
try:
os.chdir(str(dirname))
# print("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError as e:
print(
f"{bcolors.FAIL}Directory: {dirname} does not exist. \
Stopping here.{bcolors.ENDC}"
)
raise SystemExit(1) from e
except NotADirectoryError:
print(f"{dirname} is not a directory")
except PermissionError:
print(f"You do not have permissions to change to {dirname}")
return None
def checkifinpath(executable: str) -> None:
try:
sp.run(["which", executable], stdout=sp.DEVNULL, stderr=sp.DEVNULL, check=True)
except sp.CalledProcessError as exc:
raise FileNotFoundError(f"'{executable}' is not in PATH") from exc
return None