eecli.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. """Executable for the Earth Engine command line interface.
  3. This executable starts a Python Cmd instance to receive and process command
  4. line input entered by the user. If the executable is invoked with some
  5. command line arguments, the Cmd is launched in the one-off mode, where
  6. the provided arguments are processed as a single command after which the
  7. program is terminated. Otherwise, this executable will launch the Cmd in the
  8. interactive (looping) mode, where the user will be able to run multiple
  9. commands as in a typical terminal program.
  10. """
  11. import argparse
  12. import sys
  13. import ee
  14. from ee.cli import commands
  15. from ee.cli import utils
  16. class CommandDispatcher(commands.Dispatcher):
  17. name = 'main'
  18. COMMANDS = commands.EXTERNAL_COMMANDS
  19. def _run_command(*argv):
  20. """Runs an eecli command."""
  21. _ = argv
  22. # Set the program name to 'earthengine' for proper help text display.
  23. parser = argparse.ArgumentParser(
  24. prog='earthengine', description='Earth Engine Command Line Interface.')
  25. parser.add_argument(
  26. '--ee_config', help='Path to the earthengine configuration file. '
  27. 'Defaults to "~/%s".' % utils.DEFAULT_EE_CONFIG_FILE_RELATIVE)
  28. parser.add_argument(
  29. '--service_account_file', help='Path to a service account credentials'
  30. 'file. Overrides any ee_config if specified.')
  31. parser.add_argument(
  32. '--project',
  33. help='Specifies a Google Cloud Platform Project id to override the call.',
  34. dest='project_override')
  35. dispatcher = CommandDispatcher(parser)
  36. # Print the list of commands if the user supplied no arguments at all.
  37. if len(sys.argv) == 1:
  38. parser.print_help()
  39. return
  40. args = parser.parse_args()
  41. config = utils.CommandLineConfig(
  42. args.ee_config, args.service_account_file, args.project_override
  43. )
  44. # Catch EEException errors, which wrap server-side Earth Engine
  45. # errors, and print the error message without the irrelevant local
  46. # stack trace. (Individual commands may also catch EEException if
  47. # they want to be able to continue despite errors.)
  48. try:
  49. dispatcher.run(args, config)
  50. except ee.EEException as e:
  51. print(e)
  52. sys.exit(1)
  53. def _get_tensorflow():
  54. try:
  55. # pylint: disable=g-import-not-at-top
  56. import tensorflow.compat.v1 as tf
  57. return tf
  58. except ImportError:
  59. return None
  60. def main():
  61. tf_module = _get_tensorflow()
  62. if tf_module:
  63. # We need InitGoogle initialization since TensorFlow expects it.
  64. tf_module.app.run(_run_command, argv=sys.argv[:1])
  65. else:
  66. _run_command()
  67. if __name__ == '__main__':
  68. main()