run_ffx_command.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 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. """Deploys packages and runs an FFX command on a Fuchsia target."""
  7. import argparse
  8. import logging
  9. import os
  10. import pkg_repo
  11. import shlex
  12. import sys
  13. import tempfile
  14. import time
  15. from common_args import AddCommonArgs, AddTargetSpecificArgs, \
  16. ConfigureLogging, GetDeploymentTargetForArgs
  17. def main():
  18. parser = argparse.ArgumentParser()
  19. logging.getLogger().setLevel(logging.INFO)
  20. parser.add_argument('--command',
  21. required=True,
  22. help='FFX command to run. Runtime arguments are handled '
  23. 'using the %%args%% placeholder.')
  24. parser.add_argument('child_args',
  25. nargs='*',
  26. help='Arguments for the command.')
  27. AddCommonArgs(parser)
  28. AddTargetSpecificArgs(parser)
  29. args = parser.parse_args()
  30. # Prepare the arglist for "ffx". %args% is replaced with all positional
  31. # arguments given to the script.
  32. ffx_args = shlex.split(args.command)
  33. # replace %args% in the command with the given arguments.
  34. try:
  35. args_index = ffx_args.index('%args%')
  36. ffx_args[args_index:args_index + 1] = args.child_args
  37. except ValueError:
  38. # %args% is not present; use the command as-is.
  39. pass
  40. with GetDeploymentTargetForArgs(args) as target:
  41. target.Start()
  42. target.StartSystemLog(args.package)
  43. # Extend the lifetime of |pkg_repo| beyond InstallPackage so that the
  44. # package can be instantiated after resolution.
  45. with target.GetPkgRepo() as pkg_repo:
  46. target.InstallPackage(args.package)
  47. process = target.RunFFXCommand(ffx_args)
  48. # It's possible that components installed by this script may be
  49. # instantiated at arbitrary points in the future.
  50. # This script (specifically |pkg_repo|) must be kept alive until it
  51. # is explicitly terminated by the user, otherwise pkgsvr will
  52. # throw an error when launching components.
  53. logging.info('Command is now running. Press CTRL-C to exit.')
  54. try:
  55. while True:
  56. time.sleep(1)
  57. except KeyboardInterrupt:
  58. pass
  59. return 0
  60. if __name__ == '__main__':
  61. sys.exit(main())