aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/urpmi_backend/DB.pm
blob: e646d8e6ab8a63610b4f6bf3e55bf599bf16257b (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
# vim: set et ts=4 sw=4:
package ManaTools::Shared::urpmi_backend::DB;

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

=head1 NAME

    ManaTools::Shared::urpmi_backend::DB - urpm DB object

=head1 SYNOPSIS

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

    my $db_man = ManaTools::Shared::urpmi_backend::DB->new();
    my $urpm = $db_man->fast_open_urpmi_db();


=head1 DESCRIPTION

    This module is a backend to urpm open db

=head1 SUPPORT

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

    perldoc ManaTools::Shared::urpmi_backend::DB


=head1 AUTHOR

    Angelo Naselli <anaselli@linux.it>

=head1 COPYRIGHT and LICENSE

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

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 MDK::Common::File qw(cat_ mkdir_p);
use MDK::Common::Func qw(if_);

use urpm;
use urpm::media;
use urpm::select;
use urpm::mirrors;

use URPM;
use feature 'state';

# TODO evaluate if managing urpm object inside
#      left as a parameter to back compatibility by now.
#

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

=head2 new - optional parameters

=head3 urpmi_root

    optional parameter urpmi_root directory,
    default value is undef

=cut

#=============================================================
has 'urpmi_root' => (
    is => 'rw',
    isa => 'Str'
);

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

=head2 new - optional parameters

=head3 rpm_root

    optional parameter rpm_root directory,
    default value is undef

=cut

#=============================================================
has 'rpm_root'   => (
    is => 'rw',
    isa => 'Str'
);

# product_id contains the product id file pathname
has 'product_id' => (
    is        => 'ro',
    isa       => 'Str',
    init_arg  => undef,
    lazy    => 1,
    builder => '_product_id_init',
);

sub _product_id_init {
    my $self = shift;

    return  ($self->urpmi_root() || '') . '/etc/product.id',
}

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

=head2 open_rpm_db

=head3 OUTPUT

URPM::DB: an URPM opened dataase

=head3 DESCRIPTION

    this method return an URPM::DB object

=cut

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

    URPM::DB::open($self->rpm_root() ||'')  or die "Couldn't open RPM DB " . ($self->rpm_root() ||'');
}

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

=head2 fast_open_urpmi_db

=head3 OUTPUT

urpm: an urpm object

=head3 DESCRIPTION

    this method return an urpm object

=cut

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

    my $urpm = urpm->new;

    urpm::set_files($urpm, $self->urpmi_root()) if $self->urpmi_root();
    my $rpm_root = $self->rpm_root() || $self->urpmi_root();
    urpm::args::set_root($urpm, $rpm_root) if $rpm_root;

    $urpm->get_global_options;
    urpm::media::read_config($urpm);
    $urpm;
}

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

=head2 is_it_a_devel_distro

=head3 DESCRIPTION

    This method returns if current distro is not stable

=cut

#=============================================================
sub is_it_a_devel_distro {
    my $self = shift;
    state $res;

    return $res if defined $res;
    $res = urpm::mirrors::parse_LDAP_namespace_structure(cat_($self->product_id()))->{branch} eq 'Devel';
    return $res;
}

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

=head2 get_backport_media

=head3 INPUT

    $urpm: an urpm object

=head3 DESCRIPTION

    This method returns a list of backport media

=cut

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

    grep { $_->{name} =~ /backport/i &&
           $_->{name} !~ /debug|sources|testing/i } @{$urpm->{media}};
}


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

=head2 get_inactive_backport_media

=head3 INPUT

    $urpm: an urpm object

=head3 DESCRIPTION

    This method returns a list of inactive backport media

=cut

#=============================================================
sub get_inactive_backport_media {
    my ($self, $urpm) = @_;
    map { $_->{name} } grep { $_->{ignore} } $self->get_backport_media($urpm);
}

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

=head2 get_update_medias

=head3 INPUT

    $urpm: an urpm object

=head3 DESCRIPTION

    This method returns a list of update media

=cut

#=============================================================
sub get_update_medias {
    my ($self, $urpm) = @_;
    if ($self->is_it_a_devel_distro()) {
        grep { !$_->{ignore} } @{$urpm->{media}};
    } else {
        grep { !$_->{ignore} && $_->{update} } @{$urpm->{media}};
    }
}

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

=head2 open_urpmi_db

=head3 INPUT

    %urpmi_options: urpmi options used to open and lock urpmi
                    db.

=head3 DESCRIPTION

    This method returns an urpm option with a lcok on db

=cut

#=============================================================
sub open_urpmi_db {
    my ($self, %urpmi_options) = @_;
    my $urpm = $self->fast_open_urpmi_db();

    my $searchmedia = $urpmi_options{update} ? undef : join(',', $self->get_inactive_backport_media($urpm));

    $self->lock($urpm);

    #next part could be changed in extended implementation on media and priority
    urpm::select::set_priority_upgrade_option($urpm, ());
    urpm::media::configure($urpm, media => '', if_($searchmedia, searchmedia => $searchmedia), %urpmi_options);

    $urpm;
}


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

=head2 lock

=head3 INPUT

    $urpm: urpm object

=head3 OUTPUT

    0 if already locked, 1 otherwhise

=head3 DESCRIPTION

    This method locks the db passed into urpm object

=cut

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

    return 0 if $urpm->{lock}; # already locked

    $urpm->{lock} = urpm::lock::urpmi_db($urpm, undef, wait => $urpm->{options}{wait_lock});

    return 1;
}

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

=head2 unlock

=head3 INPUT

    $urpm: urpm object

=head3 DESCRIPTION

    This method unlocks the db passed into urpm object

=cut

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

    $urpm->{lock} = undef;

    return;
}

1;