-
Notifications
You must be signed in to change notification settings - Fork 0
/
FoldersUtils.py
71 lines (66 loc) · 2.4 KB
/
FoldersUtils.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
#We import the module os to handle the change of folder
import os
#We import re so we can perform operations with regular expression
import re
#We import sys so we can end our execution if we get an exception
import sys
def isChangeDirectory(command):
"""
This function checks if our command is a cd command
@param command: The command we want to check
@return: True if cd command, False if not
"""
if command.startswith("cd"):
return True
else:
return False
def environmentVariablesPattern(command):
"""
This function searchs for environment variables in our path
@param command: Command where we want to find environment variables
@return: a list with all the environment variables used in this command
"""
#We define a pattern that searchs for string that start with $ and end or not with /
pattern = r"\$[^/]+[ |/]?"
#We compile our regular expression
regexp = re.compile(pattern)
#We return all the environment variables used in a list
return re.findall(pattern,command)
def getPathEnvironment(command):
"""
This method replaces environment variables for their current value
@param command: Command where we want to replace environment variable for their current value
@return: the command where the environment variables have been replaced
"""
#We get the environment variables used in the command in order
envVars = environmentVariablesPattern(command)
#We define an empty path to our folder
pathFolder = ""
#We replace each enviromment variable in our command variable by variable
for envVar in envVars:
#We clean all $ and / off our variable
envVar1 = envVar.replace('$','')
envVar1 = envVar1.replace('/','')
#We get the value of this environment variable
path = os.environ.get(envVar1)
#We replace our environment variable
command = command.replace(envVar,path+"/")
#We return our command with their environment variables replaced
return command
def changeFolder(command):
"""
This function changes the current folder to the one we set in command
@param command: cd command
"""
#We clean all \n off our command
command = command.replace("\n","")
#We replace all the environment variables in our path
varCom = getPathEnvironment(command)
#We erase "cd " from our command as we are not going to use it
varCom = varCom.replace("cd ","")
try:
#We try to change our current folder
os.chdir(varCom)
except:
print("The folder {0} does not exist".format(varCom))
sys.exit()