]> git.donarmstrong.com Git - debhelper.git/blob - dh_compress
r156: Initial Import
[debhelper.git] / dh_compress
1 #!/usr/bin/perl -w
2 #
3 # Compresses files and makes sure that symlinks pointing to the 
4 # compressed files get fixed.
5
6 use Cwd;
7 BEGIN { push @INC, "debian", "/usr/lib/debhelper" }
8 use Dh_Lib;
9 init();
10
11 foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
12         $TMP=tmpdir($PACKAGE);
13         $compress=pkgfile($PACKAGE,"compress");
14
15         # Run the file name gathering commands from within the directory
16         # structure that will be effected.
17         $olddir=getcwd();
18         chdir($TMP) || error("Can't cd to $TMP: $!");
19
20         # Figure out what files to compress.
21         @files=();
22         # First of all, deal with any files specified right on the command line.
23         if (($PACKAGE eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
24                 push @files,#ARGV;
25         }
26         if ($compress) {
27                 # The config file is a sh script that outputs the files to be compressed
28                 # (typically using find).
29                 push @files, split(/\n/,`sh $olddir/$compress 2>/dev/null`);
30         }
31         else {
32                 # By default, fall back to what the policy manual says to compress.
33                 push @files, split(/\n/,`
34                         find usr/info usr/man usr/X11*/man -type f ! -name "*.gz" 2>/dev/null || true;
35                         find usr/doc -type f \\( -size +4k -or -name "changelog*" \\) \\
36                                 ! -name "*.htm*" ! -name "*.gif" ! -name "*.gz" \\
37                                 ! -name "copyright" 2>/dev/null || true
38                 `);
39         }
40
41         # Exclude files from compression.
42         if (@files && defined($dh{EXCLUDE}) && $dh{EXCLUDE}) {
43                 @new=();
44                 foreach (@files) {
45                         $ok=1;
46                         foreach $x (@{$dh{EXCLUDE}}) {
47                                 if (/\Q$x\E/) {
48                                         $ok='';
49                                         last;
50                                 }
51                         }
52                         push @new,$_ if $ok;
53                 }
54                 @files=@new;
55         }
56         
57         # Look for files with hard links. If we are going to compress both,
58         # we can preserve the hard link across the compression and save
59         # space in the end.
60         my @f=();
61         foreach (@files) {
62                 ($dev, $inode, undef, $nlink)=stat($_);
63                 if ($nlink > 1) {
64                         if (! $seen{"$inode.$dev"}) {
65                                 $seen{"$inode.$dev"}=$_;
66                                 push @f, $_;
67                         }
68                         else {
69                                 # This is a hardlink.
70                                 $hardlinks{$_}=$seen{"$inode.$dev"};
71                         }
72                 }
73                 else {
74                         push @f, $_;
75                 }
76         }
77
78         if (@f) {
79                 doit("gzip","-9f",@f);
80         }
81         
82         # Now change over any files we can that used to be hard links so
83         # they are again.
84         foreach (keys %hardlinks) {
85                 # Remove old file.
86                 doit("rm","-f","$_");
87                 # Make new hardlink.
88                 doit("ln","$hardlinks{$_}.gz","$_.gz");
89         }
90
91         chdir($olddir);
92
93         # Fix up symlinks that were pointing to the uncompressed files.
94         open (FIND,"find $TMP -type l |");
95         while (<FIND>) {
96                 chomp;
97                 ($directory)=m:(.*)/:;
98                 $linkval=readlink($_);
99                 if (! -e "$directory/$linkval" && -e "$directory/$linkval.gz") {
100                         doit("rm","-f",$_);
101                         doit("ln","-sf","$linkval.gz","$_.gz");
102                 }
103         }
104 }