blob: 9f65977bc8c76a91c613fe245b299f22adbe5d11 [file] [log] [blame]
# Copyright 2012 Google Inc. All Rights Reserved.
"""Test configurations. Includes a list of auto-probed bruno devices."""
import re
import sys
from configs import hosts
from configs.core import Host
from configs.core import QueryError
from portsh import options
__author__ = 'Avery Pennarun ('
optspec = """
config [options...] <bool|^bool|key=value>...
random Choose random matches instead of the first ones
max= Return no more than max= matches
min= If less than min= matches, return an error code
n,num= Return exactly num= matches
v,verbose Display the entire config entry for each match, not just the name
k,keys= If -v isn't given, show these comma/space-separated attrs [name]
def main():
o = options.Options(optspec)
opt, _, extra = o.parse(sys.argv[1:])
if opt.num:
if opt.min or opt.max:
o.fatal("don't specify --num with --min/--max")
opt.min = opt.max = opt.num
kwargs = dict(is_alive=True)
for kvs in extra:
kv = kvs.split('=', 1) + [None]
key = kv[0]
value = kv[1]
if not hasattr(Host, key):
o.fatal('no key named %r; valid keys: %s'
% (key, ', '.join(Host.__slots__)))
if key.startswith('^'):
kwargs[key[1:]] = False
elif value is not None:
kwargs[key] = value
kwargs[key] = True
matches = hosts.Query(mincount=opt.min, maxcount=opt.max,
randomize=opt.random, **kwargs)
except QueryError, e:
if opt.verbose:
for match in matches:
print match
keys = re.split(r'[,\s]', opt.keys)
for key in keys:
if not hasattr(Host, key):
o.fatal('no key named %r; valid keys: %s'
% (key, ', '.join(Host.__slots__)))
for match in matches:
print ','.join(str(getattr(match, key) or '') for key in keys)
if __name__ == '__main__':