mwm_diff_tool in C++ instead of python

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk 2021-12-05 02:09:08 +01:00 committed by Alexander Borsuk
parent e2b2c0a402
commit 66c647eee3
7 changed files with 44 additions and 87 deletions

View file

@ -12,4 +12,6 @@ target_link_libraries(${PROJECT_NAME}
coding
)
add_subdirectory(mwm_diff_tool)
omim_add_test_subdirectory(mwm_diff_tests)

View file

@ -0,0 +1,7 @@
project(pymwm_diff)
set(SRC mwm_diff_tool.cpp)
omim_add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} mwm_diff)

View file

@ -0,0 +1,32 @@
#include "generator/mwm_diff/diff.hpp"
#include "base/cancellable.hpp"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char ** argv)
{
if (argc < 5)
{
cout << "Usage: " << argv[0] << " make|apply olderMWMDir newerMWMDir diffDir\n"
"make\n"
" Creates the diff between newer and older MWM versions at `diffDir`\n"
"apply\n"
" Applies the diff at `diffDir` to the mwm at `olderMWMDir` and stores result at `newerMWMDir`.\n"
"WARNING: THERE IS NO MWM VALIDITY CHECK!\m";
return -1;
}
const string olderMWMDir{argv[2]}, newerMWMDir{argv[3]}, diffDir{argv[4]};
if (argv[1] == "make")
return generator::mwm_diff::MakeDiff(olderMWMDir, newerMWMDir, diffDir);
// apply
base::Cancellable cancellable;
auto const res = generator::mwm_diff::ApplyDiff(olderMWMDir, newerMWMDir, diffDir, cancellable);
if (res == generator::mwm_diff::DiffApplicationResult::Ok)
return 0;
return -1; // Failed, Cancelled.
}

View file

@ -1,7 +0,0 @@
project(pymwm_diff)
set(SRC bindings.cpp)
omim_add_library(${PROJECT_NAME} MODULE ${SRC})
target_link_libraries(${PROJECT_NAME} mwm_diff)

View file

@ -1,59 +0,0 @@
#include "generator/mwm_diff/diff.hpp"
#include "base/assert.hpp"
#include "base/cancellable.hpp"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-local-typedef"
#endif
#include "pyhelpers/module_version.hpp"
#include <boost/python.hpp>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#pragma GCC diagnostic pop
using namespace std;
namespace
{
// Applies the diff at |diffPath| to the mwm at |oldMwmPath|. The resulting
// mwm is stored at |newMwmPath|.
// It is assumed that the file at |oldMwmPath| is a valid mwm and the file
// at |diffPath| is a valid mwmdiff.
// Returns true on success and false on failure.
bool ApplyDiff(string const & oldMwmPath, string const & newMwmPath, string const & diffPath)
{
base::Cancellable cancellable;
auto const result = generator::mwm_diff::ApplyDiff(oldMwmPath, newMwmPath, diffPath, cancellable);
switch (result)
{
case generator::mwm_diff::DiffApplicationResult::Ok:
return true;
case generator::mwm_diff::DiffApplicationResult::Failed:
return false;
case generator::mwm_diff::DiffApplicationResult::Cancelled:
UNREACHABLE();
break;
}
UNREACHABLE();
}
} // namespace
BOOST_PYTHON_MODULE(pymwm_diff)
{
using namespace boost::python;
scope().attr("__version__") = PYBINDINGS_VERSION;
def("make_diff", generator::mwm_diff::MakeDiff);
def("apply_diff", ApplyDiff);
}

View file

@ -1,13 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
module_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(module_dir, '..', '..', '..'))
from pyhelpers.setup import setup_omim_pybinding
NAME = "pymwm_diff"
setup_omim_pybinding(name=NAME)

View file

@ -1,13 +1,8 @@
from pathlib import Path
import subprocess
import warnings
try:
from pymwm_diff import make_diff
except ImportError:
warnings.warn('No pymwm_diff module found', ImportWarning)
class Status:
NO_NEW_VERSION = "Failed: new version doesn't exist: {new}"
INTERNAL_ERROR = "Failed: internal error (C++ module) while calculating"
@ -37,8 +32,8 @@ def calculate_diff(params):
if out.exists():
status = Status.NOTHING_TO_DO
else:
res = make_diff(old.as_posix(), new.as_posix(), out.as_posix())
if not res:
res = subprocess.run("mwm_diff_tool " + old.as_posix() + " " + new.as_posix() + " " + out.as_posix())
if res.returncode != 0:
return Status.INTERNAL_ERROR, params
diff_size = out.stat().st_size