fastboot_debug.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. # Copyright 2014 Google Inc. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Fastboot in python.
  16. Call it similar to how you call android's fastboot. Call it similar to how you
  17. call android's fastboot, but this only accepts usb paths and no serials.
  18. """
  19. import argparse
  20. import inspect
  21. import logging
  22. import sys
  23. from adb import common_cli
  24. from adb import fastboot
  25. try:
  26. import progressbar
  27. except ImportError:
  28. # progressbar is optional.
  29. progressbar = None
  30. def Devices(args):
  31. """Lists the available devices.
  32. List of devices attached
  33. 015DB7591102001A device
  34. """
  35. for device in fastboot.FastbootCommands.Devices():
  36. print('%s\tdevice' % device.serial_number)
  37. return 0
  38. def _InfoCb(message):
  39. # Use an unbuffered version of stdout.
  40. if not message.message:
  41. return
  42. sys.stdout.write('%s: %s\n' % (message.header, message.message))
  43. sys.stdout.flush()
  44. def main():
  45. common = common_cli.GetCommonArguments()
  46. device = common_cli.GetDeviceArguments()
  47. device.add_argument(
  48. '--chunk_kb', type=int, default=1024, metavar='1024',
  49. help='Size of packets to write in Kb. For older devices, it may be '
  50. 'required to use 4.')
  51. parents = [common, device]
  52. parser = argparse.ArgumentParser(
  53. description=sys.modules[__name__].__doc__, parents=[common])
  54. subparsers = parser.add_subparsers(title='Commands', dest='command_name')
  55. subparser = subparsers.add_parser(
  56. name='help', help='Prints the commands available')
  57. subparser = subparsers.add_parser(
  58. name='devices', help='Lists the available devices', parents=[common])
  59. common_cli.MakeSubparser(
  60. subparsers, parents, fastboot.FastbootCommands.Continue)
  61. common_cli.MakeSubparser(
  62. subparsers, parents, fastboot.FastbootCommands.Download,
  63. {'source_file': 'Filename on the host to push'})
  64. common_cli.MakeSubparser(
  65. subparsers, parents, fastboot.FastbootCommands.Erase)
  66. common_cli.MakeSubparser(
  67. subparsers, parents, fastboot.FastbootCommands.Flash)
  68. common_cli.MakeSubparser(
  69. subparsers, parents, fastboot.FastbootCommands.Getvar)
  70. common_cli.MakeSubparser(
  71. subparsers, parents, fastboot.FastbootCommands.Oem)
  72. common_cli.MakeSubparser(
  73. subparsers, parents, fastboot.FastbootCommands.Reboot)
  74. if len(sys.argv) == 1:
  75. parser.print_help()
  76. return 2
  77. args = parser.parse_args()
  78. if args.verbose:
  79. logging.basicConfig(level=logging.DEBUG)
  80. if args.command_name == 'devices':
  81. return Devices(args)
  82. if args.command_name == 'help':
  83. parser.print_help()
  84. return 0
  85. kwargs = {}
  86. argspec = inspect.getargspec(args.method)
  87. if 'info_cb' in argspec.args:
  88. kwargs['info_cb'] = _InfoCb
  89. if 'progress_callback' in argspec.args and progressbar:
  90. bar = progressbar.ProgessBar(
  91. widgets=[progressbar.Bar(), progressbar.Percentage()])
  92. bar.start()
  93. def SetProgress(current, total):
  94. bar.update(current / total * 100.0)
  95. if current == total:
  96. bar.finish()
  97. kwargs['progress_callback'] = SetProgress
  98. return common_cli.StartCli(
  99. args,
  100. fastboot.FastbootCommands,
  101. chunk_kb=args.chunk_kb,
  102. extra=kwargs)
  103. if __name__ == '__main__':
  104. sys.exit(main())