Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Core/ConsoleApplication/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SCIRUN_ADD_LIBRARY(Core_ConsoleApplication
TARGET_LINK_LIBRARIES(Core_ConsoleApplication
Core_Application
Core_Command
Core_Serialization_Network_Importer
)

IF(BUILD_WITH_PYTHON)
Expand Down
2 changes: 2 additions & 0 deletions src/Core/ConsoleApplication/ConsoleCommandFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ CommandHandle ConsoleGlobalCommandFactory::create(GlobalCommands type) const
return makeShared<PrintVersionCommand>();
case GlobalCommands::LoadNetworkFile:
return makeShared<LoadFileCommandConsole>();
case GlobalCommands::ImportNetworkFile:
return makeShared<ImportFileCommandConsole>();
case GlobalCommands::PrintModules:
return makeShared<PrintModulesCommand>();
case GlobalCommands::SaveNetworkFile:
Expand Down
70 changes: 54 additions & 16 deletions src/Core/ConsoleApplication/ConsoleCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
*/


#include <cstdlib>
#include <Core/ConsoleApplication/ConsoleCommands.h>
#include <Core/Algorithms/Base/AlgorithmVariableNames.h>
#include <Dataflow/Engine/Controller/NetworkEditorController.h>
#include <Core/Application/Application.h>
#include <Dataflow/Serialization/Network/XMLSerializer.h>
#include <Dataflow/Serialization/Network/NetworkDescriptionSerialization.h>
#include <Dataflow/Serialization/Network/Importer/NetworkIO.h>
#include <Dataflow/Network/Module.h>
#include <Core/Logging/ConsoleLogger.h>
#include <Core/Python/PythonInterpreter.h>
Expand Down Expand Up @@ -63,45 +65,81 @@ namespace
/// @todo: real logger
#define LOG_CONSOLE(x) std::cout << "[SCIRun] " << x << std::endl;

bool LoadFileCommandConsole::execute()
std::string NetworkFileProcessCommandConsole::resolveFilename() const
{
return get(Variables::Filename).toFilename().string();
}

bool NetworkFileProcessCommandConsole::execute()
{
quietModulesIfNotVerbose();

auto inputFiles = Application::Instance().parameters()->inputFiles();
std::string filename;
if (!inputFiles.empty())
filename = inputFiles[0];
else
{
filename = get(Variables::Filename).toFilename().string();
}
auto filename = resolveFilename();
const auto verb = actionVerb();

LOG_CONSOLE("Attempting load of " << filename);
LOG_CONSOLE("Attempting " << verb << " of " << filename);
if (!boost::filesystem::exists(filename))
{
LOG_CONSOLE("File does not exist: " << filename);
return false;
}
try
{
auto openedFile = XMLSerializer::load_xml<NetworkFile>(filename);
auto file = processFile(filename);

if (openedFile)
if (file)
{
Application::Instance().controller()->clear();
Application::Instance().controller()->loadNetwork(openedFile);
LOG_CONSOLE("File load done: " << filename);
Application::Instance().controller()->loadNetwork(file);
LOG_CONSOLE("File " << verb << " done: " << filename);
return true;
}
LOG_CONSOLE("File load failed: " << filename);
LOG_CONSOLE("File " << verb << " failed: " << filename);
}
catch (std::exception& e)
{
LOG_CONSOLE("File " << verb << " failed: " << filename << ", exception: " << e.what());
}
catch (...)
{
LOG_CONSOLE("File load failed: " << filename);
LOG_CONSOLE("File " << verb << " failed: " << filename);
}

// Mirrors FileImportCommand/NetworkFileProcessCommand (GUI): a failure that
// matters in regression mode must exit non-zero so ctest reports it,
// instead of silently continuing.
if (failTestOnErrorInRegressionMode() && Application::Instance().parameters()->isRegressionMode())
{
LOG_CONSOLE("Regression " << verb << " failed, exiting non-zero: " << filename);
// std:: qualifier avoided: libc++'s using-declaration for quick_exit fails
// to resolve on Xcode 15.4 (macOS-14 CI runners); the global C symbol works fine.
quick_exit(1);
}
return false;
}

std::string LoadFileCommandConsole::resolveFilename() const
{
auto inputFiles = Application::Instance().parameters()->inputFiles();
if (!inputFiles.empty())
return inputFiles[0];
return NetworkFileProcessCommandConsole::resolveFilename();
}

NetworkFileHandle LoadFileCommandConsole::processFile(const std::string& filename) const
{
return XMLSerializer::load_xml<NetworkFile>(filename);
}

NetworkFileHandle ImportFileCommandConsole::processFile(const std::string& filename) const
{
auto dtdPath = Core::Application::Instance().executablePath();
const auto& modFactory = Core::Application::Instance().controller()->moduleFactory();
std::ostringstream legacyImportLog;
LegacyNetworkIO lnio(dtdPath.string(), modFactory, legacyImportLog);
return lnio.load_net(filename);
}

bool SaveFileCommandConsole::execute()
{
return !saveImpl(get(Variables::Filename).toFilename().string()).empty();
Expand Down
34 changes: 30 additions & 4 deletions src/Core/ConsoleApplication/ConsoleCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,45 @@

#include <Core/Command/Command.h>
#include <Core/Application/Application.h>
#include <Dataflow/Network/NetworkFwd.h>
#include <Core/ConsoleApplication/share.h>

namespace SCIRun {
namespace Core {
namespace Console {

class SCISHARE LoadFileCommandConsole : public Commands::FileCommand<Commands::ConsoleCommand>
// Shared logic for the two commands that turn a file on disk into a loaded
// network: resolve a filename, hand it to a format-specific parser, and
// load the result into the controller. Load (.srn5) and Import (legacy
// .srn/.net) differ only in how the filename is resolved, how the file is
// parsed, and whether a parse failure should fail a regression test.
class SCISHARE NetworkFileProcessCommandConsole : public Commands::FileCommand<Commands::ConsoleCommand>
{
public:
bool execute() final;
protected:
virtual std::string resolveFilename() const;
virtual Dataflow::Networks::NetworkFileHandle processFile(const std::string& filename) const = 0;
virtual std::string actionVerb() const = 0;
virtual bool failTestOnErrorInRegressionMode() const { return false; }
};

class SCISHARE LoadFileCommandConsole : public NetworkFileProcessCommandConsole
{
public:
LoadFileCommandConsole();
bool execute() override;
// private:
// int index_ = 0;
protected:
std::string resolveFilename() const override;
Dataflow::Networks::NetworkFileHandle processFile(const std::string& filename) const override;
std::string actionVerb() const override { return "load"; }
};

class SCISHARE ImportFileCommandConsole : public NetworkFileProcessCommandConsole
{
protected:
Dataflow::Networks::NetworkFileHandle processFile(const std::string& filename) const override;
std::string actionVerb() const override { return "import"; }
bool failTestOnErrorInRegressionMode() const override { return true; }
};

class SCISHARE SaveFileCommandConsole : public Commands::FileCommand<Commands::ConsoleCommand>, public Commands::SaveFileCommandHelper
Expand Down
Loading