// Copyright (c) 2020 Nicholas Corgan // SPDX-License-Identifier: BSL-1.0 #include #include #include #include #include static std::string __getPythonInfoJSON() { auto env = Pothos::ProxyEnvironment::make("python"); auto sys = env->findProxy("sys"); nlohmann::json topObj; auto& pythonInfo = topObj["Python Info"]; pythonInfo["Exec Prefix"] = sys.get("exec_prefix"); #if PY_MAJOR_VERSION >= 3 pythonInfo["Implementation"] = sys.get("implementation").get("name"); pythonInfo["Cache Tag"] = sys.get("implementation").get("cache_tag"); #endif auto& versionInfo = pythonInfo["Version Info"]; auto sysVersionInfo = sys.get("version_info"); versionInfo["Major"] = sysVersionInfo.call("__getitem__", 0); versionInfo["Minor"] = sysVersionInfo.call("__getitem__", 1); versionInfo["Patch"] = sysVersionInfo.call("__getitem__", 2); versionInfo["Release Level"] = sysVersionInfo.call("__getitem__", 3); versionInfo["Serial"] = sysVersionInfo.call("__getitem__", 4); auto fullVersionString = sys.get("version"); versionInfo["Version String"] = fullVersionString.substr(0, fullVersionString.find(" ")); std::string hexVersion; std::stringstream hexVersionStream; hexVersionStream << "0x"; hexVersionStream << std::hex << sys.get("hexversion"); hexVersionStream >> hexVersion; versionInfo["Version Hex"] = hexVersion; return topObj.dump(); } static std::string getPythonInfoJSON() { // Only do this once. static const std::string pythonInfoJSON = __getPythonInfoJSON(); return pythonInfoJSON; } pothos_static_block(registerPythonInfo) { Pothos::PluginRegistry::addCall( "/devices/python/info", Pothos::Callable(&getPythonInfoJSON)); }