1 #!/usr/bin/python 2 3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 import pprint 8 import sys 9 10 import common 11 from autotest_lib.client.cros.networking import shill_proxy 12 13 def usage(): 14 """ Prints a script usage message. """ 15 cmd = sys.argv[0] 16 print 'Usage: %s <command> [more parameters]' % cmd 17 print 'Example uses:' 18 print cmd, 'create <name> - Create a profile called |name|.' 19 print cmd, 'push <name> - Push a previously created profile called |name|.' 20 print cmd, 'pop <name> - Pop a profile called |name|.' 21 print cmd, 'pop - Pop the top-most profile.' 22 print cmd, 'remove <name> - Remove a profile called |name| from disk.' 23 print cmd, 'clean - Pop and remove profiles above the default profile.' 24 print cmd, 'list - List profiles and their properties.' 25 print cmd, 'list-entries - List profile entries.' 26 print cmd, 'delete-entry <entry> [name] - Delete an entry from a ' \ 27 'profile called |name| or all profiles if no name is given.' 28 return False 29 30 31 def print_profile_path(profile_path, active_path): 32 """ Print profile_path and indicate if it is the active profile. """ 33 if profile_path == active_path: 34 print 'Profile: %s <== active' % profile_path 35 else: 36 print 'Profile: %s' % profile_path 37 38 39 def clean_profiles(): 40 """ Pop and remove all profiles until 'default' is found. """ 41 shill = shill_proxy.ShillProxy() 42 while True: 43 active_profile = shill.get_active_profile() 44 properties = active_profile.GetProperties(utf8_strings=True) 45 active_name = shill.dbus2primitive( 46 properties[shill.PROFILE_PROPERTY_NAME]) 47 if active_name == 'default': 48 return True 49 else: 50 print 'Removing profile: %s' % active_name 51 shill.manager.PopProfile(active_name) 52 shill.manager.RemoveProfile(active_name) 53 54 55 def delete_entry(): 56 """ 57 Remove an entry from the specified profile, or all profiles if no profile 58 is given. 59 60 """ 61 if len(sys.argv) <= 2: 62 return usage() 63 identifier = sys.argv[2] 64 profile_path = None 65 if len(sys.argv) > 3: 66 profile_path = sys.argv[3] 67 shill = shill_proxy.ShillProxy() 68 properties = shill.dbus2primitive( 69 shill.manager.GetProperties(utf8_strings=True)) 70 active_profile = shill.get_active_profile() 71 for path in properties[shill.MANAGER_PROPERTY_PROFILES]: 72 print_profile_path(path, active_profile.object_path) 73 if profile_path and path != profile_path: 74 continue 75 76 profile = shill.get_dbus_object(shill.DBUS_TYPE_PROFILE, path) 77 try: 78 profile.DeleteEntry(identifier) 79 print " -> delete succeeded" 80 except: 81 print " -> delete failed" 82 83 84 def list_entries(): 85 """ Display detailed profile entry information. """ 86 shill = shill_proxy.ShillProxy() 87 active_profile = shill.get_active_profile() 88 for profile in shill.get_profiles(): 89 print_profile_path(profile.object_path, active_profile.object_path) 90 properties = shill.dbus2primitive( 91 profile.GetProperties(utf8_strings=True)) 92 if not shill.PROFILE_PROPERTY_ENTRIES in properties: 93 continue 94 for ident in properties[shill.PROFILE_PROPERTY_ENTRIES]: 95 print 'Entry: %s' % ident 96 pprint.pprint(shill.dbus2primitive(profile.GetEntry(ident)), 97 indent=2) 98 print 99 return True 100 101 102 def list_profiles(): 103 """ List shill profiles and their properties. """ 104 shill = shill_proxy.ShillProxy() 105 active_profile = shill.get_active_profile() 106 for profile in shill.get_profiles(): 107 print_profile_path(profile.object_path, active_profile.object_path) 108 properties = shill.dbus2primitive( 109 profile.GetProperties(utf8_strings=True)) 110 pprint.pprint(properties, indent=2) 111 print 112 return True 113 114 115 def main(): 116 """ Main entry point for the profile script. """ 117 if len(sys.argv) < 2: 118 return usage() 119 120 command = sys.argv[1] 121 shill = shill_proxy.ShillProxy() 122 123 if command == 'clean': 124 return clean_profiles() 125 126 if command == 'delete-entry': 127 return delete_entry() 128 129 if command == 'list': 130 return list_profiles() 131 132 if command == 'list-entries': 133 return list_entries() 134 135 if command == 'pop' and len(sys.argv) == 2: 136 shill.manager.PopAnyProfile() 137 print 'Popped profile.' 138 return True 139 140 # All the remaining operations require a profile name. 141 if len(sys.argv) < 3: 142 return usage() 143 144 name = sys.argv[2] 145 146 if command == 'pop': 147 shill.manager.PopProfile(name) 148 print 'Popped profile %s.' % name 149 return True 150 151 if command == 'create': 152 path = shill.manager.CreateProfile(name) 153 print 'New profile created at %s.' % path 154 return True 155 156 if command == 'push': 157 path = shill.manager.PushProfile(name) 158 print 'Pushed profile %s.' % path 159 return True 160 161 if command == 'remove': 162 shill.manager.RemoveProfile(name) 163 print 'Removed profile %s.' % name 164 return True 165 166 return usage() 167 168 169 if __name__ == '__main__': 170 if not main(): 171 sys.exit(1) 172