- hide solver internals
[platform/upstream/libsolv.git] / examples / p5solv
index 0af2452..bf3dd67 100755 (executable)
@@ -3,7 +3,6 @@
 use POSIX;
 use Fcntl;
 use Config::IniFiles;
-use File::FnMatch;
 use Data::Dumper;
 use solv;
 use Devel::Peek;
@@ -14,8 +13,10 @@ use strict;
 package Repo::generic;
 
 sub new {
-  my ($class, $attr) = @_;
-  my $r = { %$attr };
+  my ($class, $alias, $type, $attr) = @_;
+  my $r = { %{$attr || {}} };
+  $r->{'alias'} = $alias;
+  $r->{'type'} = $type;
   return bless $r, $class;
 }
 
@@ -61,6 +62,10 @@ sub load {
   return $self->load_if_changed();
 }
 
+sub load_if_changed {
+  return 0;
+}
+
 sub load_ext {
   return 0;
 }
@@ -89,16 +94,16 @@ sub download {
   if ($chksum) {
     my $fchksum = solv::Chksum->new($chksum->{'type'});
     $fchksum->add_fd(fileno($f));
-    if (!$fchksum->matches($chksum)) {
+    if ($fchksum != $chksum) {
       print "$file: checksum error\n";
       $self->{'incomplete'} = 1 if $markincomplete;
       return undef;
     }
   }
   if ($uncompress) {
-    return solv::xfopen_fd($file, POSIX::dup(fileno($f)));
+    return solv::xfopen_dup($file, fileno($f));
   } else {
-    return solv::xfopen_fd('', POSIX::dup(fileno($f)));
+    return solv::xfopen_dup(undef, fileno($f));
   }
 }
 
@@ -113,19 +118,19 @@ sub usecachedrepo {
     my $fcookie = '';
     return undef if sysread($f, $fcookie, 32) != 32;
     return undef if $cookie && $fcookie ne $cookie;
-    if ($self->{'alias'} ne '@System' && !$ext) {
+    if ($self->{'type'} ne 'system' && !$ext) {
       sysseek($f, -32 * 2, Fcntl::SEEK_END);
       return undef if sysread($f, $fextcookie, 32) != 32;
     }
     sysseek($f, 0, Fcntl::SEEK_SET);
-    $f = solv::xfopen_fd('', POSIX::dup(fileno($f)));
+    my $fd = solv::xfopen_dup(undef, fileno($f));
     my $flags = $ext ? $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES : 0;
     $flags |= $solv::Repo::REPO_LOCALPOOL if $ext && $ext ne 'DL';
-    if (!$self->{'handle'}->add_solv($f, $flags)) {
-      solv::xfclose($f);
+    if (!$self->{'handle'}->add_solv($fd, $flags)) {
+      solv::xfclose($fd);
       return undef;
     }
-    solv::xfclose($f);
+    solv::xfclose($fd);
     $self->{'cookie'} = $fcookie unless $ext;
     $self->{'extcookie'} = $fextcookie if $fextcookie;
     utime undef, undef, $f if $mark;
@@ -156,7 +161,7 @@ sub writecachedrepo {
   };
   return unless $f;
   chmod 0444, $f;
-  my $ff = solv::xfopen_fd('', POSIX::dup(fileno($f)));
+  my $ff = solv::xfopen_dup(undef, fileno($f));
   if (!$info) {
     $self->{'handle'}->write($ff);
   } elsif ($ext) {
@@ -165,7 +170,7 @@ sub writecachedrepo {
      $self->{'handle'}->write_first_repodata($ff);
   }
   solv::xfclose($ff);
-  if ($self->{'alias'} ne '@System' && !$ext) {
+  if ($self->{'type'} ne 'system' && !$ext) {
     $self->genextcookie($f) unless $self->{'extcookie'};
     syswrite($f, $self->{'extcookie'});
   }
@@ -179,7 +184,7 @@ sub writecachedrepo {
         die("internal error, cannot reload solv file\n") unless $self->{'handle'}->add_solv($f, $solv::Repo::SOLV_ADD_NO_STUBS);
       } else {
        $info->extend_to_repo();
-       $info->read_solv_flags($f, $solv::Repo::REPO_EXTEND_SOLVABLES);
+       $info->add_solv($f, $solv::Repo::REPO_EXTEND_SOLVABLES);
       }
       solv::xfclose($f);
     }
@@ -198,12 +203,13 @@ sub find {
   for my $d (@$di) {
     $d->setpos_parent();
     my $filename = $d->{'pool'}->lookup_str($solv::SOLVID_POS, $solv::REPOSITORY_REPOMD_LOCATION);
+    next unless $filename;
     my $chksum = $d->{'pool'}->lookup_checksum($solv::SOLVID_POS, $solv::REPOSITORY_REPOMD_CHECKSUM);
-    if ($filename && !$chksum) {
+    if (!$chksum) {
       print "no $filename file checksum!\n";
       return (undef, undef);
     }
-    return ($filename, $chksum) if $filename;
+    return ($filename, $chksum);
   }
   return (undef, undef);
 }
@@ -216,7 +222,7 @@ sub add_ext {
   my $handle = $repodata->new_handle();
   $repodata->set_poolstr($handle, $solv::REPOSITORY_REPOMD_TYPE, $what);
   $repodata->set_str($handle, $solv::REPOSITORY_REPOMD_LOCATION, $filename);
-  $repodata->set_bin_checksum($handle, $solv::REPOSITORY_REPOMD_CHECKSUM, $chksum);
+  $repodata->set_checksum($handle, $solv::REPOSITORY_REPOMD_CHECKSUM, $chksum);
   if ($ext eq 'DL') {
     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
@@ -341,7 +347,7 @@ sub add_ext {
   my ($filename, $chksum) = $self->find($what);
   my $handle = $repodata->new_handle();
   $repodata->set_str($handle, $solv::SUSETAGS_FILE_NAME, $filename);
-  $repodata->set_bin_checksum($handle, $solv::SUSETAGS_FILE_CHECKSUM, $chksum);
+  $repodata->set_checksum($handle, $solv::SUSETAGS_FILE_CHECKSUM, $chksum);
   if ($ext eq 'DL') {
     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
@@ -366,7 +372,7 @@ sub add_exts {
   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME, undef, 0);
   $di->prepend_keyname($solv::SUSETAGS_FILE);
   for my $d (@$di) {
-    my $filename = $d->match_str();
+    my $filename = $d->str();
     next unless $filename && $filename =~ /^packages\.(..)(?:\..*)$/;
     next if $1 eq 'en' || $1 eq 'gz';
     $self->add_ext($repodata, $filename, $1);
@@ -471,7 +477,8 @@ sub load {
   }
   print "reading\n";
   $self->{'handle'}->add_products("/etc/products.d", $solv::Repo::REPO_NO_INTERNALIZE);
-  $self->{'handle'}->add_rpmdb(undef, 0);
+  $self->{'handle'}->add_rpmdb(undef, $solv::Repo::REPO_REUSE_REPODATA);
+  $self->writecachedrepo();
   return 1;
 }
 
@@ -489,13 +496,12 @@ sub depglob {
   my $id = $pool->str2id($name, 0);
   if ($id) {
     my $match;
-    for my $s ($pool->providers($id)) {
+    for my $s ($pool->whatprovides($id)) {
       return $pool->Job($solv::Job::SOLVER_SOLVABLE_NAME, $id) if $globname && $s->{'nameid'} == $id;
       $match = 1;
     }
     if ($match) {
       print "[using capability match for '$name']\n" if $globname && $globdep;
-      my @j = $pool->Job($solv::Job::SOLVER_SOLVABLE_PROVIDES, $id);
       return $pool->Job($solv::Job::SOLVER_SOLVABLE_PROVIDES, $id);
     }
   }
@@ -521,8 +527,9 @@ sub depglob {
 }
 
 sub limitjobs {
-  my ($pool, $jobs, $flags, $evr) = @_;
+  my ($pool, $jobs, $flags, $evrstr) = @_;
   my @jobs;
+  my $evr = $pool->str2id($evrstr);
   for my $j (@$jobs) {
     my $how = $j->{'how'};
     my $sel = $how & $solv::Job::SOLVER_SELECTMASK;
@@ -530,21 +537,20 @@ sub limitjobs {
     if ($flags == $solv::REL_ARCH) {
       $how |= $solv::Job::SOLVER_SETARCH;
     } elsif ($flags == $solv::REL_EQ && $sel == $solv::Job::SOLVER_SOLVABLE_NAME) {
-      $how |= $pool->id2str($evr) =~ /-/ ? $solv::Job::SOLVER_SETEVR : $solv::Job::SOLVER_SETEV;
+      $how |= $evrstr =~ /-/ ? $solv::Job::SOLVER_SETEVR : $solv::Job::SOLVER_SETEV;
     }
     push @jobs, $pool->Job($how, $what);
   }
   return @jobs;
 }
 
-sub limitjobs_arch {
+sub limitjobs_evrarch {
   my ($pool, $jobs, $flags, $evrstr) = @_;
   if ($evrstr =~ /^(.+)\.(.+?)$/ && validarch($pool, $2)) {
-    my $evr = $pool->str2id($1);
-    my @jobs = limitjobs($pool, $jobs, $solv::REL_ARCH, $pool->str2id($2));
-    return limitjobs($pool, \@jobs, $flags, $evr);
+    $evrstr = $1;
+    $jobs = [ limitjobs($pool, $jobs, $solv::REL_ARCH, $2) ];
   }
-  return limitjobs($pool, $jobs, $flags, $pool->str2id($evrstr));
+  return limitjobs($pool, $jobs, $flags, $evrstr);
 }
 
 sub mkjobs_rel {
@@ -554,13 +560,13 @@ sub mkjobs_rel {
   $flags |= $solv::REL_EQ if $rel =~ /=/;
   $flags |= $solv::REL_GT if $rel =~ />/;
   my @jobs = depglob($pool, $name, 1, 1);
-  return limitjobs($pool, \@jobs, $flags, $pool->str2id($evr)) if @jobs;
+  return limitjobs($pool, \@jobs, $flags, $evr) if @jobs;
   if (($name =~ /^(.+)\.(.+?)$/s) && validarch($pool, $2)) {
     my $arch = $2;
     @jobs = depglob($pool, $1, 1, 1);
     if (@jobs) {
-      @jobs = limitjobs($pool, \@jobs, $solv::REL_ARCH, $pool->str2id($arch));
-      return limitjobs($pool, \@jobs, $flags, $pool->str2id($evr));
+      @jobs = limitjobs($pool, \@jobs, $solv::REL_ARCH, $arch);
+      return limitjobs($pool, \@jobs, $flags, $evr);
     }
   }
   return ();
@@ -573,17 +579,17 @@ sub mkjobs_nevra {
   if (($arg =~ /^(.+)\.(.+?)$/s) && validarch($pool, $2)) {
     my $arch = $2;
     @jobs = depglob($pool, $1, 1, 1);
-    return limitjobs($pool, \@jobs, $solv::REL_ARCH, $pool->str2id($arch)) if @jobs;
+    return limitjobs($pool, \@jobs, $solv::REL_ARCH, $arch) if @jobs;
   }
   if ($arg =~ /^(.+)-(.+?)$/s) {
     my $evr = $2;
     @jobs = depglob($pool, $1, 1, 0);
-    return limitjobs_arch($pool, \@jobs, $solv::REL_EQ, $evr) if @jobs;
+    return limitjobs_evrarch($pool, \@jobs, $solv::REL_EQ, $evr) if @jobs;
   }
   if ($arg =~ /^(.+)-(.+?-.+?)$/s) {
     my $evr = $2;
     @jobs = depglob($pool, $1, 1, 0);
-    return limitjobs_arch($pool, \@jobs, $solv::REL_EQ, $evr) if @jobs;
+    return limitjobs_evrarch($pool, \@jobs, $solv::REL_EQ, $evr) if @jobs;
   }
   return ();
 }
@@ -641,9 +647,6 @@ $cmd = 'erase' if $cmd eq 'rm';
 $cmd = 'verify' if $cmd eq 've';
 $cmd = 'search' if $cmd eq 'se';
 
-my $pool = solv::Pool->new();
-$pool->setarch((POSIX::uname())[4]);
-$pool->set_loadcallback(\&load_stub);
 my @repos;
 for my $reposdir ('/etc/zypp/repos.d') {
   next unless -d $reposdir;
@@ -657,18 +660,22 @@ for my $reposdir ('/etc/zypp/repos.d') {
       }
       my $repo;
       if ($repoattr->{'type'} eq 'rpm-md') {
-       $repo = Repo::rpmmd->new($repoattr);
+       $repo = Repo::rpmmd->new($alias, 'repomd', $repoattr);
       } elsif ($repoattr->{'type'} eq 'yast2') {
-       $repo = Repo::susetags->new($repoattr);
+       $repo = Repo::susetags->new($alias, 'susetags', $repoattr);
       } else {
-       $repo = Repo::unknown->new($repoattr);
+       $repo = Repo::unknown->new($alias, 'unknown', $repoattr);
       }
       push @repos, $repo;
     }
   }
 }
 
-my $sysrepo = Repo::system->new({'alias' => '@System', 'type' => 'system'});
+my $pool = solv::Pool->new();
+$pool->setarch((POSIX::uname())[4]);
+$pool->set_loadcallback(\&load_stub);
+
+my $sysrepo = Repo::system->new('@System', 'system');
 $sysrepo->load($pool);
 for my $repo (@repos) {
   $repo->load($pool) if $repo->{'enabled'};
@@ -700,7 +707,7 @@ for my $arg (@ARGV) {
 if ($cmd eq 'list' || $cmd eq 'info') {
   die("no package matched.\n") unless @jobs;
   for my $job (@jobs) {
-    for my $s ($pool->jobsolvables($job)) {
+    for my $s ($job->solvables()) {
       if ($cmd eq 'info') {
         printf "Name:        %s\n", $s->str();
         printf "Repo:        %s\n", $s->{'repo'}->{'name'};
@@ -721,15 +728,15 @@ if ($cmd eq 'list' || $cmd eq 'info') {
 
 if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cmd eq 'verify') {
   if (!@jobs) {
-    if ($cmd eq 'up' || $cmd eq 'verify') {
+    if ($cmd eq 'up' || $cmd eq 'verify' || $cmd eq 'dup') {
       push @jobs, $pool->Job($solv::Job::SOLVER_SOLVABLE_ALL, 0);
-    } elsif ($cmd ne 'dup') {
+    } else {
       die("no package matched.\n");
     }
   }
   for my $job (@jobs) {
     if ($cmd eq 'up') {
-      if ($job->{'how'} == $solv::Job::SOLVER_SOLVABLE_ALL || grep {$_->isinstalled()} $pool->jobsolvables($job)) {
+      if ($job->{'how'} == $solv::Job::SOLVER_SOLVABLE_ALL || grep {$_->isinstalled()} $job->solvables()) {
         $job->{'how'} |= $solv::Job::SOLVER_UPDATE;
       } else {
         $job->{'how'} |= $solv::Job::SOLVER_INSTALL;
@@ -746,18 +753,16 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
   }
   my $solver;
   while (1) {
-    $solver = $pool->create_solver();
-    $solver->{'ignorealreadyrecommended'} = 1;
-    $solver->{'allowuninstall'} = 1 if $cmd eq 'erase';
-    if ($cmd eq 'dup' && !@jobs) {
-      $solver->{'distupgrade'} = 1;
-      $solver->{'updatesystem'} = 1;
-      $solver->{'allowdowngrade'} = 1;
-      $solver->{'allowvendorchange'} = 1;
-      $solver->{'allowarchchange'} = 1;
-      $solver->{'dosplitprovides'} = 1;
-    } elsif ($cmd eq 'up' and @jobs == 1 and $jobs[0]->{'how'} == ($solv::Job::SOLVER_UPDATE | $solv::Job::SOLVER_SOLVABLE_ALL)) {
-      $solver->{'dosplitprovides'} = 1;
+    $solver = $pool->Solver();
+    $solver->set_flag($solv::Solver::SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED, 1);
+    $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if $cmd eq 'erase';
+    if ($cmd eq 'dup' && @jobs == 1 $jobs[0]->{'how'} == ($solv::Job::SOLVER_DISTUPGRADE | $solv::Job::SOLVER_SOLVABLE_ALL)) {
+      $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_DOWNGRADE, 1);
+      $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_VENDERCHANGE, 1);
+      $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_ARCHCHANGE, 1);
+      $solver->set_flag($solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1);
+    } elsif ($cmd eq 'up' && @jobs == 1 && $jobs[0]->{'how'} == ($solv::Job::SOLVER_UPDATE | $solv::Job::SOLVER_SOLVABLE_ALL)) {
+      $solver->set_flag($solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1);
     }
     my @problems = $solver->solve(\@jobs);
     last unless @problems;
@@ -793,6 +798,7 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
            print "  - allow something else\n";
          }
         }
+        print "\n";
       }
       my $sol;
       while (1) {
@@ -810,7 +816,7 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
          $jobs[$element->{'jobidx'}] = $pool->Job($solv::Job::SOLVER_NOOP, 0);
         } else {
          my $newjob = $element->Job();
-         push @jobs, $newjob if $newjob && !grep {$_->{'how'} == $newjob->{'how'} && $_->{'what'} == $newjob->{'what'}} @jobs;
+         push @jobs, $newjob if $newjob && !grep {$_ == $newjob} @jobs;
        }
       }
     }
@@ -823,28 +829,27 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
   }
   print "\nTransaction summary:\n\n";
   for my $c ($trans->classify()) {
-    my ($ctype, $pkgs, $fromid, $toid) = @$c;
-    if ($ctype == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
-      printf "%d erased packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_INSTALL) {
-      printf "%d installed packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_REINSTALLED) {
-      printf "%d reinstalled packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
-      printf "%d downgraded packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_CHANGED) {
-      printf "%d changed packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED) {
-      printf "%d upgraded packages:\n", scalar(@$pkgs);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE) {
-      printf "%d vendor changes from '%s' to '%s':\n", scalar(@$pkgs), $pool->id2str($fromid), $pool->id2str($toid);
-    } elsif ($ctype == $solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE) {
-      printf "%d arch changes from '%s' to '%s':\n", scalar(@$pkgs), $pool->id2str($fromid), $pool->id2str($toid);
+    if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
+      print "$c->{'count'} erased packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_INSTALL) {
+      print "$c->{'count'} installed packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_REINSTALLED) {
+      print "$c->{'count'} reinstalled packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
+      print "$c->{'count'} downgraded packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_CHANGED) {
+      print "$c->{'count'} changed packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED) {
+      print "$c->{'count'} upgraded packages:\n";
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE) {
+      printf "$c->{'count'} vendor changes from '%s' to '%s':\n", $pool->id2str($c->{'fromid'}), $pool->id2str($c->{'toid'});
+    } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE) {
+      printf "$c->{'count'} arch changes from '%s' to '%s':\n", $pool->id2str($c->{'fromid'}), $pool->id2str($c->{'toid'});
     } else {
       next;
     }
-    for my $p (@$pkgs) {
-      if ($ctype == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED || $ctype == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
+    for my $p ($c->solvables()) {
+      if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED || $c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
        my $other = $trans->othersolvable($p);
        printf "  - %s -> %s\n", $p->str(), $other->str();
       } else {
@@ -871,7 +876,7 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
       my $repo = $p->{'repo'}->{'appdata'};
       my ($location, $medianr) = $p->lookup_location();
       next unless $location;
-      if ($repo->{'type'} eq 'yast2') {
+      if ($repo->{'type'} eq 'susetags') {
        $location = ($repo->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DATADIR) || 'suse') ."/$location";
       }
       my $chksum = $p->lookup_checksum($solv::SOLVABLE_CHECKSUM);