Last active
November 22, 2017 09:38
-
-
Save efaisal/415a7a15f546446d845322eb04d9a54c to your computer and use it in GitHub Desktop.
A patch to sps.py or sps.py replacement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- sps.py.old 2017-11-16 15:31:01.000000000 +0800 | |
+++ sps.py 2017-11-22 16:53:36.822417986 +0800 | |
@@ -8,6 +8,27 @@ | |
import errno | |
import glob | |
+class CalledProcessError(Exception): | |
+ def __init__(self, returncode, cmd, output=None): | |
+ self.returncode = returncode | |
+ self.cmd = cmd | |
+ self.output = output | |
+ def __str__(self): | |
+ return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) | |
+ | |
+def check_output(*popenargs, **kwargs): | |
+ if 'stdout' in kwargs: | |
+ raise ValueError('stdout argument not allowed, it will be overridden.') | |
+ process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) | |
+ output, unused_err = process.communicate() | |
+ retcode = process.poll() | |
+ if retcode: | |
+ cmd = kwargs.get("args") | |
+ if cmd is None: | |
+ cmd = popenargs[0] | |
+ raise CalledProcessError(retcode, cmd, output=output) | |
+ return output | |
+ | |
class SPSError(Exception): | |
'''Base class for SPS tool exceptions''' | |
pass | |
@@ -34,9 +55,9 @@ | |
'''Run SPSInfo tool''' | |
try: | |
if use_old: | |
- info = subprocess.check_output(["./spsInfoLinux64_3", "-NOCOLORS"]).strip() | |
+ info = check_output(["./spsInfoLinux64_3", "-NOCOLORS"]).strip() | |
else: | |
- info = subprocess.check_output(["./spsInfoLinux64", "-NOCOLORS"]).strip() | |
+ info = check_output(["./spsInfoLinux64", "-NOCOLORS"]).strip() | |
#with open("sample.output", 'r') as content_file: | |
# info = content_file.read() | |
opr_ver = "" | |
@@ -51,7 +72,8 @@ | |
break | |
if not opr_ver or not rec_ver: | |
raise SPSVerNotFound() | |
- except subprocess.CalledProcessError as the_err: | |
+ #except subprocess.CalledProcessError as the_err: | |
+ except CalledProcessError as the_err: | |
if the_err.returncode == 244: | |
raise SPSUnsupportedError() | |
raise SPSToolError(the_err.returncode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Copyright (C) 2017, Intel Corporation. All rights reserved. | |
SPS tool runner | |
""" | |
import subprocess | |
import errno | |
import glob | |
class CalledProcessError(Exception): | |
def __init__(self, returncode, cmd, output=None): | |
self.returncode = returncode | |
self.cmd = cmd | |
self.output = output | |
def __str__(self): | |
return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) | |
def check_output(*popenargs, **kwargs): | |
if 'stdout' in kwargs: | |
raise ValueError('stdout argument not allowed, it will be overridden.') | |
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) | |
output, unused_err = process.communicate() | |
retcode = process.poll() | |
if retcode: | |
cmd = kwargs.get("args") | |
if cmd is None: | |
cmd = popenargs[0] | |
raise CalledProcessError(retcode, cmd, output=output) | |
return output | |
class SPSError(Exception): | |
'''Base class for SPS tool exceptions''' | |
pass | |
class SPSToolError(SPSError): | |
'''SPS tool errors''' | |
def __init__(self, ret): | |
SPSError.__init__(self) | |
self.ret = ret | |
class SPSUnsupportedError(SPSError): | |
'''Platform is unsupported''' | |
pass | |
class SPSVerNotFound(SPSError): | |
'''No version found''' | |
pass | |
class SPSNoTool(SPSError): | |
'''Can't execute tool''' | |
pass | |
def run_tool(use_old=False): | |
'''Run SPSInfo tool''' | |
try: | |
if use_old: | |
info = check_output(["./spsInfoLinux64_3", "-NOCOLORS"]).strip() | |
else: | |
info = check_output(["./spsInfoLinux64", "-NOCOLORS"]).strip() | |
#with open("sample.output", 'r') as content_file: | |
# info = content_file.read() | |
opr_ver = "" | |
rec_ver = "" | |
for line in info.split("\n"): | |
if "SPS Image FW version" in line: | |
ver0 = line.split(':')[1].split(',') | |
ver1 = ver0[1].strip().split(' ')[0] | |
ver2 = ver0[0].strip().split(' ')[0] | |
opr_ver = ver1.split('.') | |
rec_ver = ver2.split('.') | |
break | |
if not opr_ver or not rec_ver: | |
raise SPSVerNotFound() | |
#except subprocess.CalledProcessError as the_err: | |
except CalledProcessError as the_err: | |
if the_err.returncode == 244: | |
raise SPSUnsupportedError() | |
raise SPSToolError(the_err.returncode) | |
except OSError as the_err: | |
if the_err.errno == errno.ENOENT: | |
raise SPSNoTool() | |
raise | |
if int(opr_ver[0]) != 4 and int(rec_ver[0]) != 4: | |
code = glob.NOTVULNERABLE | |
else: | |
if int(opr_ver[2]) < 4 or int(rec_ver[2]) < 4: | |
code = glob.DISCOVERY_VULNERABLE | |
else: | |
code = glob.DISCOVERY_NOT_VULNERABLE_PATCHED | |
family = glob.SPS | |
ver_str = "%s.%s.%s.%s (Operational) %s.%s.%s.%s (Recovery)" % \ | |
(opr_ver[0], opr_ver[1], opr_ver[2], opr_ver[3], | |
rec_ver[0], rec_ver[1], rec_ver[2], rec_ver[3]) | |
return ver_str, code, family |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment