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 
29 #include <ArmarXCore/interface/components/PythonApplicationManagerInterface.h>
31 
32 
33 namespace armarx
34 {
35 
36  /**
37  * @defgroup Component-PythonApplicationManager PythonApplicationManager
38  * @ingroup ArmarXCore-Components
39  *
40  * The PythonApplicationManager is designed to run a python script located
41  * in an ArmarX package. It is based on the `ExternalApplicationManager`,
42  * but provides more suitable properties for such python scripts and some
43  * path discovery.
44  *
45  * @class PythonApplicationManager
46  * @ingroup Component-PythonApplicationManager
47  * @brief Replaces some parts of the `ExternalApplicationManager` to be
48  * more suitable for python scripts.
49  *
50  */
52  virtual public armarx::ExternalApplicationManager, // Component
53  virtual public armarx::PythonApplicationManagerInterface
54  {
55  public:
56 
58 
59  enum class VenvType
60  {
61  Auto,
62  Dedicated,
63  Shared,
64  };
65  static const simox::meta::EnumNames<VenvType> VenvTypeNames;
66 
67  public:
68 
69 
70 
71  protected:
72 
73  std::string getDefaultName() const override
74  {
75  return "PythonApplicationManager";
76  }
77 
79 
80  void onInitComponent() override;
81  void onConnectComponent() override;
82  void onDisconnectComponent() override;
83  void onExitComponent() override;
84 
85 
86  std::string deriveWorkingDir() const override;
87  std::string deriveApplicationPath() const override;
88  void addApplicationArguments(Ice::StringSeq& args) override;
89 
90 
91  public:
92 
93  struct Properties
94  {
95  std::string armarxPackageName;
96  std::string armarxPythonPackagesDir = "python/";
97 
98 
99 
100  std::string pythonPackageName;
101  std::string pythonScriptPath;
102  /// Whitespace separated list of arguments.
104  std::vector<std::string> pythonScriptArgumentsVector;
106  std::vector<std::string> pythonPathEntriesVector;
107 
108  // This is the name generated by Axii.
109  std::string venvName = ".venv";
111 
112  bool pythonPoetry = false;
113 
114  std::string workingDir = "";
115 
116  void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix = "");
117  void read(PropertyUser& props, const std::string& prefix = "");
118  };
119 
120  struct Paths
121  {
122  using path = std::filesystem::path;
123 
124  std::string sharedVenvsDir = "shared_venvs";
125 
126  // These are absolute paths.
127 
134 
135  void derive(const Properties& properties);
136 
137  path findArmarXPackagePath(const Properties& properties) const;
138  path findPythonPackagePath(const Properties& properties) const;
139  path findPythonScriptPath(const Properties& properties) const;
140  path findVenvPath(const Properties& properties) const;
141  path findPythonBinaryPath(const Properties& properties) const;
142 
143  path getDedicatedVenvPath(const Properties& properties) const;
144  path getSharedVenvPath(const Properties& properties) const;
145 
146  friend std::ostream& operator<<(std::ostream& os, const Paths& rhs);
147 
148  static std::optional<path> checkCandidateFiles(const std::vector<path>& candidates);
149  static std::optional<path> checkCandidateDirectories(const std::vector<path>& candidates);
150  static std::optional<path> checkCandidatePaths(const std::vector<path>& candidates, std::function<bool(path)> existsFn);
151 
152  };
153 
154 
155  private:
156 
157  Properties properties;
158  Paths paths;
159 
160  bool inputOk = false;
161 
162 
163  };
164 }
165 
armarx::PythonApplicationManager::Properties::pythonPoetry
bool pythonPoetry
Definition: PythonApplicationManager.h:112
armarx::PythonApplicationManager::Paths::sharedVenvsDir
std::string sharedVenvsDir
Definition: PythonApplicationManager.h:124
armarx::PythonApplicationManager::Properties::pythonScriptPath
std::string pythonScriptPath
Definition: PythonApplicationManager.h:101
armarx::PythonApplicationManager::onExitComponent
void onExitComponent() override
Definition: PythonApplicationManager.cpp:193
armarx::PythonApplicationManager::Paths::findPythonScriptPath
path findPythonScriptPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:296
armarx::PythonApplicationManager
Replaces some parts of the ExternalApplicationManager to be more suitable for python scripts.
Definition: PythonApplicationManager.h:51
armarx::PythonApplicationManager::Properties::pythonPathEntriesVector
std::vector< std::string > pythonPathEntriesVector
Definition: PythonApplicationManager.h:106
ExternalApplicationManager.h
armarx::PythonApplicationManager::Paths::derive
void derive(const Properties &properties)
Definition: PythonApplicationManager.cpp:232
armarx::PythonApplicationManager::Paths::findArmarXPackagePath
path findArmarXPackagePath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:277
armarx::PythonApplicationManager::Properties::read
void read(PropertyUser &props, const std::string &prefix="")
Definition: PythonApplicationManager.cpp:72
armarx::PythonApplicationManager::VenvTypeNames
static const simox::meta::EnumNames< VenvType > VenvTypeNames
Definition: PythonApplicationManager.h:65
armarx::PythonApplicationManager::Paths::getDedicatedVenvPath
path getDedicatedVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:426
armarx::PythonApplicationManager::Paths::venvPath
path venvPath
Definition: PythonApplicationManager.h:131
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:110
armarx::PythonApplicationManager::Properties::armarxPackageName
std::string armarxPackageName
Definition: PythonApplicationManager.h:95
armarx::PythonApplicationManager::Paths::pythonPackagePath
path pythonPackagePath
Definition: PythonApplicationManager.h:129
armarx::PythonApplicationManager::Paths::checkCandidateDirectories
static std::optional< path > checkCandidateDirectories(const std::vector< path > &candidates)
Definition: PythonApplicationManager.cpp:448
armarx::PythonApplicationManager::VenvType
VenvType
Definition: PythonApplicationManager.h:59
armarx::PythonApplicationManager::Paths::checkCandidateFiles
static std::optional< path > checkCandidateFiles(const std::vector< path > &candidates)
Definition: PythonApplicationManager.cpp:440
armarx::PythonApplicationManager::Paths::pythonBinPath
path pythonBinPath
Definition: PythonApplicationManager.h:132
armarx::PythonApplicationManager::Properties
Definition: PythonApplicationManager.h:93
armarx::PythonApplicationManager::deriveApplicationPath
std::string deriveApplicationPath() const override
Definition: PythonApplicationManager.cpp:208
armarx::PythonApplicationManager::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PythonApplicationManager.cpp:86
armarx::PythonApplicationManager::onDisconnectComponent
void onDisconnectComponent() override
Definition: PythonApplicationManager.cpp:182
armarx::PythonApplicationManager::onConnectComponent
void onConnectComponent() override
Definition: PythonApplicationManager.cpp:171
armarx::PythonApplicationManager::Paths::findPythonBinaryPath
path findPythonBinaryPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:394
armarx::PythonApplicationManager::Paths::findPythonPackagePath
path findPythonPackagePath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:289
armarx::PythonApplicationManager::addApplicationArguments
void addApplicationArguments(Ice::StringSeq &args) override
Definition: PythonApplicationManager.cpp:213
armarx::PythonApplicationManager::Paths::path
std::filesystem::path path
Definition: PythonApplicationManager.h:122
armarx::PythonApplicationManager::onInitComponent
void onInitComponent() override
Definition: PythonApplicationManager.cpp:118
armarx::PythonApplicationManager::Paths::pythonScriptPath
path pythonScriptPath
Definition: PythonApplicationManager.h:130
armarx::PythonApplicationManager::Properties::armarxPythonPackagesDir
std::string armarxPythonPackagesDir
Definition: PythonApplicationManager.h:96
armarx::PythonApplicationManager::Properties::venvName
std::string venvName
Definition: PythonApplicationManager.h:109
armarx::PythonApplicationManager::Paths::checkCandidatePaths
static std::optional< path > checkCandidatePaths(const std::vector< path > &candidates, std::function< bool(path)> existsFn)
Definition: PythonApplicationManager.cpp:456
armarx::PythonApplicationManager::Paths::findVenvPath
path findVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:326
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::PythonApplicationManager::VenvType::Shared
@ Shared
armarx::PythonApplicationManager::deriveWorkingDir
std::string deriveWorkingDir() const override
Definition: PythonApplicationManager.cpp:203
armarx::PythonApplicationManager::Properties::pythonPackageName
std::string pythonPackageName
Definition: PythonApplicationManager.h:100
armarx::PythonApplicationManager::Properties::workingDir
std::string workingDir
Definition: PythonApplicationManager.h:114
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:120
armarx::PropertyUser
Abstract PropertyUser class.
Definition: PropertyUser.h:62
armarx::PythonApplicationManager::Properties::pythonScriptArgumentsVector
std::vector< std::string > pythonScriptArgumentsVector
Definition: PythonApplicationManager.h:104
armarx::PythonApplicationManager::Paths::workingDir
path workingDir
Definition: PythonApplicationManager.h:133
armarx::PythonApplicationManager::Paths::armarxPackagePath
path armarxPackagePath
Definition: PythonApplicationManager.h:128
armarx::PythonApplicationManager::Paths::getSharedVenvPath
path getSharedVenvPath(const Properties &properties) const
Definition: PythonApplicationManager.cpp:432
armarx::PythonApplicationManager::Properties::pythonScriptArgumentsString
std::string pythonScriptArgumentsString
Whitespace separated list of arguments.
Definition: PythonApplicationManager.h:103
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::PythonApplicationManager::Paths::operator<<
friend std::ostream & operator<<(std::ostream &os, const Paths &rhs)
Definition: PythonApplicationManager.cpp:221
armarx::PythonApplicationManager::getDefaultName
std::string getDefaultName() const override
Definition: PythonApplicationManager.h:73
armarx::PythonApplicationManager::Properties::pythonPathEntriesString
std::string pythonPathEntriesString
Definition: PythonApplicationManager.h:105