1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*
Copyright 2019 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/Parser/ErrorGuard.hpp>
#include <opm/input/eclipse/Parser/InputErrorAction.hpp>
#include <opm/input/eclipse/Parser/ParseContext.hpp>
#include <opm/input/eclipse/Parser/Parser.hpp>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include <fmt/format.h>
#include <getopt.h>
namespace {
struct keyword
{
keyword(const std::string& name_arg,
const std::string& filename_arg,
const std::size_t line_number_arg,
const std::size_t content_hash_arg)
: name(name_arg)
, filename(filename_arg)
, line_number(line_number_arg)
, content_hash(content_hash_arg)
{}
std::string name;
std::string filename;
std::size_t line_number;
std::size_t content_hash;
};
std::vector<keyword> load_deck(const std::string& deck_file) {
Opm::ParseContext parseContext;
Opm::ErrorGuard errors;
Opm::Parser parser;
std::vector<keyword> keywords;
/* Use the same default ParseContext as flow. */
parseContext.update(Opm::ParseContext::PARSE_RANDOM_SLASH, Opm::InputErrorAction::IGNORE);
parseContext.update(Opm::ParseContext::PARSE_MISSING_DIMS_KEYWORD, Opm::InputErrorAction::WARN);
parseContext.update(Opm::ParseContext::SUMMARY_UNKNOWN_WELL, Opm::InputErrorAction::WARN);
parseContext.update(Opm::ParseContext::SUMMARY_UNKNOWN_GROUP, Opm::InputErrorAction::WARN);
auto deck = parser.parseFile(deck_file, parseContext, errors);
for (const auto& kw : deck) {
std::stringstream ss;
const auto& location = kw.location();
ss << kw;
keywords.emplace_back(kw.name(), location.filename, location.lineno, std::hash<std::string>{}(ss.str()));
}
return keywords;
}
std::size_t make_deck_hash(const std::vector<keyword>& keywords) {
std::stringstream ss;
for (const auto& kw : keywords)
ss << kw.content_hash;
return std::hash<std::string>{}(ss.str());
}
void print_keywords(const std::vector<keyword>& keywords, std::size_t deck_hash, bool location_info) {
for (const auto& kw : keywords) {
if (location_info)
fmt::print("{:8s} : {}:{} {} \n", kw.name, kw.filename, kw.line_number, kw.content_hash);
else
fmt::print("{:8s} : {} \n", kw.name, kw.content_hash);
}
fmt::print("\n{:8s} : {}\n", "Total", deck_hash);
}
void print_help_and_exit() {
const char * help_text = R"(The purpose of the opmhash program is to load a deck and create a summary, by
diffing two such summaries it is simple to determine if two decks are similar.
For each keyword a hash of the normalized content is calculated. The output of
the program will look like this:
RUNSPEC : 13167205945009276792
TITLE : 16047371705964514902
DIMENS : 1264233216877515756
NONNC : 10052807539267647959
OIL : 6013609912232720008
WATER : 14106203893673265964
Total : 7362809723723482303
Where the 'random' integer following each keyword is the hash of the content of
that keyword. The hashing is insensitive to changes in white-space and comments
and file location. At the bottom comes a total hash of the complete content. The
hash of each keyword is insensitive to shuffling of keywords, but the total hash
depends on the keyword order.
Options:
-l : Add filename and linenumber information to each keyword.
-s : Short form - only print the hash of the complete deck.
-S : Silent form - will not print any deck output.
It is possible to add multiple deck arguments, they are then scanned repeatedly,
and the decks are compared. In the case of multiple deck arguments the exit
status of the program will be zero if all are equal and nonzero in case of
differences.
)";
std::cerr << help_text << std::endl;
exit(1);
}
} // Anonymous namespace
int main(int argc, char** argv) {
int arg_offset = 1;
bool location_info = false;
bool short_form = false;
bool silent = false;
while (true) {
int c;
c = getopt(argc, argv, "lsS");
if (c == -1)
break;
switch(c) {
case 'l':
location_info = true;
break;
case 's':
short_form = true;
break;
case 'S':
silent = true;
break;
}
}
arg_offset = optind;
if (arg_offset >= argc) {
print_help_and_exit();
}
std::vector<std::pair<std::string, std::size_t>> deck_hash_table;
for (int iarg = arg_offset; iarg < argc; iarg++) {
const std::string deck_file = argv[iarg];
auto keywords = load_deck(deck_file);
auto deck_hash = make_deck_hash(keywords);
deck_hash_table.emplace_back(deck_file, deck_hash);
if (silent) {
continue;
}
if (short_form) {
std::cout << deck_hash << std::endl;
}
else {
print_keywords(keywords, deck_hash, location_info);
}
}
if (deck_hash_table.size() > 1) {
bool equal = true;
const auto& [first_deck, first_hash] = deck_hash_table[0];
for (std::size_t index = 1; index < deck_hash_table.size(); index++) {
const auto& [deck, hash] = deck_hash_table[index];
if (first_hash != hash) {
equal = false;
}
if (silent) {
continue;
}
fmt::print("{} {} {}\n",
first_deck,
(first_hash == hash) ? "==" : "!=",
deck);
}
if (equal) {
std::exit(EXIT_SUCCESS);
}
else {
std::exit(EXIT_FAILURE);
}
}
}
|