PythonApplicationManager.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXCore::ArmarXObjects::PythonApplicationManager
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <filesystem>
26 
27 #include <SimoxUtility/meta/EnumNames.hpp>
28 
30 #include <ArmarXCore/interface/components/PythonApplicationManagerInterface.h>
31 
32 namespace armarx
33 {
34 
35  /**
36  * @defgroup Component-PythonApplicationManager PythonApplicationManager
37  * @ingroup ArmarXCore-Components
38  *
39  * The PythonApplicationManager is designed to run a python script located
40  * in an ArmarX package. It is based on the `ExternalApplicationManager`,
41  * but provides more suitable properties for such python scripts and some
42  * path discovery.
43  *
44  * @class PythonApplicationManager
45  * @ingroup Component-PythonApplicationManager
46  * @brief Replaces some parts of the `ExternalApplicationManager` to be
47  * more suitable for python scripts.
48  *
49  */
51  virtual public armarx::ExternalApplicationManager, // Component
52  virtual public armarx::PythonApplicationManagerInterface
53  {
54  public:
56 
57  enum class VenvType
58  {
59  Auto,
60  Dedicated,
61  Shared,
62  };
63  static const simox::meta::EnumNames<VenvType> VenvTypeNames;
64 
65  public:
66  protected:
67  std::string
68  getDefaultName() const override
69  {
70  return "PythonApplicationManager";
71  }
72 
74 
75  void onInitComponent() override;
76  void onConnectComponent() override;
77  void onDisconnectComponent() override;
78  void onExitComponent() override;
79 
80 
81  std::string deriveWorkingDir() const override;
82  std::string deriveApplicationPath() const override;
83  void addApplicationArguments(Ice::StringSeq& args) override;
84 
85 
86  public:
87  struct Properties
88  {
89  std::string armarxPackageName;
90  std::string armarxPythonPackagesDir = "python/";
91 
92 
93  std::string pythonPackageName;
94  std::string pythonScriptPath;
95  /// Whitespace separated list of arguments.
97  std::vector<std::string> pythonScriptArgumentsVector;
99  std::vector<std::string> pythonPathEntriesVector;
100 
101  // This is the name generated by Axii.
102  std::string venvName = ".venv";
104 
105  bool pythonPoetry = false;
106 
107  std::string workingDir = "";
108 
110  const std::string& prefix = "");
111  void read(PropertyUser& props, const std::string& prefix = "");
112  };
113 
114  struct Paths
115  {
116  using path = std::filesystem::path;
117 
118  std::string sharedVenvsDir = "shared_venvs";
119 
120  // These are absolute paths.
121 
128 
129  void derive(const Properties& properties);
130 
131  path findArmarXPackagePath(const Properties& properties) const;
132  path findPythonPackagePath(const Properties& properties) const;
133  path findPythonScriptPath(const Properties& properties) const;
134  path findVenvPath(const Properties& properties) const;
135  path findPythonBinaryPath(const Properties& properties) const;
136 
137  path getDedicatedVenvPath(const Properties& properties) const;
138  path getSharedVenvPath(const Properties& properties) const;
139 
140  friend std::ostream& operator<<(std::ostream& os, const Paths& rhs);
141 
142  static std::optional<path> checkCandidateFiles(const std::vector<path>& candidates);
143  static std::optional<path>
144  checkCandidateDirectories(const std::vector<path>& candidates);
145  static std::optional<path> checkCandidatePaths(const std::vector<path>& candidates,
146  std::function<bool(path)> existsFn);
147  };
148 
149 
150  private:
151  Properties properties;
152  Paths paths;
153 
154  bool inputOk = false;
155  };
156 } // namespace armarx
armarx::PythonApplicationManager::Properties::pythonPoetry
bool pythonPoetry
Definition: PythonApplicationManager.h:105
armarx::PythonApplicationManager::Paths::sharedVenvsDir
std::string sharedVenvsDir
Definition: PythonApplicationManager.h:118
armarx::PythonApplicationManager::Properties::pythonScriptPath
std::string pythonScriptPath
Definition: PythonApplicationManager.h:94
armarx::PythonApplicationManager::onExitComponent
void onExitComponent() override
Definition: PythonApplicationManager.cpp:251
armarx::PythonApplicationManager::Paths::findPythonScriptPath
path findPythonScriptPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:368
armarx::PythonApplicationManager
Replaces some parts of the ExternalApplicationManager to be more suitable for python scripts.
Definition: PythonApplicationManager.h:50
armarx::PythonApplicationManager::Properties::pythonPathEntriesVector
std::vector< std::string > pythonPathEntriesVector
Definition: PythonApplicationManager.h:99
ExternalApplicationManager.h
armarx::PythonApplicationManager::Paths::derive
void derive(const Properties &properties)
Definition: PythonApplicationManager.cpp:297
armarx::PythonApplicationManager::Paths::findArmarXPackagePath
path findArmarXPackagePath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:346
armarx::PythonApplicationManager::Properties::read
void read(PropertyUser &props, const std::string &prefix="")
Definition: PythonApplicationManager.cpp:91
armarx::PythonApplicationManager::VenvTypeNames
static const simox::meta::EnumNames< VenvType > VenvTypeNames
Definition: PythonApplicationManager.h:63
armarx::PythonApplicationManager::Paths::getDedicatedVenvPath
path getDedicatedVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:499
armarx::PythonApplicationManager::Paths::venvPath
path venvPath
Definition: PythonApplicationManager.h:125
armarx::PythonApplicationManager::VenvType::Auto
@ Auto
armarx::PythonApplicationManager::Properties::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="")
Definition: PythonApplicationManager.cpp:44
armarx::PythonApplicationManager::VenvType::Dedicated
@ Dedicated
armarx::PythonApplicationManager::Properties::venvType
VenvType venvType
Definition: PythonApplicationManager.h:103
armarx::PythonApplicationManager::Properties::armarxPackageName
std::string armarxPackageName
Definition: PythonApplicationManager.h:89
armarx::PythonApplicationManager::Paths::pythonPackagePath
path pythonPackagePath
Definition: PythonApplicationManager.h:123
armarx::PythonApplicationManager::Paths::checkCandidateDirectories
static std::optional< path > checkCandidateDirectories(const std::vector< path > &candidates)
Definition: PythonApplicationManager.cpp:521
armarx::PythonApplicationManager::VenvType
VenvType
Definition: PythonApplicationManager.h:57
armarx::PythonApplicationManager::Paths::checkCandidateFiles
static std::optional< path > checkCandidateFiles(const std::vector< path > &candidates)
Definition: PythonApplicationManager.cpp:513
armarx::PythonApplicationManager::Paths::pythonBinPath
path pythonBinPath
Definition: PythonApplicationManager.h:126
armarx::PythonApplicationManager::Properties
Definition: PythonApplicationManager.h:87
armarx::PythonApplicationManager::deriveApplicationPath
std::string deriveApplicationPath() const override
Definition: PythonApplicationManager.cpp:268
armarx::PythonApplicationManager::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PythonApplicationManager.cpp:107
armarx::PythonApplicationManager::onDisconnectComponent
void onDisconnectComponent() override
Definition: PythonApplicationManager.cpp:240
armarx::PythonApplicationManager::onConnectComponent
void onConnectComponent() override
Definition: PythonApplicationManager.cpp:229
armarx::PythonApplicationManager::Paths::findPythonBinaryPath
path findPythonBinaryPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:465
armarx::PythonApplicationManager::Paths::findPythonPackagePath
path findPythonPackagePath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:360
armarx::PythonApplicationManager::addApplicationArguments
void addApplicationArguments(Ice::StringSeq &args) override
Definition: PythonApplicationManager.cpp:274
armarx::PythonApplicationManager::Paths::path
std::filesystem::path path
Definition: PythonApplicationManager.h:116
armarx::PythonApplicationManager::onInitComponent
void onInitComponent() override
Definition: PythonApplicationManager.cpp:174
armarx::PythonApplicationManager::Paths::pythonScriptPath
path pythonScriptPath
Definition: PythonApplicationManager.h:124
armarx::PythonApplicationManager::Properties::armarxPythonPackagesDir
std::string armarxPythonPackagesDir
Definition: PythonApplicationManager.h:90
armarx::PythonApplicationManager::Properties::venvName
std::string venvName
Definition: PythonApplicationManager.h:102
armarx::PythonApplicationManager::Paths::checkCandidatePaths
static std::optional< path > checkCandidatePaths(const std::vector< path > &candidates, std::function< bool(path)> existsFn)
Definition: PythonApplicationManager.cpp:529
armarx::PythonApplicationManager::Paths::findVenvPath
path findVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:395
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::PythonApplicationManager::VenvType::Shared
@ Shared
armarx::PythonApplicationManager::deriveWorkingDir
std::string deriveWorkingDir() const override
Definition: PythonApplicationManager.cpp:262
armarx::PythonApplicationManager::Properties::pythonPackageName
std::string pythonPackageName
Definition: PythonApplicationManager.h:93
armarx::PythonApplicationManager::Properties::workingDir
std::string workingDir
Definition: PythonApplicationManager.h:107
armarx::ExternalApplicationManager
Executes a given application and keeps track if it is still running. Provides methods for stopping an...
Definition: ExternalApplicationManager.h:79
armarx::PythonApplicationManager::Paths
Definition: PythonApplicationManager.h:114
armarx::PropertyUser
Abstract PropertyUser class.
Definition: PropertyUser.h:63
armarx::PythonApplicationManager::Properties::pythonScriptArgumentsVector
std::vector< std::string > pythonScriptArgumentsVector
Definition: PythonApplicationManager.h:97
armarx::PythonApplicationManager::Paths::workingDir
path workingDir
Definition: PythonApplicationManager.h:127
armarx::PythonApplicationManager::Paths::armarxPackagePath
path armarxPackagePath
Definition: PythonApplicationManager.h:122
armarx::PythonApplicationManager::Paths::getSharedVenvPath
path getSharedVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:506
armarx::PythonApplicationManager::Properties::pythonScriptArgumentsString
std::string pythonScriptArgumentsString
Whitespace separated list of arguments.
Definition: PythonApplicationManager.h:96
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::PythonApplicationManager::Paths::operator<<
friend std::ostream & operator<<(std::ostream &os, const Paths &rhs)
Definition: PythonApplicationManager.cpp:285
armarx::PythonApplicationManager::getDefaultName
std::string getDefaultName() const override
Definition: PythonApplicationManager.h:68
armarx::PythonApplicationManager::Properties::pythonPathEntriesString
std::string pythonPathEntriesString
Definition: PythonApplicationManager.h:98