From b1cc3b07a0711883d5fe39a41bc262d8bafad6d8 Mon Sep 17 00:00:00 2001 From: Pete Peterson <petersonpf@ornl.gov> Date: Mon, 17 Nov 2014 16:49:42 -0500 Subject: [PATCH] Refs #10566. Adding linux version of getOSVersionReadable. --- .../Kernel/inc/MantidKernel/ConfigService.h | 2 + .../Framework/Kernel/src/ConfigService.cpp | 75 +++++++++++++++++++ .../Framework/Kernel/test/ConfigServiceTest.h | 2 + 3 files changed, 79 insertions(+) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h index 498154a9719..3e184f71fd2 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h @@ -152,6 +152,8 @@ namespace Mantid std::string getOSArchitecture(); /// Returns the OS version std::string getOSVersion(); + /// Returns a human readable version of the OS version + std::string getOSVersionReadable(); /// Returns the username std::string getUsername(); /// Returns the current directory diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp index f81d562cb7a..ed79563c5d8 100644 --- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp +++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp @@ -25,6 +25,7 @@ #include <Poco/DOM/NodeList.h> #include <Poco/Notification.h> #include <Poco/Environment.h> +#include <Poco/File.h> #include <Poco/Process.h> #include <Poco/String.h> #ifdef _WIN32 @@ -1241,6 +1242,80 @@ std::string ConfigServiceImpl::getOSVersion() return m_pSysConfig->getString("system.osVersion"); } +/// @returns true if the file exists and can be read +bool canRead(const std::string &filename) { + // check for existence of the file + Poco::File pocoFile(filename); + if (!pocoFile.exists()) { + return false; + } + + // just return if it is readable + return pocoFile.canRead(); +} + +/** + * Gets the name of the operating system version in a human readable form. + * + * @returns The operating system desciption + */ +std::string ConfigServiceImpl::getOSVersionReadable() { + std::string description; + +#ifdef __linux__ + // read os-release + static const std::string OS_RELEASE("/etc/os-release"); + if (canRead(OS_RELEASE)) { + static const std::string PRETTY_NAME("PRETTY_NAME="); + + // open it to see if it has the magic line + std::ifstream handle(OS_RELEASE.c_str(), std::ios::in); + + // go through the file + std::string line; + while (std::getline(handle, line)) { + if (line.find(PRETTY_NAME) != std::string::npos) { + if (line.length() > PRETTY_NAME.length() + 1) { + size_t length = line.length() - PRETTY_NAME.length() - 2; + description = line.substr(PRETTY_NAME.length() + 1, length); + } + break; + } + } + + // cleanup + handle.close(); + if (!description.empty()) { + return description; + } + } + + // read redhat-release + static const std::string REDHAT_RELEASE("/etc/redhat-release"); + if (canRead(REDHAT_RELEASE)) { + // open it to see if it has the magic line + std::ifstream handle(REDHAT_RELEASE.c_str(), std::ios::in); + + // go through the file + std::string line; + while (std::getline(handle, line)) { + if (!line.empty()) { + description = line; + break; + } + } + + // cleanup + handle.close(); + if (!description.empty()) { + return description; + } + } +#endif + + return description; +} + /// @returns The name of the current user as reported by the environment. std::string ConfigServiceImpl::getUsername() { std::string username; diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h index c52d513e0fe..5f57df4a5f3 100644 --- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h +++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h @@ -186,6 +186,8 @@ public: std::string username = ConfigService::Instance().getUsername(); TS_ASSERT_LESS_THAN(0, username.length()); TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getOSVersion().length()); //check that the string is not empty + TS_ASSERT_LESS_THAN( + 0, ConfigService::Instance().getOSVersionReadable().length()); TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getCurrentDir().length()); //check that the string is not empty // TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getHomeDir().length()); //check that the string is not empty TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getTempDir().length()); //check that the string is not empty -- GitLab