mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-07-05 20:41:34 -07:00
Move output_grabber
This commit is contained in:
parent
11c1c8490c
commit
e3fb13de39
6 changed files with 16 additions and 88 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -119,4 +119,7 @@ fpga_version_info.c
|
||||||
|
|
||||||
# local codeql
|
# local codeql
|
||||||
_codeql*
|
_codeql*
|
||||||
/codeql
|
/codeql
|
||||||
|
|
||||||
|
# Pyton venvs
|
||||||
|
venv/
|
||||||
|
|
|
@ -8,6 +8,6 @@ p=pm3.pm3()
|
||||||
print("Device:", p.name)
|
print("Device:", p.name)
|
||||||
with out:
|
with out:
|
||||||
p.console("hw status")
|
p.console("hw status")
|
||||||
for line in out.capturedtext.split('\n'):
|
for line in out.captured_output.split('\n'):
|
||||||
if "Unique ID" in line:
|
if "Unique ID" in line:
|
||||||
print(line)
|
print(line)
|
||||||
|
|
|
@ -1,78 +0,0 @@
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
# From https://stackoverflow.com/a/29834357
|
|
||||||
class OutputGrabber(object):
|
|
||||||
"""
|
|
||||||
Class used to grab standard output or another stream.
|
|
||||||
"""
|
|
||||||
escape_char = "\b"
|
|
||||||
|
|
||||||
def __init__(self, stream=None, threaded=False):
|
|
||||||
self.origstream = stream
|
|
||||||
self.threaded = threaded
|
|
||||||
if self.origstream is None:
|
|
||||||
self.origstream = sys.stdout
|
|
||||||
self.origstreamfd = self.origstream.fileno()
|
|
||||||
self.capturedtext = ""
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
self.start()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
|
||||||
self.stop()
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
"""
|
|
||||||
Start capturing the stream data.
|
|
||||||
"""
|
|
||||||
self.capturedtext = ""
|
|
||||||
# Create a pipe so the stream can be captured:
|
|
||||||
self.pipe_out, self.pipe_in = os.pipe()
|
|
||||||
# Save a copy of the stream:
|
|
||||||
self.streamfd = os.dup(self.origstreamfd)
|
|
||||||
# Replace the original stream with our write pipe:
|
|
||||||
os.dup2(self.pipe_in, self.origstreamfd)
|
|
||||||
if self.threaded:
|
|
||||||
# Start thread that will read the stream:
|
|
||||||
self.workerThread = threading.Thread(target=self.readOutput)
|
|
||||||
self.workerThread.start()
|
|
||||||
# Make sure that the thread is running and os.read() has executed:
|
|
||||||
time.sleep(0.01)
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
"""
|
|
||||||
Stop capturing the stream data and save the text in `capturedtext`.
|
|
||||||
"""
|
|
||||||
# Print the escape character to make the readOutput method stop:
|
|
||||||
self.origstream.write(self.escape_char)
|
|
||||||
# Flush the stream to make sure all our data goes in before
|
|
||||||
# the escape character:
|
|
||||||
self.origstream.flush()
|
|
||||||
if self.threaded:
|
|
||||||
# wait until the thread finishes so we are sure that
|
|
||||||
# we have until the last character:
|
|
||||||
self.workerThread.join()
|
|
||||||
else:
|
|
||||||
self.readOutput()
|
|
||||||
# Close the pipe:
|
|
||||||
os.close(self.pipe_in)
|
|
||||||
os.close(self.pipe_out)
|
|
||||||
# Restore the original stream:
|
|
||||||
os.dup2(self.streamfd, self.origstreamfd)
|
|
||||||
# Close the duplicate stream:
|
|
||||||
os.close(self.streamfd)
|
|
||||||
|
|
||||||
def readOutput(self):
|
|
||||||
"""
|
|
||||||
Read the stream data (one byte at a time)
|
|
||||||
and save the text in `capturedtext`.
|
|
||||||
"""
|
|
||||||
while True:
|
|
||||||
char = os.read(self.pipe_out,1).decode(self.origstream.encoding, errors='replace')
|
|
||||||
if not char or self.escape_char in char:
|
|
||||||
break
|
|
||||||
self.capturedtext += char
|
|
|
@ -8,6 +8,6 @@ p=pm3.pm3("/dev/ttyACM0")
|
||||||
print("Device:", p.name)
|
print("Device:", p.name)
|
||||||
with out:
|
with out:
|
||||||
p.console("hw status")
|
p.console("hw status")
|
||||||
for line in out.capturedtext.split('\n'):
|
for line in out.captured_output.split('\n'):
|
||||||
if "Unique ID" in line:
|
if "Unique ID" in line:
|
||||||
print(line)
|
print(line)
|
||||||
|
|
13
client/experimental_client_with_swig/output_grabber.py → client/pyscripts/output_grabber.py
Executable file → Normal file
13
client/experimental_client_with_swig/output_grabber.py → client/pyscripts/output_grabber.py
Executable file → Normal file
|
@ -16,7 +16,7 @@ class OutputGrabber(object):
|
||||||
if self.origstream is None:
|
if self.origstream is None:
|
||||||
self.origstream = sys.stdout
|
self.origstream = sys.stdout
|
||||||
self.origstreamfd = self.origstream.fileno()
|
self.origstreamfd = self.origstream.fileno()
|
||||||
self.capturedtext = ""
|
self.captured_output = ""
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.start()
|
self.start()
|
||||||
|
@ -29,7 +29,7 @@ class OutputGrabber(object):
|
||||||
"""
|
"""
|
||||||
Start capturing the stream data.
|
Start capturing the stream data.
|
||||||
"""
|
"""
|
||||||
self.capturedtext = ""
|
self.captured_output = ""
|
||||||
# Create a pipe so the stream can be captured:
|
# Create a pipe so the stream can be captured:
|
||||||
self.pipe_out, self.pipe_in = os.pipe()
|
self.pipe_out, self.pipe_in = os.pipe()
|
||||||
# Save a copy of the stream:
|
# Save a copy of the stream:
|
||||||
|
@ -45,7 +45,7 @@ class OutputGrabber(object):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""
|
"""
|
||||||
Stop capturing the stream data and save the text in `capturedtext`.
|
Stop capturing the stream data and save the text in `captured_output`.
|
||||||
"""
|
"""
|
||||||
# Print the escape character to make the readOutput method stop:
|
# Print the escape character to make the readOutput method stop:
|
||||||
self.origstream.write(self.escape_char)
|
self.origstream.write(self.escape_char)
|
||||||
|
@ -69,10 +69,13 @@ class OutputGrabber(object):
|
||||||
def readOutput(self):
|
def readOutput(self):
|
||||||
"""
|
"""
|
||||||
Read the stream data (one byte at a time)
|
Read the stream data (one byte at a time)
|
||||||
and save the text in `capturedtext`.
|
and save the text in `captured_output`.
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
char = os.read(self.pipe_out,1).decode(self.origstream.encoding, errors='replace')
|
char = os.read(self.pipe_out,1).decode(self.origstream.encoding, errors='replace')
|
||||||
if not char or self.escape_char in char:
|
if not char or self.escape_char in char:
|
||||||
break
|
break
|
||||||
self.capturedtext += char
|
self.captured_output += char
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("This is a library, don't use it as a script")
|
|
@ -76,5 +76,5 @@ class pm3(object):
|
||||||
# Register pm3 in _pm3:
|
# Register pm3 in _pm3:
|
||||||
_pm3.pm3_swigregister(pm3)
|
_pm3.pm3_swigregister(pm3)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("This is a library, don't use it as a script")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue