-
Notifications
You must be signed in to change notification settings - Fork 4
/
setupdistarch
executable file
·281 lines (202 loc) · 7.25 KB
/
setupdistarch
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
#!/usr/bin/perl
use warnings;
use strict;
use English qw(no_match_vars);
use CPANPLUS::Internals::Constants qw(CONFIG_USER);
use File::Basename qw[dirname];
use CPANPLUS::Backend;
use Getopt::Long;
use Pod::Usage;
use IPC::Cmd qw(can_run);
use Carp qw(croak);
## CONSTANTS
##############################################################################
my $ROOT_UID = 0;
my $ROOT_WARNING = <<'END_WARNING';
*** Warning: Running this script as root (without sudo) will set the
*** system-wide default for ALL users.
***
*** DO NOT run this script with sudo or your config file
*** will become owned by root.
END_WARNING
my $SUDO_WARNING = <<'END_WARNING';
*** Warning: You must have sudo or some similar program in order to
*** install packages as a non-root user.
***
*** This is preferable to building packages as root which might
*** be dangerous.
END_WARNING
## GLOBALS
##############################################################################
my ($DO_REMOVAL, $DO_FORCE);
# Params: $message - Yes or no question to ask user.
# $default - Whether 'yes' or 'no' is the default.
sub prompt_yn
{
die 'Invalid arguments to prompt_yn' unless ( @_ == 2 );
my ($message, $default) = @_;
my $first = lc substr $default, 0, 1;
$default = ( $first eq 'y' ? 1 : $first eq 'n' ? 0 : 1 );
chomp $message;
$message .= q{ } . ( $default ? '[Yn]' : '[yN]' ) . q{ };
my $answer;
QUESTION: {
local $OUTPUT_AUTOFLUSH = 1;
print $message;
$answer = <STDIN>;
chomp $answer;
return $default if ( length $answer == 0 );
redo QUESTION unless $answer =~ /\A[yYnN]/;
}
return 0 if $answer =~ /\A[nN]/;
return 1;
}
my @Config_fixups;
sub add_cfg_fixup
{
croak 'Params for add_cfg_fixup must be a hash' unless @_ % 2 == 0;
my %fixup = @_;
croak q{You must specify 'checker', 'prompt', 'fixer', and 'success'
in the hash parameters to add_cfg_fixup} unless 4 == grep { defined }
my ($checker_ref, $prompt, $fixer_ref, $success) =
@fixup{ qw/checker prompt fixer success/ };
my $prompter_ref = sub { prompt_yn( $prompt => 'Y' ) };
chomp $success;
push @Config_fixups, [ $checker_ref, $prompter_ref, $fixer_ref, $success ];
return;
}
sub run_cfg_fixups
{
my $cfg_obj = CPANPLUS::Backend->new->configure_object;
my $fixedup = 0;
FIXUP_LOOP:
for my $fixup ( @Config_fixups ) {
my ($checker_ref, $prompter_ref, $fixer_ref, $success) = @$fixup;
next FIXUP_LOOP unless ( $checker_ref->( $cfg_obj ) );
next FIXUP_LOOP unless ( $DO_FORCE || $prompter_ref->() );
$fixer_ref->( $cfg_obj );
print $success, "\n" if ( $success );
++$fixedup;
}
$cfg_obj->save;
return $fixedup;
}
{
my ($show_help, $show_man);
GetOptions( help => \$show_help,
man => \$show_man,
remove => \$DO_REMOVAL,
force => \$DO_FORCE );
pod2usage( verbose => 2 ) if $show_man;
pod2usage( verbose => 1 ) if $show_help;
if ( $DO_REMOVAL ) {
my $prompt = <<'END_PROMPT';
Are you sure you want to stop packaging CPAN installs?
END_PROMPT
add_cfg_fixup
( checker => sub { 1 },
fixer => sub { shift->set_conf( 'dist_type' => q{} ) },
prompt => $prompt,
success => 'CPANPLUS will no longer auto-package modules.',
);
}
else {
add_cfg_fixup
( checker => sub {
shift->get_conf( 'dist_type' ) ne 'CPANPLUS::Dist::Arch';
},
fixer => sub {
shift->set_conf( 'dist_type' => 'CPANPLUS::Dist::Arch' );
},
prompt => 'Are you sure you want to auto-package CPAN installs?',
success => 'CPANPLUS will now auto-package modules.',
);
my $prompt = <<'END_PROMPT';
Would you like to automatically install module pre-requisites?
END_PROMPT
my $success = <<'END_SUCCESS';
CPANPLUS will install pre-requisite modules without asking.
END_SUCCESS
add_cfg_fixup
( checker => sub { shift->get_conf( 'prereqs' ) != 1 },
fixer => sub { shift->set_conf( 'prereqs' => 1 ) },
prompt => $prompt,
success => $success,
);
}
if ( $UID == $ROOT_UID ) {
print $ROOT_WARNING;
}
unless ( can_run( 'sudo' )) {
print $SUDO_WARNING;
}
my $count = run_cfg_fixups();
if ( $count == 0 ) {
print "CPANPLUS is already setup correctly.\n";
}
exit 0;
}
__END__
=head1 NAME
setupdistarch - Script to set CPANPLUS::Dist::Arch as the default packager for CPANPLUS
=head1 SYNOPSIS
Run this script from your command shell to set CPANPLUS to package all
modules through CPANPLUS::Dist::Arch by default or to disable
automatic packaging in CPANPLUS. setupdistarch also turns on recursive
package installation and warns if sudo is not installed.
$ ./setupdistarch
Are you sure you want to auto-package CPAN installs? [Yn]
CPANPLUS will now auto-package modules.
$ ./setupdistarch --force
CPANPLUS will now auto-package modules.
$ setupdistarch --remove --force
CPANPLUS will no longer auto-package modules.
$ setupdistarch -h
(Displays this usage information.)
DO NOT run this script with sudo or your configuration file will now
be owned by root.
=head1 OPTIONS
=over
=item B<-h, --help>
Print a brief help message and exit.
=item B<-m, --man>
Prints the manual page and exists.
=item B<-r, --remove>
Configures CPANPLUS to not use any packager when installing modules.
=item B<-f, --force>
Forces the changes to CPANPLUS configuration without prompting for
confirmation. Might be useful when running as root uninteractively,
for example.
=back
=head1 DESCRIPTION
This script was created to make setting up or disabling
L<CPANPLUS::Dist::Arch> a little easier. Without using the force
option, the user must confirm they want to change CPANPLUS's
configuration.
If you run this script as root, it will change the B<SYSTEM-WIDE>
default for all CPANPLUS users. A warning message is displayed
when run as root.
=head1 TROUBLESHOOTING
=over
=item B<sudo>
DO NOT run this script with sudo or your CPANPLUS configuration file
will become owned by root. To change it back, use sudo and chown
on your configuration file. For example:
sudo chown <yourname>.<yourgroup> ~/.cpanplus/lib/CPANPLUS/Config/User.pm
=item B<PATH>
I<This problem has been fixed in recent versions of Archlinux.>
Archlinux currently installs binaries from perl modules (like this one
here) under the C</usr/bin/perlbin/vendor> directory. Make sure this
is in your PATH environment variable if you are not typing the entire
path.
To the perl binary directories to your runtime PATH add something like
the following to your /home/(username)/.profile file:
export PATH=/bin:/usr/bin:/usr/bin/perlbin/core:/usr/bin/perlbin/vendor
=back
=head1 AUTHOR
Justin Davis C<< <juster at cpan dot org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2010 Justin Davis, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut