diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h index 498154a9719c8372bca2b4eb9c5442680b732c79..3e184f71fd21b18f88037ab09cdbae81add41b5f 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 f81d562cb7a124bcc169ed39e19de9f024c8497a..ed79563c5d879caa11195a037f5697ebc57946c1 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 c52d513e0fe1b327637e723fb37b0c397016f976..5f57df4a5f390c0180dc97a6b8f3fff9c8abda7d 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