-
Notifications
You must be signed in to change notification settings - Fork 10
/
setup.py
executable file
·225 lines (146 loc) · 4.68 KB
/
setup.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
# /usr/bin/python
# Os level operations
import os
from getpass import getuser
# Determinates if the program is installed
from shutil import which
# Get the OS I'm in
import platform
INSTALL_PATH = ".local/bin"
def print_program_welcome():
row = "=" * 55
sep = "||"
welcome = "SETUP SCRIPT OF DANIEL'S QTILE CONFIG".center(len(row) - len(sep) * 2)
welcome = sep + welcome + sep
print(row)
print("")
print(welcome)
print("")
print(row + "\n")
print(f"Hi there {getuser()}, you are about to setup Qtile".center(len(row)))
print("\n" + row)
def print_current_dir():
print(f"Currently you are in {os.getcwd()} directory")
def get_correct_os():
"""
Returns the Os name:
get_os -> bool
Linux: linux
Mac: darwin
Windows: windows
"""
os = platform.system().lower()
return os == "linux"
def get_home_path():
"""
Get Linux home path
get_home_path -> str
"""
return os.path.expanduser("~")
def get_local_bin(home):
"""
Returns the local bin path of the User
"""
return f"{home}/.local/bin"
def check_local_folder_exists():
"""
Check if .local/bin exists
"""
home = get_home_path()
local = get_local_bin(home)
return os.path.exists(local)
def create_local_install_folder(local_bin):
"""
Creates local bin folder
"""
print("")
print(f"Creating folder ar {local_bin}")
print("")
try:
os.mkdir(local_bin)
except OSError:
raise OSError(f"Creation of the directory {local_bin} failed")
else:
print(f"Successfully created the directory {local_bin}")
def get_response(message="yes/no"):
"""
returns -> bool
"""
while True:
response = input(message).lower()
if response == "yes":
return True
elif response == "no":
return False
else:
print("\nSorry your response must be yes or no\n")
continue
LOCAL_BIN = get_local_bin(get_home_path())
def link_scripts(path):
path = os.path.abspath(path)
if not os.path.exists(path):
raise OSError("Sorry that path doesn't exist")
os.chdir(path)
i = 0
for file_ in os.listdir(path):
file_path = os.path.abspath(file_)
file_name = file_.split(".")[0]
link_path = LOCAL_BIN + f"/{file_name}"
if os.path.exists(link_path):
print("You have already in path", file_name)
continue
os.link(file_path, link_path)
i += 1
return f"Linked {i} files to .local/bin"
def get_dependencies(software_path="software.txt"):
"""software_path -> path of software.txt
returns list of dependencies
"""
if not os.path.exists(software_path):
print(f"The file {software_path} wasn't found. Please clone again or provide one")
if get_response("Continue without dependencies warnings? [yes/no]"):
print("Dependencies won't be shown")
return None
else:
print("Clone again or provide a file.")
exit()
software = []
with open(software_path, "r") as file_:
for line in file_:
software.append(line.strip())
return software
def check_dependencies(dependencies=None):
"""dependencies -> list
returns -> str programs that aren't installed
"""
if dependencies is None:
return ""
not_installed = []
for program in dependencies:
if which(program) is not None:
continue
not_installed.append(program)
first_row = "The following software is not installed, some scripts may not work\n"
if not_installed:
return first_row + "\n".join(not_installed)
else:
return ""
def main():
print_program_welcome()
if not get_correct_os():
raise OSError("Sorry Qtile is only supported in Linux")
if not check_local_folder_exists():
print("\n.local/bin folder doesn't exists\n")
response = get_response(message="Do you want to create that directory [yes, no] >> ")
if not response:
print("Setup can't continue if .local/bin doesn't exists")
print("Rerun the script or set up the scripts manually")
exit()
print("Creating .local/bin/folder")
create_local_install_folder(LOCAL_BIN)
dependencies = get_dependencies()
scripts_path = "./scripts"
print(check_dependencies(dependencies=dependencies))
print(link_scripts(path=scripts_path))
if __name__ == "__main__":
main()