11f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley#!/usr/bin/python
21f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
31f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
41f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley# Use of this source code is governed by a BSD-style license that can be
51f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley# found in the LICENSE file.
61f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
71f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileyimport pprint
81f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileyimport sys
91f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1011deef18d2b8f0babb8361be8be1844534aff6cfChristopher Wileyimport common
111f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileyfrom autotest_lib.client.cros.networking import shill_proxy
121f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
131f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef usage():
141f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ Prints a script usage message. """
151f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    cmd = sys.argv[0]
161f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print 'Usage: %s <command> [more parameters]' % cmd
171f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print 'Example uses:'
181f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'create <name> - Create a profile called |name|.'
191f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'push <name> - Push a previously created profile called |name|.'
201f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'pop <name> - Pop a profile called |name|.'
211f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'pop - Pop the top-most profile.'
221f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'remove <name> - Remove a profile called |name| from disk.'
231f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'clean - Pop and remove profiles above the default profile.'
241f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'list - List profiles and their properties.'
251f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'list-entries - List profile entries.'
261f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    print cmd, 'delete-entry <entry> [name] - Delete an entry from a ' \
271f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        'profile called |name| or all profiles if no name is given.'
281f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    return False
291f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
301f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
311f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef print_profile_path(profile_path, active_path):
321f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ Print profile_path and indicate if it is the active profile. """
331f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if profile_path == active_path:
341f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Profile: %s  <== active' % profile_path
351f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    else:
361f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Profile: %s' % profile_path
371f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
381f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
391f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef clean_profiles():
401f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ Pop and remove all profiles until 'default' is found. """
411f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    shill = shill_proxy.ShillProxy()
421f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    while True:
431f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        active_profile = shill.get_active_profile()
441f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        properties = active_profile.GetProperties(utf8_strings=True)
451f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        active_name = shill.dbus2primitive(
461f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley                properties[shill.PROFILE_PROPERTY_NAME])
471f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        if active_name == 'default':
481f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            return True
491f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        else:
501f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            print 'Removing profile: %s' % active_name
511f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            shill.manager.PopProfile(active_name)
521f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            shill.manager.RemoveProfile(active_name)
531f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
541f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
551f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef delete_entry():
561f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """
571f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    Remove an entry from the specified profile, or all profiles if no profile
581f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    is given.
591f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
601f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """
611f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if len(sys.argv) <= 2:
621f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return usage()
631f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    identifier = sys.argv[2]
641f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    profile_path = None
651f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if len(sys.argv) > 3:
661f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley      profile_path = sys.argv[3]
671f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    shill = shill_proxy.ShillProxy()
681f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    properties = shill.dbus2primitive(
691f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        shill.manager.GetProperties(utf8_strings=True))
701f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    active_profile = shill.get_active_profile()
711f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    for path in properties[shill.MANAGER_PROPERTY_PROFILES]:
721f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print_profile_path(path, active_profile.object_path)
731f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        if profile_path and path != profile_path:
741f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            continue
751f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
761f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        profile = shill.get_dbus_object(shill.DBUS_TYPE_PROFILE, path)
771f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        try:
781f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            profile.DeleteEntry(identifier)
791f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            print " -> delete succeeded"
801f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        except:
811f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            print " -> delete failed"
821f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
831f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
841f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef list_entries():
851f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ Display detailed profile entry information. """
861f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    shill = shill_proxy.ShillProxy()
871f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    active_profile = shill.get_active_profile()
881f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    for profile in shill.get_profiles():
891f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print_profile_path(profile.object_path, active_profile.object_path)
901f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        properties = shill.dbus2primitive(
911f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley                profile.GetProperties(utf8_strings=True))
921f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        if not shill.PROFILE_PROPERTY_ENTRIES in properties:
931f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            continue
941f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        for ident in properties[shill.PROFILE_PROPERTY_ENTRIES]:
951f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            print 'Entry: %s' % ident
961f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley            pprint.pprint(shill.dbus2primitive(profile.GetEntry(ident)),
971f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley                          indent=2)
981f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print
991f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    return True
1001f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1011f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1021f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef list_profiles():
1031f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ List shill profiles and their properties. """
1041f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    shill = shill_proxy.ShillProxy()
1051f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    active_profile = shill.get_active_profile()
1061f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    for profile in shill.get_profiles():
1071f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print_profile_path(profile.object_path, active_profile.object_path)
1081f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        properties = shill.dbus2primitive(
1091f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley                profile.GetProperties(utf8_strings=True))
1101f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        pprint.pprint(properties, indent=2)
1111f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print
1121f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    return True
1131f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1141f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1151f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileydef main():
1161f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    """ Main entry point for the profile script. """
1171f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if len(sys.argv) < 2:
1181f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return usage()
1191f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1201f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    command = sys.argv[1]
1211f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    shill = shill_proxy.ShillProxy()
1221f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1231f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'clean':
1241f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return clean_profiles()
1251f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1261f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'delete-entry':
1271f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return delete_entry()
1281f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1291f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'list':
1301f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley      return list_profiles()
1311f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1321f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'list-entries':
1331f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return list_entries()
1341f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1351f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'pop' and len(sys.argv) == 2:
1361f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        shill.manager.PopAnyProfile()
1371f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Popped profile.'
1381f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return True
1391f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1401f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    # All the remaining operations require a profile name.
1411f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if len(sys.argv) < 3:
1421f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return usage()
1431f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1441f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    name = sys.argv[2]
1451f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1461f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'pop':
1471f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        shill.manager.PopProfile(name)
1481f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Popped profile %s.' % name
1491f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return True
1501f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1511f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'create':
1521f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        path = shill.manager.CreateProfile(name)
1531f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'New profile created at %s.' % path
1541f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return True
1551f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1561f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'push':
1571f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        path = shill.manager.PushProfile(name)
1581f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Pushed profile %s.' % path
1591f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return True
1601f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1611f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if command == 'remove':
1621f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        shill.manager.RemoveProfile(name)
1631f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        print 'Removed profile %s.' % name
1641f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        return True
1651f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1661f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    return usage()
1671f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1681f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley
1691f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wileyif __name__ == '__main__':
1701f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley    if not main():
1711f89fd2f9f2010e732a87ed3c8f7f25777862c59Christopher Wiley        sys.exit(1)
172