pysolv: do not import from solv
[platform/upstream/libsolv.git] / examples / p5solv
1 #!/usr/bin/perl -w
2
3 use POSIX;
4 use Fcntl;
5 use Config::IniFiles;
6 use Data::Dumper;
7 use solv;
8 use Devel::Peek;
9 use FileHandle;
10 use File::Temp ();
11 use strict;
12
13 package Repo::generic;
14
15 sub new {
16   my ($class, $alias, $type, $attr) = @_;
17   my $r = { %{$attr || {}} };
18   $r->{'alias'} = $alias;
19   $r->{'type'} = $type;
20   return bless $r, $class;
21 }
22
23 sub calc_cookie_fp {
24   my ($self, $fp) = @_;
25   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
26   $chksum->add("1.1");
27   $chksum->add_fp($fp);
28   return $chksum->raw();
29 }
30
31 sub calc_cookie_file {
32   my ($self, $filename) = @_;
33   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
34   $chksum->add("1.1");
35   $chksum->add_stat($filename);
36   return $chksum->raw();
37 }
38
39 sub calc_cookie_ext {
40   my ($self, $f, $cookie) = @_;
41   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
42   $chksum->add("1.1");
43   $chksum->add($cookie);
44   $chksum->add_fstat(fileno($f));
45   my $extcookie = $chksum->raw();
46   substr($extcookie, 0, 1) = chr(1) if ord(substr($extcookie, 0, 1)) == 0;
47   return $extcookie;
48 }
49
50 sub cachepath {
51   my ($self, $ext) = @_;
52   my $path = $self->{'alias'};
53   $path =~ s/^\./_/s;
54   $path .= $ext ? "_$ext.solvx" : '.solv';
55   $path =~ s/\//_/gs;
56   return "/var/cache/solv/$path";
57 }
58
59 sub load {
60   my ($self, $pool) = @_;
61   $self->{'handle'} = $pool->add_repo($self->{'alias'});
62   $self->{'handle'}->{'appdata'} = $self;
63   $self->{'handle'}->{'priority'} = 99 - $self->{'priority'};
64   my $dorefresh = $self->{'autorefresh'};
65   if ($dorefresh) {
66     my @s = stat($self->cachepath());
67     $dorefresh = 0 if @s && ($self->{'metadata_expire'} == -1 || time() - $s[9] < $self->{'metadata_expire'});
68   }
69   $self->{'cookie'} = '';
70   if (!$dorefresh && $self->usecachedrepo()) {
71     print "repo: '$self->{'alias'}' cached\n";
72     return 1;
73   }
74   return 0;
75 }
76
77 sub load_ext {
78   return 0;
79 }
80
81 sub download {
82   my ($self, $file, $uncompress, $chksum, $markincomplete) = @_;
83   if (!$self->{'baseurl'}) {
84     print "$self->{'alias'}: no baseurl\n";
85     return undef;
86   }
87   my $url = $self->{'baseurl'};
88   $url =~ s/\/$//;
89   $url .= "/$file";
90   open(my $f, '+>', undef) || die;
91   fcntl($f, Fcntl::F_SETFD, 0);
92   my $st = system('curl', '-f', '-s', '-L', '-o', "/dev/fd/".fileno($f), '--', $url);
93   if (POSIX::lseek(fileno($f), 0, POSIX::SEEK_END) == 0 && ($st == 0 || !$chksum)) {
94     return undef;
95   }
96   POSIX::lseek(fileno($f), 0, POSIX::SEEK_SET);
97   if ($st) {
98     print "$file: download error #$st\n";
99     $self->{'incomplete'} = 1 if $markincomplete;
100     return undef;
101   }
102   if ($chksum) {
103     my $fchksum = solv::Chksum->new($chksum->{'type'});
104     $fchksum->add_fd(fileno($f));
105     if ($fchksum != $chksum) {
106       print "$file: checksum error\n";
107       $self->{'incomplete'} = 1 if $markincomplete;
108       return undef;
109     }
110   }
111   if ($uncompress) {
112     return solv::xfopen_fd($file, fileno($f));
113   } else {
114     return solv::xfopen_fd(undef, fileno($f));
115   }
116 }
117
118 sub usecachedrepo {
119   my ($self, $ext, $mark) = @_;
120   my $cookie = $ext ? $self->{'extcookie'} : $self->{'cookie'};
121   my $handle = $self->{'handle'};
122   my $cachepath = $self->cachepath($ext);
123   my $fextcookie;
124   if (sysopen(my $f, $cachepath, POSIX::O_RDONLY)) {
125     sysseek($f, -32, Fcntl::SEEK_END);
126     my $fcookie = '';
127     return undef if sysread($f, $fcookie, 32) != 32;
128     return undef if $cookie && $fcookie ne $cookie;
129     if ($self->{'type'} ne 'system' && !$ext) {
130       sysseek($f, -32 * 2, Fcntl::SEEK_END);
131       return undef if sysread($f, $fextcookie, 32) != 32;
132     }
133     sysseek($f, 0, Fcntl::SEEK_SET);
134     my $fd = solv::xfopen_fd(undef, fileno($f));
135     my $flags = $ext ? $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES : 0;
136     $flags |= $solv::Repo::REPO_LOCALPOOL if $ext && $ext ne 'DL';
137     if (!$self->{'handle'}->add_solv($fd, $flags)) {
138       return undef;
139     }
140     $self->{'cookie'} = $fcookie unless $ext;
141     $self->{'extcookie'} = $fextcookie if $fextcookie;
142     utime undef, undef, $f if $mark;
143     return 1;
144   }
145   return undef;
146 }
147
148 sub writecachedrepo {
149   my ($self, $ext, $info) = @_;
150   return if $self->{'incomplete'};
151   mkdir("/var/cache/solv", 0755) unless -d "/var/cache/solv";
152   my ($f, $tmpname);
153   eval {
154     ($f, $tmpname) = File::Temp::tempfile(".newsolv-XXXXXX", 'DIR' => '/var/cache/solv');
155   };
156   return unless $f;
157   chmod 0444, $f;
158   my $ff = solv::xfopen_fd(undef, fileno($f));
159   if (!$info) {
160     $self->{'handle'}->write($ff);
161   } elsif ($ext) {
162     $info->write($ff);
163   } else {
164      $self->{'handle'}->write_first_repodata($ff);
165   }
166   undef $ff;    # also flushes
167   if ($self->{'type'} ne 'system' && !$ext) {
168     $self->{'extcookie'} ||= $self->calc_cookie_ext($f, $self->{'cookie'});
169     syswrite($f, $self->{'extcookie'});
170   }
171   syswrite($f, $ext ? $self->{'extcookie'} : $self->{'cookie'});
172   close($f);
173   if ($self->{'handle'}->iscontiguous()) {
174     $f = solv::xfopen($tmpname);
175     if ($f) {
176       if (!$ext) {
177         $self->{'handle'}->empty();
178         die("internal error, cannot reload solv file\n") unless $self->{'handle'}->add_solv($f, $solv::Repo::SOLV_ADD_NO_STUBS);
179       } else {
180         $info->extend_to_repo();
181         my $flags = $solv::Repo::REPO_EXTEND_SOLVABLES;
182         $flags |= $solv::Repo::REPO_LOCALPOOL if $ext ne 'DL';
183         $info->add_solv($f, $flags);
184       }
185     }
186   }
187   rename($tmpname, $self->cachepath($ext));
188 }
189
190 sub packagespath {
191   my ($self) = @_;
192   return '';
193 }
194
195 package Repo::rpmmd;
196
197 our @ISA = ('Repo::generic');
198
199 sub find {
200   my ($self, $what) = @_;
201   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_TYPE, $what, $solv::Dataiterator::SEARCH_STRING);
202   $di->prepend_keyname($solv::REPOSITORY_REPOMD);
203   for my $d (@$di) {
204     my $dp = $d->parentpos();
205     my $filename = $dp->lookup_str($solv::REPOSITORY_REPOMD_LOCATION);
206     next unless $filename;
207     my $chksum = $dp->lookup_checksum($solv::REPOSITORY_REPOMD_CHECKSUM);
208     if (!$chksum) {
209       print "no $filename file checksum!\n";
210       return (undef, undef);
211     }
212     return ($filename, $chksum);
213   }
214   return (undef, undef);
215 }
216
217 sub add_ext {
218   my ($self, $repodata, $what, $ext) = @_;
219   my ($filename, $chksum) = $self->find($what);
220   ($filename, $chksum) = $self->find('prestodelta') if !$filename && $what eq 'deltainfo';
221   return unless $filename;
222   my $handle = $repodata->new_handle();
223   $repodata->set_poolstr($handle, $solv::REPOSITORY_REPOMD_TYPE, $what);
224   $repodata->set_str($handle, $solv::REPOSITORY_REPOMD_LOCATION, $filename);
225   $repodata->set_checksum($handle, $solv::REPOSITORY_REPOMD_CHECKSUM, $chksum);
226   if ($ext eq 'DL') {
227     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
228     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
229   } elsif ($ext eq 'FL') {
230     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_FILELIST);
231     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRSTRARRAY);
232   }
233   $repodata->add_flexarray($solv::SOLVID_META, $solv::REPOSITORY_EXTERNAL, $handle);
234 }
235
236 sub add_exts {
237   my ($self) = @_;
238   my $repodata = $self->{'handle'}->add_repodata(0);
239   $self->add_ext($repodata, 'deltainfo', 'DL');
240   $self->add_ext($repodata, 'filelists', 'FL');
241   $repodata->internalize();
242 }
243
244 sub load_ext {
245   my ($self, $repodata) = @_;
246   my $repomdtype = $repodata->lookup_str($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_TYPE);
247   my $ext;
248   if ($repomdtype eq 'filelists') {
249     $ext = 'FL';
250   } elsif ($repomdtype eq 'deltainfo') {
251     $ext = 'DL';
252   } else {
253     return 0;
254   }
255   print("[$self->{'alias'}:$ext: ");
256   STDOUT->flush();
257   if ($self->usecachedrepo($ext)) {
258     print "cached]\n";
259     return 1;
260   }
261   print "fetching]\n";
262   my $filename = $repodata->lookup_str($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_LOCATION);
263   my $filechksum = $repodata->lookup_checksum($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_CHECKSUM);
264   my $f = $self->download($filename, 1, $filechksum);
265   return 0 unless $f;
266   if ($ext eq 'FL') {
267     $self->{'handle'}->add_rpmmd($f, 'FL', $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES|$solv::Repo::REPO_LOCALPOOL);
268   } elsif ($ext eq 'DL') {
269     $self->{'handle'}->add_deltainfoxml($f, $solv::Repo::REPO_USE_LOADING);
270   }
271   $self->writecachedrepo($ext, $repodata);
272   return 1;
273 }
274
275 sub load {
276   my ($self, $pool) = @_;
277   return 1 if $self->Repo::generic::load($pool);
278   print "rpmmd repo '$self->{'alias'}': ";
279   STDOUT->flush();
280   my $f = $self->download("repodata/repomd.xml");
281   if (!$f) {
282     print "no repomd.xml file, skipped\n";
283     $self->{'handle'}->free(1);
284     delete $self->{'handle'};
285     return undef;
286   }
287   $self->{'cookie'} = $self->calc_cookie_fp($f);
288   if ($self->usecachedrepo(undef, 1)) {
289     print "cached\n";
290     return 1;
291   }
292   $self->{'handle'}->add_repomdxml($f, 0);
293   print "fetching\n";
294   my ($filename, $filechksum) = $self->find('primary');
295   if ($filename) {
296     $f = $self->download($filename, 1, $filechksum, 1);
297     if ($f) {
298       $self->{'handle'}->add_rpmmd($f, undef, 0);
299     }
300     return undef if $self->{'incomplete'};
301   }
302   ($filename, $filechksum) = $self->find('updateinfo');
303   if ($filename) {
304     $f = $self->download($filename, 1, $filechksum, 1);
305     if ($f) {
306       $self->{'handle'}->add_updateinfoxml($f, 0);
307     }
308   }
309   $self->add_exts();
310   $self->writecachedrepo();
311   $self->{'handle'}->create_stubs();
312   return 1;
313 }
314
315 package Repo::susetags;
316
317 our @ISA = ('Repo::generic');
318
319 sub find {
320   my ($self, $what) = @_;
321   
322   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME, $what, $solv::Dataiterator::SEARCH_STRING);
323   $di->prepend_keyname($solv::SUSETAGS_FILE);
324   for my $d (@$di) {
325     my $dp = $d->parentpos();
326     my $chksum = $dp->lookup_checksum($solv::SUSETAGS_FILE_CHECKSUM);
327     return ($what, $chksum);
328   }
329   return (undef, undef);
330 }
331
332 my %langtags = (
333   $solv::SOLVABLE_SUMMARY     => $solv::REPOKEY_TYPE_STR,
334   $solv::SOLVABLE_DESCRIPTION => $solv::REPOKEY_TYPE_STR,
335   $solv::SOLVABLE_EULA        => $solv::REPOKEY_TYPE_STR,
336   $solv::SOLVABLE_MESSAGEINS  => $solv::REPOKEY_TYPE_STR,
337   $solv::SOLVABLE_MESSAGEDEL  => $solv::REPOKEY_TYPE_STR,
338   $solv::SOLVABLE_CATEGORY    => $solv::REPOKEY_TYPE_ID,
339 );
340
341 sub add_ext {
342   my ($self, $repodata, $what, $ext) = @_;
343   my ($filename, $chksum) = $self->find($what);
344   my $handle = $repodata->new_handle();
345   $repodata->set_str($handle, $solv::SUSETAGS_FILE_NAME, $filename);
346   $repodata->set_checksum($handle, $solv::SUSETAGS_FILE_CHECKSUM, $chksum);
347   if ($ext eq 'DL') {
348     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
349     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
350   } elsif ($ext eq 'DU') {
351     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_DISKUSAGE);
352     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRNUMNUMARRAY);
353   } elsif ($ext eq 'FL') {
354     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_FILELIST);
355     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRSTRARRAY);
356   } else {
357     for my $langid (sort {$a <=> $b} keys %langtags) {
358       $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $self->{'handle'}->{'pool'}->id2langid($langid, $ext, 1));
359       $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $langtags{$langid});
360     }
361   }
362   $repodata->add_flexarray($solv::SOLVID_META, $solv::REPOSITORY_EXTERNAL, $handle);
363 }
364
365 sub add_exts {
366   my ($self) = @_;
367   my $repodata = $self->{'handle'}->add_repodata(0);
368   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME, undef, 0);
369   $di->prepend_keyname($solv::SUSETAGS_FILE);
370   for my $d (@$di) {
371     my $filename = $d->str();
372     next unless $filename && $filename =~ /^packages\.(..)(?:\..*)$/;
373     next if $1 eq 'en' || $1 eq 'gz';
374     $self->add_ext($repodata, $filename, $1);
375   }
376   $repodata->internalize();
377 }
378
379 sub load_ext {
380   my ($self, $repodata) = @_;
381   my $filename = $repodata->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME);
382   my $ext = substr($filename, 9, 2);
383   print("[$self->{'alias'}:$ext: ");
384   STDOUT->flush();
385   if ($self->usecachedrepo($ext)) {
386     print "cached]\n";
387     return 1;
388   }
389   print "fetching]\n";
390   my $defvendorid = $self->{'handle'}->lookup_id($solv::SOLVID_META, $solv::SUSETAGS_DEFAULTVENDOR);
391   my $descrdir = $self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DESCRDIR) || 'suse/setup/descr'; 
392   my $filechksum = $repodata->lookup_checksum($solv::SOLVID_META, $solv::SUSETAGS_FILE_CHECKSUM);
393   my $f = $self->download("$descrdir/$filename", 1, $filechksum);
394   return 0 unless $f;
395   my $flags = $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES;
396   $flags |= $solv::Repo::REPO_LOCALPOOL if $ext ne 'DL';
397   $self->{'handle'}->add_susetags($f, $defvendorid, $ext, $flags);
398   $self->writecachedrepo($ext, $repodata);
399   return 1;
400 }
401
402 sub load {
403   my ($self, $pool) = @_;
404   return 1 if $self->Repo::generic::load($pool);
405   print "susetags repo '$self->{'alias'}': ";
406   STDOUT->flush();
407   my $f = $self->download("content");
408   if (!$f) {
409     print "no content file, skipped\n";
410     $self->{'handle'}->free(1);
411     delete $self->{'handle'};
412     return undef;
413   }
414   $self->{'cookie'} = $self->calc_cookie_fp($f);
415   if ($self->usecachedrepo(undef, 1)) {
416     print "cached\n";
417     return 1;
418   }
419   $self->{'handle'}->add_content($f, 0);
420   print "fetching\n";
421   my $defvendorid = $self->{'handle'}->lookup_id($solv::SOLVID_META, $solv::SUSETAGS_DEFAULTVENDOR);
422   my $descrdir = $self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DESCRDIR) || 'suse/setup/descr'; 
423   my ($filename, $filechksum) = $self->find('packages.gz');
424   ($filename, $filechksum) = $self->find('packages') unless $filename;
425   if ($filename) {
426     $f = $self->download("$descrdir/$filename", 1, $filechksum, 1);
427     if ($f) {
428       $self->{'handle'}->add_susetags($f, $defvendorid, undef, $solv::Repo::REPO_NO_INTERNALIZE|$solv::Repo::SUSETAGS_RECORD_SHARES);
429       ($filename, $filechksum) = $self->find('packages.en.gz');
430       ($filename, $filechksum) = $self->find('packages.en') unless $filename;
431       if ($filename) {
432         $f = $self->download("$descrdir/$filename", 1, $filechksum, 1);
433         if ($f) {
434           $self->{'handle'}->add_susetags($f, $defvendorid, undef, $solv::Repo::REPO_NO_INTERNALIZE|$solv::Repo::REPO_REUSE_REPODATA|$solv::Repo::REPO_EXTEND_SOLVABLES);
435         }
436       }
437       $self->{'handle'}->internalize();
438     }
439   }
440   $self->add_exts();
441   $self->writecachedrepo();
442   $self->{'handle'}->create_stubs();
443   return undef;
444 }
445
446 sub packagespath {
447   my ($self) = @_;
448   return ($self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DATADIR) || 'suse') . '/';
449 }
450
451 package Repo::unknown;
452
453 our @ISA = ('Repo::generic');
454
455 sub load {
456   my ($self, $pool) = @_;
457   print "unsupported repo '$self->{'alias'}': skipped\n";
458   return 0;
459 }
460
461 package Repo::system;
462
463 our @ISA = ('Repo::generic');
464
465 sub load {
466   my ($self, $pool) = @_;
467
468   $self->{'handle'} = $pool->add_repo($self->{'alias'});
469   $self->{'handle'}->{'appdata'} = $self;
470   $pool->{'installed'} = $self->{'handle'};
471   print "rpm database: ";
472   $self->{'cookie'} = $self->calc_cookie_file('/var/lib/rpm/Packages');
473   if ($self->usecachedrepo()) {
474     print "cached\n";
475     return 1;
476   }
477   print "reading\n";
478   if (defined(&solv::Repo::add_products)) {
479     $self->{'handle'}->add_products("/etc/products.d", $solv::Repo::REPO_NO_INTERNALIZE);
480   }
481   my $f = solv::xfopen($self->cachepath());
482   $self->{'handle'}->add_rpmdb_reffp($f, $solv::Repo::REPO_REUSE_REPODATA);
483   $self->writecachedrepo();
484   return 1;
485 }
486
487 package main;
488
489 sub load_stub {
490   my ($repodata) = @_;
491   my $repo = $repodata->{'repo'}->{'appdata'};
492   return $repo ? $repo->load_ext($repodata) : 0;
493 }
494
495 die("Usage: p5solv COMMAND [ARGS]\n") unless @ARGV;
496 my $cmd = shift @ARGV;
497 my %cmdabbrev = ( 'li' => 'list', 'in' => 'install', 'rm' => 'erase',
498                   've' => 'verify', 'se' => 'search' );
499 $cmd = $cmdabbrev{$cmd} if $cmdabbrev{$cmd};
500
501 my %cmdactionmap = (
502   'install' => $solv::Job::SOLVER_INSTALL,
503   'erase'   => $solv::Job::SOLVER_ERASE,
504   'up'      => $solv::Job::SOLVER_UPDATE,
505   'dup'     => $solv::Job::SOLVER_DISTUPGRADE,
506   'verify'  => $solv::Job::SOLVER_VERIFY,
507   'list'    => 0,  
508   'info'    => 0,
509 );
510
511 my @repos;
512 my @reposdirs;
513 if (-d '/etc/zypp/repos.d') {
514   @reposdirs = ( '/etc/zypp/repos.d' );
515 } else {
516   @reposdirs = ( '/etc/yum/repos.d' );
517 }
518 for my $reposdir (@reposdirs) {
519   next unless -d $reposdir;
520   next unless opendir(DIR, $reposdir);
521   for my $reponame (sort(grep {/\.repo$/} readdir(DIR))) {
522     my $cfg = new Config::IniFiles('-file' => "$reposdir/$reponame");
523     for my $alias ($cfg->Sections()) {
524       my $repoattr = {'alias' => $alias, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900};
525       for my $p ($cfg->Parameters($alias)) {
526         $repoattr->{$p} = $cfg->val($alias, $p);
527       }
528       my $repo;
529       if ($repoattr->{'type'} eq 'rpm-md') {
530         $repo = Repo::rpmmd->new($alias, 'repomd', $repoattr);
531       } elsif ($repoattr->{'type'} eq 'yast2') {
532         $repo = Repo::susetags->new($alias, 'susetags', $repoattr);
533       } else {
534         $repo = Repo::unknown->new($alias, 'unknown', $repoattr);
535       }
536       push @repos, $repo;
537     }
538   }
539 }
540
541 my $pool = solv::Pool->new();
542 $pool->setarch();
543 $pool->set_loadcallback(\&load_stub);
544
545 my $sysrepo = Repo::system->new('@System', 'system');
546 $sysrepo->load($pool);
547 for my $repo (@repos) {
548   $repo->load($pool) if $repo->{'enabled'};
549 }
550
551 if ($cmd eq 'search') {
552   $pool->createwhatprovides();
553   my $sel = $pool->Selection();
554   my $di = $pool->Dataiterator(0, $solv::SOLVABLE_NAME, $ARGV[0], $solv::Dataiterator::SEARCH_SUBSTRING | $solv::Dataiterator::SEARCH_NOCASE);
555   for my $d (@$di) {
556     $sel->add_raw($solv::Job::SOLVER_SOLVABLE, $d->{'solvid'});
557   }
558   for my $s ($sel->solvables()) {
559     print "- ".$s->str()." [$s->{'repo'}->{'name'}]: ".$s->lookup_str($solv::SOLVABLE_SUMMARY)."\n";
560   }
561   exit(0);
562 }
563
564 die("unknown command '$cmd'\n") unless defined $cmdactionmap{$cmd};
565
566 my @addedprovides = $pool->addfileprovides_queue();
567 $pool->createwhatprovides();
568
569 my @jobs;
570 for my $arg (@ARGV) {
571   my $flags = $solv::Selection::SELECTION_NAME | $solv::Selection::SELECTION_PROVIDES | $solv::Selection::SELECTION_GLOB;
572   $flags |= $solv::Selection::SELECTION_CANON | $solv::Selection::SELECTION_DOTARCH | $solv::Selection::SELECTION_REL;
573   if ($arg =~ /^\//) {
574     $flags |= $solv::Selection::SELECTION_FILELIST;
575     $flags |= $solv::Selection::SELECTION_INSTALLED_ONLY if $cmd eq 'erase';
576   }
577   my $sel = $pool->select($arg, $flags);
578   if ($sel->isempty()) {
579     $sel = $pool->select($arg, $flags | $solv::Selection::SELECTION_NOCASE);
580     print "[ignoring case for '$arg']\n" unless $sel->isempty();
581   }
582   die("nothing matches '$arg'\n") if $sel->isempty();
583   print "[using file list match for '$arg']\n" if $sel->flags() & $solv::Selection::SELECTION_FILELIST;
584   print "[using capability match for '$arg']\n" if $sel->flags() & $solv::Selection::SELECTION_PROVIDES;
585   push @jobs, $sel->jobs($cmdactionmap{$cmd});
586 }
587
588 if (!@jobs && ($cmd eq 'up' || $cmd eq 'dup' || $cmd eq 'verify')) {
589   my $sel = $pool->Selection_all();
590   push @jobs, $sel->jobs($cmdactionmap{$cmd});
591 }
592
593 die("no package matched.\n") unless @jobs;
594
595 if ($cmd eq 'list' || $cmd eq 'info') {
596   for my $job (@jobs) {
597     for my $s ($job->solvables()) {
598       if ($cmd eq 'info') {
599         printf "Name:        %s\n", $s->str();
600         printf "Repo:        %s\n", $s->{'repo'}->{'name'};
601         printf "Summary:     %s\n", $s->lookup_str($solv::SOLVABLE_SUMMARY);
602         my $str = $s->lookup_str($solv::SOLVABLE_URL);
603         printf "Url:         %s\n", $str if $str;
604         $str = $s->lookup_str($solv::SOLVABLE_LICENSE);
605         printf "License:     %s\n", $str if $str;
606         printf "Description:\n%s\n", $s->lookup_str($solv::SOLVABLE_DESCRIPTION);
607       } else {
608         printf "  - %s [%s]\n", $s->str(), $s->{'repo'}->{'name'};
609         printf "    %s\n", $s->lookup_str($solv::SOLVABLE_SUMMARY);
610       }
611     }
612   }
613   exit 0;
614 }
615
616 # up magic, turn into install if nothing matches
617 for my $job (@jobs) {
618   $job->{'how'} ^= $solv::Job::SOLVER_UPDATE ^ $solv::Job::SOLVER_INSTALL if $cmd eq 'up' && $job->isemptyupdate();
619 }
620
621 my $solver = $pool->Solver();
622 $solver->set_flag($solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1);
623 $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if $cmd eq 'erase';
624
625 while (1) {
626   my @problems = $solver->solve(\@jobs);
627   last unless @problems;
628   for my $problem (@problems) {
629     print "Problem $problem->{'id'}/".@problems.":\n";
630     my $r = $problem->findproblemrule();
631     my $ri = $r->info();
632     print $ri->problemstr()."\n";
633     my @solutions = $problem->solutions();
634     for my $solution (@solutions) {
635       print "  Solution $solution->{'id'}:\n";
636       for my $element ($solution->elements(1)) {
637         print "  - ".$element->str()."\n";
638       }
639       print "\n";
640     }
641     my $sol;
642     while (1) {
643       print "Please choose a solution: ";
644       $sol = <STDIN>;
645       chomp $sol;
646       last if $sol eq 's' || $sol eq 'q' || ($sol =~ /^\d+$/ && $sol >= 1 && $sol <= @solutions);
647     }
648     next if $sol eq 's';
649     exit(1) if $sol eq 'q';
650     my $solution = $solutions[$sol - 1];
651     for my $element ($solution->elements()) {
652       my $newjob = $element->Job();
653       if ($element->{'type'} == $solv::Solver::SOLVER_SOLUTION_JOB) {
654         $jobs[$element->{'jobidx'}] = $newjob;
655       } else {
656         push @jobs, $newjob if $newjob && !grep {$_ == $newjob} @jobs;
657       }
658     }
659   }
660 }
661
662 my $trans = $solver->transaction();
663 undef $solver;
664 if ($trans->isempty()) {
665   print "Nothing to do.\n";
666   exit 0;
667 }
668
669 print "\nTransaction summary:\n\n";
670 for my $c ($trans->classify($solv::Transaction::SOLVER_TRANSACTION_SHOW_OBSOLETES|$solv::Transaction::SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE)) {
671   if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
672     print "$c->{'count'} erased packages:\n";
673   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_INSTALL) {
674     print "$c->{'count'} installed packages:\n";
675   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_REINSTALLED) {
676     print "$c->{'count'} reinstalled packages:\n";
677   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
678     print "$c->{'count'} downgraded packages:\n";
679   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_CHANGED) {
680     print "$c->{'count'} changed packages:\n";
681   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED) {
682     print "$c->{'count'} upgraded packages:\n";
683   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE) {
684     printf "$c->{'count'} vendor changes from '%s' to '%s':\n", $c->fromdep()->str(), $c->todep()->str();
685   } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE) {
686     printf "$c->{'count'} arch changes from '%s' to '%s':\n", $c->fromdep()->str(), $c->todep()->str();
687   } else {
688     next;
689   }
690   for my $p ($c->solvables()) {
691     if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED || $c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
692       my $other = $trans->othersolvable($p);
693       printf "  - %s -> %s\n", $p->str(), $other->str();
694     } else {
695       printf "  - %s\n", $p->str();
696     }
697   }
698   print "\n";
699 }
700 printf "install size change: %d K\n\n", $trans->calc_installsizechange();
701
702 while (1) {
703   print("OK to continue (y/n)? ");
704   my $yn = <STDIN>;
705   chomp $yn;
706   last if $yn eq 'y';
707   exit(1) if $yn eq 'n' || $yn eq 'q';
708 }
709
710 my @newpkgs = $trans->newpackages();
711 my %newpkgsfps;
712 if (@newpkgs) {
713   my $downloadsize = 0;
714   $downloadsize += $_->lookup_num($solv::SOLVABLE_DOWNLOADSIZE) for @newpkgs;
715   printf "Downloading %d packages, %d K\n", scalar(@newpkgs), $downloadsize;
716   for my $p (@newpkgs) {
717     my $repo = $p->{'repo'}->{'appdata'};
718     my ($location, $medianr) = $p->lookup_location();
719     next unless $location;
720     $location = $repo->packagespath() . $location;
721     my $chksum = $p->lookup_checksum($solv::SOLVABLE_CHECKSUM);
722     my $f = $repo->download($location, 0, $chksum);
723     die("\n$repo->{'alias'}: $location not found in repository\n") unless $f;
724     $newpkgsfps{$p->{'id'}} = $f;
725     print ".";
726     STDOUT->flush();
727   }
728   print "\n";
729 }
730
731 print "Committing transaction:\n\n";
732 $trans->order(0);
733 for my $p ($trans->steps()) {
734   my $steptype = $trans->steptype($p, $solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY);
735   if ($steptype == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
736     print "erase ".$p->str()."\n";
737     next unless $p->lookup_num($solv::RPM_RPMDBID);
738     my $evr = $p->{'evr'};
739     $evr =~ s/^[0-9]+://;       # strip epoch
740     system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "$p->{'name'}-$evr.$p->{'arch'}") && die("rpm failed: $?\n");
741   } elsif ($steptype == $solv::Transaction::SOLVER_TRANSACTION_INSTALL || $steptype == $solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL) {
742     print "install ".$p->str()."\n";
743     my $f = $newpkgsfps{$p->{'id'}};
744     my $mode = $steptype == $solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i';
745     system('rpm', $mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/".$f->fileno()) && die("rpm failed: $?\n");
746     delete $newpkgsfps{$p->{'id'}};
747   }
748 }
749
750 exit 0;