Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Feat: add autoremove #578

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
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"
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
#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
121 changes: 121 additions & 0 deletions src/commands/autoremove.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*---------------------------------------------------------------------------*\
____ _ _ __ _ __ ___ _ _
|_ / || | '_ \ '_ \/ -_) '_|
/__|\_, | .__/ .__/\___|_|
|__/|_| |_|
\*---------------------------------------------------------------------------*/
#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
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
_("Remove unneeded packages due to updates or installs."), // TODO: better explnations
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
// 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"
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
" This may remove unintended packages please confirm the packages that will be removed before you"
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
" 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.
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
const ResStatus &status{pi_r.status()};
if ((_orpahned && status.isOrphaned()) || status.isUnneeded())
{
return true;
}
return false;
};

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

SolverRequester::Options sropts;

SolverRequester sr(sropts);

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;
}
std::vector<std::string> a;
a.push_back(pi.asString());
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;
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
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
Lunarequest marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<std::string> _fromSystem;
};
using ArmSettings = GlobalSettingSingleton<ArmSettingsData>;

struct FileConflictPolicyData
{
bool _replaceFiles = false;
Expand Down