-
Notifications
You must be signed in to change notification settings - Fork 3
/
motelist.py
executable file
·181 lines (155 loc) · 6.7 KB
/
motelist.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
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# Copyright (c) 2013, Janis Judvaitis
# Copyright (c) 2018, University of Bristol <www.bristol.ac.uk>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors:
# Janis Judvaitis
# Atis Elsts <[email protected]>
# George Oikonomou <[email protected]>
import sys
import os
import backends.backend
import argparse
import collections
import subprocess
class Motelist(object):
defaults = {
'omit_header': False,
'csv_out': False,
'brief': False,
'patterns': [],
}
version_string = '0.5'
def __init__(self, omit_header=defaults['omit_header'],
csv_out=defaults['csv_out'],
brief=defaults['brief'],
patterns=defaults['patterns']):
self.__omit_header = omit_header
self.__csv_out = csv_out
self.__brief = brief
self.__motes = []
self.__backend = backends.backend.Backend.detect(self)
try:
self.__backend.run(patterns)
except AttributeError:
raise
def create_mote(self):
mote = Mote()
self.__motes.append(mote)
return mote
def __str__(self):
if len(self.__motes) == 0:
if self.__brief:
return ''
else:
return 'No motes detected'
# Map output column headings to class Mote attribute names. Allows
# us to easily change column text later, or re-order cols.
if self.__brief:
cols = collections.OrderedDict([
('Port', 'port'),
])
else:
cols = collections.OrderedDict([
('Port', 'port'),
('Serial', 'serial'),
('VID', 'vid'),
('PID', 'pid'),
('Product', 'product'),
('Vendor', 'vendor'),
])
s = ''
if self.__csv_out:
if not self.__omit_header:
s += ';'.join(heading for heading in cols.keys()) + '\n'
for mote in self.__motes:
s += ';'.join(getattr(mote, attr)
for attr in cols.values()) + '\n'
else:
# Prepare table column width
lengths = dict(
[(heading, len(heading)) for heading in cols.keys()]
)
for mote in self.__motes:
for c, a in cols.items():
lengths[c] = max(lengths[c], len(getattr(mote, a)))
# Create the string format on the fly. Easy way to handle brief out
string_fmt = '{} '.join('' for col in cols.keys()) + '{}\n'
if not self.__omit_header:
s = string_fmt.format(*[c.ljust(lengths[c]) for c in cols.keys()])
s += string_fmt.format(*[''.ljust(lengths[c], '-') for c in cols.keys()])
for mote in self.__motes:
s+= string_fmt.format(*[getattr(mote, a).ljust(lengths[c]) for c, a in cols.items()])
# Remove the trailing newline
return s[:-1]
class Mote(object):
def __init__(self):
self.port = "n/a"
self.vid = "n/a"
self.pid = "n/a"
self.product = "n/a"
self.vendor = "n/a"
self.serial = "n/a"
def print_version():
git_describe_cmd = ['git', '-C', os.path.dirname(os.path.abspath(__file__)), 'describe', '--tags', '--always']
try:
# Suppress git stderr - portable across 2.7 and 3+
with open(os.devnull, 'w') as devnull:
# In 2.7 check_output returns string, in 3+ it returns bytes.
# This should be portable.
version = subprocess.check_output(git_describe_cmd, stderr=devnull).decode('utf-8')
except (IOError, OSError, subprocess.CalledProcessError):
version = Motelist.version_string
return version
if __name__ == '__main__':
parser = argparse.ArgumentParser(add_help=False,
description='Automatically detect and '
'print out a list of motes '
'connected to this computer')
parser.add_argument('-c', '--csv', action='store_true',
default=Motelist.defaults['csv_out'],
help = 'Print list in CSV format')
parser.add_argument('-o', '--omit-header', action='store_true',
default=Motelist.defaults['omit_header'],
help='Omit header row')
parser.add_argument('-b', '--brief', action='store_true',
default=Motelist.defaults['brief'],
help='Only print serial port paths')
parser.add_argument('-p', '--patterns', nargs="+",
default=Motelist.defaults['patterns'],
help='Search for additional port filename patterns, for example '
'you can pass -p "/dev/tty.usbserial*"')
parser.add_argument('-h', '--help', action='help',
help='Show this message and exit')
parser.add_argument('-v', '--version', action='version',
version='%(prog)s ' + print_version(),
help='Prints software version')
args = parser.parse_args()
output = str(Motelist(omit_header=args.omit_header,
csv_out=args.csv,
brief=args.brief,
patterns=args.patterns))
if output:
print(output)