-
Notifications
You must be signed in to change notification settings - Fork 0
/
1. prepare_system.py
169 lines (128 loc) · 4.98 KB
/
1. prepare_system.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
'''
This scripts installs Ubuntu KDE on WSL2
You need to make sure that the latest kernel for WSL 2 is installed
and that WSL is already
'''
# import modules
import time
from os import chdir, getcwd, system, path, makedirs, remove, write, getenv
from sys import stdout, exit
from shutil import copyfile
def exists(path_):
if path.isdir(path_):
return True
if path.isfile(path_):
return True
return False
def copy_file(filename, destination_path):
'''
a function to copy files
'''
if not path.isdir(path.dirname(destination_path)):
try:
makedirs(path.dirname(destination_path))
except:
pass
copyfile(filename, destination_path)
def write_to(filename, text_to_write, v=False, mode = "overwrite"):
'''
a function to write to file
modes: overwrite; append
'''
try:
if not path.isdir(path.dirname(filename)):
makedirs(path.dirname(filename))
if v:
print("! the directory {0} has been created".format(
path.dirname(filename)))
except:
pass
if mode == "overwrite":
g = open(filename, 'w', encoding="utf-8")
elif mode == "append":
g = open(filename, 'a', encoding="utf-8")
try:
g.write(text_to_write)
if v:
print('\n\t> file saved to ' + filename)
except PermissionError:
print("\t> error writing to {0}, make sure the file is not open in another program".format(
filename))
response = input("\t> continue with the error? (Y/N): ")
if response == "N" or response == "n":
exit()
g.close
me = f'{path.realpath(__file__)}'
chdir(path.dirname(me))
packages_done = exists("packages.ind")
if not packages_done:
# install necessary modules
system('pip install requests --user')
system('pip install winshell --user')
system('pip install pywin32 --user')
system('pip install requests')
system('pip install tqdm --user')
system('pip install pandas --user')
write_to("packages.ind", "Packages necessary for this script already exist")
# import modules
import requests
import subprocess
import winshell
import pandas
from tqdm import tqdm
# declare functions
def delete_file(file_name):
if path.isfile(file_name):
try:
remove(file_name)
except:
print("\t! could not delete file {fn}".format(fn=file_name))
else:
print("\t! the file, {fn}, does not exist".format(fn=file_name))
def download_file(url, save_path, final_fn = None):
if not final_fn is None:
fname = final_fn
else:
fname = path.basename(url)
save_dir = path.dirname(save_path)
save_fname = "{0}/{1}".format(save_dir, fname)
if path.isfile(save_fname):
print("\t! the specified file already exists, skipping")
return
response = requests.get(url, stream=True)
if not path.isdir(save_dir):
makedirs(save_dir)
with open(save_fname, "wb") as handle:
for data in tqdm(response.iter_content()):
handle.write(data)
def run(cmd):
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
return completed
if __name__ == '__main__':
# global variables
restart_countdown = 10
continur_file = exists("continue.ind")
if not continur_file:
# Enable WSL and VM platform on Windows
system("dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart")
system("dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart")
# need to restart
print("\n\t your computer needs to be restarted. We will pass the command to restart after the countdown")
write_to("continue.ind", "Set up will continue after restarting")
batch_content = f'''%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit \nstart "python" "{me}"'''
write_to(f"{getenv('APPDATA')}\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\resume_setup.bat", batch_content)
while restart_countdown > 0:
stdout.write(f"\r {restart_countdown} "); stdout.flush()
restart_countdown -= 1
time.sleep(1)
system("shutdown /r /t 0")
exit() # in case the scripts starts to continue executing outside the loop
else:
print('\t This script was already run on this computer, go to step 2. If you run this script again, WSL & VMP will be re enabled.')
delete_file("continue.ind")
delete_file(f"{getenv('APPDATA')}\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\resume_setup.bat".replace("\\\\", "/"))
restart_countdown = 30
while restart_countdown > 0:
stdout.write(f"\r {restart_countdown} "); stdout.flush()
restart_countdown -= 1
time.sleep(1)