PythonSystemComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <EditorPythonBindings/EditorPythonBindingsSymbols.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/std/containers/unordered_set.h>
  12. #include <AzCore/std/parallel/semaphore.h>
  13. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  14. #include <AzToolsFramework/API/EditorPythonRunnerRequestsBus.h>
  15. #include <AzToolsFramework/API/PythonLoader.h>
  16. #include <Source/ActionManager/PythonActionManagerHandler.h>
  17. namespace EditorPythonBindings
  18. {
  19. /**
  20. * Manages the Python interpreter inside this Gem (Editor only)
  21. * - redirects the Python standard output and error streams to AZ_TracePrintf and AZ_Warning, respectively
  22. */
  23. class PythonSystemComponent
  24. : public AZ::Component
  25. , protected AzToolsFramework::EditorPythonEventsInterface
  26. , protected AzToolsFramework::EditorPythonRunnerRequestBus::Handler
  27. {
  28. public:
  29. AZ_COMPONENT(PythonSystemComponent, PythonSystemComponentTypeId, AZ::Component);
  30. static void Reflect(AZ::ReflectContext* context);
  31. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  32. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  33. protected:
  34. ////////////////////////////////////////////////////////////////////////
  35. // AZ::Component interface implementation
  36. void Activate() override;
  37. void Deactivate() override;
  38. ////////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////
  40. // AzToolsFramework::EditorPythonEventsInterface
  41. bool StartPython(bool silenceWarnings = false) override;
  42. bool StopPython(bool silenceWarnings = false) override;
  43. bool IsPythonActive() override;
  44. void WaitForInitialization() override;
  45. void ExecuteWithLock(AZStd::function<void()> executionCallback) override;
  46. bool TryExecuteWithLock(AZStd::function<void()> executionCallback) override;
  47. ////////////////////////////////////////////////////////////////////////
  48. ////////////////////////////////////////////////////////////////////////
  49. // AzToolsFramework::EditorPythonRunnerRequestBus::Handler interface implementation
  50. void ExecuteByString(AZStd::string_view script, bool printResult) override;
  51. bool ExecuteByFilename(AZStd::string_view filename) override;
  52. bool ExecuteByFilenameWithArgs(AZStd::string_view filename, const AZStd::vector<AZStd::string_view>& args) override;
  53. bool ExecuteByFilenameAsTest(AZStd::string_view filename, AZStd::string_view testCase, const AZStd::vector<AZStd::string_view>& args) override;
  54. ////////////////////////////////////////////////////////////////////////
  55. private:
  56. class SymbolLogHelper;
  57. class PythonGILScopedLock;
  58. // handle multiple Python initializers and threads
  59. AZStd::atomic_int m_initalizeWaiterCount {0};
  60. AZStd::semaphore m_initalizeWaiter;
  61. AZStd::recursive_mutex m_lock;
  62. int m_lockRecursiveCounter = 0;
  63. AZStd::shared_ptr<SymbolLogHelper> m_symbolLogHelper;
  64. PythonActionManagerHandler m_pythonActionManagerHandler;
  65. AzToolsFramework::EmbeddedPython::PythonLoader m_pythonLoader;
  66. enum class Result
  67. {
  68. Okay,
  69. Error_IsNotInitialized,
  70. Error_InvalidFilename,
  71. Error_MissingFile,
  72. Error_FileOpenValidation,
  73. Error_InternalException,
  74. Error_PythonException,
  75. };
  76. Result EvaluateFile(AZStd::string_view filename, const AZStd::vector<AZStd::string_view>& args);
  77. // bootstrap logic and data
  78. using PythonPathStack = AZStd::vector<AZStd::string>;
  79. void DiscoverPythonPaths(PythonPathStack& pythonPathStack);
  80. void ExecuteBootstrapScripts(const PythonPathStack& pythonPathStack);
  81. bool ExtendSysPath(const AZStd::unordered_set<AZStd::string>& extendPaths);
  82. // starts the Python interpreter
  83. bool StartPythonInterpreter(const PythonPathStack& pythonPathStack);
  84. bool StopPythonInterpreter();
  85. };
  86. }