forked from merwin-asm/UFT
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyPath.py
87 lines (84 loc) · 2.87 KB
/
pyPath.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
import os
import stat
import shutil
# MYPATH IS A MODULE MADE FOR LINUX. IT CAN BE USED TO
# PERFORM PATH RELATED FUNCTIONS.
# AUTHOR : MERWIN
class MYPATH:
def __init__(self,):
pass
def make_redirecting_bashfile(self,filename,target_pyfile="",content_edited=None,allow_args=False,max_num_args=0):
if not allow_args:
default_command = f"#!/bin/bash \npython3 {target_pyfile}"
else:
sub = ""
for e in range(0,max_num_args):
sub += ' ${'
sub += f'args[{e}]'
sub += '} '
default_command = f"#!/bin/bash\nargs=('$@')\npython3 {target_pyfile} {sub}"
try:
file = open(filename, "x")
if content_edited != None:
file.write(content_edited)
else:
file.write(default_command)
file.close()
except:
file = open(filename, "w")
if content_edited != None:
file.write(content_edited)
else:
file.write(default_command)
file.close()
def addfile_to_path(self,path,file_name,exe=False,permissions=None):
target = rf'/usr/bin/{file_name}'
if os.path.isfile(path):
print(True)
shutil.copyfile(path,target)
os.system("cd /usr/bin")
if exe:
print(True)
os.chmod(target,0o755)
if permissions != None:
os.system(f"sudo chmod {permissions} {file_name}") #SET Permissions
return True
else:
return False
def seek_to_path(self):
return os.environ.get("PATH")
def path_var_exists(self,path):
path_vars = self.seek_to_path()
path_vars = path_vars.split(":")
for each in path_vars:
if each == path:
return True
return False
def add_dir_to_path_TEMP(self,path):
if os.path.isdir(path):
os.system(f'PATH="$PATH:{path}"')
return True
return False
def add_dir_to_path_PERMANENT(self,path,path_to_bashrc):
if os.path.isdir(path):
bashrc = open(path_to_bashrc,"a")
bashrc.write(f'PATH="$PATH:{path}"')
return True
return False
def reparsed_path(self,path):
path_final = ""
for each in str(path):
if each == " ":
path_final += "\ "
elif each == "(":
path_final += "\("
elif each == ")":
path_final += "\)"
else:
path_final += each
return path_final
if __name__ == '__main__':
my_path = MYPATH()
# print(my_path.addfile_to_path("/home/merwin/programming/MYPATH/test_123_2.sh","test_123_2.sh",exe=True))
# print(my_path.seek_to_path())
# print(my_path.path_var_exists("/usr/bin"))