-
Notifications
You must be signed in to change notification settings - Fork 5
/
pm-uninstall
executable file
·158 lines (121 loc) · 3.65 KB
/
pm-uninstall
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
#!perl
use strict;
use warnings;
use File::Spec;
use Getopt::Long qw/GetOptions/;
use Pod::Usage qw/pod2usage/;
use Config;
use ExtUtils::MakeMaker;
use LWP::Simple;
use YAML;
use CPAN::DistnameInfo;
my $base_url = 'http://deps.cpantesters.org/depended-on-by.pl?dist=';
my $cpanmetadb = 'http://cpanmetadb.appspot.com/v1.0/package';
our $VERSION = "0.03";
my $opt = +{};
GetOptions(
'f|force' => \$opt->{force},
'v|verbose!' => \$opt->{verbose},
'c|checkdeps' => \$opt->{check_deps},
'h|help!' => \$opt->{help},
);
pod2usage 1 if $opt->{help} or !@ARGV;
main(@ARGV);
exit;
sub main {
my @modules = @_;
for my $module (@modules) {
my($packlist, $dist, $vname) = find_packlist($module);
unless ($packlist) {
warn "Module $module not installed\n";
next;
}
if ($opt->{force} or ask_permission($module, $dist, $vname, $packlist)) {
uninstall_from_packlist($packlist);
warn "Module $module successfully uninstalled.\n";
}
}
}
sub vname_for {
my $module = shift;
my $yaml = get("$cpanmetadb/$module") or return;
my $meta = YAML::Load($yaml);
my $info = CPAN::DistnameInfo->new($meta->{distfile}) or return;
return $info->distvname;
}
sub ask_permission {
my($module, $dist, $vname, $packlist) = @_;
my(@deps, %seen);
if ($opt->{check_deps}) {
$vname = vname_for($module);
warn "Checking modules depending on $vname\n" if $opt->{verbose};
my $content = get("$base_url$vname");
for my $dep ($content =~ m|<th align=left>([^<]+)</th>|smg) {
$dep =~ s/\-[^\-]+$//; # version
next if $seen{$dep}++;
push @deps, $dep if locate_pack($dep);
}
}
warn "$module is included in the distribution $dist and contains:\n\n";
open my $in, "<", $packlist or die "$packlist: $!";
while (<$in>) {
warn " $_";
}
warn "\n";
my $default = 'y';
if (@deps) {
warn "Also, they're depended on by the following dists you have:\n\n";
for my $dep (@deps) {
warn " $dep\n";
}
warn "\n";
$default = 'n';
}
lc(prompt("Are you sure to uninstall $dist?", $default)) eq 'y';
}
sub find_packlist {
my $module = shift;
warn "Finding $module in your \@INC\n" if $opt->{verbose};
# find with the given name first
(my $try_dist = $module) =~ s!::!-!g;
my $pl = locate_pack($try_dist);
return ($pl, $try_dist) if $pl;
warn "Looking up $module on cpanmetadb\n" if $opt->{verbose};
# map module -> dist and retry
my $yaml = get("$cpanmetadb/$module") or return;
my $meta = YAML::Load($yaml);
my $info = CPAN::DistnameInfo->new($meta->{distfile});
my $pl2 = locate_pack($info->dist);
return ($pl2, $info->dist, $info->distvname) if $pl2;
return;
}
sub locate_pack {
my $dist = shift;
$dist =~ s!-!/!g;
for my $lib (@INC) {
my $packlist = "$lib/auto/$dist/.packlist";
return $packlist if -f $packlist && -r _;
}
return;
}
sub uninstall_from_packlist {
my $packlist = shift;
open my $fh, '<', $packlist or die "$packlist: $!";
while (<$fh>) {
chomp;
print -f $_ ? 'unlink ' : 'not found', " : $_\n" if $opt->{verbose};
unlink $_ or warn "$_: $!\n";
}
close $fh;
unlink $packlist;
}
__END__
=head1 NAME
pm-uninstall - Uninstall modules
=head1 SYNOPSIS
pm-uninstall [options] Module ...
options:
-v,--verbose Turns on chatty output
-f,--force Uninstalls without prompts
-c,--checkdeps Check dependencies
-h,--help This help message