21#include <pybind11/embed.h>
22#include <pybind11/stl.h>
34std::vector<std::string> configure_time_pylib_paths_str() {
38namespace shambindings {
40 std::optional<std::string> get_binary_path() {
44 return std::filesystem::read_symlink(
"/proc/self/exe");
45 }
catch (
const std::filesystem::filesystem_error &e) {
51 py::module_ sys = py::module_::import(
"sys");
52 std::string executable = sys.attr(
"executable").cast<std::string>();
54 }
catch (
const std::exception &e) {
59 std::optional<std::string> is_path_valid_pylib(
const std::string &path) {
60 std::filesystem::path path_fs(path);
61 if (!std::filesystem::exists(path_fs)) {
62 return "does not exist";
64 if (!std::filesystem::is_directory(path_fs)) {
65 return "is not a directory";
69 if (!std::filesystem::exists(path_fs /
"shamrock")) {
70 return "does not contain shamrock folder";
74 if (!std::filesystem::is_directory(path_fs /
"shamrock")) {
75 return "shamrock folder is not a directory";
79 if (!std::filesystem::exists(path_fs /
"shamrock" /
"__init__.py")) {
80 return "shamrock/__init__.py does not exist";
86 std::vector<std::string> get_site_packages() {
87 py::module_ site = py::module_::import(
"site");
88 auto site_packages = site.attr(
"getsitepackages")();
89 return site_packages.cast<std::vector<std::string>>();
92 std::string locate_pylib_path(
bool do_print) {
94 auto get_binary_dir = []() -> std::filesystem::path {
95 auto bpath = get_binary_path();
96 if (bpath.has_value()) {
97 return std::filesystem::path(bpath.value()).parent_path();
99 return std::filesystem::path(
".");
103 std::filesystem::path binary_dir = get_binary_dir();
105 std::filesystem::path pyshamrock_path_relative1 = binary_dir /
".." /
"pylib";
106 std::filesystem::path pyshamrock_path_relative2 = binary_dir /
".." /
"src" /
"pylib";
108 std::vector<std::string> possible_paths = {};
110 for (
const auto &path : get_site_packages()) {
111 possible_paths.push_back(path);
114 possible_paths.push_back(
"pylib");
115 possible_paths.push_back(pyshamrock_path_relative1);
116 possible_paths.push_back(pyshamrock_path_relative2);
118 for (
const auto &path : configure_time_pylib_paths_str()) {
119 possible_paths.push_back(path);
122 if (pylib_path_env_var.has_value()) {
124 shambase::println(
"using pylib path from env var: " + pylib_path_env_var.value());
126 possible_paths = {pylib_path_env_var.value()};
131 for (
const auto &path : possible_paths) {
136 std::optional<std::string> ret = std::nullopt;
138 for (
const auto &path : possible_paths) {
139 auto err = is_path_valid_pylib(path);
140 if (
err.has_value()) {
148 if (ret.has_value()) {
const char * configure_time_pylib_paths()
Path to shamrock utils lib supplied at configure time.
void println(std::string_view sv)
Prints a string to the console followed by a newline.
std::vector< std::string > split_str(std::string s, std::string delimiter)
Splits a string into a vector of substrings according to a delimiter.
std::optional< std::string > getenv_str(const char *env_var)
Get the content of the environment variable if it exist.
void err(std::string module_name, Types... var2)
Prints a log message with multiple arguments.