PythonBindings.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #pragma once
  9. #include <PythonBindingsInterface.h>
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzCore/std/parallel/semaphore.h>
  12. // Qt defines slots, which interferes with the use here.
  13. #pragma push_macro("slots")
  14. #undef slots
  15. #include <Python.h>
  16. #include <pybind11/pybind11.h>
  17. #pragma pop_macro("slots")
  18. namespace O3DE::ProjectManager
  19. {
  20. class PythonBindings
  21. : public PythonBindingsInterface::Registrar
  22. {
  23. public:
  24. PythonBindings() = default;
  25. PythonBindings(const AZ::IO::PathView& enginePath);
  26. ~PythonBindings() override;
  27. // PythonBindings overrides
  28. bool PythonStarted() override;
  29. bool StartPython() override;
  30. // Engine
  31. AZ::Outcome<QVector<EngineInfo>> GetAllEngineInfos() override;
  32. AZ::Outcome<EngineInfo> GetEngineInfo() override;
  33. AZ::Outcome<EngineInfo> GetEngineInfo(const QString& engineName) override;
  34. AZ::Outcome<EngineInfo> GetProjectEngine(const QString& projectPath) override;
  35. DetailedOutcome SetEngineInfo(const EngineInfo& engineInfo, bool force = false) override;
  36. // Remote sources
  37. bool ValidateRepository(const QString& repoUri) override;
  38. // Gem
  39. AZ::Outcome<GemInfo> CreateGem(const QString& templatePath, const GemInfo& gemInfo, bool registerGem = true) override;
  40. AZ::Outcome<GemInfo> EditGem(const QString& oldGemName, const GemInfo& newGemInfo) override;
  41. AZ::Outcome<GemInfo> GetGemInfo(const QString& path, const QString& projectPath = {}) override;
  42. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetEngineGemInfos() override;
  43. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetAllGemInfos(const QString& projectPath) override;
  44. AZ::Outcome<QHash<QString /*gem name with specifier*/, QString /* gem path */>, AZStd::string> GetEnabledGems(
  45. const QString& projectPath, bool includeDependencies) const override;
  46. AZ::Outcome<void, AZStd::string> RegisterGem(const QString& gemPath, const QString& projectPath = {}) override;
  47. AZ::Outcome<void, AZStd::string> UnregisterGem(const QString& gemPath, const QString& projectPath = {}) override;
  48. // Project
  49. AZ::Outcome<ProjectInfo> CreateProject(const QString& projectTemplatePath, const ProjectInfo& projectInfo, bool registerProject = true) override;
  50. AZ::Outcome<ProjectInfo> GetProject(const QString& path) override;
  51. AZ::Outcome<QVector<ProjectInfo>> GetProjects() override;
  52. AZ::Outcome<QVector<ProjectInfo>, AZStd::string> GetProjectsForRepo(const QString& repoUri) override;
  53. AZ::Outcome<QVector<ProjectInfo>, AZStd::string> GetProjectsForAllRepos() override;
  54. DetailedOutcome AddProject(const QString& path) override;
  55. DetailedOutcome RemoveProject(const QString& path) override;
  56. AZ::Outcome<void, AZStd::string> UpdateProject(const ProjectInfo& projectInfo) override;
  57. AZ::Outcome<void, AZStd::string> AddGemToProject(const QString& gemPath, const QString& projectPath) override;
  58. AZ::Outcome<void, AZStd::string> RemoveGemFromProject(const QString& gemPath, const QString& projectPath) override;
  59. bool RemoveInvalidProjects() override;
  60. // Gem Repos
  61. AZ::Outcome<void, AZStd::string> RefreshGemRepo(const QString& repoUri) override;
  62. bool RefreshAllGemRepos() override;
  63. DetailedOutcome AddGemRepo(const QString& repoUri) override;
  64. bool RemoveGemRepo(const QString& repoUri) override;
  65. AZ::Outcome<QVector<GemRepoInfo>, AZStd::string> GetAllGemRepoInfos() override;
  66. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetGemInfosForRepo(const QString& repoUri) override;
  67. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetGemInfosForAllRepos() override;
  68. DetailedOutcome DownloadGem(
  69. const QString& gemName, const QString& path, std::function<void(int, int)> gemProgressCallback, bool force = false) override;
  70. DetailedOutcome DownloadProject(
  71. const QString& projectName, const QString& path, std::function<void(int, int)> projectProgressCallback, bool force = false) override;
  72. DetailedOutcome DownloadTemplate(
  73. const QString& templateName, const QString& path, std::function<void(int, int)> templateProgressCallback, bool force = false) override;
  74. void CancelDownload() override;
  75. bool IsGemUpdateAvaliable(const QString& gemName, const QString& lastUpdated) override;
  76. // Templates
  77. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplates() override;
  78. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplatesForRepo(const QString& repoUri) const override;
  79. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplatesForAllRepos() const override;
  80. AZ::Outcome<QVector<TemplateInfo>> GetGemTemplates() override;
  81. void AddErrorString(AZStd::string errorString) override;
  82. protected:
  83. static void OnStdOut(const char* msg);
  84. static void OnStdError(const char* msg);
  85. private:
  86. AZ_DISABLE_COPY_MOVE(PythonBindings);
  87. AZ::Outcome<void, AZStd::string> ExecuteWithLockErrorHandling(AZStd::function<void()> executionCallback) const;
  88. bool ExecuteWithLock(AZStd::function<void()> executionCallback) const;
  89. EngineInfo EngineInfoFromPath(pybind11::handle enginePath);
  90. GemInfo GemInfoFromPath(pybind11::handle path, pybind11::handle pyProjectPath);
  91. GemRepoInfo GetGemRepoInfo(pybind11::handle repoUri);
  92. ProjectInfo ProjectInfoFromPath(pybind11::handle path);
  93. ProjectInfo ProjectInfoFromDict(pybind11::handle projectData, const QString& path = {});
  94. ProjectTemplateInfo ProjectTemplateInfoFromPath(pybind11::handle path) const;
  95. TemplateInfo TemplateInfoFromPath(pybind11::handle path) const;
  96. AZ::Outcome<void, AZStd::string> GemRegistration(const QString& gemPath, const QString& projectPath, bool remove = false);
  97. bool StopPython();
  98. IPythonBindings::ErrorPair GetErrorPair();
  99. bool m_pythonStarted = false;
  100. AZ::IO::FixedMaxPath m_enginePath;
  101. mutable AZStd::recursive_mutex m_lock;
  102. pybind11::handle m_gemProperties;
  103. pybind11::handle m_engineTemplate;
  104. pybind11::handle m_engineProperties;
  105. pybind11::handle m_register;
  106. pybind11::handle m_manifest;
  107. pybind11::handle m_enableGemProject;
  108. pybind11::handle m_disableGemProject;
  109. pybind11::handle m_editProjectProperties;
  110. pybind11::handle m_projectManagerInterface;
  111. pybind11::handle m_download;
  112. pybind11::handle m_repo;
  113. pybind11::handle m_pathlib;
  114. bool m_requestCancelDownload = false;
  115. mutable AZStd::vector<AZStd::string> m_pythonErrorStrings;
  116. };
  117. } // namespace O3DE::ProjectManager