aboutsummaryrefslogtreecommitdiff
path: root/src/clients/cave/cmd_execute_resolution.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/clients/cave/cmd_execute_resolution.cc')
-rw-r--r--src/clients/cave/cmd_execute_resolution.cc276
1 files changed, 276 insertions, 0 deletions
diff --git a/src/clients/cave/cmd_execute_resolution.cc b/src/clients/cave/cmd_execute_resolution.cc
new file mode 100644
index 000000000..ebfa68b32
--- /dev/null
+++ b/src/clients/cave/cmd_execute_resolution.cc
@@ -0,0 +1,276 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2009 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 "cmd_execute_resolution.hh"
+#include "cmd_resolve_cmdline.hh"
+#include "exceptions.hh"
+#include "command_command_line.hh"
+#include "formats.hh"
+#include "colour_formatter.hh"
+#include <paludis/args/do_help.hh>
+#include <paludis/util/make_shared_ptr.hh>
+#include <paludis/util/safe_ifstream.hh>
+#include <paludis/util/system.hh>
+#include <paludis/util/destringify.hh>
+#include <paludis/util/stringify.hh>
+#include <paludis/util/join.hh>
+#include <paludis/util/iterator_funcs.hh>
+#include <paludis/util/options.hh>
+#include <paludis/resolver/resolutions.hh>
+#include <paludis/resolver/serialise.hh>
+#include <paludis/resolver/reason.hh>
+#include <paludis/resolver/sanitised_dependencies.hh>
+#include <paludis/resolver/resolution.hh>
+#include <paludis/resolver/decision.hh>
+#include <paludis/resolver/destinations.hh>
+#include <paludis/resolver/constraint.hh>
+#include <paludis/resolver/resolver.hh>
+#include <paludis/resolver/qpn_s.hh>
+#include <paludis/package_id.hh>
+#include <paludis/version_spec.hh>
+#include <paludis/metadata_key.hh>
+#include <paludis/choice.hh>
+#include <paludis/user_dep_spec.hh>
+#include <paludis/match_package.hh>
+
+#include <set>
+#include <iterator>
+#include <iostream>
+#include <cstdlib>
+
+using namespace paludis;
+using namespace cave;
+using namespace paludis::resolver;
+
+using std::cout;
+using std::endl;
+
+namespace
+{
+ struct ExecuteResolutionCommandLine :
+ CaveCommandCommandLine
+ {
+ args::ArgsGroup g_general_options;
+ args::SwitchArg a_pretend;
+
+ ResolveCommandLineExecutionOptions execution_options;
+ ResolveCommandLineProgramOptions program_options;
+
+ ExecuteResolutionCommandLine() :
+ g_general_options(main_options_section(), "General Options", "General options."),
+ a_pretend(&g_general_options, "pretend", '\0', "Only carry out the pretend action", false),
+ execution_options(this),
+ program_options(this)
+ {
+ add_environment_variable("PALUDIS_SERIALISED_RESOLUTION_FD",
+ "The file descriptor on which the serialised resolution can be found.");
+ }
+
+ virtual std::string app_name() const
+ {
+ return "cave execute-resolution";
+ }
+
+ virtual std::string app_synopsis() const
+ {
+ return "Executes a dependency resolution created using 'cave execute'.";
+ }
+
+ virtual std::string app_description() const
+ {
+ return "Execute a dependency resolution created using 'cave resolve'. Mostly for "
+ "internal use; most users will not use this command directly.";
+ }
+ };
+
+ int do_pretend(
+ const std::tr1::shared_ptr<Environment> &,
+ const ExecuteResolutionCommandLine & cmdline,
+ const std::tr1::shared_ptr<const Decision> & c)
+ {
+ const std::tr1::shared_ptr<const PackageID> id(c->if_package_id());
+ Context context("When pretending for '" + stringify(*id) + "':");
+
+ std::string command(cmdline.program_options.a_perform_program.argument());
+ if (command.empty())
+ command = "$CAVE perform";
+
+ command.append(" pretend --hooks ");
+ command.append(stringify(id->uniquely_identifying_spec()));
+
+ paludis::Command cmd(command);
+ return run_command(cmd);
+ }
+
+ void starting_action(
+ const std::string & action,
+ const std::tr1::shared_ptr<const Decision> & c)
+ {
+ cout << endl;
+ cout << c::bold_blue() << "Starting " << action << " for "
+ << *c->if_package_id() << "..." << c::normal() << endl;
+ cout << endl;
+ }
+
+ void done_action(
+ const std::string & action,
+ const std::tr1::shared_ptr<const Decision> & c,
+ const bool success)
+ {
+ cout << endl;
+ if (success)
+ cout << c::bold_green() << "Done " << action << " for "
+ << *c->if_package_id() << c::normal() << endl;
+ else
+ cout << c::bold_red() << "Failed " << action << " for "
+ << *c->if_package_id() << c::normal() << endl;
+ cout << endl;
+ }
+
+ int do_fetch(
+ const std::tr1::shared_ptr<Environment> &,
+ const ExecuteResolutionCommandLine & cmdline,
+ const std::tr1::shared_ptr<const Decision> & c)
+ {
+ const std::tr1::shared_ptr<const PackageID> id(c->if_package_id());
+ Context context("When fetching for '" + stringify(*id) + "':");
+
+ starting_action("fetch", c);
+
+ std::string command(cmdline.program_options.a_perform_program.argument());
+ if (command.empty())
+ command = "$CAVE perform";
+
+ command.append(" fetch --hooks ");
+ command.append(stringify(id->uniquely_identifying_spec()));
+
+ paludis::Command cmd(command);
+ int retcode(run_command(cmd));
+
+ done_action("fetch", c, 0 == retcode);
+ return retcode;
+ }
+
+ int do_install_slash(
+ const std::tr1::shared_ptr<Environment> &,
+ const ExecuteResolutionCommandLine & cmdline,
+ const std::tr1::shared_ptr<const Resolution> & r)
+ {
+ const std::tr1::shared_ptr<const PackageID> id(r->decision()->if_package_id());
+ Context context("When installing to / for '" + stringify(*id) + "':");
+
+ starting_action("install to /", r->decision());
+
+ std::string command(cmdline.program_options.a_perform_program.argument());
+ if (command.empty())
+ command = "$CAVE perform";
+
+ command.append(" install --hooks ");
+ command.append(stringify(id->uniquely_identifying_spec()));
+ command.append(" --destination " + stringify(r->destinations()->slash()->repository()));
+ for (PackageIDSequence::ConstIterator i(r->destinations()->slash()->replacing()->begin()),
+ i_end(r->destinations()->slash()->replacing()->end()) ;
+ i != i_end ; ++i)
+ command.append(" --replacing " + stringify((*i)->uniquely_identifying_spec()));
+
+ paludis::Command cmd(command);
+ int retcode(run_command(cmd));
+
+ done_action("install to /", r->decision(), 0 == retcode);
+ return retcode;
+ }
+
+ int execute_resolution(
+ const std::tr1::shared_ptr<Environment> & env,
+ const ResolutionLists & lists,
+ const ExecuteResolutionCommandLine & cmdline)
+ {
+ Context context("When executing chosen resolution:");
+
+ int retcode(0);
+
+ for (Resolutions::ConstIterator c(lists.ordered()->begin()), c_end(lists.ordered()->end()) ;
+ c != c_end ; ++c)
+ retcode |= do_pretend(env, cmdline, (*c)->decision());
+
+ if (0 != retcode || cmdline.a_pretend.specified())
+ return retcode;
+
+ for (Resolutions::ConstIterator c(lists.ordered()->begin()), c_end(lists.ordered()->end()) ;
+ c != c_end ; ++c)
+ {
+ retcode = do_fetch(env, cmdline, (*c)->decision());
+ if (0 != retcode)
+ return retcode;
+
+ if ((*c)->destinations()->slash())
+ {
+ retcode = do_install_slash(env, cmdline, *c);
+ if (0 != retcode)
+ return retcode;
+ }
+ else
+ throw InternalError(PALUDIS_HERE, "destination != / not done yet");
+ }
+
+ return retcode;
+ }
+}
+
+bool
+ExecuteResolutionCommand::important() const
+{
+ return false;
+}
+
+int
+ExecuteResolutionCommand::run(
+ const std::tr1::shared_ptr<Environment> & env,
+ const std::tr1::shared_ptr<const Sequence<std::string > > & args
+ )
+{
+ ExecuteResolutionCommandLine cmdline;
+ cmdline.run(args, "CAVE", "CAVE_EXECUTE_RESOLUTION_OPTIONS", "CAVE_EXECUTE_RESOLUTION_CMDLINE");
+
+ if (cmdline.a_help.specified())
+ {
+ cout << cmdline;
+ return EXIT_SUCCESS;
+ }
+
+ if (getenv_with_default("PALUDIS_SERIALISED_RESOLUTION_FD", "").empty())
+ throw args::DoHelp("PALUDIS_SERIALISED_RESOLUTION_FD must be provided");
+
+ int fd(destringify<int>(getenv_with_default("PALUDIS_SERIALISED_RESOLUTION_FD", "")));
+ SafeIFStream deser_stream(fd);
+ const std::string deser_str((std::istreambuf_iterator<char>(deser_stream)), std::istreambuf_iterator<char>());
+ Deserialiser deserialiser(env.get(), deser_str);
+ Deserialisation deserialisation("ResolutionLists", deserialiser);
+ ResolutionLists lists(ResolutionLists::deserialise(deserialisation));
+
+ return execute_resolution(env, lists, cmdline);
+}
+
+std::tr1::shared_ptr<args::ArgsHandler>
+ExecuteResolutionCommand::make_doc_cmdline()
+{
+ return make_shared_ptr(new ExecuteResolutionCommandLine);
+}
+
+