-
Notifications
You must be signed in to change notification settings - Fork 140
/
testparser.sh.py
executable file
·55 lines (43 loc) · 1.04 KB
/
testparser.sh.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
#!/usr/bin/env python
outfile = file('testparser.sh', 'w')
outfile.write('#!/bin/sh\n')
hash = '#'
colon = ':'
shell_prompt = '% '
echo_format = "echo '%s'\n"
newline = '\n'
echo_by_itself = 'echo\n'
import re
bs_exp = re.compile('(\\\\*)\n')
escape_newline_exp = re.compile('\\\\\n')
empty = ''
import fileinput
holding = []
for line in fileinput.input(['testparser.sh.in']):
if len(line) <= 1:
#Empty line.
continue
if(len(bs_exp.search(line).group(1)) % 2):
holding.append(line[:-1])
continue
elif holding:
holding.append(line)
line = '\n'.join(holding)
del holding[:]
line = escape_newline_exp.sub(empty, line)
is_comment = line.startswith(hash)
if is_comment:
line_for_display = line[:-1].strip(hash) + colon
else:
line_for_display = shell_prompt + line[:-1]
echo_line = echo_format % (line_for_display,)
lines = [newline, echo_line]
if is_comment:
lines.insert(1, echo_by_itself)
else:
lines.append(line)
outfile.writelines(lines)
outfile.close()
# Make it executable.
import os
os.chmod('testparser.sh', 0755)