aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/urpmi_backend/tools.pm
blob: 7573e38744d7dfc3092226d994cdc5caeada1529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# vim: set et ts=4 sw=4:
package ManaTools::Shared::urpmi_backend::tools;

#============================================================= -*-perl-*-

=head1 NAME

    ManaTools::Shared::urpmi_backend::tools - urpmi backend tools object

=head1 SYNOPSIS

    use ManaTools::Shared::urpmi_backend::tools;

    my $urpm_tools = ManaTools::Shared::urpmi_backend::tools->new();


=head1 DESCRIPTION

    This module is a backend to some urpmi funcitionalities

=head1 SUPPORT

    You can find documentation for this module with the perldoc command:

    perldoc ManaTools::Shared::urpmi_backend::tools

=head1 SEE also

    ManaTools::Shared::urpmi_backend::DB

=head1 AUTHOR

    Angelo Naselli <anaselli@linux.it>

=head1 COPYRIGHT and LICENSE

Copyright (c) 2015-2016 Angelo Naselli <anaselli@linux.it>
    from Rpmdrake:
     Copyright (c) 2002 Guillaume Cottenceau
     Copyright (c) 2003-2005 MandrakeSoft SA
     Copyright (c) 2005-2007 Mandriva SA
     Copyright (c) 2008 Aurelien Lefebvre <alkh@mandriva.org>
     Copyright (c) 2002-2014 Thierry Vignaud <thierry.vignaud@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=head1 METHODS

=cut

use Moose;

use strict;
use ManaTools::Shared::urpmi_backend::DB;

use MDK::Common::File qw(cat_);
use URPM;
use urpm::msg;


#=============================================================

=head2 object attribute

=head3 urpmi_db_backend

    a ManaTools::Shared::urpmi_backend::DB object

=cut

#=============================================================
has 'urpmi_db_backend' => (
    is        => 'rw',
    init_arg  => undef,
    lazy    => 1,
    builder => '_urpmi_db_backend_init',
);

sub _urpmi_db_backend_init {
    my $self = shift;

    return  ManaTools::Shared::urpmi_backend::DB->new(),
}


has 'loc' => (
        is => 'rw',
        isa => 'ManaTools::Shared::Locales',
        lazy => 1,
        builder => '_localeInitialize',
);


sub _localeInitialize {
    my $self = shift;

    $self->loc(
        ManaTools::Shared::Locales->new(
            domain_name => 'manatools',
        )
    );
}

#=============================================================

=head2 get_update_medias

=head3 INPUT

    $urpm: an urpm object

=head3 DESCRIPTION

    This method returns a list of update media
    (here for convenience, implemented in
    ManaTools::Shared::urpmi_backend::DB)

=cut

#=============================================================
sub get_update_medias {
    my ($self, $urpm) = @_;

    $self->urpmi_db_backend()->get_update_medias($urpm);
}

#=============================================================

=head2 ensure_utf8

=head3 INPUT

    text: a string that is converted or left to utf8

=head3 DESCRIPTION

    This method ensure the given string is utf8
=cut

#=============================================================
sub ensure_utf8 {
    my ($self, $text) = @_;
    return '' if !$text;

    if (utf8::is_utf8($text)) {
        utf8::valid($text) and return;
        utf8::encode($text); #- disable utf8 flag
        utf8::upgrade($text);
    } else {
        utf8::decode($text); #- try to set utf8 flag
        utf8::valid($text) and return;
        warn "do not know what to with $text\n";
    }
}

#=============================================================

=head2 rpm_description

=head3 INPUT

#     description: package description field

=head3 DESCRIPTION

    Retrieve the rpm desctiption

=cut

#=============================================================
sub rpm_description {
    my ($self, $description) = @_;
    ensure_utf8($description);
    my ($t, $tmp);
    foreach (split "\n", $description) {
        s/^\s*//;
        if (/^$/ || /^\s*(-|\*|\+|o)\s/) {
            $t || $tmp and $t .= "$tmp\n";
            $tmp = $_;
        } else {
            $tmp = ($tmp ? "$tmp " : ($t && "\n") . $tmp) . $_;
        }
    }
    "$t$tmp\n";
}

#=============================================================

=head2 urpm_name

=head3 INPUT

    $pkg: package

=head3 DESCRIPTION

    This method returns the urpm format name e.g.
    name-version-release.arch

=cut

#=============================================================
sub urpm_name {
    my ($self, $pkg) = @_;

    return '?-?-?.?' unless ref($pkg) eq 'URPM::Package';
#     my ($name, $version, $release, $arch) = $pkg->fullname;
#     return "$name-$version-$release.$arch";

    return scalar $pkg->fullname;
}

#=============================================================

=head2 is_package_installed

=head3 INPUT

    $package: package to find (URPM::Package) or package name

=head3 DESCRIPTION

    This method returns if a package is installed

=cut

#=============================================================
sub is_package_installed {
    my ($self, $pkg) = @_;

    my $installed = 0;
    my $db = $self->urpmi_db_backend()->open_rpm_db();
    if (ref($pkg) eq 'URPM::Package') {
        $installed = URPM::is_package_installed(URPM::DB::open(), $pkg);
    }
    else {
        my $version = 0;
        $db->traverse_tag_find('name', $pkg, sub { $version = $_[0]->EVR; return ($version ? 1 : 0) });
        $installed = $version ? 1 : 0;
    }

    return $installed;
}

#=============================================================

=head2 find_installed_fullname

=head3 INPUT

    $package: package to find (URPM::Package) or package name

=head3 DESCRIPTION

    This method returns the full name of the package if installed

=cut

#=============================================================
sub find_installed_fullname {
  my ($self, $p) = @_;

  # we can call it with the name or packge
  my $name = ref($p) eq 'URPM::Package' ? $p->name : $p;

  my @fullname;
  my $db = $self->urpmi_db_backend()->open_rpm_db();
  $db->traverse_tag('name', [ $name ], sub { push @fullname, scalar($_[0]->fullname) });

  return @fullname ? join(',', sort @fullname) : "";
}


#=============================================================

=head2 is_mageia

=head3 DESCRIPTION

    This method returns if the system is mageia linux

=cut

#=============================================================
sub is_mageia {
    my $self = shift;

    return cat_('/etc/release') =~ /Mageia/;
}

#=============================================================

=head2 vendor

=head3 DESCRIPTION

    This method returns if the vendor is mageia or mandriva

=cut

#=============================================================
sub vendor {
    my $self = shift;

    return $self->is_mageia() ? "mageia" : "mandriva";
}


#=============================================================

=head2 get_package_id

=head3 INPUT

    $package: package (URPM::Package)

=head3 DESCRIPTION

    This method returns the package id meant as
    (name;version-release;arch;vendor)

=cut

#=============================================================
sub get_package_id {
  my ($self, $pkg) = @_;

  return '?;?-?;?;?' unless ref($pkg) eq 'URPM::Package';

  return join(';', $pkg->name, $pkg->version . "-" . $pkg->release, $pkg->arch, $self->vendor());
}


#=============================================================

=head2 pkg2medium

=head3 INPUT

    $p:     package (URPM::Package)
    $urpm:  urpm object

=head3 DESCRIPTION

    Returns the medium that contains the URPM::Package $pkg

=cut

#=============================================================
sub pkg2medium {
    my ($self, $p, $urpm) = @_;

    return if ref($p) ne 'URPM::Package';

    return {
        name => $self->loc->N_("None (installed)")
    } if !defined($p->id); # if installed

    return URPM::pkg2media($urpm->{media}, $p) || {
        name => $self->loc->N("Unknown"), fake => 1
    };
}

#=============================================================

=head2 fullname_to_package_id

=head3 INPUT

    $pkg_string: package fullname

=head3 DESCRIPTION

    Returns package id meant as "name;varsion-release;arch;vendor"

=cut

#=============================================================
sub fullname_to_package_id {
    # fullname, ie 'xeyes-1.0.1-5mdv2008.1.i586'
    my ($self, $pkg_string) = @_;
    chomp($pkg_string);
    if ($pkg_string =~ /^(.*)-([^-]*)-([^-]*)\.([^\.]*)$/) {
# TODO NOTE check package kit backend it seems the urpm package_id is "name;varsion-release;arch;vendor"
        return join(';', $1, "$2-$3", $4, $self->vendor());
    }
}

#=============================================================

=head2 get_package_by_package_id

=head3 INPUT

    $urpm:  urpm object
    $package_id: package id (see fullname_to_package_id)

=head3 DESCRIPTION

    Returns URPM::Package package

=cut

#=============================================================
sub get_package_by_package_id {
    my ($self, $urpm, $package_id) = @_;
    my @depslist = @{$urpm->{depslist}};
    foreach (@depslist) {
        if ($self->get_package_id($_) eq $package_id) {
            return $_;
        }
    }

    return;
}

#=============================================================

=head2 get_installed_fullname_pkid

    $pkg:     package (URPM::Package) or package name (string)


=head3 DESCRIPTION

    Returns package id of the given package

=cut

#=============================================================
sub get_installed_fullname_pkid {
    my ($self, $pkg) = @_;
    my $pkgname = ref($pkg) eq 'URPM::Package' ? $pkg->name : $pkg;
    my $db = $self->urpmi_db_backend()->open_rpm_db();
    my $installed_pkid;
    $db->traverse_tag_find('name', $pkgname, sub {
        my ($p) = @_;
        $installed_pkid = $self->get_package_id($p);
        return $installed_pkid ? 1 : 0;
    });
    return $installed_pkid;
}

#=============================================================

=head2 get_package_upgrade

    $urpm:  urpm object
    $pkg:   package (URPM::Package) or package name (string)


=head3 DESCRIPTION

    Returns package to upgrade

=cut

#=============================================================
sub get_package_upgrade {
    my ($self, $urpm, $pkg) = @_;

    my $db = $self->urpmi_db_backend()->open_rpm_db();
    $urpm->compute_installed_flags($db);
    my @depslist = @{$urpm->{depslist}};
    my $pkgname = ref($pkg) eq 'URPM::Package' ? $pkg->name : $pkg;

    foreach (@depslist) {
        if ($_->name =~ /^$pkgname$/ && $_->flag_upgrade) {
            return $_;
        }
    }

    return;
}


1;