Debian Codegraph @

lib/App/pmuninstall.pm

raw symbols SCIP
1 package App::pmuninstall;
2 use strict;
3 use warnings;
4 use File::Spec;
5 use File::Basename qw(dirname);
6 use ExtUtils::Packlist;
7 use Getopt::Long qw(GetOptions :config bundling);
8 use Config;
9 use YAML ();
10 use CPAN::DistnameInfo;
11 use version;
12 use HTTP::Tiny;
13 use Term::ANSIColor qw(colored);
14 use Cwd ();
15 use JSON::PP qw(decode_json);
16
17 our $VERSION = "0.33";
18
19 my $perl_version = version->new($])->numify;
20 my $depended_on_by = 'http://deps.cpantesters.org/depended-on-by.pl?dist=';
21 my $cpanmetadb = 'http://cpanmetadb.plackperl.org/v1.0/package';
22 my @core_modules_dir = do { my %h; grep !$h{$_}++, @Config{qw/archlib archlibexp privlib privlibexp/} };
23
24 $ENV{ANSI_COLORS_DISABLED} = 1 if $^O eq 'MSWin32';
25
26 our $OUTPUT_INDENT_LEVEL = 0;
27
28 sub new {
29 my ($class, $inc) = @_;
30 $inc = [@INC] unless ref $inc eq 'ARRAY';
31 bless {
32 check_deps => 1,
33 verbose => 0,
34 inc => $class->prepare_include_paths($inc),
35 }, $class;
36 }
37
38 sub run {
39 my ($self, @args) = @_;
40 local @ARGV = @args;
41 GetOptions(
42 'f|force' => \$self->{force},
43 'v|verbose!' => sub { ++$self->{verbose} },
44 'c|checkdeps!' => \$self->{check_deps},
45 'n|no-checkdeps!' => sub { $self->{check_deps} = 0 },
46 'q|quiet!' => \$self->{quiet},
47 'h|help!' => sub { $self->usage },
48 'V|version!' => \$self->{version},
49 'l|local-lib=s' => \$self->{local_lib},
50 'L|local-lib-contained=s' => sub {
51 $self->{local_lib} = $_[1];
52 $self->{self_contained} = 1;
53 },
54 ) or $self->usage;
55
56 if ($self->{version}) {
57 $self->puts("pm-uninstall (App::pmuninstall) version $App::pmuninstall::VERSION");
58 exit;
59 }
60
61 $self->short_usage unless @ARGV;
62
63 $self->uninstall(@ARGV);
64 }
65
66 sub uninstall {
67 my ($self, @modules) = @_;
68
69 $self->setup_local_lib;
70
71 my $uninstalled = 0;
72 for my $module (@modules) {
73 $self->puts("--> Working on $module") unless $self->{quiet};
74 my ($packlist, $dist, $vname) = $self->find_packlist($module);
75
76 $packlist = File::Spec->canonpath($packlist);
77 if ($self->is_core_module($module, $packlist)) {
78 $self->puts(colored ['red'], "! $module is a core module!! Can't be uninstalled.");
79 $self->puts unless $self->{quiet};
80 next;
81 }
82
83 unless ($dist) {
84 $self->puts(colored ['red'], "! $module not found.");
85 $self->puts unless $self->{quiet};
86 next;
87 }
88
89 unless ($packlist) {
90 $self->puts(colored ['red'], "! $module is not installed.");
91 $self->puts unless $self->{quiet};
92 next;
93 }
94
95 if ($self->ask_permission($module, $dist, $vname, $packlist)) {
96 if ($self->uninstall_from_packlist($packlist)) {
97 $self->puts(colored ['green'], "Successfully uninstalled $module");
98 ++$uninstalled;
99 }
100 else {
101 $self->puts(colored ['red'], "! Failed to uninstall $module");
102 }
103 $self->puts unless $self->{quiet};
104 }
105 }
106
107 if ($uninstalled) {
108 $self->puts if $self->{quiet};
109 $self->puts("You may want to rebuild man(1) entries. Try `mandb -c` if needed");
110 }
111
112 return $uninstalled;
113 }
114
115 sub uninstall_from_packlist {
116 my ($self, $packlist) = @_;
117
118 my $inc = {
119 map { File::Spec->canonpath($_) => 1 } @{$self->{inc}}
120 };
121
122 my $failed;
123 for my $file ($self->fixup_packlist($packlist)) {
124 chomp $file;
125 $self->puts(-f $file ? 'unlink ' : 'not found', " : $file") if $self->{verbose};
126 unlink $file or $self->puts("$file: $!") and $failed++;
127 $self->rm_empty_dir_from_file($file, $inc);
128 }
129 $self->puts("unlink : $packlist") if $self->{verbose};
130 unlink $packlist or $self->puts("$packlist: $!") and $failed++;
131 $self->rm_empty_dir_from_file($packlist, $inc);
132
133 if (my $install_json = $self->{install_json}) {
134 $self->puts("unlink : $install_json") if $self->{verbose};
135 unlink $install_json or $self->puts("$install_json: $!") and $failed++;
136 $self->rm_empty_dir_from_file($install_json);
137 }
138
139 $self->puts unless $self->{quiet} || $self->{force};
140 return !$failed;
141 }
142
143 sub rm_empty_dir_from_file {
144 my ($self, $file, $inc) = @_;
145 my $dir = dirname $file;
146 return unless -d $dir;
147 return if $inc->{+File::Spec->canonpath($dir)};
148
149 my $failed;
150 if ($self->is_empty_dir($dir)) {
151 $self->puts("rmdir : $dir") if $self->{verbose};
152 rmdir $dir or $self->puts("$dir: $!") and $failed++;
153 $self->rm_empty_dir_from_file($dir, $inc);
154 }
155
156 return !$failed;
157 }
158
159 sub is_empty_dir {
160 my ($self, $dir) = @_;
161 opendir my $dh, $dir or die "$dir: $!";
162 my @dir = grep !/^\.{1,2}$/, readdir $dh;
163 closedir $dh;
164 return @dir ? 0 : 1;
165 }
166
167 sub find_packlist {
168 my ($self, $module) = @_;
169 $self->puts("Finding $module in your \@INC") if $self->{verbose};
170
171 # find with the given name first
172 (my $try_dist = $module) =~ s!::!-!g;
173 if (my $pl = $self->locate_pack($try_dist)) {
174 $self->puts("-> Found $pl") if $self->{verbose};
175 return ($pl, $try_dist);
176 }
177
178 $self->puts("Looking up $module on cpanmetadb") if $self->{verbose};
179
180 # map module -> dist and retry
181 my $yaml = $self->fetch("$cpanmetadb/$module") or return;
182 my $meta = YAML::Load($yaml);
183 my $info = CPAN::DistnameInfo->new($meta->{distfile});
184
185 my $name = $self->find_meta($info->distvname) || $info->dist;
186 if (my $pl = $self->locate_pack($name)) {
187 $self->puts("-> Found $pl") if $self->{verbose};
188 return ($pl, $info->dist, $info->distvname);
189 }
190 }
191
192 sub find_meta {
193 my ($self, $distvname) = @_;
194
195 my $name;
196 for my $lib (@{$self->{inc}}) {
197 next unless $lib =~ /$Config{archname}/;
198 my $install_json = "$lib/.meta/$distvname/install.json";
199 next unless -f $install_json && -r _;
200 my $data = decode_json +$self->slurp($install_json);
201 $name = $data->{name} || next;
202 $self->puts("-> Found $install_json") if $self->{verbose};
203 $self->{meta} = $install_json;
204 last;
205 }
206 return $name;
207 }
208
209 sub locate_pack {
210 my ($self, $dist) = @_;
211 $dist =~ s!-!/!g;
212 for my $lib (@{$self->{inc}}) {
213 my $packlist = "$lib/auto/$dist/.packlist";
214 $self->puts("-> Finding .packlist $packlist") if $self->{verbose} > 1;
215 return $packlist if -f $packlist && -r _;
216 }
217 return;
218 }
219
220 sub is_core_module {
221 my ($self, $dist, $packlist) = @_;
222 require Module::CoreList;
223 return unless exists $Module::CoreList::version{$perl_version}{$dist};
224 return 1 unless $packlist;
225
226 my $is_core = 0;
227 for my $dir (@core_modules_dir) {
228 my $safe_dir = quotemeta $dir; # workaround for MSWin32
229 if ($packlist =~ /^$safe_dir/) {
230 $is_core = 1;
231 last;
232 }
233 }
234 return $is_core;
235 }
236
237 sub ask_permission {
238 my($self, $module, $dist, $vname, $packlist) = @_;
239
240 my @deps = $self->find_deps($vname, $module);
241
242 $self->puts if $self->{verbose};
243 $self->puts("$module is included in the distribution $dist and contains:\n")
244 unless $self->{quiet};
245 for my $file ($self->fixup_packlist($packlist)) {
246 chomp $file;
247 $self->puts(" $file") unless $self->{quiet};
248 }
249 $self->puts unless $self->{quiet};
250
251 return 'force uninstall' if $self->{force};
252
253 my $default = 'y';
254 if (@deps) {
255 $self->puts("Also, they're depended on by the following installed dists:\n");
256 for my $dep (@deps) {
257 $self->puts(" $dep");
258 }
259 $self->puts;
260 $default = 'n';
261 }
262
263 return lc($self->prompt("Are you sure you want to uninstall $dist?", $default)) eq 'y';
264 }
265
266 sub find_deps {
267 my ($self, $vname, $module) = @_;
268
269 return unless $self->{check_deps} && !$self->{force};
270 $vname ||= $self->vname_for($module) or return;
271
272 $self->puts("Checking modules depending on $vname") if $self->{verbose};
273 my $content = $self->fetch("$depended_on_by$vname") or return;
274
275 my (@deps, %seen);
276 for my $dep ($content =~ m|<li><a href=[^>]+>([a-zA-Z0-9_:-]+)|smg) {
277 $dep =~ s/^\s+|\s+$//smg; # trim
278 next if $seen{$dep}++;
279 local $OUTPUT_INDENT_LEVEL = $OUTPUT_INDENT_LEVEL + 1;
280 $self->puts("Finding $dep in your \@INC (dependencies)") if $self->{verbose};
281 push @deps, $dep if $self->locate_pack($dep);
282 }
283
284 return @deps;
285 }
286
287 sub prompt {
288 my ($self, $msg, $default) = @_;
289 require ExtUtils::MakeMaker;
290 ExtUtils::MakeMaker::prompt($msg, $default);
291 }
292
293 sub fixup_packlist {
294 my ($self, $packlist) = @_;
295 my @target_list;
296 my $is_local_lib = $self->is_local_lib($packlist);
297 my $plist = ExtUtils::Packlist->new($packlist);
298 while (my $file = each %$plist) {
299 if ($is_local_lib) {
300 next unless $self->is_local_lib($file);
301 }
302 push @target_list, $file;
303 }
304 return @target_list;
305 }
306
307 # NOTE only use this for comparing paths
308 sub _canon_path_compare {
309 my ($self, $path) = @_;
310 $path = Cwd::realpath($path);
311 if( $^O eq 'MSWin32' ) {
312 require Win32;
313 $path = Win32::GetLongPathName($path);
314 }
315
316 return $path;
317 }
318
319 sub is_local_lib {
320 my ($self, $file) = @_;
321 return unless $self->{local_lib};
322
323 my $local_lib_base = quotemeta $self->_canon_path_compare($self->{local_lib});
324 $file = $self->_canon_path_compare($file);
325
326 return $file =~ /^$local_lib_base(?:\/|\z)/ ? 1 : 0;
327 }
328
329 sub vname_for {
330 my ($self, $module) = @_;
331
332 $self->puts("Fetching $module vname on cpanmetadb") if $self->{verbose};
333 my $yaml = $self->fetch("$cpanmetadb/$module") or return;
334 my $meta = YAML::Load($yaml);
335 my $info = CPAN::DistnameInfo->new($meta->{distfile}) or return;
336
337 return $info->distvname;
338 }
339
340 # taken from cpan-outdated
341 sub setup_local_lib {
342 my $self = shift;
343 return unless $self->{local_lib};
344
345 unless (-d $self->{local_lib}) {
346 $self->puts(colored ['red'], "! $self->{local_lib} : no such directory");
347 exit 1;
348 }
349
350 local $SIG{__WARN__} = sub { }; # catch 'Attempting to write ...'
351 $self->{inc} = [
352 grep { defined }
353 map { Cwd::realpath($_) }
354 @{$self->build_active_perl5lib($self->{local_lib}, $self->{self_contained})}
355 ];
356 push @{$self->{inc}}, @INC unless $self->{self_contained};
357 }
358
359 sub build_active_perl5lib {
360 my ($self, $path, $interpolate) = @_;
361 my $perl5libs = [
362 $self->install_base_arch_path($path),
363 $self->install_base_perl_path($path),
364 $interpolate && $ENV{PERL5LIB} ? split(/\Q$Config{path_sep}\E/, $ENV{PERL5LIB}) : (),
365 ];
366 return $perl5libs;
367 }
368
369 sub install_base_perl_path {
370 my ($self, $path) = @_;
371 File::Spec->catdir($path, 'lib', 'perl5');
372 }
373
374 sub install_base_arch_path {
375 my ($self, $path) = @_;
376 File::Spec->catdir($self->install_base_perl_path($path), $Config{archname});
377 }
378
379 sub fetch {
380 my ($self, $url) = @_;
381 $self->puts("-> Fetching from $url") if $self->{verbose};
382 my $res = HTTP::Tiny->new->get($url);
383 return if $res->{status} == 404;
384 die "[$res->{status}] fetch $url failed!!\n" if !$res->{success};
385 return $res->{content};
386 }
387
388 sub slurp {
389 my ($self, $file) = @_;
390 open my $fh, '<', $file or die "$file $!";
391 do { local $/; <$fh> };
392 }
393
394 sub puts {
395 my ($self, @msg) = @_;
396 push @msg, '' unless @msg;
397 print ' ' x $OUTPUT_INDENT_LEVEL if $OUTPUT_INDENT_LEVEL;
398 print @msg, "\n";
399 }
400
401 sub usage {
402 my $self = shift;
403 $self->puts(<< 'USAGE');
404 Usage:
405 pm-uninstall [options] Module [...]
406
407 options:
408 -v,--verbose Turns on chatty output
409 -f,--force Uninstalls without prompts
410 -c,--checkdeps Check dependencies (defaults to on)
411 -n,--no-checkdeps Don't check dependencies
412 -q,--quiet Suppress some messages
413 -h,--help This help message
414 -V,--version Show version
415 -l,--local-lib Additional module path
416 -L,--local-lib-contained Additional module path (don't include non-core modules)
417 USAGE
418
419 exit 1;
420 }
421
422 sub short_usage {
423 my $self = shift;
424 $self->puts(<< 'USAGE');
425 Usage: pm-uninstall [options] Module [...]
426
427 Try `pm-uninstall --help` or `man pm-uninstall` for more options.
428 USAGE
429
430 exit 1;
431 }
432
433 sub prepare_include_paths {
434 my ($class, $inc) = @_;
435 my $new_inc = [];
436 my $archname = quotemeta $Config{archname};
437 for my $path (@$inc) {
438 push @$new_inc, $path;
439 next if $path eq '.' or $path =~ /$archname/;
440 push @$new_inc, File::Spec->catdir($path, $Config{archname});
441 }
442 return [do { my %h; grep !$h{$_}++, @$new_inc }];
443 }
444
445 1;
446 __END__
447
448 =head1 NAME
449
450 App::pmuninstall - Uninstall modules
451
452 =head1 DESCRIPTION
453
454 App::pmuninstall is a fast module uninstaller.
455 delete files from B<.packlist>.
456
457 L<App::cpanminus> and, L<App::cpanoutdated> with a high affinity.
458
459 =head1 SYNOPSIS
460
461 uninstall MODULE
462
463 $ pm-uninstall App::pmuninstall
464
465 =head1 OPTIONS
466
467 =over
468
469 =item -f, --force
470
471 Uninstalls without prompts
472
473 $ pm-uninstall -f App::pmuninstall
474
475 =item -v, --verbose
476
477 Turns on chatty output
478
479 $ pm-uninstall -v App::cpnaminus
480
481 =item -c, --checkdeps
482
483 Check dependencies ( default on )
484
485 $ pm-uninstall -c Plack
486
487 =item -n, --no-checkdeps
488
489 Don't check dependencies
490
491 $ pm-uninstall -n LWP
492
493 =item -q, --quiet
494
495 Suppress some messages
496
497 $ pm-uninstall -q Furl
498
499 =item -h, --help
500
501 Show help message
502
503 $ pm-uninstall -h
504
505 =item -V, --version
506
507 Show version
508
509 $ pm-uninstall -V
510
511 =item -l, --local-lib
512
513 Additional module path
514
515 $ pm-uninstall -l extlib App::pmuninstall
516
517 =item -L, --local-lib-contained
518
519 Additional module path (don't include non-core modules)
520
521 $ pm-uninstall -L extlib App::pmuninstall
522
523 =back
524
525 =head1 AUTHOR
526
527 Yuji Shimada
528
529 Tatsuhiko Miyagawa
530
531 =head1 SEE ALSO
532
533 L<pm-uninstall>
534
535 =head1 LICENSE
536
537 This library is free software; you can redistribute it and/or modify
538 it under the same terms as Perl itself.
539
540 =cut
541