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

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

=head1 NAME

    ManaTools::Rpmdragora::DB - Rpmdragora extension of urpm DB object

=head1 SYNOPSIS

    use ManaTools::Rpmdragora::DB;

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


=head1 DESCRIPTION

    This module is the Rpmdragora extension for the backend to urpm open db

=head1 SUPPORT

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

    perldoc ManaTools::Rpmdragora::DB

=head1 SEE also

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

use Moose;
extends 'ManaTools::Shared::urpmi_backend::DB';
use feature 'state';

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

=head2 open_rpm_db

=head3 OUTPUT

    URPM::DB: an URPM opened dataase

=head3 DESCRIPTION

    this method return an URPM::DB object

=cut

#=============================================================
override 'open_rpm_db' => sub {
    my ($self, $o_force) = @_;

    $self->rpm_root($::rpmdragora_options{'rpm-root'}[0]) if $::rpmdragora_options{'rpm-root'}[0];

    my $host;
    Sys::Syslog::syslog('info|local1', "opening the RPM database");
    if ($::rpmdragora_options{parallel} && ((undef, $host) = @{$::rpmdragora_options{parallel}})) {
        state $done;
        my $dblocation = "/var/cache/urpmi/distantdb/$host";
        if (!$done || $o_force) {

            print "syncing db from $host to $dblocation...";

            mkdir_p "$dblocation/var/lib/rpm";
            system "rsync -Sauz -e ssh $host:/var/lib/rpm/ $dblocation/var/lib/rpm";
            $? == 0 or die "Couldn't sync db from $host to $dblocation";
            $done = 1;

            print "done.\n";
        }
        URPM::DB::open($dblocation) or die "Couldn't open RPM DB";
    } else {
        my $db;
        if ($::env) {
            #- URPM has same methods as URPM::DB and empty URPM will be seen as empty URPM::DB.
            $db = new URPM;
            $db->parse_synthesis("$::env/rpmdb.cz");
        } else {
            $db = super();
        }
        $db or die "Couldn't open RPM DB (" . ($::env ? "$::env/rpmdb.cz" : $::rpmdragora_options{'rpm-root'}[0]) . ")";
    }
};

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

=head2 fast_open_urpmi_db

=head3 OUTPUT

urpm: an urpm object

=head3 DESCRIPTION

    this method return an urpm object

=cut

#=============================================================
override 'fast_open_urpmi_db' => sub  {
    my $self = shift;

    $self->urpmi_root($::rpmdragora_options{'urpmi-root'}[0]) if $::rpmdragora_options{'urpmi-root'}[0];
    $self->rpm_root($::rpmdragora_options{'rpm-root'}[0]) if $::rpmdragora_options{'rpm-root'}[0];
    $self->wait_lock($::rpmdragora_options{'wait-lock'}) if $::rpmdragora_options{'wait-lock'};
    $self->verify_rpm(!$::rpmdragora_options{'no-verify-rpm'}) if defined $::rpmdragora_options{'no-verify-rpm'};
    $self->auto($::rpmdragora_options{auto}) if $::rpmdragora_options{auto};
    $self->set_verbosity(1);
    $self->justdb($::rpmdragora_options{justdb}) if $::rpmdragora_options{justdb};

    my $urpm = super();

    if ($::rpmdragora_options{env} && $::rpmdragora_options{env}[0]) {
        $::env = $::rpmdragora_options{env}[0];
        # prevent crashing in URPM.pm prevent when using --env:
        $::env = "$ENV{PWD}/$::env" if $::env !~ m!^/!;
        urpm::set_env($urpm, $::env);
    }

    foreach (@{$urpm->{media}}) {
            next if $_->{ignore};
            urpm::media::_tempignore($_, 1) if $ignore_debug_media->[0] && $_->{name} =~ /debug/i;
    }

    # TODO check if no media present and find a way to run edit urpm media

    return $urpm;
};

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

=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,
    this method totally overrides the super class

=cut

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

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

    $self->lock($urpm) if !$::env;

    my $previous = $::rpmdragora_options{'previous-priority-upgrade'};
    urpm::select::set_priority_upgrade_option($urpm, (ref $previous ? join(',', @$previous) : ()));
    urpm::media::configure($urpm, media => $media, MDK::Common::Func::if_($searchmedia, searchmedia => $searchmedia), %urpmi_options);

    return $urpm;
};


1;