Skip to content

Commit

Permalink
re #11569 change date to checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed May 20, 2015
1 parent 25279b9 commit 969bca7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
6 changes: 5 additions & 1 deletion Code/Mantid/Framework/Geometry/src/Instrument/IDFObject.cpp
@@ -1,5 +1,7 @@
#include "MantidGeometry/Instrument/IDFObject.h"
#include "MantidKernel/ChecksumHelper.h"
#include <Poco/DateTimeFormatter.h>
#include <Poco/String.h>

namespace Mantid {
namespace Geometry {
Expand Down Expand Up @@ -87,7 +89,9 @@ Gets the idf file as a mangled name.
@return the idf file as a mangled name.
*/
std::string IDFObject::getMangledName() const {
return this->getFileNameOnly() + this->getFormattedLastModified();
std::string idfText = Kernel::ChecksumHelper::loadFile(getFileFullPathStr(),true);
std::string checksum = Kernel::ChecksumHelper::sha1FromString(Poco::trim(idfText));
return this->getFileNameOnly() + checksum;
}

/**
Expand Down
Expand Up @@ -11,11 +11,13 @@
#include "MantidGeometry/Rendering/vtkGeometryCacheReader.h"
#include "MantidGeometry/Rendering/vtkGeometryCacheWriter.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/ChecksumHelper.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/ProgressBase.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/Strings.h"

#include <Poco/String.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/DOMWriter.h>
Expand All @@ -27,6 +29,7 @@

#include <boost/make_shared.hpp>
#include <boost/assign/list_of.hpp>
#include <MantidKernel/ChecksumHelper.h>

using namespace Mantid;
using namespace Mantid::Kernel;
Expand Down Expand Up @@ -151,19 +154,16 @@ void InstrumentDefinitionParser::initialize(
*attribute of the XML contents
* */
std::string InstrumentDefinitionParser::getMangledName() {

// Use the file in preference if possible.
if (this->m_xmlFile->exists()) {
return m_xmlFile->getMangledName();
} else if (!pDoc.isNull()) {
std::string lastModified = pRootElem->getAttribute("last-modified");
if (lastModified.empty()) {
g_log.warning() << "The IDF that you are using doesn't contain a "
"'last-modified' field. ";
g_log.warning() << "You may not get the correct definition file loaded."
<< std::endl;
}
return m_instName + lastModified;
} else {
}
auto xml = Poco::trim(m_instrument->getXmlText());
if (!(xml.empty())) {
std::string checksum = Kernel::ChecksumHelper::sha1FromString(xml);
return m_instName + checksum;
} else {
throw std::runtime_error(
"Call InstrumentDefinitionParser::initialize() before getMangledName.");
}
Expand Down
31 changes: 28 additions & 3 deletions Code/Mantid/Framework/Geometry/test/IDFObjectTest.h
Expand Up @@ -8,6 +8,8 @@
#include <Poco/Path.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/Thread.h>
#include <Poco/SHA1Engine.h>
#include <Poco/DigestStream.h>

using Mantid::Geometry::IDFObject;
using Mantid::Kernel::ConfigService;
Expand Down Expand Up @@ -97,9 +99,32 @@ class IDFObjectTest : public CxxTest::TestSuite
{
const std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING.xml";

Poco::File file(filename);
auto head = "IDF_for_UNIT_TESTING.xml";
auto tail = Poco::DateTimeFormatter::format(file.getLastModified(), "%Y-%d-%mT%H:%M:%S");
Poco::Path path(filename);

using Poco::DigestEngine;
using Poco::SHA1Engine;
using Poco::DigestOutputStream;

std::ifstream filein(filename.c_str(), std::ios::in | std::ios::binary);
if (!filein) {
TS_FAIL("Cannot open file: " + filename);
return;
}

std::string contents;
filein.seekg(0, std::ios::end);
contents.resize(filein.tellg());
filein.seekg(0, std::ios::beg);
filein.read(&contents[0], contents.size());
filein.close();

SHA1Engine sha1;
DigestOutputStream outstr(sha1);
outstr << contents;
outstr.flush(); // to pass everything to the digest engine

auto head = path.getFileName();
auto tail = DigestEngine::digestToHex(sha1.digest());

IDFObject obj(filename);

Expand Down
Expand Up @@ -31,15 +31,18 @@ namespace Kernel {
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
namespace ChecksumHelper {
//loads a file, optionally converting line endings
MANTID_KERNEL_DLL std::string loadFile(const std::string &filepath, const bool unixEOL);
/// create a md5 checksum from a string
MANTID_KERNEL_DLL std::string md5FromString(const std::string &input);

/// create a SHA-1 checksum from a string
MANTID_KERNEL_DLL std::string sha1FromString(const std::string &input);
/// create a SHA-1 checksum from a file
MANTID_KERNEL_DLL std::string sha1FromFile(const std::string &filepath);
MANTID_KERNEL_DLL std::string sha1FromFile(const std::string &filepath, const bool unixEOL);
/// create a git checksum from a file (these match the git hash-object command)
MANTID_KERNEL_DLL std::string gitSha1FromFile(const std::string &filepath);


}

} // namespace Kernel
Expand Down
61 changes: 32 additions & 29 deletions Code/Mantid/Framework/Kernel/src/ChecksumHelper.cpp
@@ -1,8 +1,6 @@
#include "MantidKernel/ChecksumHelper.h"

#include <boost/regex.hpp>
#include <boost/shared_array.hpp>

#include <Poco/MD5Engine.h>
#include <Poco/SHA1Engine.h>
#include <Poco/DigestStream.h>
Expand All @@ -16,30 +14,7 @@ namespace Kernel {
namespace ChecksumHelper {
namespace // anonymous
{
/**
* Load contents of file into a string. The line endings are preserved
* @param filepath Full path to the file to be opened
* @param unixEOL If true convert all lineendings to Unix-style \n
*/
std::string loadFile(const std::string &filepath, const bool unixEOL = false) {
std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
if (!filein)
return "";

std::string contents;
filein.seekg(0, std::ios::end);
contents.resize(filein.tellg());
filein.seekg(0, std::ios::beg);
filein.read(&contents[0], contents.size());
filein.close();

if (unixEOL) {
static boost::regex eol(
"\\R"); // \R is Perl syntax for matching any EOL sequence
contents = boost::regex_replace(contents, eol, "\n"); // converts all to LF
}
return contents;
}

/**
* Create sha1 out of data and an optional header
Expand All @@ -54,7 +29,6 @@ std::string createSHA1(const std::string &data,

SHA1Engine sha1;
DigestOutputStream outstr(sha1);
outstr << header << data;
outstr.flush(); // to pass everything to the digest engine
return DigestEngine::digestToHex(sha1.digest());
}
Expand Down Expand Up @@ -95,12 +69,13 @@ std::string sha1FromString(const std::string &input) {

/** Creates a SHA-1 checksum from a file
* @param filepath The path to the file
* @param unixEOL If true convert all lineendings to Unix-style \n
* @returns a checksum string
**/
std::string sha1FromFile(const std::string &filepath) {
std::string sha1FromFile(const std::string &filepath, const bool unixEOL = false) {
if (filepath.empty())
return "";
return ChecksumHelper::createSHA1(loadFile(filepath));
return ChecksumHelper::createSHA1(loadFile(filepath,unixEOL));
}

/** Creates a git checksum from a file (these match the git hash-object
Expand All @@ -117,12 +92,40 @@ std::string gitSha1FromFile(const std::string &filepath) {
if (filepath.empty())
return "";
const bool unixEOL(true);
std::string contents = loadFile(filepath, unixEOL);
std::string contents = ChecksumHelper::loadFile(filepath, unixEOL);
std::stringstream header;
header << "blob " << contents.size() << '\0';
return ChecksumHelper::createSHA1(contents, header.str());
}

/**
* Load contents of file into a string. The line endings are preserved
* @param filepath Full path to the file to be opened
* @param unixEOL If true convert all lineendings to Unix-style \n
*/
std::string loadFile(const std::string &filepath, const bool unixEOL = false) {

std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
if (!filein)
return "";

std::string contents;
filein.seekg(0, std::ios::end);
contents.resize(filein.tellg());
filein.seekg(0, std::ios::beg);
filein.read(&contents[0], contents.size());
filein.close();

if (unixEOL) {
static boost::regex eol(
"\\R"); // \R is Perl syntax for matching any EOL sequence
contents = boost::regex_replace(contents, eol, "\n"); // converts all to LF
}
return contents;
}



} // namespace ChecksumHelper
} // namespace Kernel
} // namespace Mantid
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/test/ChecksumHelperTest.h
Expand Up @@ -38,7 +38,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite
const std::string data = "ChecksumHelperTest_testSha1FromFile Test this string out for size in a file";
createFile(filename,data);

std::string response = ChecksumHelper::sha1FromFile(filename);
std::string response = ChecksumHelper::sha1FromFile(filename,false);
TSM_ASSERT_EQUALS("The calculated SHA-1 hash is not as expected", "363cbe9c113b8bcba9e0aa94dbe45e67856ff26b", response);
Poco::File(filename).remove();
}
Expand Down

0 comments on commit 969bca7

Please sign in to comment.