Skip to content

Commit 3ac10d0

Browse files
committed
Python2 compatibility
1 parent dc2e11c commit 3ac10d0

3 files changed

Lines changed: 17 additions & 30 deletions

File tree

cortexutils/analyzer.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ def build_artifact(self, data_type, data, **kwargs):
7979
(dst, filename) = tempfile.mkstemp(dir=os.path.join(self.job_directory, "output"))
8080
with open(data, 'r') as src:
8181
copyfileobj(src, os.fdopen(dst, 'w'))
82-
return {**kwargs, 'dataType': data_type, 'file': ntpath.basename(filename),
83-
'filename': ntpath.basename(data)}
82+
kwargs.update({'dataType': data_type, 'file': ntpath.basename(filename),
83+
'filename': ntpath.basename(data)})
84+
return kwargs
8485
else:
85-
return {**kwargs, 'dataType': data_type, 'data': data}
86+
kwargs.update({'dataType': data_type, 'data': data})
87+
return kwargs
8688

8789
def report(self, full_report, ensure_ascii=False):
8890
"""Returns a json dict via stdout.
@@ -96,15 +98,12 @@ def report(self, full_report, ensure_ascii=False):
9698
except Exception:
9799
pass
98100

99-
report = {
101+
super(Analyzer, self).report({
100102
'success': True,
101103
'summary': summary,
102104
'artifacts': self.artifacts(full_report),
103105
'full': full_report
104-
}
105-
os.makedirs('%s/output' % self.job_directory, exist_ok=True)
106-
with open('%s/output/output.json' % self.job_directory, mode='w') as f_output:
107-
json.dump(report, f_output, ensure_ascii=ensure_ascii)
106+
}, ensure_ascii)
108107

109108
def run(self):
110109
"""Overwritten by analyzers"""

cortexutils/responder.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@ def report(self, full_report, ensure_ascii=False):
5151
operation_list = self.operations(full_report)
5252
except Exception:
5353
pass
54-
55-
report = {
54+
super(Responder, self).report({
5655
'success': True,
5756
'full': full_report,
5857
'operations': operation_list
59-
}
60-
os.makedirs('%s/output' % self.job_directory, exist_ok=True)
61-
with open('%s/output/output.json' % self.job_directory, mode='w') as f_output:
62-
json.dump(report, f_output, ensure_ascii=ensure_ascii)
58+
}, ensure_ascii)
6359

6460
def run(self):
6561
"""Overwritten by responders"""

cortexutils/worker.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import select
99

1010

11-
class Worker:
11+
class Worker(object):
1212
READ_TIMEOUT = 3 # seconds
1313

1414
def __init__(self, job_directory):
@@ -115,7 +115,10 @@ def __write_output(self, data, ensure_ascii=False):
115115
if self.job_directory is None:
116116
json.dump(data, sys.stdout, ensure_ascii=ensure_ascii)
117117
else:
118-
os.makedirs('%s/output' % self.job_directory, exist_ok=True)
118+
try:
119+
os.makedirs('%s/output' % self.job_directory)
120+
except:
121+
pass
119122
with open('%s/output/output.json' % self.job_directory, mode='w') as f_output:
120123
json.dump(data, f_output, ensure_ascii=ensure_ascii)
121124

@@ -166,24 +169,13 @@ def summary(self, raw):
166169
def artifacts(self, raw):
167170
return []
168171

169-
def report(self, full_report, ensure_ascii=False):
172+
def report(self, output, ensure_ascii=False):
170173
"""Returns a json dict via stdout.
171174
172-
:param full_report: Analyzer results as dict.
175+
:param output: worker output.
173176
:param ensure_ascii: Force ascii output. Default: False"""
174177

175-
summary = {}
176-
try:
177-
summary = self.summary(full_report)
178-
except Exception:
179-
pass
180-
181-
self.__write_output({
182-
'success': True,
183-
'summary': summary,
184-
'artifacts': self.artifacts(full_report),
185-
'full': full_report
186-
}, ensure_ascii=ensure_ascii)
178+
self.__write_output(output, ensure_ascii=ensure_ascii)
187179

188180
def run(self):
189181
"""Overwritten by analyzers"""

0 commit comments

Comments
 (0)