PythonBindings.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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> GetAllGemInfos(const QString& projectPath) override;
  43. AZ::Outcome<QHash<QString /*gem name with specifier*/, QString /* gem path */>, AZStd::string> GetEnabledGems(
  44. const QString& projectPath, bool includeDependencies) const override;
  45. AZ::Outcome<void, AZStd::string> RegisterGem(const QString& gemPath, const QString& projectPath = {}) override;
  46. AZ::Outcome<void, AZStd::string> UnregisterGem(const QString& gemPath, const QString& projectPath = {}) override;
  47. // Project
  48. AZ::Outcome<ProjectInfo, IPythonBindings::ErrorPair> CreateProject(const QString& projectTemplatePath, const ProjectInfo& projectInfo, bool registerProject = true) override;
  49. AZ::Outcome<ProjectInfo> GetProject(const QString& path) override;
  50. AZ::Outcome<QVector<ProjectInfo>> GetProjects() override;
  51. AZ::Outcome<QVector<ProjectInfo>, AZStd::string> GetProjectsForRepo(const QString& repoUri, bool enabledOnly = true) override;
  52. AZ::Outcome<QVector<ProjectInfo>, AZStd::string> GetProjectsForAllRepos(bool enabledOnly = true) override;
  53. DetailedOutcome AddProject(const QString& path, bool force = false) override;
  54. DetailedOutcome RemoveProject(const QString& path) override;
  55. AZ::Outcome<void, AZStd::string> UpdateProject(const ProjectInfo& projectInfo) override;
  56. AZ::Outcome<QStringList, AZStd::string> GetIncompatibleProjectGems(
  57. const QStringList& gemPaths, const QStringList& gemNames, const QString& projectPath) override;
  58. AZ::Outcome<QStringList, IPythonBindings::ErrorPair> GetProjectEngineIncompatibleObjects(
  59. const QString& projectPath, const QString& enginePath = "") override;
  60. DetailedOutcome AddGemsToProject(
  61. const QStringList& gemPaths, const QStringList& gemNames, const QString& projectPath, bool force = false) override;
  62. AZ::Outcome<void, AZStd::string> RemoveGemFromProject(const QString& gemName, const QString& projectPath) override;
  63. bool RemoveInvalidProjects() override;
  64. // Remote Repos
  65. AZ::Outcome<void, AZStd::string> RefreshGemRepo(const QString& repoUri, bool downloadMissingOnly = false) override;
  66. bool RefreshAllGemRepos(bool downloadMissingOnly = false) override;
  67. DetailedOutcome AddGemRepo(const QString& repoUri) override;
  68. bool RemoveGemRepo(const QString& repoUri) override;
  69. bool SetRepoEnabled(const QString& repoUri, bool enabled) override;
  70. AZ::Outcome<QVector<GemRepoInfo>, AZStd::string> GetAllGemRepoInfos() override;
  71. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetGemInfosForRepo(const QString& repoUri, bool enabledOnly = true) override;
  72. AZ::Outcome<QVector<GemInfo>, AZStd::string> GetGemInfosForAllRepos(const QString& projectPath, bool enabledOnly = true) override;
  73. DetailedOutcome DownloadGem(
  74. const QString& gemName, const QString& path, std::function<void(int, int)> gemProgressCallback, bool force = false) override;
  75. DetailedOutcome DownloadProject(
  76. const QString& projectName, const QString& path, std::function<void(int, int)> projectProgressCallback, bool force = false) override;
  77. DetailedOutcome DownloadTemplate(
  78. const QString& templateName, const QString& path, std::function<void(int, int)> templateProgressCallback, bool force = false) override;
  79. void CancelDownload() override;
  80. bool IsGemUpdateAvaliable(const QString& gemName, const QString& lastUpdated) override;
  81. // Templates
  82. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplates() override;
  83. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplatesForRepo(const QString& repoUri, bool enabledOnly = true) const override;
  84. AZ::Outcome<QVector<ProjectTemplateInfo>> GetProjectTemplatesForAllRepos(bool enabledOnly = true) const override;
  85. AZ::Outcome<QVector<TemplateInfo>> GetGemTemplates() override;
  86. void AddErrorString(AZStd::string errorString) override;
  87. protected:
  88. static void OnStdOut(const char* msg);
  89. static void OnStdError(const char* msg);
  90. private:
  91. AZ_DISABLE_COPY_MOVE(PythonBindings);
  92. AZ::Outcome<void, AZStd::string> ExecuteWithLockErrorHandling(AZStd::function<void()> executionCallback) const;
  93. bool ExecuteWithLock(AZStd::function<void()> executionCallback) const;
  94. EngineInfo EngineInfoFromPath(pybind11::handle enginePath);
  95. GemInfo GemInfoFromPath(pybind11::handle path, pybind11::handle pyProjectPath);
  96. GemRepoInfo GetGemRepoInfo(pybind11::handle repoUri);
  97. ProjectInfo ProjectInfoFromPath(pybind11::handle path);
  98. ProjectInfo ProjectInfoFromDict(pybind11::handle projectData, const QString& path = {});
  99. ProjectTemplateInfo ProjectTemplateInfoFromPath(pybind11::handle path) const;
  100. ProjectTemplateInfo ProjectTemplateInfoFromDict(pybind11::handle templateData, const QString& path = {}) const;
  101. TemplateInfo TemplateInfoFromPath(pybind11::handle path) const;
  102. TemplateInfo TemplateInfoFromDict(pybind11::handle templateData, const QString& path = {}) const;
  103. AZ::Outcome<void, AZStd::string> GemRegistration(const QString& gemPath, const QString& projectPath, bool remove = false);
  104. bool StopPython();
  105. IPythonBindings::ErrorPair GetErrorPair();
  106. bool ExtendSysPath(const AZStd::vector<AZ::IO::Path>& extendPaths);
  107. bool m_pythonStarted = false;
  108. AZ::IO::FixedMaxPath m_enginePath;
  109. mutable AZStd::recursive_mutex m_lock;
  110. pybind11::handle m_gemProperties;
  111. pybind11::handle m_engineTemplate;
  112. pybind11::handle m_engineProperties;
  113. pybind11::handle m_register;
  114. pybind11::handle m_manifest;
  115. pybind11::handle m_enableGemProject;
  116. pybind11::handle m_disableGemProject;
  117. pybind11::handle m_editProjectProperties;
  118. pybind11::handle m_projectManagerInterface;
  119. pybind11::handle m_download;
  120. pybind11::handle m_repo;
  121. pybind11::handle m_pathlib;
  122. bool m_requestCancelDownload = false;
  123. mutable AZStd::vector<AZStd::string> m_pythonErrorStrings;
  124. };
  125. } // namespace O3DE::ProjectManager