diff options
author | Jeff Lane <jeffrey.lane@canonical.com> | 2017-01-10 16:28:22 -0500 |
---|---|---|
committer | Jeff Lane <jeffrey.lane@canonical.com> | 2017-01-10 16:28:22 -0500 |
commit | 1b17750f7aee07b13b5685f5425f20197fdde2ae (patch) | |
tree | af342d9478d4cc080100f39540c6ff2596ed0a26 | |
parent | 004fe2c354bc49eb498e58caf4ef1fa506171d4e (diff) |
Fixed UnicodeDecodeError when bad data is in a good DMI output. LP: #1655155
-rwxr-xr-x | bin/dmitest | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/bin/dmitest b/bin/dmitest index 27cfc8e..4385095 100755 --- a/bin/dmitest +++ b/bin/dmitest @@ -28,6 +28,8 @@ :param --dmifile: Input filename; optional. If specified, file is used instead of dmidecode output. +:param --show_dmi: + Print the DMI data used. For debugging purposes if errors are encountered. :param --test_versions: Include chassis, system, and base boad version numbers among tests. :param --test_serials: @@ -228,6 +230,8 @@ def main(): choices=['server', 'desktop', 'cpu-check']) parser.add_argument('--dmifile', help="File to use in lieu of dmidecode.") + parser.add_argument('--show_dmi', action="store_true", + help="Print DMI Data used for debugging purposes.") parser.add_argument('--test_versions', action="store_true", help="Set to check version information") parser.add_argument('--test_serials', action="store_true", @@ -236,18 +240,31 @@ def main(): # Command to retrieve DMI information COMMAND = "dmidecode" + if args.dmifile: + COMMAND = ['cat', args.dmifile] + print("Reading " + args.dmifile + " as DMI data") try: - if args.dmifile: - print("Reading " + args.dmifile + " as DMI data") - stream = subprocess.check_output(['cat', args.dmifile], - universal_newlines=True).splitlines() - else: - stream = subprocess.check_output(COMMAND, - universal_newlines=True).splitlines() + dmi_out = subprocess.check_output(COMMAND).splitlines() except subprocess.CalledProcessError as err: print("Error running {}: {}".format(COMMAND, err)) return 1 - + + # Convert the bytes output separately, line by line, because it's possible + # that someone put non-encodable characters in DMI, which cases a + # UnicodeDecodeError that is non-helpful. LP: 1655155 + stream = [] + for line in dmi_out: + try: + stream.append(line.decode('utf-8')) + except UnicodeDecodeError as ude: + print("DATA ERROR: {} \n\t{}".format(ude,line)) + stream.append("ERROR: BAD DATA FOUND HERE") + if args.show_dmi: + print("===== DMI Data Used: =====") + for line in stream: + print(line) + print("===== DMI Output Complete =====") + retval = 0 if args.test_type == 'server' or args.test_type == 'desktop': retval += standard_tests(args, stream) |