aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-20 16:39:04 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-22 09:46:18 +0100
commit85a54868f84e002457d003ccabed9597383b9612 (patch)
treee159d46390ca691f80dc3006556191c3fad28fe1
parent1f139106854e161569f8e24ac7c0283e42bdb9be (diff)
downloadpaludis-85a54868f84e002457d003ccabed9597383b9612.tar.gz
paludis-85a54868f84e002457d003ccabed9597383b9612.tar.xz
CommandOutputManager
-rw-r--r--paludis/command_output_manager-fwd.hh28
-rw-r--r--paludis/command_output_manager.cc260
-rw-r--r--paludis/command_output_manager.hh78
-rw-r--r--paludis/files.m41
-rw-r--r--paludis/output_manager_factory.cc2
-rw-r--r--vim/syntax/paludis-output-conf.vim6
6 files changed, 373 insertions, 2 deletions
diff --git a/paludis/command_output_manager-fwd.hh b/paludis/command_output_manager-fwd.hh
new file mode 100644
index 0000000..564b34b
--- /dev/null
+++ b/paludis/command_output_manager-fwd.hh
@@ -0,0 +1,28 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_COMMAND_OUTPUT_MANAGER_FWD_HH
+#define PALUDIS_GUARD_PALUDIS_COMMAND_OUTPUT_MANAGER_FWD_HH 1
+
+namespace paludis
+{
+ struct CommandOutputManager;
+}
+
+#endif
diff --git a/paludis/command_output_manager.cc b/paludis/command_output_manager.cc
new file mode 100644
index 0000000..19a0c77
--- /dev/null
+++ b/paludis/command_output_manager.cc
@@ -0,0 +1,260 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/command_output_manager.hh>
+#include <paludis/util/pimp-impl.hh>
+#include <paludis/util/safe_ofstream.hh>
+#include <paludis/util/set.hh>
+#include <paludis/util/map.hh>
+#include <paludis/util/wrapped_forward_iterator.hh>
+#include <paludis/util/destringify.hh>
+#include <paludis/util/log.hh>
+#include <paludis/util/stringify.hh>
+#include <paludis/util/process.hh>
+#include <paludis/util/pipe.hh>
+
+using namespace paludis;
+
+CommandOutputManagerError::CommandOutputManagerError(const std::string & s) throw () :
+ Exception(s)
+{
+}
+
+namespace paludis
+{
+ template <>
+ struct Imp<CommandOutputManager>
+ {
+ std::string start_command;
+ std::string end_command;
+ std::string stdout_command;
+ std::string stderr_command;
+ std::string succeeded_command;
+ std::string nothing_more_to_come_command;
+
+ std::unique_ptr<Process> stdout_process;
+ std::unique_ptr<RunningProcessHandle> stdout_process_handle;
+ std::unique_ptr<Pipe> stdout_pipe;
+ std::unique_ptr<SafeOFStream> stdout_stream;
+
+ std::unique_ptr<Process> stderr_process;
+ std::unique_ptr<RunningProcessHandle> stderr_process_handle;
+ std::unique_ptr<Pipe> stderr_pipe;
+ std::unique_ptr<SafeOFStream> stderr_stream;
+
+ bool already_nothing_more_to_come;
+
+ Imp(
+ const std::string & s,
+ const std::string & e,
+ const std::string & so,
+ const std::string & se,
+ const std::string & su,
+ const std::string & n
+ ) :
+ start_command(s),
+ end_command(e),
+ stdout_command(so),
+ stderr_command(se),
+ succeeded_command(su),
+ nothing_more_to_come_command(n),
+ already_nothing_more_to_come(false)
+ {
+ }
+ };
+}
+
+CommandOutputManager::CommandOutputManager(const std::string & s, const std::string & e,
+ const std::string & so, const std::string & se, const std::string & su, const std::string & n) :
+ Pimp<CommandOutputManager>(s, e, so, se, su, n)
+{
+ if (! _imp->start_command.empty())
+ {
+ Process process(ProcessCommand(_imp->start_command));
+ if (0 != process.run().wait())
+ throw CommandOutputManagerError("start command returned non-zero");
+ }
+
+ _imp->stdout_pipe.reset(new Pipe(true));
+ _imp->stdout_process.reset(new Process(ProcessCommand(_imp->stdout_command)));
+ _imp->stdout_process->set_stdin_fd(_imp->stdout_pipe->read_fd());
+ _imp->stdout_process_handle.reset(new RunningProcessHandle(_imp->stdout_process->run()));
+
+ if (0 != ::close(_imp->stdout_pipe->read_fd()))
+ throw CommandOutputManagerError("close stdout_pipe read_fd failed");
+ _imp->stdout_pipe->clear_read_fd();
+ _imp->stdout_stream.reset(new SafeOFStream(_imp->stdout_pipe->write_fd()));
+
+ _imp->stderr_pipe.reset(new Pipe(true));
+ _imp->stderr_process.reset(new Process(ProcessCommand(_imp->stderr_command)));
+ _imp->stderr_process->set_stdin_fd(_imp->stderr_pipe->read_fd());
+ _imp->stderr_process_handle.reset(new RunningProcessHandle(_imp->stderr_process->run()));
+
+ if (0 != ::close(_imp->stderr_pipe->read_fd()))
+ throw CommandOutputManagerError("close stderr_pipe read_fd failed");
+ _imp->stderr_pipe->clear_read_fd();
+ _imp->stderr_stream.reset(new SafeOFStream(_imp->stderr_pipe->write_fd()));
+}
+
+CommandOutputManager::~CommandOutputManager()
+{
+ nothing_more_to_come();
+
+ if (_imp->stdout_process)
+ {
+ _imp->stdout_stream.reset();
+
+ if (0 != ::close(_imp->stdout_pipe->write_fd()))
+ throw CommandOutputManagerError("close stdout_pipe write_fd failed");
+ _imp->stdout_pipe->clear_write_fd();
+
+ if (0 != _imp->stdout_process_handle->wait())
+ Log::get_instance()->message("command_output_manager.stdout_process.non_zero", ll_warning, lc_context)
+ << "Command output manager stdout process returned non-zero";
+
+ _imp->stdout_process_handle.reset();
+ _imp->stdout_process.reset();
+ _imp->stdout_stream.reset();
+ _imp->stdout_pipe.reset();
+ }
+
+ if (_imp->stderr_process)
+ {
+ _imp->stderr_stream.reset();
+
+ if (0 != ::close(_imp->stderr_pipe->write_fd()))
+ throw CommandOutputManagerError("close stderr_pipe write_fd failed");
+ _imp->stderr_pipe->clear_write_fd();
+
+ if (0 != _imp->stderr_process_handle->wait())
+ Log::get_instance()->message("command_output_manager.stderr_process.non_zero", ll_warning, lc_context)
+ << "Command output manager stderr process returned non-zero";
+
+ _imp->stderr_process_handle.reset();
+ _imp->stderr_process.reset();
+ _imp->stderr_stream.reset();
+ _imp->stderr_pipe.reset();
+ }
+
+ if (! _imp->end_command.empty())
+ {
+ Process process(ProcessCommand(_imp->end_command));
+ if (0 != process.run().wait())
+ throw CommandOutputManagerError("end command returned non-zero");
+ }
+}
+
+std::ostream &
+CommandOutputManager::stdout_stream()
+{
+ return *_imp->stdout_stream;
+}
+
+std::ostream &
+CommandOutputManager::stderr_stream()
+{
+ return *_imp->stderr_stream;
+}
+
+void
+CommandOutputManager::message(const MessageType, const std::string &)
+{
+}
+
+void
+CommandOutputManager::succeeded()
+{
+ if (! _imp->succeeded_command.empty())
+ {
+ Process process(ProcessCommand(_imp->succeeded_command));
+ if (0 != process.run().wait())
+ throw CommandOutputManagerError("succeeded command returned non-zero");
+ }
+}
+
+void
+CommandOutputManager::flush()
+{
+}
+
+bool
+CommandOutputManager::want_to_flush() const
+{
+ return false;
+}
+
+void
+CommandOutputManager::nothing_more_to_come()
+{
+ if (_imp->already_nothing_more_to_come)
+ return;
+ _imp->already_nothing_more_to_come = true;
+
+ if (! _imp->nothing_more_to_come_command.empty())
+ {
+ stdout_stream() << std::flush;
+ stderr_stream() << std::flush;
+
+ Process process(ProcessCommand(_imp->nothing_more_to_come_command));
+ if (0 != process.run().wait())
+ throw CommandOutputManagerError("nothing more to come command returned non-zero");
+ }
+}
+
+const std::shared_ptr<const Set<std::string> >
+CommandOutputManager::factory_managers()
+{
+ std::shared_ptr<Set<std::string> > result(std::make_shared<Set<std::string>>());
+ result->insert("command");
+ return result;
+}
+
+const std::shared_ptr<OutputManager>
+CommandOutputManager::factory_create(
+ const OutputManagerFactory::KeyFunction & key_func,
+ const OutputManagerFactory::CreateChildFunction &,
+ const OutputManagerFactory::ReplaceVarsFunc & replace_vars_func)
+{
+ std::string start_command_s(key_func("start_command"));
+ std::string end_command_s(key_func("end_command"));
+ std::string stdout_command_s(key_func("stdout_command"));
+ std::string stderr_command_s(key_func("stderr_command"));
+ std::string succeeded_command_s(key_func("succeeded_command"));
+ std::string nothing_more_to_come_command_s(key_func("nothing_more_to_come_command"));
+
+ if (stdout_command_s.empty())
+ throw ConfigurationError("Key 'stdout_command' not specified when creating a command output manager");
+ stdout_command_s = replace_vars_func(stdout_command_s, std::make_shared<Map<std::string, std::string>>());
+
+ if (stderr_command_s.empty())
+ throw ConfigurationError("Key 'stderr_command' not specified when creating a command output manager");
+ stderr_command_s = replace_vars_func(stderr_command_s, std::make_shared<Map<std::string, std::string>>());
+
+ start_command_s = replace_vars_func(start_command_s, std::make_shared<Map<std::string, std::string>>());
+ end_command_s = replace_vars_func(end_command_s, std::make_shared<Map<std::string, std::string>>());
+ succeeded_command_s = replace_vars_func(succeeded_command_s, std::make_shared<Map<std::string, std::string>>());
+ nothing_more_to_come_command_s = replace_vars_func(nothing_more_to_come_command_s, std::make_shared<Map<std::string, std::string>>());
+
+ return std::make_shared<CommandOutputManager>(
+ start_command_s, end_command_s, stdout_command_s, stderr_command_s,
+ succeeded_command_s, nothing_more_to_come_command_s);
+}
+
+template class Pimp<CommandOutputManager>;
+
diff --git a/paludis/command_output_manager.hh b/paludis/command_output_manager.hh
new file mode 100644
index 0000000..5ca8af3
--- /dev/null
+++ b/paludis/command_output_manager.hh
@@ -0,0 +1,78 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_COMMAND_OUTPUT_MANAGER_HH
+#define PALUDIS_GUARD_PALUDIS_COMMAND_OUTPUT_MANAGER_HH 1
+
+#include <paludis/command_output_manager-fwd.hh>
+#include <paludis/output_manager.hh>
+#include <paludis/output_manager_factory.hh>
+#include <paludis/util/set-fwd.hh>
+#include <paludis/util/pimp.hh>
+#include <paludis/util/exception.hh>
+#include <memory>
+#include <functional>
+
+namespace paludis
+{
+ class PALUDIS_VISIBLE CommandOutputManagerError :
+ public Exception
+ {
+ public:
+ CommandOutputManagerError(const std::string &) throw ();
+ };
+
+ class PALUDIS_VISIBLE CommandOutputManager :
+ private Pimp<CommandOutputManager>,
+ public OutputManager
+ {
+ public:
+ CommandOutputManager(
+ const std::string & start_command,
+ const std::string & end_command,
+ const std::string & stdout_command,
+ const std::string & stderr_command,
+ const std::string & succeeded_command,
+ const std::string & nothing_more_to_come_command
+ );
+ ~CommandOutputManager();
+
+ virtual std::ostream & stdout_stream() PALUDIS_ATTRIBUTE((warn_unused_result));
+ virtual std::ostream & stderr_stream() PALUDIS_ATTRIBUTE((warn_unused_result));
+
+ virtual void succeeded();
+ virtual void flush();
+ virtual bool want_to_flush() const;
+ virtual void nothing_more_to_come();
+ virtual void message(const MessageType, const std::string &);
+
+ static const std::shared_ptr<const Set<std::string> > factory_managers()
+ PALUDIS_ATTRIBUTE((warn_unused_result));
+
+ static const std::shared_ptr<OutputManager> factory_create(
+ const OutputManagerFactory::KeyFunction &,
+ const OutputManagerFactory::CreateChildFunction &,
+ const OutputManagerFactory::ReplaceVarsFunc &)
+ PALUDIS_ATTRIBUTE((warn_unused_result));
+ };
+
+ extern template class Pimp<CommandOutputManager>;
+}
+
+#endif
diff --git a/paludis/files.m4 b/paludis/files.m4
index 16713e5..6657ec7 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -20,6 +20,7 @@ add(`changed_choices', `hh', `cc', `fwd')
add(`choice', `hh', `cc', `fwd')
add(`comma_separated_dep_parser', `hh', `cc', `test')
add(`comma_separated_dep_printer', `hh', `cc')
+add(`command_output_manager', `hh', `cc', `fwd')
add(`common_sets', `hh', `cc', `fwd')
add(`contents', `hh', `cc', `fwd')
add(`create_output_manager_info', `hh', `cc', `fwd', `se')
diff --git a/paludis/output_manager_factory.cc b/paludis/output_manager_factory.cc
index 37bd78d..bbdfcd7 100644
--- a/paludis/output_manager_factory.cc
+++ b/paludis/output_manager_factory.cc
@@ -35,6 +35,7 @@
#include <list>
#include <paludis/buffer_output_manager.hh>
+#include <paludis/command_output_manager.hh>
#include <paludis/file_output_manager.hh>
#include <paludis/format_messages_output_manager.hh>
#include <paludis/forward_at_finish_output_manager.hh>
@@ -95,6 +96,7 @@ OutputManagerFactory::OutputManagerFactory() :
{
/* we might want to make this plugin loadable at some point */
add_manager(BufferOutputManager::factory_managers(), BufferOutputManager::factory_create);
+ add_manager(CommandOutputManager::factory_managers(), CommandOutputManager::factory_create);
add_manager(FileOutputManager::factory_managers(), FileOutputManager::factory_create);
add_manager(FormatMessagesOutputManager::factory_managers(), FormatMessagesOutputManager::factory_create);
add_manager(ForwardAtFinishOutputManager::factory_managers(), ForwardAtFinishOutputManager::factory_create);
diff --git a/vim/syntax/paludis-output-conf.vim b/vim/syntax/paludis-output-conf.vim
index 4f00f48..fb23228 100644
--- a/vim/syntax/paludis-output-conf.vim
+++ b/vim/syntax/paludis-output-conf.vim
@@ -44,10 +44,12 @@ syn keyword PaludisOutputConfKnownKey contained
\ child condition_variable if_true if_false if_unset
\ filename keep_on_empty keep_on_success summary_output_manager
\ format_debug format_info format_warn format_error format_log
- \ summary_output_message
+ \ summary_output_message start_command end_command
+ \ nothing_more_to_come_command succeeded_command
+ \ stdout_command stderr_command
syn keyword PaludisOutputConfKnownValue contained
- \ buffer file format_messages forward_at_finish ipc tee standard
+ \ buffer file format_messages forward_at_finish ipc tee standard command
syn match PaludisOutputConfVariable contained
\ /\$\({[^}]\+}\|[a-zA-Z0-9_]\+\)/ skipwhite