5 dh_compress - compress files and fix symlinks in package build directories
11 use Debian::Debhelper::Dh_Lib;
15 B<dh_compress> [S<I<debhelper options>>] [B<-X>I<item>] [B<-A>] [S<I<file ...>>]
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
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
36 =item debian/I<package>.compress
38 If this file exists, the default files are not compressed. Instead, the
39 file is ran as a shell script, and all filenames that the shell script
40 outputs will be compressed. The shell script will be run from inside the
41 package build directory. Note though that using -X is a much better idea in
42 general; you should only use a debian/package.compress file if you really
51 =item B<-X>I<item>, B<--exclude=>I<item>
53 Exclude files that contain "item" anywhere in their filename from being
54 compressed. For example, -X.tiff will exclude tiff files from compression.
55 You may use this option multiple times to build up a list of things to
56 exclude. You can accomplish the same thing by using a debian/compress file,
61 Compress all files specified by command line parameters in ALL packages
66 Add these files to the list of files to compress.
72 Debian policy, version 3.0
78 foreach my $package (@{$dh{DOPACKAGES}}) {
79 my $tmp=tmpdir($package);
80 my $compress=pkgfile($package,"compress");
82 # Run the file name gathering commands from within the directory
83 # structure that will be effected.
85 verbose_print("cd $tmp");
86 chdir($tmp) || error("Can't cd to $tmp: $!");
88 # Figure out what files to compress.
90 # First of all, deal with any files specified right on the command line.
91 if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
95 # The config file is a sh script that outputs the files to be compressed
96 # (typically using find).
97 push @files, split(/\n/,`sh $olddir/$compress 2>/dev/null`);
100 # Note that all the excludes of odd things like _z
101 # are because gzip refuses to compress such files, assumming
102 # they are zip files. I looked at the gzip source to get the
103 # complete list of such extensions: ".gz", ".z", ".taz",
104 # ".tgz", "-gz", "-z", "_z"
105 push @files, split(/\n/,`
106 find usr/info usr/share/info usr/man usr/share/man usr/X11*/man -type f ! -name "*.gz" 2>/dev/null || true;
107 find usr/share/doc -type f \\( -size +4k -or -name "changelog*" -or -name "NEWS*" \\) \\
108 \\( -name changelog.html -or ! -iname "*.htm*" \\) \\
109 ! -iname "*.gif" ! -iname "*.png" ! -iname "*.jpg" \\
110 ! -iname "*.jpeg" ! -iname "*.gz" ! -iname "*.taz" \\
111 ! -iname "*.tgz" ! -iname "*.z" ! -iname "*.bz2" \\
112 ! -iname "*-gz" ! -iname "*-z" ! -iname "*_z" \\
113 ! -iname "*.jar" ! -iname "*.zip" ! -iname "*.css" \\
114 ! -iname "*.svg" ! -iname "*.svgz" \\
115 ! -name "index.sgml" \\
116 ! -name "copyright" 2>/dev/null || true;
117 find usr/X11R6/lib/X11/fonts usr/share/fonts/X11 -type f -name "*.pcf" 2>/dev/null || true;
121 # Exclude files from compression.
122 if (@files && defined($dh{EXCLUDE}) && $dh{EXCLUDE}) {
126 foreach my $x (@{$dh{EXCLUDE}}) {
137 # Look for files with hard links. If we are going to compress both,
138 # we can preserve the hard link across the compression and save
144 my ($dev, $inode, undef, $nlink)=stat($_);
146 if (! $seen{"$inode.$dev"}) {
147 $seen{"$inode.$dev"}=$_;
151 # This is a hardlink.
152 $hardlinks{$_}=$seen{"$inode.$dev"};
161 # Make executables not be anymore.
162 xargs(\@f,"chmod","a-x");
164 xargs(\@f,"gzip","-9nf");
167 # Now change over any files we can that used to be hard links so
169 foreach (keys %hardlinks) {
171 doit("rm","-f","$_");
173 doit("ln","$hardlinks{$_}.gz","$_.gz");
176 verbose_print("cd '$olddir'");
179 # Fix up symlinks that were pointing to the uncompressed files.
180 my %links = map { chomp; $_ => 1 } `find $tmp -type l`;
182 # Keep looping through looking for broken links until no more
183 # changes are made. This is done in case there are links pointing
184 # to links, pointing to compressed files.
187 foreach my $link (keys %links) {
188 my ($directory) = $link =~ m:(.*)/:;
189 my $linkval = readlink($link);
190 if (! -e "$directory/$linkval" && -e "$directory/$linkval.gz") {
191 doit("rm","-f",$link);
192 doit("ln","-sf","$linkval.gz","$link.gz");
193 delete $links{$link};
204 This program is a part of debhelper.
208 Joey Hess <joeyh@debian.org>