Skip to content

Commit 5587839

Browse files
committed
Merge pull request #77 from ARMmbed/issue_popen
Add command execution feature with subprocess.communicate()
2 parents 7e75344 + 942f79d commit 5587839

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

mbed_lstools/lstools_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,15 @@ def scan_html_line_for_target_id(self, line):
576576
self.debug(self.scan_html_line_for_target_id.__name__, (m.groups(), result))
577577
return result
578578
return None
579+
580+
@staticmethod
581+
def run_cli_process(cmd, shell=True):
582+
"""! Runs command as a process and return stdout, stderr and ret code
583+
@param cmd Command to execute
584+
@return Tuple of (stdout, stderr, returncode)
585+
"""
586+
from subprocess import Popen, PIPE
587+
588+
p = Popen(cmd, shell=shell, stdout=PIPE, stderr=PIPE)
589+
_stdout, _stderr = p.communicate()
590+
return _stdout, _stderr, p.returncode

mbed_lstools/lstools_linux_generic.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""
1717

1818
import re
19-
import subprocess
2019

2120
from lstools_base import MbedLsToolsBase
2221

@@ -127,10 +126,8 @@ def get_dev_by_id_cmd(self, subdir):
127126
@return tuple(stdout lines, retcode)
128127
"""
129128
cmd = 'ls -oA /dev/' + subdir + '/by-id/'
130-
if self.DEBUG_FLAG:
131-
self.debug(self.get_dev_by_id_cmd.__name__, cmd)
132-
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
133-
return (p.stdout.readlines(), p.wait())
129+
_stdout, _, retval = self.run_cli_process(cmd)
130+
return (_stdout.splitlines(), retval)
134131

135132
def get_dev_by_id_process(self, lines, retval):
136133
"""! Remove unnecessary lines from command line output
@@ -159,13 +156,14 @@ def get_mounts(self):
159156
"""
160157
result = []
161158
cmd = 'mount | grep vfat'
159+
162160
if self.DEBUG_FLAG:
163161
self.debug(self.get_mounts.__name__, cmd)
164162

165-
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
166-
retval = p.wait()
163+
_stdout, _, retval = self.run_cli_process(cmd)
164+
167165
if not retval:
168-
for line in p.stdout.readlines():
166+
for line in _stdout.splitlines():
169167
line = line.rstrip()
170168
result.append(line)
171169
if self.DEBUG_FLAG:

0 commit comments

Comments
 (0)