o3de.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import logging
  10. import pathlib
  11. import sys
  12. logger = logging.getLogger('o3de')
  13. logger.setLevel(logging.INFO)
  14. def add_args(parser: argparse.ArgumentParser) -> None:
  15. """
  16. add_args is called to add expected parser arguments and subparsers arguments to each command such that it can be
  17. invoked by o3de.py
  18. Ex o3de.py can invoke the register downloadable commands by importing register,
  19. call add_args and execute: python o3de.py register --gem-path "C:/TestGem"
  20. :param parser: the caller instantiates an ArgumentParser and passes it in here
  21. """
  22. subparsers = parser.add_subparsers(help='To get help on a sub-command:\no3de.py <sub-command> -h',
  23. title='Sub-Commands')
  24. # As o3de.py shares the same name as the o3de package attempting to use a regular
  25. # from o3de import <module> line tries to import from the current o3de.py script and not the package
  26. # So the {current script directory} / 'o3de' is added to the front of the sys.path
  27. script_dir = pathlib.Path(__file__).parent
  28. o3de_package_dir = (script_dir / 'o3de').resolve()
  29. # add the scripts/o3de directory to the front of the sys.path
  30. sys.path.insert(0, str(o3de_package_dir))
  31. from o3de import android, engine_properties, engine_template, gem_properties, \
  32. global_project, register, print_registration, get_registration, \
  33. enable_gem, disable_gem, project_properties, sha256, download, \
  34. export_project, repo, repo_properties
  35. # Remove the temporarily added path
  36. sys.path = sys.path[1:]
  37. # global project
  38. global_project.add_args(subparsers)
  39. # engine template
  40. engine_template.add_args(subparsers)
  41. # registration
  42. register.add_args(subparsers)
  43. # show registration
  44. print_registration.add_args(subparsers)
  45. # get registration
  46. get_registration.add_args(subparsers)
  47. # add a gem to a project
  48. enable_gem.add_args(subparsers)
  49. # remove a gem from a project
  50. disable_gem.add_args(subparsers)
  51. # modify engine properties
  52. engine_properties.add_args(subparsers)
  53. # modify project properties
  54. project_properties.add_args(subparsers)
  55. # modify gem properties
  56. gem_properties.add_args(subparsers)
  57. # sha256
  58. sha256.add_args(subparsers)
  59. # download
  60. download.add_args(subparsers)
  61. # export_project
  62. export_project.add_args(subparsers)
  63. # repo
  64. repo.add_args(subparsers)
  65. # modify remote repo
  66. repo_properties.add_args(subparsers)
  67. # Android
  68. android.add_args(subparsers)
  69. if __name__ == "__main__":
  70. # parse the command line args
  71. the_parser = argparse.ArgumentParser()
  72. # add args to the parser
  73. add_args(the_parser)
  74. # if empty print help
  75. if len(sys.argv) == 1:
  76. the_parser.print_help(sys.stderr)
  77. sys.exit(1)
  78. # parse args
  79. # argparse stores unknown arguments separately as a tuple,
  80. # not packed in the same NameSpace as known arguments
  81. known_args, unknown_args = the_parser.parse_known_args()
  82. if hasattr(known_args, 'accepts_partial_args'):
  83. ret = known_args.func(known_args, unknown_args) if hasattr(known_args, 'func') else 1
  84. elif unknown_args:
  85. # since we expect every command which doesn't accept partial args to process only known args,
  86. # if we face unknown args in such cases, we should throw an error.
  87. # parse_args() calls parse_known_args() and will issue an error
  88. # https://hg.python.org/cpython/file/bb9fc884a838/Lib/argparse.py#l1725
  89. the_parser.parse_args()
  90. else:
  91. ret = known_args.func(known_args) if hasattr(known_args, 'func') else 1
  92. # run
  93. logger.info('Success!' if ret == 0 else 'Completed with issues: result {}'.format(ret))
  94. # return
  95. sys.exit(ret)