Uh oh!
There was an error while loading. Please reload this page.
forked from rtcTo/rtc2git
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
Latest commit
60 lines (46 loc) · 1.6 KB
/
Copy pathshell.py
File metadata and controls
60 lines (46 loc) · 1.6 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
importsys
fromsubprocessimportcall, check_output, CalledProcessError
importshouter
logcommands=False
encoding=None
defexecute(command, outputfile=None, openmode="w"):
shout_command_to_log(command, outputfile)
ifnotoutputfile:
returncall(command, shell=True)
else:
withopen(outputfile, openmode, encoding=encoding) asfile:
returncall(command, stdout=file, shell=True)
defgetoutput(command, stripped=True):
shout_command_to_log(command)
try:
outputasbytestring=check_output(command, shell=True)
output=outputasbytestring.decode(sys.stdout.encoding).splitlines()
exceptCalledProcessErrorase:
shouter.shout(e)
output=""
ifnotstripped:
returnoutput
else:
lines= []
forlineinoutput:
strippedline=line.strip()
ifstrippedline:
lines.append(strippedline)
returnlines
defquote(stringtoquote):
stringtoquote=stringtoquote.replace('\"', "'") # replace " with '
quotedstring='\"'+stringtoquote+'\"'
returnescapeShellVariableExpansion(quotedstring)
defescapeShellVariableExpansion(comment):
returncomment.replace('$', '"\\$"')
defshout_command_to_log(command, outputfile=None):
iflogcommands:
logmessage="Executed Command: "+quote(command)
ifoutputfile:
shouter.shout(logmessage+" --> "+outputfile)
else:
shouter.shout(logmessage)
defsetencoding(encodingtobeset):
globalencoding
ifencodingtobeset:
encoding=encodingtobeset