forked from uwcms/ctp6commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·169 lines (127 loc) · 5.34 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
""" Command line interface to the CTP6. """
import argparse
import logging
import os
import sys
from termcolor import colored
import uhal
import api
log = logging.getLogger(__name__)
def do_reset(hw, args):
""" Reset the desired links.
Returns 1 on success
"""
return api.reset(hw, args.links, args.power_down)
def print_status_header(links):#output, links):
""" Print out the status header row for the desired links.
Two horizontal lines, with the link label for each link between the lines.
"""
def write_hdiv():
sys.stdout.write('----------')#output('----------')
for underline in range(len(links)):
sys.stdout.write('---')#output('---')
sys.stdout.write('\n')#output('\n')
write_hdiv()
sys.stdout.write('Flag ')#output('Flag ')
for link in links:
sys.stdout.write('%3i' % link)#output('%3i' % link)
sys.stdout.write('\n')#output('\n')
write_hdiv()
def do_status(hw, args):#, output):
""" Print out the status registers from all of the links. """
status_flags = api.status(hw, args.links)
print(status_flags)
print_status_header(args.links)#output, args.links)
for flag, flag_cfg in status_flags.iteritems():
sys.stdout.write(flag)#output(flag)
for link in args.links:
if flag_cfg['links'][link]:
sys.stdout.write(colored(' * ', 'green'))#output(colored(' * ', 'green'))
else:
sys.stdout.write(colored(' E ', 'red'))#output(colored(' E ', 'red'))
output('\n')
def do_capture(hw, args):
""" Capture an orbit of data and summarize the result. """
sys.stdout.write(' word: ')
sys.stdout.write(' '.join('%8i' % word for word in range(args.ncapture)))
sys.stdout.write('\n')
expected_word_fn = None
if args.expected:
log.info("Loading expected pattern from %s", args.expected)
mod = __import__(args.expected.replace('.py', ''))
expected_word_fn = getattr(mod, 'pattern')
capture = api.capture(hw, args.links, args.ncapture,
args.capture_char, expected_word_fn)
for link in sorted(capture.keys()):
capture_data = capture[link]
sys.stdout.write('link %5i ' % link)
result_strs = []
expected_data = capture_data['expected']
for idx, word in enumerate(capture_data['capture']):
color_fn = lambda x: x
if expected_data:
expected = expected_data[idx % len(expected_data)]
if word == expected:
color_fn = lambda x: colored(x, 'green')
else:
color_fn = lambda x: colored(x, 'red')
result_strs.append(color_fn('%8x' % word))
sys.stdout.write(' '.join(result_strs))
sys.stdout.write('\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=['reset', 'status', 'capture'],
help="Action to perform")
parser.add_argument("links", nargs='*', help="Specify links. "
"If none are specified, reset all.")
parser.add_argument("--connection",
help="Manually connection XML file. "
"If not specified, take from CTP6_CONNECTION env.")
parser.add_argument("--verbose", action='store_true',
help="Increase verbosity")
parser_rst = parser.add_argument_group('reset')
parser_stat = parser.add_argument_group('status')
parser_cap = parser.add_argument_group('capture')
# Reset arguments
parser_rst.add_argument("--power-down", action='store_true',
dest='power_down',
help="Additionally power down the links")
# Capture arguments
parser_cap.add_argument("--orbit-capture-char", dest='capture_char',
type=str, metavar='0xbc', default='0xbc',
help="character to trigger capture "
"- default: %(default)s")
parser_cap.add_argument("--nwords", dest='ncapture',
type=int, metavar='N', default=4,
help="Number of words to capture from each link"
"- default: 0x%(default)i")
parser_cap.add_argument("--expected", metavar='pattern.txt',
help="Pattern file with 1 word/line in hex")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.INFO)
uhal.setLogLevelTo(uhal.LogLevel.WARNING)
else:
uhal.setLogLevelTo(uhal.LogLevel.WARNING)
logging.basicConfig(level=logging.WARNING)
if not args.connection:
args.connection = os.environ['CTP6_CONNECTION']
if not args.links:
log.debug("Using all links")
args.links = range(48)
else:
args.links = list(api.expand_links(args.links))
hw_connection = 'file://%s' % args.connection
log.info("Setting up connection: %s", hw_connection)
manager = uhal.ConnectionManager(hw_connection)
hw = manager.getDevice('ctp6.frontend')
commands = {
'reset': do_reset,
'status': do_status,
'capture': do_capture
}
print hw
print args
commands[args.command](hw, args)
log.info("done.")