@@ -5,7 +5,10 @@ import os
5
5
import sys
6
6
import subprocess
7
7
import zmq
8
- import ConfigParser
8
+ try :
9
+ import configparser
10
+ except ImportError :
11
+ import configparser as ConfigParser
9
12
import logging
10
13
11
14
import threebot_crypto
@@ -27,15 +30,15 @@ LOGFILE = os.path.join(BASEDIR, '3bot.log')
27
30
LOGLEVEL = 'ERROR'
28
31
29
32
30
- Config = ConfigParser .ConfigParser ()
33
+ Config = configparser .ConfigParser ()
31
34
32
35
if os .path .isfile (CONFIGFILE ):
33
36
Config .read (CONFIGFILE )
34
37
else :
35
- print "No configfile found in: '%s'" % CONFIGFILE
36
- print "----"
37
- print "Creating basic configfile in '%s'" % CONFIGFILE
38
- print "----"
38
+ print ( "No configfile found in: '%s'" % CONFIGFILE )
39
+ print ( "----" )
40
+ print ( "Creating basic configfile in '%s'" % CONFIGFILE )
41
+ print ( "----" )
39
42
40
43
os .makedirs (BASEDIR )
41
44
cfgfile = open (CONFIGFILE , 'w' )
@@ -44,10 +47,10 @@ else:
44
47
Config .add_section ('3bot-settings' )
45
48
Config .set ('3bot-settings' , 'BOT_ENDPOINT' , '*' )
46
49
47
- port = raw_input ('Enter PORT: ' )
50
+ port = input ('Enter PORT: ' )
48
51
Config .set ('3bot-settings' , 'PORT' , port )
49
52
50
- sec_key = raw_input ('Enter SECRET_KEY: ' )
53
+ sec_key = input ('Enter SECRET_KEY: ' )
51
54
Config .set ('3bot-settings' , 'SECRET_KEY' , sec_key )
52
55
53
56
Config .write (cfgfile )
59
62
BOT = Config .get ('3bot-settings' , 'BOT_ENDPOINT' )
60
63
PORT = Config .get ('3bot-settings' , 'PORT' )
61
64
except :
62
- print "Invalid config file in: '%s'. Could not find BOT or PORT declaration" % CONFIGFILE
63
- print "You can find a basic config file in the documentation."
65
+ print ( "Invalid config file in: '%s'. Could not find BOT or PORT declaration" % CONFIGFILE )
66
+ print ( "You can find a basic config file in the documentation." )
64
67
sys .exit (2 )
65
68
66
69
try :
67
70
SECRET_KEY = Config .get ('3bot-settings' , 'SECRET_KEY' )
68
71
except :
69
- print "Invalid config file in: '%s'. Could not find SECRET_KEY declaration" % CONFIGFILE
70
- print "You can find a basic config file in the documentation."
72
+ print ( "Invalid config file in: '%s'. Could not find SECRET_KEY declaration" % CONFIGFILE )
73
+ print ( "You can find a basic config file in the documentation." )
71
74
sys .exit (2 )
72
75
73
76
try :
74
77
DRY_RUN = Config .getboolean ('3bot-settings' , 'DRY_RUN' )
75
- except ConfigParser .NoOptionError :
78
+ except configparser .NoOptionError :
76
79
DRY_RUN = False
77
80
78
81
79
82
try :
80
83
LOGFILE = Config .get ('3bot-settings' , 'LOGFILE' )
81
- except ConfigParser .NoOptionError :
84
+ except configparser .NoOptionError :
82
85
pass
83
86
84
87
try :
85
88
LOGLEVEL = Config .get ('3bot-settings' , 'LOGLEVEL' )
86
- except ConfigParser .NoOptionError :
89
+ except configparser .NoOptionError :
87
90
pass
88
91
89
92
if LOGLEVEL == 'DEBUG' :
@@ -112,10 +115,8 @@ def write_script(directory, script, body):
112
115
with open (task_path , 'w+' ) as task_file :
113
116
task_file .write (body .encode ('utf8' ).replace ('\r \n ' , '\n ' ))
114
117
logging .info ("Saving new Script file at: %s" % task_path )
115
-
116
118
# change permission
117
- os .chmod (task_path , 0755 )
118
-
119
+ os .chmod (task_path , 0o755 )
119
120
return task_path
120
121
121
122
@@ -162,7 +163,7 @@ def run_command(request):
162
163
callable = " && " .join (script_bits )
163
164
else :
164
165
callable = script_bits [0 ]
165
-
166
+
166
167
if DRY_RUN :
167
168
# dumping path to task script to stdout instead of executing it, allows to 'cat' later on the target node
168
169
response = {'stdout' : callable , 'stderr' : '' , 'exit_code' : 0 }
@@ -176,31 +177,31 @@ def run_command(request):
176
177
177
178
try :
178
179
t_stdout = p .stdout .read ()
179
- except AttributeError , e :
180
+ except AttributeError as e :
180
181
logging .info (e )
181
182
182
183
try :
183
184
t_stderr = p .stderr .read ()
184
- except AttributeError , e :
185
+ except AttributeError as e :
185
186
logging .info (e )
186
187
187
188
response = {'stdout' : t_stdout , 'stderr' : t_stderr , 'exit_code' : p .wait ()}
188
189
del p
189
-
190
+
190
191
return response
191
192
192
193
if sys .argv [1 ] in ['restart' , 'start' , 'status' , ]:
193
- print '---'
194
- print 'BASEDIR: %s' % str (BASEDIR )
195
- print 'CONFIGFILE: %s' % str (CONFIGFILE )
196
- print 'SCRIPTDIR: %s' % str (SCRIPTDIR )
197
- print 'ENDPOINT: %s' % str (BOT )
198
- print 'PORT: %s' % str (PORT )
199
- print 'LOGFILE: %s' % str (LOGFILE )
200
- print 'LOGLEVEL: %s' % str (LOGLEVEL )
201
- print 'PIDFILE: %s' % str (PIDFILE )
202
- print 'DRY_RUN: %s' % str (DRY_RUN )
203
- print '---'
194
+ print ( '---' )
195
+ print ( 'BASEDIR: %s' % str (BASEDIR ) )
196
+ print ( 'CONFIGFILE: %s' % str (CONFIGFILE ) )
197
+ print ( 'SCRIPTDIR: %s' % str (SCRIPTDIR ) )
198
+ print ( 'ENDPOINT: %s' % str (BOT ) )
199
+ print ( 'PORT: %s' % str (PORT ) )
200
+ print ( 'LOGFILE: %s' % str (LOGFILE ) )
201
+ print ( 'LOGLEVEL: %s' % str (LOGLEVEL ) )
202
+ print ( 'PIDFILE: %s' % str (PIDFILE ) )
203
+ print ( 'DRY_RUN: %s' % str (DRY_RUN ) )
204
+ print ( '---' )
204
205
205
206
206
207
class WorkerDeamon (Daemon ):
@@ -252,9 +253,9 @@ if __name__ == "__main__":
252
253
elif 'status' == sys .argv [1 ]:
253
254
daemon .status ()
254
255
else :
255
- print "Unknown command"
256
+ print ( "Unknown command" )
256
257
sys .exit (2 )
257
258
sys .exit (0 )
258
259
else :
259
- print "usage: %s start|stop|restart|status" % sys .argv [0 ]
260
+ print ( "usage: %s start|stop|restart|status" % sys .argv [0 ])
260
261
sys .exit (2 )
0 commit comments