Skip to content

Commit

Permalink
Update run.py, bdmesg.py, disk.py from pymodules
Browse files Browse the repository at this point in the history
  • Loading branch information
corpnewt committed Dec 7, 2018
1 parent c664b60 commit ca6e869
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
8 changes: 5 additions & 3 deletions Scripts/bdmesg.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import binascii, subprocess
import binascii, subprocess, sys

def get_clover_uuid():
bd = bdmesg()
Expand Down Expand Up @@ -30,7 +30,9 @@ def _bdmesg(comm):
# Runs ioreg -l -p IODeviceTree -w0 and searches for "boot-log"
p = subprocess.Popen(comm, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
bd, be = p.communicate()
for line in bd.decode("utf-8").split("\n"):
if sys.version_info >= (3,0) and isinstance(bd, bytes):
bd = bd.decode("utf-8","ignore")
for line in bd.split("\n"):
# We're just looking for the "boot-log" property, then we need to format it
if not '"boot-log"' in line:
# Skip it!
Expand All @@ -43,4 +45,4 @@ def _bdmesg(comm):
# Failed to convert
return ""
# Didn't find it
return ""
return ""
8 changes: 7 additions & 1 deletion Scripts/disk.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def __init__(self):
self.apfs = {}
self._update_disks()

def _get_str(self, val):
# Helper method to return a string value based on input type
if (sys.version_info < (3,0) and isinstance(val, (str, unicode))) or (sys.version_info >= (3,0) and isinstance(val, str)):
return val
return str(val)

def _get_plist(self, s):
p = {}
try:
Expand Down Expand Up @@ -181,7 +187,7 @@ def get_identifier(self, disk):
# Should be able to take a mount point, disk name, or disk identifier,
# and return the disk's identifier
# Iterate!!
if not disk or not len(str(disk)):
if not disk or not len(self._get_str(disk)):
return None
disk = disk.lower()
if disk.startswith("/dev/r"):
Expand Down
56 changes: 35 additions & 21 deletions Scripts/run.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
except:
from queue import Queue, Empty

ON_POSIX = 'posix' in sys.builtin_module_names

class Run:

def __init__(self):
Expand All @@ -15,9 +17,9 @@ def __init__(self):
def _read_output(self, pipe, q):
while True:
try:
c = pipe.read(1)
q.put(c)
q.put(pipe.read(1))
except ValueError:
pipe.close()
break

def _stream_output(self, comm, shell = False):
Expand All @@ -28,33 +30,40 @@ def _stream_output(self, comm, shell = False):
comm = " ".join(shlex.quote(x) for x in comm)
if not shell and type(comm) is str:
comm = shlex.split(comm)
p = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True)
# Threading!
oq, eq = Queue(), Queue()
ot = threading.Thread(target=self._read_output, args=(p.stdout, oq))
et = threading.Thread(target=self._read_output, args=(p.stderr, eq))
ot.daemon, et.daemon = True, True
ot.start()
et.start()
p = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True, close_fds=ON_POSIX)
# Setup the stdout thread/queue
q = Queue()
t = threading.Thread(target=self._read_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
# Setup the stderr thread/queue
qe = Queue()
te = threading.Thread(target=self._read_output, args=(p.stderr, qe))
te.daemon = True # thread dies with the program
# Start both threads
t.start()
te.start()

while True:
c = z = None
c = z = ""
try:
c = oq.get_nowait()
output += c
sys.stdout.write(c)
c = q.get_nowait()
except Empty:
pass
else:
output += c
try:
z = eq.get_nowait()
error += z
sys.stdout.write(z)
z = qe.get_nowait()
except Empty:
pass
else:
error += z
sys.stdout.write(c)
sys.stdout.write(z)
sys.stdout.flush()
p.poll()
if not c and not z and p.returncode is not None:
if c==z=="" and p.returncode != None:
break

o, e = p.communicate()
ot.exit()
et.exit()
Expand All @@ -69,6 +78,12 @@ def _stream_output(self, comm, shell = False):
return (output, error, p.returncode)
return ("", "Command not found!", 1)

def _decode(self, value):
# Helper method to only decode if bytes type
if sys.version_info >= (3,0) and isinstance(value, bytes):
return value.decode("utf-8","ignore")
return value

def _run_command(self, comm, shell = False):
c = None
try:
Expand All @@ -78,11 +93,10 @@ def _run_command(self, comm, shell = False):
comm = shlex.split(comm)
p = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
c = p.communicate()
return (c[0].decode("utf-8", "ignore"), c[1].decode("utf-8", "ignore"), p.returncode)
except:
if c == None:
return ("", "Command not found!", 1)
return (c[0].decode("utf-8", "ignore"), c[1].decode("utf-8", "ignore"), p.returncode)
return (self._decode(c[0]), self._decode(c[1]), p.returncode)

def run(self, command_list, leave_on_fail = False):
# Command list should be an array of dicts
Expand Down Expand Up @@ -138,4 +152,4 @@ def run(self, command_list, leave_on_fail = False):
if len(output_list) == 1:
# We only ran one command - just return that output
return output_list[0]
return output_list
return output_list

0 comments on commit ca6e869

Please sign in to comment.