mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """ |
| 5 | The user module contains the objects and methods used to |
| 6 | manage users in Autotest. |
| 7 | |
| 8 | The valid action is: |
| 9 | list: lists user(s) |
| 10 | |
| 11 | The common options are: |
| 12 | --ulist / -U: file containing a list of USERs |
| 13 | |
| 14 | See topic_common.py for a High Level Design and Algorithm. |
| 15 | """ |
| 16 | |
| 17 | import os, sys |
| 18 | from autotest_lib.cli import topic_common, action_common |
| 19 | |
| 20 | |
| 21 | class user(topic_common.atest): |
| 22 | """User class |
| 23 | atest user list <options>""" |
| 24 | usage_action = 'list' |
| 25 | topic = msg_topic = 'user' |
| 26 | msg_items = '<users>' |
| 27 | |
| 28 | def __init__(self): |
| 29 | """Add to the parser the options common to all the |
| 30 | user actions""" |
| 31 | super(user, self).__init__() |
| 32 | |
| 33 | self.parser.add_option('-U', '--ulist', |
| 34 | help='File listing the users', |
| 35 | type='string', |
| 36 | default=None, |
| 37 | metavar='USER_FLIST') |
| 38 | |
| 39 | |
| 40 | def parse(self, flists=None, req_items='users'): |
| 41 | """Consume the common user options""" |
| 42 | if flists: |
| 43 | flists.append(('users', 'ulist', '', True)) |
| 44 | else: |
| 45 | flists = [('users', 'ulist', '', True)] |
| 46 | return self.parse_with_flist(flists, req_items) |
| 47 | |
| 48 | |
| 49 | def get_items(self): |
| 50 | return self.users |
| 51 | |
| 52 | |
| 53 | class user_help(user): |
| 54 | """Just here to get the atest logic working. |
| 55 | Usage is set by its parent""" |
| 56 | pass |
| 57 | |
| 58 | |
| 59 | class user_list(action_common.atest_list, user): |
| 60 | """atest user list <user>|--ulist <file> |
| 61 | [--acl <ACL>|--access_level <n>]""" |
| 62 | def __init__(self): |
| 63 | super(user_list, self).__init__() |
| 64 | |
| 65 | self.parser.add_option('-a', '--acl', |
| 66 | help='Only list users within this ACL') |
| 67 | |
| 68 | self.parser.add_option('-l', '--access_level', |
| 69 | help='Only list users at this access level') |
| 70 | |
| 71 | |
| 72 | def parse(self): |
| 73 | (options, leftover) = super(user_list, self).parse(req_items=None) |
| 74 | self.acl = options.acl |
| 75 | self.access_level = options.access_level |
| 76 | return (options, leftover) |
| 77 | |
| 78 | |
| 79 | def execute(self): |
| 80 | filters = {} |
| 81 | check_results = {} |
| 82 | if self.acl: |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 83 | filters['aclgroup__name__in'] = [self.acl] |
| 84 | check_results['aclgroup__name__in'] = None |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 85 | |
| 86 | if self.access_level: |
| 87 | filters['access_level__in'] = [self.access_level] |
| 88 | check_results['access_level__in'] = None |
| 89 | |
| 90 | if self.users: |
| 91 | filters['login__in'] = self.users |
| 92 | check_results['login__in'] = 'login' |
| 93 | |
| 94 | return super(user_list, self).execute(op='get_users', |
| 95 | filters=filters, |
| 96 | check_results=check_results) |
| 97 | |
| 98 | |
| 99 | def output(self, results): |
| 100 | if self.verbose: |
| 101 | keys = ['id', 'login', 'access_level'] |
| 102 | else: |
| 103 | keys = ['login'] |
| 104 | |
| 105 | super(user_list, self).output(results, keys) |