aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephan Friedrichs <deduktionstheorem@googlemail.com> 2010-11-21 13:09:34 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-11-21 13:02:32 +0000
commitdc38780cef302eebd42cd0cf45778a7659cd4577 (patch)
tree9262c7946f609d955f6027eacf595e90efdd6a56
parentdd1e06bc7f78447d128a7864e72d2700160043fb (diff)
downloadpaludis-dc38780cef302eebd42cd0cf45778a7659cd4577.tar.gz
paludis-dc38780cef302eebd42cd0cf45778a7659cd4577.tar.xz
Add cave-print-unused-distfiles command
-rw-r--r--.gitignore1
-rw-r--r--doc/clients/Makefile.am1
-rw-r--r--src/clients/cave/Makefile.am2
-rw-r--r--src/clients/cave/cmd_print_unused_distfiles.cc200
-rw-r--r--src/clients/cave/cmd_print_unused_distfiles.hh43
-rw-r--r--src/clients/cave/command_factory.cc2
6 files changed, 249 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 6108f0f..d7071dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -120,6 +120,7 @@ paludis-*.*.*.tar.bz2
/doc/clients/cave-print-sets.html
/doc/clients/cave-print-spec.html
/doc/clients/cave-print-sync-protocols.html
+/doc/clients/cave-print-unused-distfiles.html
/doc/clients/cave-purge.html
/doc/clients/cave-report.html
/doc/clients/cave-resolve.html
diff --git a/doc/clients/Makefile.am b/doc/clients/Makefile.am
index 2d713f6..fb31bb7 100644
--- a/doc/clients/Makefile.am
+++ b/doc/clients/Makefile.am
@@ -64,6 +64,7 @@ CAVE_COMMANDS_HTML = \
cave-print-set.html \
cave-print-sets.html \
cave-print-sync-protocols.html \
+ cave-print-unused-distfiles.html \
cave-purge.html \
cave-report.html \
cave-resolve.html \
diff --git a/src/clients/cave/Makefile.am b/src/clients/cave/Makefile.am
index b6677a5..c9ce9ec 100644
--- a/src/clients/cave/Makefile.am
+++ b/src/clients/cave/Makefile.am
@@ -61,6 +61,7 @@ command_MANS = \
cave-print-sets.1 \
cave-print-spec.1 \
cave-print-sync-protocols.1 \
+ cave-print-unused-distfiles.1 \
cave-purge.1 \
cave-report.1 \
cave-resolve.1 \
@@ -160,6 +161,7 @@ libcave_a_SOURCES = \
cmd_print_sets.cc cmd_print_sets.hh \
cmd_print_spec.cc cmd_print_spec.hh \
cmd_print_sync_protocols.cc cmd_print_sync_protocols.hh \
+ cmd_print_unused_distfiles.cc cmd_print_unused_distfiles.hh \
cmd_purge.cc cmd_purge.hh \
cmd_report.cc cmd_report.hh cmd_report-fmt.hh \
cmd_resolve.cc cmd_resolve.hh \
diff --git a/src/clients/cave/cmd_print_unused_distfiles.cc b/src/clients/cave/cmd_print_unused_distfiles.cc
new file mode 100644
index 0000000..deb70c8
--- /dev/null
+++ b/src/clients/cave/cmd_print_unused_distfiles.cc
@@ -0,0 +1,200 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 Stephan Friedrichs
+ *
+ * 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_print_unused_distfiles.hh"
+#include "command_command_line.hh"
+
+#include <paludis/args/args.hh>
+#include <paludis/args/do_help.hh>
+#include <paludis/environment.hh>
+#include <paludis/filter.hh>
+#include <paludis/filter_handler.hh>
+#include <paludis/filtered_generator.hh>
+#include <paludis/generator.hh>
+#include <paludis/mask.hh>
+#include <paludis/metadata_key.hh>
+#include <paludis/name.hh>
+#include <paludis/package_database.hh>
+#include <paludis/package_id.hh>
+#include <paludis/repository.hh>
+#include <paludis/selection.hh>
+#include <paludis/user_dep_spec.hh>
+#include <paludis/util/accept_visitor.hh>
+#include <paludis/util/fs_iterator.hh>
+#include <paludis/util/indirect_iterator-impl.hh>
+#include <paludis/util/map.hh>
+#include <paludis/util/simple_visitor_cast.hh>
+#include <paludis/util/wrapped_forward_iterator.hh>
+
+#include <iostream>
+#include <algorithm>
+#include <set>
+
+
+using namespace paludis;
+using namespace cave;
+using std::cout;
+using std::endl;
+
+namespace
+{
+ struct PrintUnusedDistfilesCommandLine :
+ CaveCommandCommandLine
+ {
+ virtual std::string app_name() const
+ {
+ return "cave print-unused-distfiles";
+ }
+
+ virtual std::string app_synopsis() const
+ {
+ return "Prints unused distfiles.";
+ }
+
+ virtual std::string app_description() const
+ {
+ return "Prints all distfiles not used by any installed package. No "
+ "formatting is used, making the output suitable for parsing by scripts.";
+ }
+
+ PrintUnusedDistfilesCommandLine()
+ {
+ add_usage_line("");
+ }
+ };
+
+ /* This visitor finds all distfiles used by installed packages. */
+ class DistfilesFilter
+ {
+ private:
+ std::set<std::string> & files;
+
+ public:
+ DistfilesFilter(std::set<std::string> & f) :
+ files(f)
+ {
+ }
+
+ void visit(const FetchableURISpecTree::NodeType<AllDepSpec>::Type & node)
+ {
+ std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
+ }
+
+ void visit(const FetchableURISpecTree::NodeType<ConditionalDepSpec>::Type & node)
+ {
+ // recurse iff the conditions (e.g. useflags) are met
+ if (node.spec()->condition_met())
+ std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
+ }
+
+ void visit(const FetchableURISpecTree::NodeType<FetchableURIDepSpec>::Type & node)
+ {
+ // found used distfile
+ files.insert(node.spec()->filename());
+ }
+
+ void visit(const FetchableURISpecTree::NodeType<URILabelsDepSpec>::Type &)
+ {
+ }
+ };
+}
+
+int
+PrintUnusedDistfilesCommand::run(
+ const std::shared_ptr<Environment> & env,
+ const std::shared_ptr<const Sequence<std::string > > & args
+ )
+{
+ //
+ // Handle parameteres
+ //
+
+ PrintUnusedDistfilesCommandLine cmdline;
+ cmdline.run(args, "CAVE", "CAVE_PRINT_UNUSED_DISTFILES_OPTIONS", "CAVE_PRINT_UNUSED_DISTFILES_CMDLINE");
+
+ if (cmdline.a_help.specified())
+ {
+ cout << cmdline;
+ return EXIT_SUCCESS;
+ }
+
+ if (cmdline.begin_parameters() != cmdline.end_parameters())
+ throw args::DoHelp("print-unused-distfiles takes no parameters");
+
+ //
+ // Find all distfiles needed by installed packages
+ //
+
+ std::set<std::string> used_distfiles;
+ std::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsUnsorted(
+ generator::All() | filter::InstalledAtRoot(
+ env->preferred_root_key()->value()))]);
+
+ for (PackageIDSequence::ConstIterator iter(ids->begin()), end(ids->end()) ;
+ iter != end ; ++iter)
+ {
+ if ((*iter)->fetches_key())
+ {
+ DistfilesFilter filter(used_distfiles);
+ (*iter)->fetches_key()->value()->top()->accept(filter);
+ }
+ }
+
+ //
+ // Find all distdirs
+ //
+
+ std::set<FSPath, FSPathComparator> distdirs;
+
+ const std::shared_ptr<const PackageDatabase> pkgdb(env->package_database());
+ for (auto repo(pkgdb->begin_repositories()), end(pkgdb->end_repositories()) ;
+ repo != end ; ++repo)
+ {
+ auto distdir_metadata((*repo)->find_metadata("distdir"));
+ if (distdir_metadata != (*repo)->end_metadata())
+ {
+ auto path_key(simple_visitor_cast<const MetadataValueKey<FSPath>>(**distdir_metadata));
+ if (path_key)
+ distdirs.insert(path_key->value().realpath());
+ }
+ }
+
+ //
+ // Iterate through the distdirs and compare their contents with the used distfiles
+ //
+
+ for (auto dir(distdirs.begin()), d_end(distdirs.end()) ; dir != d_end ; ++dir)
+ {
+ for (FSIterator file(*dir, {fsio_include_dotfiles, fsio_want_regular_files}), f_end ;
+ file != f_end ; ++file)
+ {
+ if (used_distfiles.find(file->basename()) == used_distfiles.end())
+ cout << *file << endl;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
+
+std::shared_ptr<args::ArgsHandler>
+PrintUnusedDistfilesCommand::make_doc_cmdline()
+{
+ return std::make_shared<PrintUnusedDistfilesCommandLine>();
+}
+
diff --git a/src/clients/cave/cmd_print_unused_distfiles.hh b/src/clients/cave/cmd_print_unused_distfiles.hh
new file mode 100644
index 0000000..812ad36
--- /dev/null
+++ b/src/clients/cave/cmd_print_unused_distfiles.hh
@@ -0,0 +1,43 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 Stephan Friedrichs
+ *
+ * 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_SRC_CLIENTS_CAVE_CMD_PRINT_UNUSED_DISTFILES_HH
+#define PALUDIS_GUARD_SRC_CLIENTS_CAVE_CMD_PRINT_UNUSED_DISTFILES_HH 1
+
+#include "command.hh"
+
+namespace paludis
+{
+ namespace cave
+ {
+ class PALUDIS_VISIBLE PrintUnusedDistfilesCommand :
+ public Command
+ {
+ public:
+ int run(
+ const std::shared_ptr<Environment> &,
+ const std::shared_ptr<const Sequence<std::string > > & args
+ );
+
+ std::shared_ptr<args::ArgsHandler> make_doc_cmdline();
+ };
+ }
+}
+
+#endif
diff --git a/src/clients/cave/command_factory.cc b/src/clients/cave/command_factory.cc
index c1b79c1..e49477f 100644
--- a/src/clients/cave/command_factory.cc
+++ b/src/clients/cave/command_factory.cc
@@ -74,6 +74,7 @@
#include "cmd_print_sets.hh"
#include "cmd_print_spec.hh"
#include "cmd_print_sync_protocols.hh"
+#include "cmd_print_unused_distfiles.hh"
#include "cmd_purge.hh"
#include "cmd_report.hh"
#include "cmd_resolve.hh"
@@ -190,6 +191,7 @@ CommandFactory::CommandFactory() :
_imp->handlers.insert(std::make_pair("print-sets", std::bind(&make_command<PrintSetsCommand>)));
_imp->handlers.insert(std::make_pair("print-spec", std::bind(&make_command<PrintSpecCommand>)));
_imp->handlers.insert(std::make_pair("print-sync-protocols", std::bind(&make_command<PrintSyncProtocolsCommand>)));
+ _imp->handlers.insert(std::make_pair("print-unused-distfiles", std::bind(&make_command<PrintUnusedDistfilesCommand>)));
_imp->handlers.insert(std::make_pair("report", std::bind(&make_command<ReportCommand>)));
_imp->handlers.insert(std::make_pair("resolve", std::bind(&make_command<ResolveCommand>)));
_imp->handlers.insert(std::make_pair("resume", std::bind(&make_command<ResumeCommand>)));