Remove Lua scripting support

This commit is contained in:
Marcus Holland-Moritz 2020-12-05 22:31:04 +01:00
parent 1def5fed5a
commit 056e11295d
5 changed files with 3 additions and 302 deletions

View File

@ -21,7 +21,6 @@ project(dwarfs)
cmake_minimum_required(VERSION 3.13.4)
option(WITH_TESTS "build with tests" OFF)
option(WITH_LUA "build with Lua scripting support" OFF)
set(default_build_type "Release")
@ -118,9 +117,6 @@ list(
src/dwarfs/util.cpp
src/dwarfs/worker_group.cpp)
if(WITH_LUA)
list(APPEND LIBDWARFS_SRC src/dwarfs/lua_script.cpp)
endif()
add_library(dwarfs ${LIBDWARFS_SRC})
@ -265,8 +261,7 @@ foreach(tgt dwarfs ${BINARY_TARGETS})
PRIVATE DWARFS_VERSION=\"${DWARFS_VERSION}\"
$<$<BOOL:${LIBLZ4_FOUND}>:DWARFS_HAVE_LIBLZ4>
$<$<BOOL:${LIBLZMA_FOUND}>:DWARFS_HAVE_LIBLZMA>
$<$<BOOL:${LIBZSTD_FOUND}>:DWARFS_HAVE_LIBZSTD>
$<$<BOOL:${WITH_LUA}>:DWARFS_HAVE_LUA>)
$<$<BOOL:${LIBZSTD_FOUND}>:DWARFS_HAVE_LIBZSTD>)
target_compile_options(${tgt} PRIVATE -Wall -Wextra -pedantic)
@ -292,9 +287,6 @@ foreach(tgt ${BINARY_TARGETS})
PkgConfig::LIBLZMA
PkgConfig::LIBZSTD)
if(WITH_LUA)
target_link_libraries(${tgt} luabind lua)
endif()
endforeach()
target_link_libraries(dwarfs-bin PkgConfig::FUSE3)

View File

@ -1,87 +0,0 @@
--
-- Copyright (c) Marcus Holland-Moritz
--
-- This file is part of dwarfs.
--
-- dwarfs 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.
--
-- dwarfs 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
-- dwarfs. If not, see <https://www.gnu.org/licenses/>.
--
function filter(f)
-- if f.name == 'Jamroot' or (f.name == 'test' and f.type == 'dir') then
-- return false
-- end
return true
end
function autovivify(C, args, num)
for i = 1, num do
local v = args[i]
if C[v] == nil then C[v] = {} end
C = C[v]
end
return C
end
function incr(C, ...)
local args = { n = select("#", ...), ... }
C = autovivify(C, args, args.n - 2)
local field = args[args.n - 1]
C[field] = (C[field] or 0) + args[args.n]
end
function push(C, ...)
local args = { n = select("#", ...), ... }
C = autovivify(C, args, args.n - 1)
table.insert(C, args[args.n])
end
function sortbysize(tbl)
return function (a, b)
return tbl[b]["size"]/tbl[b]["num"] < tbl[a]["size"]/tbl[a]["num"]
end
end
function order(filelist)
local C = {}
for _, f in pairs(filelist) do
local _, _, base, ext = string.find(f.name, "(.*)(%.%w+)$")
if ext == nil or string.find(ext, "[a-z]") == nil then
base, ext = f.name, ""
end
incr(C, ext, "size", f.size)
incr(C, ext, "num", 1)
incr(C, ext, "name", base, "size", f.size)
incr(C, ext, "name", base, "num", 1)
push(C, ext, "name", base, "files", f)
end
local ordered = {}
local exts = {}
for k, _ in pairs(C) do table.insert(exts, k) end
table.sort(exts, sortbysize(C))
for _, ext in ipairs(exts) do
local N = C[ext]["name"]
local bases = {}
for k, _ in pairs(N) do table.insert(bases, k) end
table.sort(bases, sortbysize(N))
for _, base in ipairs(bases) do
local files = N[base]["files"]
table.sort(files, function (a, b)
return b.size < a.size
end)
for _, file in ipairs(files) do
table.insert(ordered, file)
end
end
end
return ordered
end

View File

@ -1,44 +0,0 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs 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.
*
* dwarfs 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 dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include "dwarfs/script.h"
namespace dwarfs {
class logger;
class lua_script : public script {
public:
lua_script(logger& lgr, const std::string& file);
~lua_script();
bool filter(file_interface const& fi) const override;
void order(file_vector& fvi) const override;
private:
class impl;
std::unique_ptr<impl> impl_;
};
} // namespace dwarfs

View File

@ -1,116 +0,0 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs 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.
*
* dwarfs 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 dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dwarfs/lua_script.h"
#include <iostream>
#include <unordered_map>
#include <luabind/luabind.hpp>
#include "dwarfs/logger.h"
extern "C" {
#include <lualib.h>
}
namespace dwarfs {
class lua_script::impl {
public:
impl(logger& lgr, const std::string& file);
~impl();
bool filter(file_interface const& fi) const;
void order(file_vector& fv) const;
private:
lua_State* L_;
log_proxy<debug_logger_policy> log_;
};
lua_script::impl::impl(logger& lgr, const std::string& file)
: L_(luaL_newstate())
, log_(lgr) {
luabind::open(L_);
luabind::module(L_)[luabind::class_<file_interface>("file_interface")
.property("path", &file_interface::path)
.property("name", &file_interface::name)
.property("size", &file_interface::size)
.property("type", &file_interface::type_string)];
luaL_openlibs(L_);
luaL_dofile(L_, file.c_str());
}
lua_script::impl::~impl() { lua_close(L_); }
bool lua_script::impl::filter(file_interface const& fi) const {
return luabind::call_function<bool>(L_, "filter", &fi);
}
void lua_script::impl::order(file_vector& fv) const {
luabind::object in = luabind::newtable(L_);
for (size_t i = 0; i < fv.size(); ++i) {
in[i + 1] = fv[i];
}
log_.info() << "ordering " << fv.size() << " entries...";
luabind::object out;
{
auto ti = log_.timed_info();
out = luabind::call_function<luabind::object>(L_, "order", in);
ti << fv.size() << " entries ordered";
}
if (luabind::type(out) != LUA_TTABLE) {
// TODO: better error handling
throw std::runtime_error("unexpected result type");
}
std::unordered_map<const file_interface*, size_t> vmap;
for (luabind::iterator i(out), end; i != end; ++i) {
size_t key = luabind::object_cast<size_t>(i.key()) - 1;
auto val = luabind::object_cast<const file_interface*>(*i);
vmap[val] = key;
}
fv.sort([&](const file_interface* a, const file_interface* b) {
return vmap.at(a) < vmap.at(b);
});
}
lua_script::lua_script(logger& lgr, const std::string& file)
: impl_(new impl(lgr, file)) {}
lua_script::~lua_script() = default;
bool lua_script::filter(file_interface const& fi) const {
return impl_->filter(fi);
}
void lua_script::order(file_vector& fv) const { impl_->order(fv); }
} // namespace dwarfs

View File

@ -68,9 +68,6 @@
#include "dwarfs/script.h"
#include "dwarfs/util.h"
#ifdef DWARFS_HAVE_LUA
#include "dwarfs/lua_script.h"
#endif
namespace po = boost::program_options;
@ -87,16 +84,9 @@ namespace {
#endif
#endif
#ifdef DWARFS_HAVE_LUA
constexpr const char* script_name = "dwarfs.lua";
#endif
const std::map<std::string, file_order_mode> order_choices{
{"none", file_order_mode::NONE},
{"path", file_order_mode::PATH},
#ifdef DWARFS_HAVE_LUA
{"script", file_order_mode::SCRIPT},
#endif
{"similarity", file_order_mode::SIMILARITY}};
} // namespace
@ -115,35 +105,11 @@ void validate(boost::any& v, const std::vector<std::string>& values,
v = boost::any(it->second);
}
} // namespace dwarfs
namespace {
#ifdef DWARFS_HAVE_LUA
std::string find_default_script() {
using namespace boost::filesystem;
path program(get_program_path());
path dir(program.parent_path());
std::vector<path> candidates;
candidates.emplace_back(script_name);
if (!dir.empty()) {
candidates.emplace_back(dir / script_name);
candidates.emplace_back(dir / ".." / "share" / "dwarfs" / script_name);
}
for (const auto& cand : candidates) {
if (exists(cand)) {
return canonical(cand).string();
}
}
return std::string();
}
#endif
size_t get_term_width() {
struct ::winsize w;
::ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
@ -315,12 +281,6 @@ int mkdwarfs(int argc, char** argv) {
po::value<file_order_mode>(&options.file_order)
->default_value(file_order_mode::SIMILARITY, "similarity"),
order_desc.c_str())
#ifdef DWARFS_HAVE_LUA
("script",
po::value<std::string>(&script_path)
->default_value(find_default_script()),
"Lua script for file acceptance/ordering")
#endif
("blockhash-window-sizes",
po::value<std::string>(&window_sizes),
"window sizes for block hashing")
@ -466,11 +426,6 @@ int mkdwarfs(int argc, char** argv) {
std::shared_ptr<script> script;
#ifdef DWARFS_HAVE_LUA
if (!script_path.empty()) {
script = std::make_shared<lua_script>(lgr, script_path);
}
#endif
if (options.file_order == file_order_mode::SCRIPT && !script) {
throw std::runtime_error(
@ -535,6 +490,7 @@ int mkdwarfs(int argc, char** argv) {
return prog.errors > 0;
}
} // namespace
int main(int argc, char** argv) {