apply_shared_preference_file.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env vpython3
  2. #
  3. # Copyright 2018 The Chromium 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. """Manually applies a shared preference JSON file.
  7. If needed during automation, use the --shared-prefs-file in test_runner.py
  8. instead.
  9. """
  10. import argparse
  11. import sys
  12. # pylint: disable=ungrouped-imports
  13. from pylib.constants import host_paths
  14. if host_paths.DEVIL_PATH not in sys.path:
  15. sys.path.append(host_paths.DEVIL_PATH)
  16. from devil.android import device_utils
  17. from devil.android.sdk import shared_prefs
  18. from pylib.utils import shared_preference_utils
  19. def main():
  20. parser = argparse.ArgumentParser(
  21. description='Manually apply shared preference JSON files.')
  22. parser.add_argument('filepaths', nargs='*',
  23. help='Any number of paths to shared preference JSON '
  24. 'files to apply.')
  25. args = parser.parse_args()
  26. all_devices = device_utils.DeviceUtils.HealthyDevices()
  27. if not all_devices:
  28. raise RuntimeError('No healthy devices attached')
  29. for filepath in args.filepaths:
  30. all_settings = shared_preference_utils.ExtractSettingsFromJson(filepath)
  31. for setting in all_settings:
  32. for device in all_devices:
  33. shared_pref = shared_prefs.SharedPrefs(
  34. device, setting['package'], setting['filename'],
  35. use_encrypted_path=setting.get('supports_encrypted_path', False))
  36. shared_preference_utils.ApplySharedPreferenceSetting(
  37. shared_pref, setting)
  38. if __name__ == '__main__':
  39. main()