tiaf_tools.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 pathlib
  10. from tiaf_logger import get_logger
  11. from storage_query_tool import S3StorageQueryTool
  12. from storage_query_tool import LocalStorageQueryTool
  13. logger = get_logger(__file__)
  14. def parse_args():
  15. def valid_file_path(value):
  16. if pathlib.Path(value).is_file():
  17. return value
  18. else:
  19. raise FileNotFoundError(value)
  20. def valid_path(value):
  21. if pathlib.Path(value).is_dir():
  22. return value
  23. else:
  24. raise FileNotFoundError(value)
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument(
  27. '--search-in',
  28. type=str,
  29. help="Directory SQT should search in when searching locally, or directory when using S3.",
  30. required=False
  31. )
  32. parser.add_argument(
  33. '--s3-bucket',
  34. type=str,
  35. help="Flag to SQT to use S3, will search in the named bucket.",
  36. required=False
  37. )
  38. parser.add_argument(
  39. '--full-address',
  40. type=str,
  41. help="Full address to desired file, either locally or in bucket.",
  42. required=False
  43. )
  44. parser.add_argument(
  45. "--action",
  46. type=str,
  47. help="What action TIAF Tools should take.",
  48. choices=["read", "update", "delete", "create"],
  49. required=False
  50. )
  51. parser.add_argument(
  52. "--file-in",
  53. type=valid_file_path,
  54. help="Path to file to be used when creating or updating a file.",
  55. required=False
  56. )
  57. parser.add_argument(
  58. "--file-out",
  59. type=valid_path,
  60. help="Path to directory store file in when downloading.",
  61. required=False
  62. )
  63. parser.add_argument(
  64. "--file-type",
  65. choices=["json", "zip"],
  66. help="What file type SQT should expect to be interacting with. Current options are zip and json.",
  67. required=True
  68. )
  69. return parser.parse_args()
  70. def run(args: dict):
  71. try:
  72. if args.get('s3_bucket'):
  73. sqt = S3StorageQueryTool(**args)
  74. else:
  75. if args.get('search_in'):
  76. if not pathlib.Path(args['search_in']).is_dir():
  77. raise FileNotFoundError(args['search_in'])
  78. sqt = LocalStorageQueryTool(**args)
  79. elif args.get('full_address'):
  80. sqt = LocalStorageQueryTool(**args)
  81. else:
  82. logger.error(
  83. "You must define a repository to search in when searching locally")
  84. except Exception as e:
  85. logger.error(type(e).__name__)
  86. logger.error(e.args)
  87. logger.error(f"Exception caught by TIAF Tools: '{e}'.")
  88. if __name__ == "__main__":
  89. parsed_args = vars(parse_args())
  90. run(args=parsed_args)