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
32namespace 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 {
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:
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
Executes a given application and keeps track if it is still running.
Abstract PropertyUser class.
Replaces some parts of the ExternalApplicationManager to be more suitable for python scripts.
static const simox::meta::EnumNames< VenvType > VenvTypeNames
std::string deriveApplicationPath() const override
std::string deriveWorkingDir() const override
armarx::ExternalApplicationManager Base
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void addApplicationArguments(Ice::StringSeq &args) override
std::string getDefaultName() const override
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
static std::optional< path > checkCandidatePaths(const std::vector< path > &candidates, std::function< bool(path)> existsFn)
path findArmarXPackagePath(const Properties &properties) const
path getSharedVenvPath(const Properties &properties) const
friend std::ostream & operator<<(std::ostream &os, const Paths &rhs)
static std::optional< path > checkCandidateDirectories(const std::vector< path > &candidates)
path findVenvPath(const Properties &properties) const
path findPythonPackagePath(const Properties &properties) const
path findPythonBinaryPath(const Properties &properties) const
static std::optional< path > checkCandidateFiles(const std::vector< path > &candidates)
path getDedicatedVenvPath(const Properties &properties) const
path findPythonScriptPath(const Properties &properties) const
void read(PropertyUser &props, const std::string &prefix="")
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="")
std::string pythonScriptArgumentsString
Whitespace separated list of arguments.