aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Rpmdragora/open_db.pm
blob: 7bfa7bf53b86ddb1a70924b4329274eb52b1a751 (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
# vim: set et ts=4 sw=4:
package ManaTools::Rpmdragora::open_db;
#*****************************************************************************
#
#  Copyright (c) 2002 Guillaume Cottenceau
#  Copyright (c) 2002-2014 Thierry Vignaud <thierry.vignaud@gmail.com>
#  Copyright (c) 2003, 2004, 2005 MandrakeSoft SA
#  Copyright (c) 2005-2007 Mandriva SA
#  Copyright (c) 2012-2015 Matteo Pasotti <matteo.pasotti@gmail.com>
#  Copyright (c) 2014-2015 Angelo Naselli <anaselli@linux.it>
#
#  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.
#
#*****************************************************************************
#
# $Id: open_db.pm 268344 2010-05-06 13:06:08Z jvictor $

use strict;
use Sys::Syslog;

use MDK::Common::File qw(cat_ mkdir_p);
use MDK::Common::Func;
use ManaTools::rpmdragora;
use URPM;
use urpm;
use urpm::args;
use urpm::select;
use urpm::media;
use urpm::mirrors;
use feature 'state';

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(fast_open_urpmi_db
                 get_backport_media
                 get_inactive_backport_media
                 get_update_medias
                 is_it_a_devel_distro
                 open_rpm_db
                 open_urpmi_db
            );

my $loc = ManaTools::rpmdragora::locale();


# because rpm blocks some signals when rpm DB is opened, we don't keep open around:
sub open_rpm_db {
    my ($o_force) = @_;
    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 = URPM::DB::open($::rpmdragora_options{'rpm-root'}[0]||'');
        }
        $db or die "Couldn't open RPM DB (" . ($::env ? "$::env/rpmdb.cz" : $::rpmdragora_options{'rpm-root'}[0]) . ")";
    }
}

# do not pay the urpm::media::configure() heavy cost:
sub fast_open_urpmi_db() {
    my $urpm = urpm->new;
    my $error_happened;
    $urpm->{fatal} = sub {
        $error_happened = 1;
        interactive_msg($loc->N("Fatal error"),
                         $loc->N("A fatal error occurred: %s.", $_[1]));
    };

    urpm::set_files($urpm, $::rpmdragora_options{'urpmi-root'}[0]) if $::rpmdragora_options{'urpmi-root'}[0];
    $::rpmdragora_options{'rpm-root'}[0] ||= $::rpmdragora_options{'urpmi-root'}[0];
    urpm::args::set_root($urpm, $::rpmdragora_options{'rpm-root'}[0]) if $::rpmdragora_options{'rpm-root'}[0];
    urpm::args::set_debug($urpm) if $::rpmdragora_options{debug};
    $urpm->get_global_options;
    $urpm->{options}{wait_lock} = $::rpmdragora_options{'wait-lock'};
    $urpm->{options}{'verify-rpm'} = !$::rpmdragora_options{'no-verify-rpm'} if defined $::rpmdragora_options{'no-verify-rpm'};
    $urpm->{options}{auto} = $::rpmdragora_options{auto} if defined $::rpmdragora_options{auto};
    urpm::args::set_verbosity();
    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);
    }

    $urpm::args::options{justdb} = $::rpmdragora_options{justdb};

    urpm::media::read_config($urpm, 0);
    foreach (@{$urpm->{media}}) {
	    next if $_->{ignore};
	    urpm::media::_tempignore($_, 1) if $ignore_debug_media->[0] && $_->{name} =~ /debug/i;
    }
    # FIXME: seems uneeded with newer urpmi:
    if ($error_happened) {
        touch('/etc/urpmi/urpmi.cfg');
        exec('edit-urpm-sources.pl');
    }
    $urpm;
}

sub is_it_a_devel_distro() {
    state $res;
    return $res if defined $res;

    my $path = '/etc/product.id';
    $path = $::rpmdragora_options{'urpmi-root'}[0] . $path if defined($::rpmdragora_options{'urpmi-root'}[0]);
    $res = urpm::mirrors::parse_LDAP_namespace_structure(cat_($path))->{branch} eq 'Devel';
    return $res;
}

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

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

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

sub open_urpmi_db {
    my (%urpmi_options) = @_;
    my $urpm = fast_open_urpmi_db();
    my $media = ref $::rpmdragora_options{media} ? join(',', @{$::rpmdragora_options{media}}) : '';

    my $searchmedia = $urpmi_options{update} ? undef : join(',', get_inactive_backport_media($urpm));
    $urpm->{lock} = urpm::lock::urpmi_db($urpm, undef, wait => $urpm->{options}{wait_lock}) 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);
    $urpm;
}

1;