1
1
#!/usr/bin/python
2
2
3
- __author__ = 'Ondrej Mular <[email protected] >'
4
-
5
3
import sys
6
4
import os
7
5
import subprocess
8
6
import logging
9
7
import json
10
8
import time
11
9
12
- INPUT_FILE = os .environ ["BINMITM_INPUT" ] if "BINMITM_INPUT" in os .environ else None
13
- OUTPUT_FILE = os .environ ["BINMITM_OUTPUT" ] if "BINMITM_OUTPUT" in os .environ else None
14
- COUNTER_FILE = os .environ ["BINMITM_COUNTER_FILE" ] if "BINMITM_COUNTER_FILE" in os .environ else ".binmitm_counter"
15
- ENV_VARS = os .environ ["BINMITM_ENV" ].split ("," ) if "BINMITM_ENV" in os .environ else []
10
+ INPUT_FILE = os .environ ["BINMITM_INPUT" ]\
11
+ if "BINMITM_INPUT" in os .environ else None
12
+ OUTPUT_FILE = os .environ ["BINMITM_OUTPUT" ]\
13
+ if "BINMITM_OUTPUT" in os .environ else None
14
+ COUNTER_FILE = os .environ ["BINMITM_COUNTER_FILE" ]\
15
+ if "BINMITM_COUNTER_FILE" in os .environ else ".binmitm_counter"
16
+ ENV_VARS = os .environ ["BINMITM_ENV" ].split ("," )\
17
+ if "BINMITM_ENV" in os .environ else []
16
18
IGNORE_TIMING = "BINMITM_IGNORE_TIMING" in os .environ
17
19
DEBUG = "BINMITM_DEBUG" in os .environ
18
20
@@ -22,18 +24,24 @@ def log_debug(msg):
22
24
logging .error (msg )
23
25
24
26
25
- def exit (ret ):
27
+ def _exit (ret ):
26
28
if COUNTER_FILE and os .path .isfile (COUNTER_FILE ):
27
29
try :
28
30
os .remove (COUNTER_FILE )
29
31
except Exception as e :
30
- log_debug ("Unable to delete counter file (%s): %s" % (COUNTER_FILE , e .message ))
32
+ log_debug ("Unable to delete counter file ({0}): {1}" .format (
33
+ COUNTER_FILE , e .message
34
+ ))
31
35
try :
32
36
with open (COUNTER_FILE , "w" ) as f :
33
37
f .write ("0\n " )
34
- log_debug ("0 written into counter file (%s)" % COUNTER_FILE )
38
+ log_debug (
39
+ "0 written into counter file ({0})" .format (COUNTER_FILE )
40
+ )
35
41
except Exception as e :
36
- log_debug ("Unable to write 0 to counter file (%s): %s" % (COUNTER_FILE , e .message ))
42
+ log_debug ("Unable to write 0 to counter file ({0}): {1}" .format (
43
+ COUNTER_FILE , e .message
44
+ ))
37
45
sys .exit (ret )
38
46
39
47
@@ -45,41 +53,52 @@ def get_count(max_count):
45
53
count = int (f .readline ().strip ())
46
54
f .seek (0 )
47
55
f .truncate ()
48
- f .write ("%d \n " % ((count + 1 )% max_count ))
56
+ f .write ("{0} \n " . format ((count + 1 ) % max_count ))
49
57
except Exception as e :
50
- log_debug ("Unable to read/write from/to '%s': %s" % (COUNTER_FILE , e .message ))
58
+ log_debug ("Unable to read/write from/to '{0}': {1}" .format (
59
+ COUNTER_FILE , e .message
60
+ ))
51
61
else :
52
62
if max_count != 1 :
53
63
try :
54
64
with open (COUNTER_FILE , "w" ) as f :
55
65
f .write ("1\n " )
56
66
except Exception as e :
57
- log_debug ("Unable to write to '%s': %s" % (COUNTER_FILE , e .message ))
67
+ log_debug ("Unable to write to '{0}': {1}" .format (
68
+ COUNTER_FILE , e .message
69
+ ))
58
70
return count
59
71
60
72
61
73
def record (argv ):
62
74
output = OUTPUT_FILE and not INPUT_FILE
63
75
env = os .environ .copy ()
64
76
65
- cur_output = {}
66
- cur_output ["request" ] = {}
67
- cur_output ["response" ] = {}
68
- cur_output ["time" ] = {}
69
- cur_output ["request" ]["argv" ] = argv
70
- cur_output ["request" ]["env" ] = {}
77
+ cur_output = {
78
+ "request" : {
79
+ "argv" : argv ,
80
+ "env" : {}
81
+ },
82
+ "response" : {},
83
+ "time" : {}
84
+ }
71
85
72
86
for e in ENV_VARS :
73
87
if e in env :
74
88
cur_output ["request" ]["env" ][e ] = env [e ]
75
89
76
90
proc_start_time = time .time ()
91
+ process = None
77
92
78
93
try :
79
- process = subprocess .Popen (argv , stdout = subprocess .PIPE , stderr = subprocess .PIPE , env = env )
94
+ process = subprocess .Popen (
95
+ argv , stdout = subprocess .PIPE , stderr = subprocess .PIPE , env = env
96
+ )
80
97
except OSError as e :
81
- log_debug ("Unable to run command '%s': %s" % (" " .join (argv ), e .message ))
82
- exit (127 )
98
+ log_debug ("Unable to run command '{0}': {1}" .format (
99
+ " " .join (argv ), e .message
100
+ ))
101
+ _exit (127 )
83
102
84
103
status = process .wait ()
85
104
cur_output ["time" ]["perform_duration" ] = time .time () - proc_start_time
@@ -104,7 +123,10 @@ def record(argv):
104
123
try :
105
124
output_values = json .load (f )
106
125
except :
107
- log_debug ("Parsing output file '%s' failed. It is considered as empty." % OUTPUT_FILE )
126
+ log_debug (
127
+ "Parsing output file '{0}' failed. It is "
128
+ "considered as empty." .format (OUTPUT_FILE )
129
+ )
108
130
109
131
output_values .append (cur_output )
110
132
f .truncate ()
@@ -113,11 +135,10 @@ def record(argv):
113
135
else :
114
136
with open (OUTPUT_FILE , "w" ) as f :
115
137
json .dump ([cur_output ], f , indent = 4 )
116
- except ValueError as e :
117
- log_debug ("Unable to parse output file: %s" % e .message )
118
- except IOError as e :
119
- log_debug ("Unable to open output file '%s': %s" % (OUTPUT_FILE , e .message ))
120
-
138
+ except (IOError , ValueError ) as e :
139
+ log_debug ("Unable to store data to file '{0}': {1}" .format (
140
+ OUTPUT_FILE , e .message
141
+ ))
121
142
sys .exit (status )
122
143
123
144
@@ -129,45 +150,54 @@ def replay(argv):
129
150
try :
130
151
with open (INPUT_FILE ) as f :
131
152
input_values = json .load (f )
132
- except ValueError as e :
133
- log_debug ("Unable to parse input file: %s" % e . message )
134
- except IOError as e :
135
- log_debug ( "Unable to open input file '%s': %s" % ( INPUT_FILE , e . message ))
153
+ except ( ValueError , IOError ) as e :
154
+ log_debug ("Unable to parse input file '{0}': {1}" . format (
155
+ OUTPUT_FILE , e . message
156
+ ))
136
157
137
158
if not input_values :
138
159
log_debug ("Empty input" )
139
- exit (127 )
160
+ _exit (127 )
140
161
141
162
input_number = get_count (len (input_values ))
142
163
if input_number >= len (input_values ):
143
164
log_debug ("Unable to get current input" )
144
- exit (127 )
165
+ _exit (127 )
145
166
146
167
cur_input = input_values [input_number ]
147
168
if cur_input ["request" ]["argv" ] != argv :
148
- log_debug ("Expected different command (Expected: '%s', Given: '%s')" % (" " .join (cur_input ["request" ]["argv" ]), " " .join (argv )))
149
- exit (127 )
169
+ log_debug (
170
+ "Expected different command (Expected: '{0}', Given: '{1}')" .format (
171
+ " " .join (cur_input ["request" ]["argv" ]),
172
+ " " .join (argv )
173
+ )
174
+ )
175
+ _exit (127 )
150
176
151
177
env .update (cur_input ["request" ]["env" ])
152
178
sys .stderr .write (cur_input ["response" ]["stderr" ])
153
179
sys .stdout .write (cur_input ["response" ]["stdout" ])
154
180
155
181
if not IGNORE_TIMING :
156
- time_left = cur_input ["time" ]["perform_duration" ] - (time .time () - start_time )
182
+ time_left = cur_input ["time" ]["perform_duration" ] - (
183
+ time .time () - start_time
184
+ )
157
185
158
186
if time_left > 0 :
159
- log_debug ("Sleeping for %f s" % time_left )
187
+ log_debug ("Sleeping for {0} s" . format ( time_left ) )
160
188
time .sleep (time_left )
161
189
else :
162
- log_debug ("Uooops! We are runnig %f s longer." % abs (time_left ))
190
+ log_debug ("Uooops! We are running {0} s longer." .format (
191
+ abs (time_left )
192
+ ))
163
193
164
194
sys .exit (cur_input ["response" ]["return_code" ])
165
195
166
196
167
197
def main (argv ):
168
198
if not argv :
169
199
print "No command to run"
170
- exit (127 )
200
+ _exit (127 )
171
201
if INPUT_FILE :
172
202
replay (argv )
173
203
else :
@@ -176,4 +206,3 @@ def main(argv):
176
206
177
207
if __name__ == "__main__" :
178
208
main (sys .argv [1 :])
179
-
0 commit comments