summaryrefslogtreecommitdiff
path: root/Master/tlpkg/bin/tlprm
blob: 58f82604dc114d1ff17373b4be60520444e167d9 (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
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
#!/usr/bin/env perl
# $Id$
# Copyright 2007, 2008 TeX Users Group.
# This file is licensed under the GNU General Public License version 2
# or any later version.
# 
# Remove a package from the TeX Live source repository.  We use this on
# the source tree when a package becomes obsolete, gets merged, or
# whatever.  It doesn't handle package removals from an installed tree.

our $mydir;

BEGIN {
  $^W = 1;
  ($mydir = $0) =~ s,/[^/]*$,,;
  unshift (@INC, "$mydir/..");
}

use strict;

use TeXLive::TLConfig;
use TeXLive::TLPDB;
use TeXLive::TLPSRC;
use TeXLive::TLUtils;
use Pod::Usage;
use Getopt::Long;
use Cwd 'abs_path';

my $opt_svn = 1;
my $help = 0;

TeXLive::TLUtils::process_logging_options();

GetOptions("svn!",
           "help|?" => \$help) or pod2usage(1);

pod2usage(-exitstatus => 0, -verbose => 2) if $help;

exit (&main ());


sub main
{
  my $failure_count = 0;

  chomp (my $Master = `cd $mydir/../.. && pwd`);
  my $tlpdb = TeXLive::TLPDB->new (root => $Master);

  foreach my $f (@ARGV) {
    my $obj = $tlpdb->get_package ($f);
    if (! $obj) {
      warn "$0: no TeX Live package named $f.\n";
      $failure_count++;
      next;
    }

    my @files = $obj->all_files;
    if ($opt_svn) {
      my $tlpsrc = new TeXLive::TLPSRC;
      $tlpsrc->from_file ($f);
      push (@files, $tlpsrc->_srcfile); # also want to remove the tlpsrc file.

      # The paths in tlpdb are relative to Master, so we chdir there so
      # that Cwd::abs_path can work.
      chdir ($Master) || die "chdir($Master) failed: $!";

      # Commonly, we want to remove entire directories, not just all the
      # files in the directory.
      my @removals = &collapse_dirs (@files);
      my $removals = join ("", map { "$_\n" } @removals);
      
      my $TMPDIR = $ENV{"TMPDIR"} || "/tmp";
      my $rm_file = "$TMPDIR/tlprm.rm";
      unlink ($rm_file);
      `printf "$removals" >$rm_file`;
      
      my @lines = `grep -l 'depend.* $f\$' $Master/tlpkg/tlpsrc/collection-*`;
      my $commit_file = "$TMPDIR/tlprm.commit";
      unlink ($commit_file);
      `printf "$removals" >$commit_file`;
      `printf "@lines" >>$commit_file`;
      
      print "first edit collections:\n", @lines;
      print "and if needed: $Master/tlpkg/bin/tlpkg-ctan-check\n";
      `echo $Master/tlpkg/bin/tlpkg-ctan-check >>$commit_file`;
      
      print "then:\nsvn remove `cat $rm_file`\n";
      print "svn commit -m'rm $f' `cat $commit_file`\n";
     
      if (1) {  # maybe only if debugging?
        print "\nremovals ($rm_file):\n";
        print `cat $rm_file`;
        print "\ncommits ($commit_file):\n";
        print `cat $commit_file`;
        print "\nlist of files being removed:\n";
        print "$_\n" foreach @files;
      }

    } else {
      die "sorry, can only do svn removals now.";
      # maybe later it will make sense to extend this for removals
      # from an installation.
    }
  }

  # Instead of rewriting the database here, I think we're better off
  # just doing it nightly or whatever.  For --svn purposes anyway.

  return $failure_count;
}


# Return FILES, filtered with containing directories.  That is, look at
# the containing directories of each FILE.  If every file within a given
# dir is included in FILES, replace those files with the directory name
# in the return list.  Any files with sibling files not included are
# retained.
# 
# We do not try to check anything recursively.  It doesn't buy us
# anything, since what we will eventually be doing is running svn
# commands to remove these dirs/files, and it's harmless to list subdirs.
# 
sub collapse_dirs
{
  my (@files) = @_;
  my @ret = ();
  my %by_dir;
  
  # construct hash of all directories mentioned, values are lists of the
  # files in that directory.
  for my $f (@files) {
    my $abs_f = abs_path ($f);
    die ("oops, no abs_path($f) from " . `pwd`) unless $abs_f;
    (my $d = $abs_f) =~ s,/[^/]*$,,;
    my @a = exists $by_dir{$d} ? @{$by_dir{$d}} : ();
    push (@a, $abs_f);
    $by_dir{$d} = \@a;
  }
  
  # for each of our directories, see if we are removing everything in
  # the directory.  if so, return the directory; else return the
  # individual files.
  for my $d (sort keys %by_dir) {
    opendir (DIR, $d) || die "opendir($d) failed: $!";
    my @dirents = readdir (DIR);
    closedir (DIR) || warn "closedir($d) failed: $!";
    
    # initialize test hash with all the files we saw in this dir.
    # (These idioms are due to "Finding Elements in One Array and Not
    # Another" in the Perl Cookbook.)
    my %seen;
    my @rmfiles = @{$by_dir{$d}};
    @seen{@rmfiles} = ();

    # see if everything is the same.
    my @dironly = ();
    for my $dirent (@dirents) {
      next if $dirent =~ /^\.(\.|svn)?$/;  # ignore . .. .svn

      my $item = "$d/$dirent";  # prepend directory for comparison
      if (! exists $seen{$item}) {
        push (@dironly, $item);
        last;  # no need to keep looking after the first.
      }
    }
    
    push (@ret, @dironly ? @{$by_dir{$d}} : $d);
  }
  
  return @ret;
}

__END__

=head1 NAME

tlprm - remove a TeX Live package

=head1 SYNOPSIS

tlprm [OPTION]... [TLPKG]...

=head1 OPTIONS

=over 4

=item B<-svn>
Print the Subversion commands that will do the actual removal.  This is
the default mode of operation.

=back

The standard options C<-help> and C<-debug> are also accepted.
See the tlpfiles documentation for details.

=head1 DESCRIPTION

By default (or with the C<--svn> option), B<tlprm> merely prints
commands to remove TeX Live packages from the source repository, e.g.,
when a package becomes obsolete or merged.  It does not actually run any
commands, remove anything, or change the TeX Live package database or
anything else.

No dependency or other checking is done.  It also does not remove the
given package from any collection(s) where it is present.

Perhaps someday this will be extended to remove a package from an actual
installation.

=head1 AUTHORS AND COPYRIGHT

This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.

=cut

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2: #