PythonSystemComponent.h 3.9 KB

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