File: distkill.pl

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (210 lines) | stat: -rwxr-xr-x 4,923 bytes parent folder | download | duplicates (16)
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
#!/usr/bin/perl -w
use strict;
use Socket;
use MIME::Base64;
use Sys::Hostname;

my %cfg;
my $retcode=0;

rescancache:

for my $file (</etc/apt-cacher-ng/*.conf>)
{
   my $fd;
   open($fd, $file) or next;
   while(<$fd>)
   {
      chomp;
      s/^\s+//;
      s/\s+$//;
      next if /^#/;
			my ($key, $value) = split(/\s*=\s*/);
      if(!defined($value) && /(\w+)\s*:\s*(\S+)/)
      {
         $key=$1;
         $value=$2;
      }
      next unless defined($value);
      #print "$key -> $value\n";
      $cfg{lc($key)}=$value;
   }
}

use constant
{
   DO_PROMPT => 0,
   DO_DETAILS => 1,
   DO_REMOVE => 2
};

my $debug=0;

my @killcmd=("rm", "-rf");

@killcmd=("echo", "Killing: ") if $debug;

die "Cannot find CacheDir in the configuration, aborted.\n" if(!$cfg{cachedir});

syswrite(STDOUT, "Scanning $cfg{cachedir}, please wait...\n");
my %distdirs=();
my %archfiles=();

foreach(`find $cfg{cachedir}/. | grep /dists/`)
{
   chomp;
   $_ =~ s!/./!/!g ;
   next if /\/_import/;
   if(/(.*\/dists\/([^\/]+))\//)
   {
      # There may be many of them, spread around different servers!
      # print "jo, $_ ; $1 ; $2 \n" if $debug;
      push(@{$distdirs{$2}}, $1);

      if(/\/(binary|installer)-([^\/]+)\//)
      {
         push(@{$archfiles{$2}}, $_);
      }
   }
}

my $hideidx=0;

my $cmd=0;

sub printPrompt
{
   syswrite(STDOUT, "
WARNING: The removal action may wipe out whole directories containing
         index files. Select d to see detailed list.

(Number nn: tag distribution or architecture nn; 0: exit; d: show details; r: remove tagged; q: quit): ");
}

my @tags;

for(;;)
{
   die "No distribution index files found\n" if( ! keys %distdirs);

   if(DO_PROMPT == $cmd || DO_DETAILS == $cmd)
   {
      syswrite(STDOUT, "Found distributions:\n");
      my $pos=0;
   
      # if there is anything tagged, display only them
      my $taggedcount=0;
      map { $taggedcount++ if $_ } @tags;
      print "bla, taggedcount: $taggedcount\n";

      foreach(keys %distdirs)
      {
         my $tag = ($tags[++$pos] ? "*" : " ");
         print "   $tag ".$pos.". $_ (".scalar@{$distdirs{$_}}." index files)\n";
         if(DO_DETAILS == $cmd && ($tag eq "*" || !$taggedcount))
         {
            my %uno;
            $uno{$_}=1 foreach(@{$distdirs{$_}});
            print "$_\n" foreach(keys %uno);
         }

      }
      syswrite(STDOUT, "\nFound architectures:\n");
      foreach(sort(keys %archfiles))
      {
         my $tag = ($tags[++$pos] ? "*" : " ");
         print "   $tag ".$pos.". $_ (".scalar@{$archfiles{$_}}." index files)\n";
         if(DO_DETAILS == $cmd && ($tag eq "*" || !$taggedcount))
         {
            foreach(@{$archfiles{$_}})
            {
               print "$_\n" unless $_=~/.head$/;
            }
         }
      }
   }
   elsif(DO_REMOVE==$cmd)
   {
      my $pos=0;
      foreach(keys %distdirs)
      {
         if($tags[++$pos])
         {
            my %uno;
            $uno{$_}=1 foreach(@{$distdirs{$_}});
            system(@killcmd, $_) foreach(keys %uno);
         }
      }
      foreach(sort(keys %archfiles))
      {
         if($tags[++$pos])
         {
            system(@killcmd, $_) foreach(@{$archfiles{$_}});
         }
      }
      @tags=();
      syswrite(STDOUT, <<EWARN

NOTE: some package files may become unreferenced now but they will only be
removed after one or multiple expiration runs. To do that immediately, use
the web interface to trigger the expiration or maybe delete unreferenced 
files manually. Press Return to continue...
EWARN
      );
      sysread(STDIN, $pos, 234);

      goto rescancache;
   }

   $cmd=0;
   &printPrompt;

   my $response=0;
   sysread(STDIN, $response, 234);
   exit(0) if($response=~/^q/);

   my $taggedcount=0;
   map { $taggedcount++ if $_ } @tags;

   if($response=~/^d/)
   {
      $cmd=DO_DETAILS;
   }
   elsif($response=~/^\d/)
   {
      foreach my $id (split(/\D/, $response))
      {
         next if($id > 1000);
         #print "got: $id and prev: $tags[$id]" if $debug;
         $tags[$id] = !$tags[$id];
      }
   }
   if($response=~/^r/)
   {
      $cmd=DO_REMOVE if $taggedcount;
   }
   next;
##
##   exit 0 if( ! ($response=~/\d/) || 0 == $response);
##   my $opfer=$tmp[$response];
##   syswrite(STDOUT, "Please wait, removing distribution files of $opfer ...\n");
##   foreach(keys %distdirs)
##   {
##      next if ($distdirs{$_} ne $opfer || !-d $_);
##      print "Removing $_ ...\n";
##      system("/bin/rm", "-r", $_);
##
##      delete $distdirs{$_};
##   }
##   delete $counts{$opfer};
##   syswrite(STDOUT, <<EWARN
##
##NOTE: some package files may become unreferenced now but they will only be
##removed after one or multiple expiration runs. To do that immediately, use
##the web interface to trigger the expiration and maybe delete unreferenced 
##files manually. Press Return to continue...
##EWARN
##);
##   sysread(STDIN, $response, 234);
}