1212_THIS_DIR = os .path .dirname (os .path .realpath (__file__ ))
1313_PID_FILE = os .path .join (_THIS_DIR , "supervisord.pid" )
1414_CONF_FILE = os .path .join (_THIS_DIR , "supervisord.conf" )
15- _SUPERVISORD = os .path .join (os .path .dirname (sys .executable ), "supervisord" )
16- _RQ = os .path .join (os .path .dirname (sys .executable ), "rq" )
15+ _SUPERVISORD = shutil . which ( os .path .join (os .path .dirname (sys .executable ), "supervisord" )) or shutil . which ( "supervisord" )
16+ _RQ = shutil . which ( os .path .join (os .path .dirname (sys .executable ), "rq" )) or shutil . which ( "rq" )
1717
1818SECONDS_PER_DAY = 86400
1919
@@ -48,13 +48,13 @@ def redis_connection() -> redis.Redis:
4848 return redis .Redis .from_url (config ["redis_url" ], decode_responses = True )
4949
5050
51- def create_enqueuer_wrapper ():
51+ def create_enqueuer_wrapper (rq ):
5252 with open (_CONF_FILE , "w" ) as f :
5353 f .write (HEADER )
5454 for worker_data in config ["workers" ]:
5555 c = CONTENT .format (
5656 worker_user = worker_data ["user" ],
57- rq = _RQ ,
57+ rq = rq ,
5858 worker_args = f'--url { config ["redis_url" ]} ' ,
5959 queues = " " .join (worker_data ["queues" ]),
6060 numprocs = 1 ,
@@ -63,9 +63,9 @@ def create_enqueuer_wrapper():
6363 f .write (c )
6464
6565
66- def start (extra_args ):
67- create_enqueuer_wrapper ()
68- subprocess .run ([_SUPERVISORD , "-c" , _CONF_FILE , * extra_args ], check = True , cwd = _THIS_DIR )
66+ def start (rq , supervisord , extra_args ):
67+ create_enqueuer_wrapper (rq )
68+ subprocess .run ([supervisord , "-c" , _CONF_FILE , * extra_args ], check = True , cwd = _THIS_DIR )
6969
7070
7171def stop ():
@@ -77,8 +77,8 @@ def stop():
7777 sys .stderr .write ("supervisor is already stopped" )
7878
7979
80- def stat (extra_args ):
81- subprocess .run ([_RQ , "info" , "--url" , config ["redis_url" ], * extra_args ], check = True )
80+ def stat (rq , extra_args ):
81+ subprocess .run ([rq , "info" , "--url" , config ["redis_url" ], * extra_args ], check = True )
8282
8383
8484def clean (age , dry_run ):
@@ -98,14 +98,21 @@ def clean(age, dry_run):
9898 shutil .rmtree (dir_path )
9999
100100
101+ def _exec_type (path ):
102+ exec_path = shutil .which (path )
103+ if exec_path :
104+ return exec_path
105+ raise argparse .ArgumentTypeError (f"no executable found at: '{ path } '" )
106+
107+
101108if __name__ == "__main__" :
102109 parser = argparse .ArgumentParser ()
103110 subparsers = parser .add_subparsers (dest = "command" )
104111
105- subparsers .add_parser ("start" , help = "start the autotester" )
112+ start_parser = subparsers .add_parser ("start" , help = "start the autotester" )
106113 subparsers .add_parser ("stop" , help = "stop the autotester" )
107- subparsers .add_parser ("restart" , help = "restart the autotester" )
108- subparsers .add_parser ("stat" , help = "display current status of the autotester queues" )
114+ restart_parser = subparsers .add_parser ("restart" , help = "restart the autotester" )
115+ stat_parser = subparsers .add_parser ("stat" , help = "display current status of the autotester queues" )
109116 clean_parser = subparsers .add_parser ("clean" , help = "clean up old/unused test scripts" )
110117
111118 clean_parser .add_argument (
@@ -115,15 +122,25 @@ def clean(age, dry_run):
115122 "-d" , "--dry-run" , action = "store_true" , help = "list files that will be deleted without actually removing them"
116123 )
117124
125+ for parser_ in (start_parser , restart_parser , stat_parser ):
126+ parser_ .add_argument ("--rq" , default = _RQ , type = _exec_type , help = f"path to rq executable, default={ _RQ } " )
127+ if parser_ is not stat_parser :
128+ parser_ .add_argument (
129+ "--supervisord" ,
130+ default = _SUPERVISORD ,
131+ type = _exec_type ,
132+ help = f"path to supervisord executable, default={ _SUPERVISORD } " ,
133+ )
134+
118135 args , remainder = parser .parse_known_args ()
119136 if args .command == "start" :
120- start (remainder )
137+ start (args . rq , args . supervisord , remainder )
121138 elif args .command == "stop" :
122139 stop ()
123140 elif args .command == "restart" :
124141 stop ()
125- start (remainder )
142+ start (args . rq , args . supervisord , remainder )
126143 elif args .command == "stat" :
127- stat (remainder )
144+ stat (args . rq , remainder )
128145 elif args .command == "clean" :
129146 clean (args .age , args .dry_run )
0 commit comments