Skip to content

Commit

Permalink
Feat: add autoremove
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna committed Nov 19, 2024
1 parent 54345d3 commit 2bf673c
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ SET (zypper_HEADERS
commands/query/patterns.h
commands/query/products.h
commands/solveroptionset.h
commands/autoremove.h
commands/installremove.h
commands/sourceinstall.h
commands/distupgrade.h
Expand Down Expand Up @@ -161,6 +162,7 @@ SET( zypper_SRCS
commands/query/products.cc
commands/solveroptionset.cc
commands/installremove.cc
commands/autoremove.cc
commands/sourceinstall.cc
commands/distupgrade.cc
commands/inrverify.cc
Expand Down
2 changes: 2 additions & 0 deletions src/Command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "commands/query.h"
#include "commands/installremove.h"
#include "commands/sourceinstall.h"
#include "commands/autoremove.h"
#include "commands/distupgrade.h"
#include "commands/inrverify.h"
#include "commands/patch.h"
Expand Down Expand Up @@ -124,6 +125,7 @@ namespace

makeCmd<InstallCmd> ( ZypperCommand::INSTALL_e , _("Software Management:"), { "install", "in" } ),
makeCmd<RemoveCmd> ( ZypperCommand::REMOVE_e , std::string(), { "remove", "rm" } ),
makeCmd<AutoRemoveCmd> ( ZypperCommand::AUTOREMOVE_e , std::string(), { "autoremove", "arm" } ),
makeCmd<RemovePtfCmd> ( ZypperCommand::REMOVE_PTF_e , std::string(), { "removeptf", "rmptf" } ),
makeCmd<InrVerifyCmd> ( ZypperCommand::VERIFY_e , std::string(), { "verify", "ve" }, InrVerifyCmd::Mode::Verify ),
makeCmd<SourceInstallCmd> ( ZypperCommand::SRC_INSTALL_e , std::string(), { "source-install", "si" } ),
Expand Down
1 change: 1 addition & 0 deletions src/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct ZypperCommand
LIST_PATCHES_e,
PATCH_CHECK_e,
DIST_UPGRADE_e,
AUTOREMOVE_e,

SEARCH_e,
INFO_e,
Expand Down
125 changes: 125 additions & 0 deletions src/commands/autoremove.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*---------------------------------------------------------------------------*\
____ _ _ __ _ __ ___ _ _
|_ / || | '_ \ '_ \/ -_) '_|
/__|\_, | .__/ .__/\___|_|
|__/|_| |_|
\*---------------------------------------------------------------------------*/
#include <zypp/ZYpp.h> // for ResPool::instance()
#include "autoremove.h"
#include "utils/flags/flagtypes.h"
#include "commands/conditions.h"
#include "solve-commit.h"
#include "utils/messages.h"
#include "src/SolverRequester.h"
#include "commonflags.h"
#include "Zypper.h"

extern ZYpp::Ptr God;

AutoRemoveCmd::AutoRemoveCmd(std::vector<std::string> &&commandAliases_r) : ZypperBaseCommand(std::move(commandAliases_r),
// translators: command synopsis; do not translate lowercase words
_("autoremove (arm) [OPTIONS]"),
// translators: command summary: dist-upgrade, dup
_("Remove unneeded packages due to updates or installs."), // TODO: better explnations
// translators: command description
_("Remove a unneeded packages."),
ResetRepoManager | InitTarget | InitRepos | LoadResolvables)
{
_dryRunOpts.setCompatibilityMode(CompatModeBits::EnableNewOpt | CompatModeBits::EnableRugOpt);
}

zypp::ZyppFlags::CommandGroup AutoRemoveCmd::cmdOptions() const
{
auto that = const_cast<AutoRemoveCmd *>(this);
return zypp::ZyppFlags::CommandGroup({{"remove-orphaned", '\0',
zypp::ZyppFlags::NoArgument,
zypp::ZyppFlags::BoolType(&that->_orpahned, ZyppFlags::StoreTrue, _orpahned),
_("Remove unneeded orphaned packages.")},
CommonFlags::detailsFlag(that->_details)});
}

void AutoRemoveCmd::doReset()
{
ArmSettings::reset();
_details = false;
}

std::vector<BaseCommandConditionPtr> AutoRemoveCmd::conditions() const
{
return {
std::make_shared<NeedsRootCondition>(),
std::make_shared<NeedsWritableRoot>()};
}

int AutoRemoveCmd::execute(Zypper &zypper, const std::vector<std::string> &positionalArgs_r)
{
// too many arguments
if (positionalArgs_r.size() > 0)
{
report_too_many_arguments(help());
return (ZYPPER_EXIT_ERR_INVALID_ARGS);
}

int code = defaultSystemSetup(zypper, InitTarget | InitRepos | LoadResolvables | Resolve);
if (code != ZYPPER_EXIT_OK)
return code;

zypper.out().warning(str::form(
_("You are run autoremove on the system. This is experimental and subject to change"
" This may remove unintended packages please confirm the packages that will be removed before you"
" continue. See '%s' for more information about this command."),
"man zypper"));

std::vector<std::string> pkgs;

auto checkStatus = [=](const PoolItem &pi_r) -> bool
{
// isipi : prevent returning true if identical installed items are tested.
const ResStatus &status{pi_r.status()};
if ((_orpahned && status.isOrphaned()) || status.isUnneeded())
{
return true;
}
return false;
};

God->resolver()->resolvePool();

for (const auto &sel : God->pool().proxy().byKind<Package>())
{
for (const auto &pi : sel->picklist())
{

if (pi.status().isInstalled())
{
if (!checkStatus(pi))
continue;
}
else
{
PoolItem ipi(sel->identicalInstalledObj(pi));
if (!ipi || !checkStatus(ipi))
if (!checkStatus(pi))
continue;
}
pkgs.push_back(pi.asString());
}
}

SolverRequester::Options sropts;

SolverRequester sr(sropts);
for (auto p : pkgs)
{
std::vector<std::string> a;
a.push_back(p);
sr.remove(a);
}

Summary::ViewOptions opts = Summary::DEFAULT;
if (_details)
opts = static_cast<Summary::ViewOptions>(opts | Summary::DETAILS);
// do solve
solve_and_commit(zypper, SolveAndCommitPolicy().summaryOptions(opts));
return zypper.exitCode();
}
37 changes: 37 additions & 0 deletions src/commands/autoremove.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------*\
____ _ _ __ _ __ ___ _ _
|_ / || | '_ \ '_ \/ -_) '_|
/__|\_, | .__/ .__/\___|_|
|__/|_| |_|
\*---------------------------------------------------------------------------*/
#ifndef ZYPPER_COMMANDS_AUTOREMOVE_INCLUDED
#define ZYPPER_COMMANDS_AUTOREMOVE_INCLUDED

#include "commands/basecommand.h"
#include "commands/optionsets.h"
#include "commands/solveroptionset.h"

class AutoRemoveCmd : public ZypperBaseCommand
{
public:
AutoRemoveCmd(std::vector<std::string> &&commandAliases_r);

private:
bool _details = false;
bool _orpahned = false;
DryRunOptionSet _dryRunOpts{*this};
NoConfirmRugOption _noConfirmOpts{*this};
DownloadOptionSet _downloadModeOpts{*this};
SolverCommonOptionSet _commonSolverOpts{*this};
SolverRecommendsOptionSet _recommendsSolverOpts{*this};
SolverInstallsOptionSet _installSolverOpts{*this};

// ZypperBaseCommand interface
protected:
zypp::ZyppFlags::CommandGroup cmdOptions() const override;
void doReset() override;
int execute(Zypper &zypper, const std::vector<std::string> &positionalArgs_r) override;
std::vector<BaseCommandConditionPtr> conditions() const override;
};

#endif
6 changes: 6 additions & 0 deletions src/global-settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ struct DupSettingsData
};
using DupSettings = GlobalSettingSingleton<DupSettingsData>;

struct ArmSettingsData
{
std::vector<std::string> _fromSystem;
};
using ArmSettings = GlobalSettingSingleton<ArmSettingsData>;

struct FileConflictPolicyData
{
bool _replaceFiles = false;
Expand Down

0 comments on commit 2bf673c

Please sign in to comment.