-
Notifications
You must be signed in to change notification settings - Fork 22
Fix Python 3.6.0 #5
base: master
Are you sure you want to change the base?
Changes from 1 commit
5e3b735
532cf1f
ac28a99
954e4ba
f55514a
339e7f9
3e8b117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import copy | ||
import platform | ||
import re | ||
import subprocess | ||
|
||
from .posix import get_mounts | ||
|
@@ -30,6 +31,38 @@ | |
OSX_VERSION_MICRO_INT = [int(part) for part in OSX_VERSION_STR.split('.')] | ||
|
||
|
||
def _sanitize_xml(data): | ||
pattern = re.compile(r'^(\s*?\<string\>)(.*?)(\<\/string\>.*?)$') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be compiled once instead of re-compiling on every function call? |
||
output = [] | ||
|
||
for i, line in enumerate(data.split('\n')): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to enumerate if you're not using the index. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also use splitlines(). |
||
chunk = line.decode('utf-8') | ||
match = re.match(pattern, chunk) | ||
if match: | ||
start = match.group(1) | ||
middle = match.group(2) | ||
end = match.group(3) | ||
needs_patch = False | ||
# Inspect the line and see if it has invalid characters. | ||
byte_list = bytearray([ord(byte) for byte in middle]) | ||
for byte in byte_list: | ||
if byte < 32: | ||
needs_patch = True | ||
if needs_patch: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this all be reduced to just
|
||
middle = ''.join(['%02X' % byte for byte in byte_list]) | ||
new_line = '{start}{middle}{end}'.format(start=start, | ||
middle=middle, | ||
end=end) | ||
output.append(new_line) | ||
else: | ||
output.append(line) | ||
else: | ||
output.append(line) | ||
output = '\n'.join(output) | ||
|
||
return output.encode('utf-8') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this redundant? |
||
|
||
|
||
def _ioreg_usb_devices(nodename=None): | ||
"""Returns a list of USB device tree from ioreg""" | ||
import plistlib | ||
|
@@ -38,6 +71,10 @@ def _ioreg(nodename): | |
"""Run ioreg command on specific node name""" | ||
cmd = ['ioreg', '-a', '-l', '-r', '-n', nodename] | ||
output = subprocess.check_output(cmd) | ||
# ST-Link devices (and possibly others?) erroneously store binary data | ||
# in the <string> serial number, which causes plistlib to blow up. | ||
# This will convert that to hex and preserve contents otherwise. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add this comment to the function definition. |
||
output = _sanitize_xml(output) | ||
plist_data = [] | ||
if output: | ||
try: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth investigating doing all of this in an XML library. This function is assuming that
ioreg
will always return data in a specific format.