-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbash_to_powershell.py
More file actions
219 lines (194 loc) · 6.85 KB
/
bash_to_powershell.py
File metadata and controls
219 lines (194 loc) · 6.85 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
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
import argparse
def parseArguments():
# create argument parser
parser = argparse.ArgumentParser()
# positional mandatory arguments
parser.add_argument("file",
help="Please provide a file to convert.",
type=str)
# optional arguments
parser.add_argument("-d", "--defaultParams",
help="Add default parameters for makefile.",
type=bool, default=False)
# parse arguments
args = parser.parse_args()
return args
def convertConditions(s):
s = s.replace("[", "(", 1)
s = s[::-1].replace("]", ")")[::-1]
s = s.replace("[", "")
s = s[::-1].replace("]", "")[::-1]
s = s.replace("=", "-eq")
s = s.replace("-z ", "$null -eq ")
s = s.replace("-n ", "$null -ne ")
s = s.replace("-e ", "Test-Path ")
s = convertOperators(s)
return s
def convertOperatorConditions(s, op):
if op == "&&":
newOp = "-and"
elif op == "||":
newOp = "-or"
conditions = s.split(op)
s = ""
for condition in conditions[:-1]:
s += "(" + condition + ") " + newOp + " "
s += conditions[-1] + " | out-null"
return s
def convertOperators(s):
s = s.replace("true", "$true")
s = s.replace("false", "$false")
s = s.replace("==", "-eq")
s = s.replace("!=", "-ne")
if "&&" in s:
s = convertOperatorConditions(s, "&&")
if "||" in s:
s = convertOperatorConditions(s, "||")
return s
def convertDiffStatement(s):
"""
This function converts the diff statement provided that it is
in the following format: diff a b
where a and b are two files to be compared
"""
sList = s.split()
file1, file2 = sList[1], sList[2]
return "if ( -not (Compare-Object (Get-Content " + file1 + \
") (Get-Content " + file2 + ")) ) {"
def convertFunctionArgument(s, start, arg):
return s[:start-1] + "$($args[" + \
str(int(s[start:start+len(arg)])-1) + \
"])" + s[start+len(arg):]
def findAndConvertFunctionArguments(s):
i, arg = 0, None
variableFound = False
while i < len(s):
if s[i] == "$":
variableFound = True
start = i+1
elif variableFound:
if s[start:i+1].isnumeric():
arg = s[start:i+1]
else:
if arg:
s = convertFunctionArgument(s, start, arg)
i += 5
arg = None
variableFound = False
i += 1
if variableFound and s[start:].isnumeric():
s = convertFunctionArgument(s, start, arg)
return s
args = parseArguments()
fileName = args.file
newFileName = str(fileName).split(".sh")[0] + "_powershell.ps1"
fileContent = open(fileName, "r")
powershellFile = open(newFileName, "w+")
if args.defaultParams:
powershellFile.write("$TEST_DIR = \"tests\"" + "\n")
powershellFile.write("$TEST_DATA = \"test_data\"" + "\n")
powershellFile.write("$LIB_DIR = \"pylib\"" + "\n")
powershellFile.write("$CODE_DIR = \".\"" + "\n")
powershellFile.write("$HTML_DIR = \".\"" + "\n")
powershellFile.write("$DATA_DIR = $CODE_DIR + \"/data\"" + "\n")
powershellFile.write("$DOCKER_DIR = \"docker\"" + "\n")
powershellFile.write("\n")
insideDiff = False
insideFunction = False
stack = []
for line in fileContent:
line = line.strip()
if line == "set -e":
line = "$erroractionpreference = \"stop\""
elif line.startswith("export "):
line = line.replace("export ", "$", 1)
elif line.startswith("pwd"):
line = line.replace("pwd", "Get-Location")
elif line.startswith("touch "):
line = line.replace("touch ", "echo $null >>")
elif line.startswith("tail"):
line = line.replace("tail -n", "Get-Content -Tail ")
# when -n parameter is not specified (default is -n10)
line = line.replace("tail", "Get-Content -Tail 10")
elif line.startswith("grep "):
line = line.replace("grep ", "Select-String")
elif line.startswith("find "):
line = line.replace("find ", "Get-ChildItem")
elif line.startswith("python3 ") or line.startswith("python "):
line = line.replace("python3", "python")
# handling "<" operator as it is not allowed in powershell
if "<" in line:
lineSplit = line.split(">")
interchange = lineSplit[0]
outputLocExists = False
if len(lineSplit) > 1:
outputLocExists = True
outputLoc = lineSplit[1]
lineSplit = interchange.split("<")
line = lineSplit[1] + "| " + lineSplit[0]
if outputLocExists:
line += ">" + outputLoc
line = "Get-Content" + line
elif line.startswith("if"):
line = convertConditions(line)
elif line.startswith("elif"):
line = "} \n" + line
line = line.replace("elif", "elseif")
line = convertConditions(line)
elif line.startswith("then"):
line = "{"
elif line.startswith("else"):
line = "} \n" + line
line = line.replace("else", "else {")
elif line.startswith("fi"):
line = "}"
elif "()" in line:
line = "function " + line
line = line.replace("()", "")
insideFunction = True
elif line.startswith("for"):
line = line.replace("for ", "foreach ($")
line = line.replace(";", "")
if "*" in line:
forStatement = line.split("in")
temp, directory = forStatement[0], forStatement[1]
# the Force parameter handles hidden files
line = temp + "in " + "Get-ChildItem -Path " + \
directory + " -Force"
line += ")"
elif line.startswith("done"):
line = line.replace("done", "}")
elif line.startswith("do"):
line = line.replace("do", "{")
elif line.startswith("cp "):
line = line.replace("cp ", "Copy-Item ")
elif line.startswith("mv "):
line = line.replace("mv ", "Move-Item ")
elif line.startswith("rm "):
line = line.replace("rm ", "Remove-Item ")
elif line.startswith("diff "):
insideDiff = True
line = convertDiffStatement(line)
elif line.startswith("echo "):
line = line.replace("echo ", "Write-Host ")
if insideDiff and "}" in line:
line = "}\n" + \
"else {\n" + \
"Write-Host \"Comparison failed.\"\n" + \
"}\n" + \
line
insideDiff = False
if insideFunction:
if "$" in line:
line = findAndConvertFunctionArguments(line)
elif "{" in line:
stack.append("{")
elif "}" in line:
if not stack:
insideFunction = False
else:
stack.pop()
line = convertOperators(line)
powershellFile.write(line + "\n")
fileContent.close()
powershellFile.close()