Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·160 lines (141 loc) · 6.03 KB
/
Copy pathinstall
File metadata and controls
executable file
·160 lines (141 loc) · 6.03 KB
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
import platform
import os
import sys
import shutil
USER_NAME = os.getlogin()
LINUX_APP_PATH = f"/home/{USER_NAME}"
WINDOWS_APP_PATH = f"C:\\Users\\{USER_NAME}"
APP_DIR_NAME = '.acc'
PLATFORM = platform.system()
ISSUES_LINK = 'https://github.com/Bipul-Harsh/auto-cpp-compiler/issues'
class installing_process():
def __init__(self):
self.get_path_progressor()
self.get_app_path()
self.get_installation_dir()
def get_path_progressor(self):
'''Windows has unfortunately different way to progress through directories
\n`pp` stands for path progressor
'''
if PLATFORM == "Windows":
self.pp = '\\'
else:
self.pp = '/'
def get_app_path(self):
if PLATFORM == 'Linux':
self.app_path = f'{LINUX_APP_PATH}{self.pp}{APP_DIR_NAME}'
elif PLATFORM == 'Mac':
# self.app_path = f'{MAC_APP_PATH}{self.pp}{APP_DIR_NAME}'
print(f"Currently this application cannot be installed in your system.\nBut dont worry it will be supported soon on your request on:\n{ISSUES_LINK}")
exit(0)
elif PLATFORM == 'Windows':
self.app_path = f'{WINDOWS_APP_PATH}{self.pp}{APP_DIR_NAME}'
else:
print(f'This software doesnt support this system yet :(\nPlease let us know your system to extend our application compatibility with it.\n\nYou can tell this issue at:\n{ISSUES_LINK}')
exit(0)
def get_installation_dir(self):
self.get_path_progressor()
curr_dir = os.getcwd()
argument = f'{self.pp}'.join(sys.argv[0].strip().split(f'{self.pp}')[:-1])
argument = argument.rstrip(f'{self.pp}.')
if not argument:
# Ex: ./main.py
self.INSTALLATION_DIR = curr_dir
elif argument and argument[0]=='.':
# Ex: ./auto-cpp-compiler/./main.py
self.INSTALLATION_DIR = curr_dir + self.pp + argument.lstrip()
else:
# Ex: /home/user/..../auto-cpp-compiler/main.py | C:\\Users\\...\\auto-cpp-compiler\\main.py
self.INSTALLATION_DIR = argument
assert self.all_files_exists(self.INSTALLATION_DIR),"Required files arent present in the downloaded directory.\nPlease re-download the files!"
def all_files_exists(self, path):
required_file_path = path + self.pp+'.required'
assert os.path.exists(required_file_path),".required file is not present in the directory."
with open(required_file_path, 'r') as f:
self.files = f.read().split('\n')
for file in self.files:
if not os.path.exists(f'{path}{self.pp}{file}'):
return False
else:
return True
def create_app_dir(self):
'''
Makes a forced installation if already installed. All previous setitings will be lost!!
'''
if os.path.isdir(self.app_path):
shutil.rmtree(self.app_path)
os.mkdir(self.app_path)
def get_files(self, file_path):
'''
Return given file content as a string.
Will be used by `self.put_files()` to access file content in string
'''
assert os.path.exists(file_path),f"{file_path.split(f'{self.pp}')[-1]} not found in downloaded directory"
with open(file_path, 'r') as f:
file_content = f.read()
return file_content
def put_file(self, file):
'''
Return given file content as a string.
'''
source = f'{self.INSTALLATION_DIR}{self.pp}{file}'
destination = f'{self.app_path}{self.pp}{file}'
file_content = self.get_files(source)
with open(destination, 'w') as f:
f.write(file_content)
self.make_executable(file)
def put_files(self):
self.put_file('.required')
for file in self.files:
self.put_file(file)
def make_executable(self, file):
if PLATFORM == 'Linux' or PLATFORM == "Mac":
os.system(f'sudo chmod 777 {self.app_path}/{file}')
elif PLATFORM == "Windows":
# Well I dont think its need for windows
pass
else:
print("Platform not supported!")
def remove_prev_shebang(self, content):
lines = content.split('\n')
while len(lines)>0 and lines[0].strip()[:2] == "#!":
lines.pop(0)
return '\n'.join(lines)
def add_at_beginning(self, file, content):
'''Add shebang line in the installation dir files'''
with open(f'{self.INSTALLATION_DIR}{self.pp}{file}','r') as f:
previous_content = f.read()
updated_content = content+'\n'+self.remove_prev_shebang(previous_content)
with open(f'{self.INSTALLATION_DIR}{self.pp}{file}','w') as f:
f.write(updated_content)
def create_symlink(self):
'''For Creating Symlinks'''
if PLATFORM == "Linux":
symlink_path = "/usr/local/bin/acc"
file_path = self.app_path+'/acc'
if os.path.exists(symlink_path):
os.system(f'sudo rm {symlink_path}')
os.system(f'sudo ln -s {file_path} {symlink_path}')
elif PLATFORM == "Windows":
# Will put acc.exe file .acc
shutil.copyfile(self.INSTALLATION_DIR+self.pp+'acc.exe', self.app_path+self.pp+'acc.exe')
def add_shebang(self):
if PLATFORM == 'Linux':
shebang_line = "#!/usr/bin/python3"
elif PLATFORM == "Windows":
shebang_line = "#!python"
else:
shebang_line = "#!/usr/bin/env python3"
self.add_at_beginning("acc", shebang_line)
self.add_at_beginning("main.py", shebang_line)
self.add_at_beginning("install", shebang_line)
def do_installation(self):
print(f"System : {PLATFORM} {platform.release()}")
self.add_shebang()
self.create_app_dir()
self.put_files()
self.create_symlink()
print("Installation done successfully!")
if __name__ == '__main__':
installation = installing_process()
installation.do_installation()