11#!/usr/bin/env python
22# encoding: utf-8
3+
34import os
45import sys
56import codecs
67import json
8+ import select
79
810
9- class Worker :
10-
11- def __init__ (self ):
12- self .__set_encoding ()
13-
14- # Prepare in/out/err streams
15- self .fperror = sys .stderr
16- self .fpinput = sys .stdin
17- self .fpoutput = sys .stdout
11+ class Worker (object ):
12+ READ_TIMEOUT = 3 # seconds
1813
14+ def __init__ (self , job_directory ):
15+ if job_directory is None :
16+ if len (sys .argv ) > 1 :
17+ job_directory = sys .argv [1 ]
18+ else :
19+ job_directory = '/job'
20+ self .job_directory = job_directory
1921 # Load input
20- self ._input = json .load (self .fpinput )
22+ self ._input = {}
23+ if os .path .isfile ('%s/input/input.json' % self .job_directory ):
24+ with open ('%s/input/input.json' % self .job_directory ) as f_input :
25+ self ._input = json .load (f_input )
26+ else : # If input file doesn't exist, fallback to old behavior and read input from stdin
27+ self .job_directory = None
28+ self .__set_encoding ()
29+ r , w , e = select .select ([sys .stdin ], [], [], self .READ_TIMEOUT )
30+ if sys .stdin in r :
31+ self ._input = json .load (sys .stdin )
32+ else :
33+ self .error ('Input file doesn' 't exist' )
2134
2235 # Set parameters
2336 self .data_type = self .get_param ('dataType' , None , 'Missing dataType field' )
@@ -98,10 +111,21 @@ def __check_pap(self):
98111
99112 return not (self .enable_check_pap and self .pap > self .max_pap )
100113
114+ def __write_output (self , data , ensure_ascii = False ):
115+ if self .job_directory is None :
116+ json .dump (data , sys .stdout , ensure_ascii = ensure_ascii )
117+ else :
118+ try :
119+ os .makedirs ('%s/output' % self .job_directory )
120+ except :
121+ pass
122+ with open ('%s/output/output.json' % self .job_directory , mode = 'w' ) as f_output :
123+ json .dump (data , f_output , ensure_ascii = ensure_ascii )
124+
101125 def get_data (self ):
102126 """Wrapper for getting data from input dict.
103127
104- :return: Data (observable value) given through Cortex"""
128+ :return: Data (observable value) given through Cortex"""
105129 return self .get_param ('data' , None , 'Missing data field' )
106130
107131 def get_param (self , name , default = None , message = None ):
@@ -128,34 +152,30 @@ def error(self, message, ensure_ascii=False):
128152 if 'api_key' in analyzer_input .get ('config' , {}):
129153 analyzer_input ['config' ]['api_key' ] = 'REMOVED'
130154
131- json .dump ({'success' : False ,
132- 'input' : analyzer_input ,
133- 'errorMessage' : message },
134- self .fpoutput ,
135- ensure_ascii = ensure_ascii )
155+ self .__write_output ({'success' : False ,
156+ 'input' : analyzer_input ,
157+ 'errorMessage' : message },
158+ ensure_ascii = ensure_ascii )
136159
137160 # Force exit after error
138161 sys .exit (1 )
139162
140- def report (self , full_report , ensure_ascii = False ):
163+ def summary (self , raw ):
164+ """Returns a summary, needed for 'short.html' template. Overwrite it for your needs!
165+
166+ :returns: by default return an empty dict"""
167+ return {}
168+
169+ def artifacts (self , raw ):
170+ return []
171+
172+ def report (self , output , ensure_ascii = False ):
141173 """Returns a json dict via stdout.
142174
143- :param full_report: Analyzer results as dict .
175+ :param output: worker output .
144176 :param ensure_ascii: Force ascii output. Default: False"""
145177
146- summary = {}
147- try :
148- summary = self .summary (full_report )
149- except Exception :
150- pass
151-
152- report = {
153- 'success' : True ,
154- 'summary' : summary ,
155- 'artifacts' : self .artifacts (full_report ),
156- 'full' : full_report
157- }
158- json .dump (report , self .fpoutput , ensure_ascii = ensure_ascii )
178+ self .__write_output (output , ensure_ascii = ensure_ascii )
159179
160180 def run (self ):
161181 """Overwritten by analyzers"""
0 commit comments