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