forked from rubel75/BerryPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submoduleProcess.py
64 lines (50 loc) · 2.09 KB
/
submoduleProcess.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
'''
Includes class structure to wrap shell commands and provide a virtual
interface through python
The idea is to wrap each individual shell command in it"s own virtual
shell and provide an abstract class with good exception handling.
'''
from __future__ import print_function # Python 2 & 3 compatible print function
import sys,time
import subprocess
import functools # needed for functools.reduce()
from config import BERRY_DEFAULT_CONSOLE_PREFIX as DEFAULT_PREFIX
DEFAULT_EXECUTABLE_PATH = '/bin/bash'
DEFAULT_BOOLEAN_SHELL = True
def getStringFromList(theList):
if len(theList) > 1:
theList = functools.reduce(lambda i,j: str(i) + ' ' + str(j), theList)
return str(theList)
else:
return str(theList[0])
class VirtualShellInstance:
def __init__(self, command, *arguments, **options):
if arguments:
self._arguments = getStringFromList(arguments)
else:
self._arguments = ''
self._command = command
self.output = None
#check options
if 'input' in options:
theInput = options['input']
if type(theInput) == type([]):
#each input line requires a newline character
theInput = functools.reduce(lambda i,j: str(i) + '\n' + str(j), theInput)
self._command = 'echo ' + '"' +str(theInput) + '"'+ ' | ' + self._command
def __call__(self):
self.run()
def run(self):
commandString = self._command +' ' + self._arguments
print(DEFAULT_PREFIX + 'Calling command: ' + self.getCommandString())
self.output = subprocess.check_call(commandString, shell=True,
executable='/bin/bash')
def progress(self):
print(self.output)
def getCommandString(self):
commandString = self._command +' ' + self._arguments
commandString = commandString.replace('\n', ' ')
return commandString
if __name__ == '__main__':
x = VirtualShellInstance('init_lapw', '-b', '-rkmax 6', '-numk 1')#VirtualShellInstance('ping', '-c', '2','google.com', input=2)
x()