Commit 2ba2f223 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by operator for beamline

eiger_flash: add log file creation (-o) for main and per-module outputs

parent 52489f01
......@@ -17,6 +17,24 @@ from gevent.event import Event
from gevent.subprocess import Popen, PIPE, STDOUT
from gevent.fileobject import FileObject
log_file_name = None
log_file = None
start_time = time.ctime()
def log(*args):
global log_file
msg = ' '.join(args) + '\n'
if log_file_name:
if not log_file:
log_exists = os.path.exists(log_file_name)
log_file = open(log_file_name, 'at')
if log_exists:
log_file.write('\n')
log_file.write(msg)
log_file.flush()
sys.stdout.write(msg)
sys.stdout.flush()
def get_eiger_executables_dir():
this_script = sys.argv[0]
......@@ -38,7 +56,7 @@ def get_route_table():
route_re_str = route_re_format.format(ip_re_str, '[^ \t]+', '[ \t]+')
route_re = re.compile(route_re_str)
route_table = []
for l in route.stdout.readlines():
for l in route.stdout:
m = route_re.match(l.strip())
if m:
d = dict([(k, m.group(k)) for k in route_str_group])
......@@ -78,10 +96,10 @@ class DirectEthernetConnection(Connection):
self.iface = ifaces[0]
def restart(self):
print '[%s] Disabling %s ...' % (self.name, self.iface)
log('[%s] Disabling %s ...' % (self.name, self.iface))
os.system('sudo ifconfig %s down' % self.iface)
gevent.sleep(4)
print '[%s] Enabling %s ...' % (self.name, self.iface)
log('[%s] Enabling %s ...' % (self.name, self.iface))
os.system('sudo ifconfig %s up' % self.iface)
......@@ -116,25 +134,25 @@ class ExtremeSwitchConnection(Connection):
@classmethod
def restart_extreme_ports(klass):
sw_name = klass.ExtremeSwitch
print 'Connecting to Extreme Switch %s ...' % sw_name
log('Connecting to Extreme Switch %s ...' % sw_name)
sw = Telnet(sw_name)
sw.expect(['login:[ \\t]*'])
sw.write('%s\n' % klass.Login)
sw.expect(['password:[ \\t]*'])
sw.write('%s\n' % klass.Password)
print 'Restarting all ports ...'
log('Restarting all ports ...')
sw.expect([klass.Prompt])
sw.write('disable ports all\n')
sw.expect([klass.Prompt])
sw.write('enable ports all\n')
print 'Quitting ...'
log('Quitting ...')
sw.expect([klass.Prompt])
sw.write('quit\n')
sw.expect(['Do you wish to save your config.+\\(y/N\\) '])
sw.write('\n')
print 'Done!'
log('Done!')
class EigerHalfModule:
......@@ -171,10 +189,24 @@ class EigerHalfModule:
self.xterm = None
self.in_flash_mode = None
self.con_out_task = None
self.con_file = None
self.con_pipe_os = None
self.con_pipe = None
self.log_file_name = None
self.log_file_os = None
self.log_file = None
if log_file_name:
base, ext = log_file_name, ''
if '.' in log_file_name:
token = log_file_name.split('.')
base, ext = map('.'.join, [token[:-1], ['', token[-1]]])
self.log_file_name = '%s_%s%s' % (base, self.name, ext)
def __del__(self):
if self.log_file:
self.log_file.close()
self.log_file_os.close()
if self.nc:
self.nc.terminate()
......@@ -363,10 +395,10 @@ class EigerHalfModule:
self.log('Checking flash-mode setup files ...')
cmd = md5cmd(self.LocalFlashModeDir)
local_md5 = Popen(cmd, shell=True, stdout=PIPE)
local_md5lines = [l.split() for l in local_md5.stdout.readlines()]
local_md5lines = [l.split() for l in local_md5.stdout]
cmd = md5cmd(self.RemoteFlashModeDir)
rem_md5 = self.ssh_cmd(cmd)
rem_md5lines = [l.split() for l in rem_md5.stdout.readlines()]
rem_md5lines = [l.split() for l in rem_md5.stdout]
if rem_md5lines == local_md5lines:
return
......@@ -421,6 +453,10 @@ class EigerHalfModule:
except:
self.close_xterm()
if self.log_file:
self.log_file.write('%s\n' % l)
self.log_file.flush()
self.flash_end_ts = time.time()
def start_xterm(self):
......@@ -432,17 +468,28 @@ class EigerHalfModule:
self.log('Executing: %s' % ' '.join(xterm_cmd))
self.xterm = Popen(xterm_cmd)
self.con_file = open(self.con_pipe_name, 'at')
self.con_pipe = FileObject(self.con_file, 'at')
self.con_pipe.write('*** Output from %s console ***\n' % self.name)
self.con_pipe_os = open(self.con_pipe_name, 'at')
self.con_pipe = FileObject(self.con_pipe_os, 'at')
msg = '*** Output from %s console ***\n' % self.name
self.con_pipe.write(msg)
self.con_pipe.flush()
if self.log_file_name:
log_exists = os.path.exists(self.log_file_name)
self.log_file_os = open(self.log_file_name, 'at')
self.log_file = FileObject(self.log_file_os, 'at')
if log_exists:
self.log_file.write('\n')
self.log_file.write('Eiger flash - %s\n' % start_time)
self.log_file.write(msg)
self.log_file.flush()
def close_xterm(self):
if self.con_pipe:
self.con_pipe.close()
self.con_pipe = None
self.con_file.close()
self.con_file = None
self.con_pipe_os.close()
self.con_pipe_os = None
if self.xterm:
self.xterm.terminate()
......@@ -453,7 +500,7 @@ class EigerHalfModule:
return dict(size=size)
def log(self, msg):
print('[%s] %s' % (self.name, msg))
log('[%s] %s' % (self.name, msg))
class Eiger:
......@@ -472,12 +519,12 @@ class Eiger:
])
def __init__(self, host_list):
self.upload_tasks = []
name_klass_list = [(h, k) for h in host_list
for n, (hl, k) in self.Detectors.items() if h in hl]
if not name_klass_list:
name_klass_list = [(h, DirectEthernetConnection) for h in host_list]
self.hosts = map(EigerHalfModule, name_klass_list)
self.upload_tasks = []
def __del__(self):
self.wait_tasks()
......@@ -509,14 +556,14 @@ def flash_eiger_list(fw_files, host_list):
for fw_type, fw_name in fw_files:
eiger.upload_fw(fw_type, fw_name)
except Exception, e:
print e
log(str(e))
raw_input('Press any key to quit ...')
def heart_beat(msg):
while True:
print msg
log(msg)
gevent.sleep(1)
......@@ -530,10 +577,12 @@ def usage():
print " -l <feb_l.bit file>"
print " -r <feb_r.bit file>"
print " -k <kernel_local file>"
print " -o <output log file>"
exit(1)
def main():
global log_file_name
globals().update([(n, getattr(EigerHalfModule, n))
for n in ['MAIN_BIT', 'LEFT_BIT', 'RIGHT_BIT',
'KERNEL_LOCAL']])
......@@ -546,11 +595,13 @@ def main():
KERNEL_LOCAL: '-k'}
try:
opts, args = getopt(sys.argv[1:], 'm:l:r:k:b')
opts, args = getopt(sys.argv[1:], 'm:l:r:k:o:b')
for opt, val in opts:
for fw_type, fw_opt in fw_opts.items():
if opt == fw_opt:
fw_files[fw_type] = val
if opt == '-o':
log_file_name = val
if opt == '-b':
beat = True
......@@ -565,6 +616,18 @@ def main():
except:
usage()
log('Eiger flash - %s' % start_time)
file_names = list(zip(*fw_files)[1])
md5sum = Popen(['md5sum'] + file_names, stdout=PIPE, stderr=STDOUT)
for l in md5sum.stdout:
log(l[:-1])
md5sum.wait()
if md5sum.returncode != 0:
log('Error in md5sum %s' % ' '.join(file_names))
exit(1)
log('')
if beat:
gevent.spawn(heart_beat, "Beat")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment