Code: Select all
import telnetlib
import sys
import re
import json
import time
import subprocess
tn = telnetlib.Telnet('127.0.0.1', 36330)
tn.read_until(b'> ')
tn.write(b'slot-info\n')
slotinfo = tn.read_until(b'> ').decode('ascii').strip()
slotinfo = slotinfo.replace('\n', '').replace(' False', ' 0').replace(' True', ' 1')
m = re.fullmatch('PyON \d+ slots.*?(\[.*\]).*--->', slotinfo)
slotinfo = json.loads(m.group(1)) if m else []
for slot in slotinfo:
if slot['status'] != 'PAUSED':
print(f'Finish slot {slot["id"]}')
tn.write(f'finish {slot["id"]}\n'.encode('ascii'))
tn.read_until(b'> ')
while True:
tn.write(b'queue-info\n')
queueinfo = tn.read_until(b'> ').decode('ascii').strip()
queueinfo = queueinfo.replace('\n', '').replace(' False', ' 0').replace(' True', ' 1')
m = re.fullmatch('PyON \d+ units.*?(\[.*\]).*--->', queueinfo)
queueinfo = json.loads(m.group(1)) if m else []
if not queueinfo:
print('No more entries in queue, shutting down')
subprocess.Popen(['shutdown', '/s', '/t', '0', '/c', 'FAH jobs finished']).communicate()
sys.exit(0)
print(f'Waiting for {len(queueinfo)} job(s) to finish')
time.sleep(30)
For those who want to run this script on Windows: you'll need Python (obviously): https://www.activestate.com/products/python/downloads/