]> git.donarmstrong.com Git - debhelper.git/blob - dh_compress
Merge branch 'master' into buildsystems
[debhelper.git] / dh_compress
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_compress - compress files and fix symlinks in package build directories
6
7 =cut
8
9 use strict;
10 use Cwd;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_compress> [S<I<debhelper options>>] [B<-X>I<item>] [B<-A>] [S<I<file ...>>]
16
17 =head1 DESCRIPTION
18
19 dh_compress is a debhelper program that is responsible for compressing
20 the files in package build directories, and makes sure that any symlinks
21 that pointed to the files before they were compressed are updated to point
22 to the new files.
23
24 By default, dh_compress compresses files that debian policy mandates should
25 be compressed, namely all files in usr/share/info, usr/share/man,
26 usr/X11R6/man, files in usr/share/doc that are larger than 4k in size,
27 (except the copyright file, .html and .css files, image files, and files
28 that appear to be already compressed based on their extensions), and all
29 changelog files. Plus PCF fonts underneath usr/X11R6/lib/X11/fonts/ and
30 usr/share/fonts/X11/
31
32 If a debian/package.compress file exists, the default files are not
33 compressed. Instead, the debian/packages.compress is ran as a shell
34 script, and all filenames that the shell script outputs will be compressed.
35 The shell script will be run from inside the package build directory. Note
36 though that using -X is a much better idea in general; you should only use a
37 debian/package.compress file if you really need to.
38
39 =head1 OPTIONS
40
41 =over 4
42
43 =item B<-X>I<item>, B<--exclude=>I<item>
44
45 Exclude files that contain "item" anywhere in their filename from being
46 compressed. For example, -X.tiff will exclude tiff files from compression.
47 You may use this option multiple times to build up a list of things to
48 exclude. You can accomplish the same thing by using a debian/compress file,
49 but this is easier.
50
51 =item B<-A>, B<--all>
52
53 Compress all files specified by command line parameters in ALL packages
54 acted on.
55
56 =item I<file ...>
57
58 Add these files to the list of files to compress.
59
60 =back
61
62 =head1 CONFORMS TO
63
64 Debian policy, version 3.0
65
66 =cut
67
68 init();
69
70 foreach my $package (@{$dh{DOPACKAGES}}) {
71         my $tmp=tmpdir($package);
72         my $compress=pkgfile($package,"compress");
73
74         # Run the file name gathering commands from within the directory
75         # structure that will be effected.
76         my $olddir=getcwd();
77         verbose_print("cd $tmp");
78         chdir($tmp) || error("Can't cd to $tmp: $!");
79
80         # Figure out what files to compress.
81         my @files;
82         # First of all, deal with any files specified right on the command line.
83         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
84                 push @files, @ARGV;
85         }
86         if ($compress) {
87                 # The config file is a sh script that outputs the files to be compressed
88                 # (typically using find).
89                 push @files, split(/\n/,`sh $olddir/$compress 2>/dev/null`);
90         }
91         else {
92                 # Note that all the excludes of odd things like _z 
93                 # are because gzip refuses to compress such files, assumming
94                 # they are zip files. I looked at the gzip source to get the
95                 # complete list of such extensions: ".gz", ".z", ".taz", 
96                 # ".tgz", "-gz", "-z", "_z"
97                 push @files, split(/\n/,`
98                         find usr/info usr/share/info usr/man usr/share/man usr/X11*/man -type f ! -name "*.gz" 2>/dev/null || true;
99                         find usr/share/doc -type f \\( -size +4k -or -name "changelog*" -or -name "NEWS*" \\) \\
100                                 \\( -name changelog.html -or ! -iname "*.htm*" \\) \\
101                                 ! -iname "*.gif" ! -iname "*.png" ! -iname "*.jpg" \\
102                                 ! -iname "*.jpeg" ! -iname "*.gz" ! -iname "*.taz" \\
103                                 ! -iname "*.tgz" ! -iname "*.z" ! -iname "*.bz2" \\
104                                 ! -iname "*-gz"  ! -iname "*-z" ! -iname "*_z" \\
105                                 ! -iname "*.jar" ! -iname "*.zip" ! -iname "*.css" \\
106                                 ! -iname "*.svg" ! -iname "*.svgz" \\
107                                 ! -name "index.sgml" \\
108                                 ! -name "copyright" 2>/dev/null || true;
109                         find usr/X11R6/lib/X11/fonts usr/share/fonts/X11 -type f -name "*.pcf" 2>/dev/null || true;
110                 `);
111         }
112
113         # Exclude files from compression.
114         if (@files && defined($dh{EXCLUDE}) && $dh{EXCLUDE}) {
115                 my @new=();
116                 foreach (@files) {
117                         my $ok=1;
118                         foreach my $x (@{$dh{EXCLUDE}}) {
119                                 if (/\Q$x\E/) {
120                                         $ok='';
121                                         last;
122                                 }
123                         }
124                         push @new,$_ if $ok;
125                 }
126                 @files=@new;
127         }
128         
129         # Look for files with hard links. If we are going to compress both,
130         # we can preserve the hard link across the compression and save
131         # space in the end.
132         my @f=();
133         my %hardlinks;
134         my %seen;
135         foreach (@files) {
136                 my ($dev, $inode, undef, $nlink)=stat($_);
137                 if ($nlink > 1) {
138                         if (! $seen{"$inode.$dev"}) {
139                                 $seen{"$inode.$dev"}=$_;
140                                 push @f, $_;
141                         }
142                         else {
143                                 # This is a hardlink.
144                                 $hardlinks{$_}=$seen{"$inode.$dev"};
145                         }
146                 }
147                 else {
148                         push @f, $_;
149                 }
150         }
151
152         if (@f) {
153                 # Make executables not be anymore.
154                 xargs(\@f,"chmod","a-x");
155                 
156                 xargs(\@f,"gzip","-9nf");
157         }
158         
159         # Now change over any files we can that used to be hard links so
160         # they are again.
161         foreach (keys %hardlinks) {
162                 # Remove old file.
163                 doit("rm","-f","$_");
164                 # Make new hardlink.
165                 doit("ln","$hardlinks{$_}.gz","$_.gz");
166         }
167
168         verbose_print("cd '$olddir'");
169         chdir($olddir);
170
171         # Fix up symlinks that were pointing to the uncompressed files.
172         my %links = map { chomp; $_ => 1 } `find $tmp -type l`;
173         my $changed;
174         # Keep looping through looking for broken links until no more
175         # changes are made. This is done in case there are links pointing
176         # to links, pointing to compressed files.
177         do {
178                 $changed = 0;
179                 foreach my $link (keys %links) {
180                         my ($directory) = $link =~ m:(.*)/:;
181                         my $linkval = readlink($link);
182                         if (! -e "$directory/$linkval" && -e "$directory/$linkval.gz") {
183                                 doit("rm","-f",$link);
184                                 doit("ln","-sf","$linkval.gz","$link.gz");
185                                 delete $links{$link};
186                                 $changed++;
187                         }
188                 }
189         } while $changed;
190 }
191
192 =head1 SEE ALSO
193
194 L<debhelper(7)>
195
196 This program is a part of debhelper.
197
198 =head1 AUTHOR
199
200 Joey Hess <joeyh@debian.org>
201
202 =cut