Skip to content

Commit

Permalink
Guess running kernel from testcase if --uname is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandres committed Dec 10, 2024
1 parent 94f1109 commit c760fed
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions tools/zypp-runpurge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main ( int argc, char *argv[] )

auto result = options.parse( argc, argv );

if ( result.count( "help" ) || !result.count("uname") || !result.positionals().size() )
if ( result.count( "help" ) || !result.positionals().size() )
return usage( options, 1 );

const std::string &testcaseDir = result.positionals().front();
Expand All @@ -58,24 +58,48 @@ int main ( int argc, char *argv[] )
return 1;
}

std::string unameR;
if ( result.count("uname") ) {
unameR = result["uname"].arg();
} else {
std::cout << "No --uname provided. Guessing it from the latest kernel-default installed...." << std::endl;
const PoolItem & running( ui::Selectable::get("kernel-default")->theObj() );
if ( not running ) {
std::cerr << "Oops: No installed kernel-default." << std::endl;
return usage( options, 1 );
}
std::cout << "Guess running: " << running.asString() << endl;
const Capability unameRProvides { "kernel-uname-r" }; // indicator provides
for ( const auto & cap : running.provides() ) {
if ( cap.matches( unameRProvides ) == CapMatch::yes ) {
unameR = cap.detail().ed().asString();
break;
}
}
if ( unameR.empty() ) {
std::cerr << "Oops: Guessed kernel does not provide " << unameRProvides << std::endl;
return usage( options, 1 );
}
std::cout << "Guess --uname: " << unameR << endl;
}

std::string keepSpec = "oldest,running,latest";
if ( result.count("keepSpec") ) {
keepSpec = result["keepSpec"].arg();
}


PurgeKernels krnls;
krnls.setUnameR( result["uname"].arg() );
krnls.setUnameR( unameR );
krnls.setKeepSpec( keepSpec );
krnls.markObsoleteKernels();

const auto &makeNVRA = []( const PoolItem &pck ) -> std::string {
return pck.name() + "-" + pck.edition().asString() + "." + pck.arch().asString();
};

std::cout << "Purged kernels: " << std::endl;
auto pool = ResPool::instance();
const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
for ( auto it = pool.byStatusBegin( toBeUninstalledFilter ); it != pool.byStatusEnd( toBeUninstalledFilter ); it++ ) {
std::cout << "Removing " << makeNVRA(*it) + (it->status().isByUser() ? " (by user)" : " (autoremoved)") << std::endl;
std::cout << "Removing " << it->asString() + (it->status().isByUser() ? " (by user)" : " (autoremoved)") << std::endl;
}

return 0;
Expand Down

0 comments on commit c760fed

Please sign in to comment.