aboutsummaryrefslogtreecommitdiffstats
path: root/modules/rpmdragora/dragora-urpm-addmedia
blob: b49840f99fc40c43d8b9e1255e8c4534ffe65e13 (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
#!/usr/bin/perl
#*****************************************************************************
#
#  Copyright (c) 2004 Guillaume Cottenceau (gc at mandrakesoft dot com)
#  Copyright (c) 2006-2007 Thierry Vignaud <tvignaud@mandriva.com>
#  Copyright (c) 2004-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.
#
#*****************************************************************************
#
# $Id: gurpmi.addmedia 261187 2009-10-01 14:44:34Z tv $

use strict;

BEGIN {
    @ARGV == 0 || "@ARGV" =~ /-h/ and do {
        print "usage: gurpmi.addmedia [options] <name> <url> [with <relative_path>]
where <url> is one of
       [file:/]/<path>
       ftp://<login>:<password>\@<host>/<path>
       ftp://<host>/<path>
       http://<host>/<path>
       removable://<path>

and [options] are from
  --distrib        install a set of media from a distribution
  --silent-success don't show popup window on success
  --update         create an update medium,
                   or discard non-update media (when used with --distrib)
  --mirrorlist     <url> is a mirror list
";
        exit(0);
    };
}
use AdminPanel::Rpmdragora::init;
use AdminPanel::rpmdragora;
use AdminPanel::Rpmdragora::open_db;
use AdminPanel::Rpmdragora::formatting;
use AdminPanel::Shared::Locales;
use AdminPanel::Privileges;

use lib qw(/usr/lib/libDrakX);
use urpm::media;
use MDK::Common::Func;

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


#
if (AdminPanel::Privileges::is_root_capability_required()) {
    require AdminPanel::Shared::GUI;
    my $sh_gui = AdminPanel::Shared::GUI->new();
    $sh_gui->warningMsgBox({
        title => "gurpmi.addmedia",
        text  => $loc->N("root privileges required"),
    });
    exit (-1);
}

use AdminPanel::rpmdragora;
use Getopt::Long;

## TODO remove as soon as possible
$::isStandalone = 1;

AdminPanel::rpmdragora::readconf();

my $fromfile;
if (@ARGV == 1 && $ARGV[0] =~ /\.urpmi-media$/) {
    @ARGV = map { s/^\s*//; s/\s*$//; $_ } split /\n/, cat_($ARGV[0]);
    $fromfile = 1;
}

my ($update, $distrib, $silent_success) = (0, 0, 0);
my ($mirrorlist, $with_dir);

GetOptions(
    'update'	     => \$update,
    'distrib'       => \$distrib,
    'silent-success' => \$silent_success,
    'mirrorlist=s'     => \$mirrorlist,
    'urpmi-root=s'      => \$::rpmdragora_options{'urpmi-root'}[0],
);

# compatibility with -update:
if ($ARGV[0] =~ /^-?-update/) {
    $update = 1;
    shift @ARGV;
}

my @addmedia_args;
my @names;

while (@ARGV) {
    my ($name, $url) = @ARGV;

    if ($mirrorlist) {
	($with_dir, $url) = ($url, undef);
    } elsif ($distrib) {
	($name, $url) = ('', $name);
    } elsif ($url !~ m,^(([^:]*):/)?/,) {
	AdminPanel::rpmdragora::interactive_msg('gurpmi.addmedia',
			AdminPanel::Rpmdragora::formatting::escape_text_for_TextView_markup_format(
                            $loc->N("bad <url> (for local directory, the path must be absolute)") . "\n\n$url"));
	AdminPanel::rpmdragora::myexit(-1);
    }
    push @addmedia_args, [ $name, $url ];
    push @names, $name;
    shift @ARGV foreach 1 .. 2;
}

if ($mirrorlist && $distrib && !@addmedia_args) {
    @addmedia_args = [ '' ];
}

$fromfile and do {
    AdminPanel::rpmdragora::interactive_msg('gurpmi.addmedia',
$loc->N("%s

Is it ok to continue?",
$distrib || !@names
? $loc->N("You are about to add new package media.
That means you will be able to add new software packages
to your system from these new media.")
: 1 < @names
? $loc->N("You are about to add new package medium, %s.
That means you will be able to add new software packages
to your system from these new media.", join ", ", @names)
: $loc->N("You are about to add a new package medium, `%s'.
That means you will be able to add new software packages
to your system from that new medium.", $names[0])
),
    yesno => 1) or AdminPanel::rpmdragora::myexit(-1);
};

my $urpm = AdminPanel::Rpmdragora::open_db::fast_open_urpmi_db();
my $success = 1;
foreach (@addmedia_args) {
    #- NB: that short circuits
    $success = $success && AdminPanel::rpmdragora::add_medium_and_check(
	$urpm,
	{ distrib => $distrib },
	$_->[0],
	$_->[1],
	MDK::Common::Func::if_(!$distrib, undef),
	MDK::Common::Func::if_($update, update => $update, only_updates => $update),
	mirrorlist => $mirrorlist,
	MDK::Common::Func::if_($with_dir, 'with-dir' => $with_dir),
    );
}
if ($success) {
    my $message =
      $distrib ? $loc->N("Successfully added media.")
        : @names > 1
	    ? $loc->N("Successfully added media %s.", join ", ", @names)
	    : $loc->N("Successfully added medium `%s'.", $names[0]);
    AdminPanel::rpmdragora::interactive_msg('gurpmi.addmedia', $message) if !$silent_success;
    AdminPanel::rpmdragora::myexit(0);
} else {
    AdminPanel::rpmdragora::myexit(-1);
}