quickrelease: A dumb script for making source-only tarballs
[exim.git] / release-process / scripts / mk_exim_release.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Carp;
6 use File::Copy;
7 use File::Spec;
8 use File::Path;
9 use File::Temp;
10 use FindBin;
11 use Getopt::Long;
12 use IO::File;
13 use Pod::Usage;
14
15 my $debug = 0;
16 my $verbose = 0;
17
18 # ------------------------------------------------------------------
19
20 sub get_and_check_version {
21 my $release = shift;
22 my $context = shift;
23
24 # make sure this looks like a real release version
25 # which should (currently) be 4.xx or 4.xx_RCx
26 unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
27 croak "The given version number does not look right - $release";
28 }
29 my $full_release = $1; # untainted here...
30 my $trunc_release = $full_release;
31 $trunc_release =~ s/^(4\.\d\d)(?:_RC\d+)?$/$1/;
32
33 $context->{release} = $full_release;
34 $context->{trelease} = $trunc_release;
35 }
36
37 # ------------------------------------------------------------------
38
39 sub build_tag {
40 my $context = shift;
41
42 # The CVS tag consists of exim-$version where $version
43 # is the version number with . replaced with _
44 my $modversion = $context->{release};
45 $modversion =~ tr/0-9RC/_/cs;
46
47 return sprintf( 'exim-%s', $modversion );
48 }
49
50 # ------------------------------------------------------------------
51
52 sub deal_with_working_directory {
53 my $context = shift;
54 my $delete = shift;
55
56 # Set default directory
57 $context->{directory} ||= File::Spec->rel2abs( sprintf( 'exim-packaging-%s', $context->{release} ) );
58 my $directory = $context->{directory};
59
60 # ensure the working directory is not in place
61 if ( -d $directory ) {
62 if ($delete) {
63 print "Deleting existing $directory\n" if ($verbose);
64 rmtree( $directory, { verbose => $debug } );
65 }
66 if ( -d $directory ) {
67 croak "Working directory $directory exists";
68 }
69 }
70
71 # create base directory
72 mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
73
74 # set and create subdirectories
75 foreach (qw(release_tree pkgs pkgdirs docbook tmp)) {
76 $context->{$_} = File::Spec->catdir( $context->{directory}, $_ );
77 mkpath( $context->{$_}, { verbose => ( $verbose || $debug ) } );
78 }
79 }
80
81 # ------------------------------------------------------------------
82
83 sub export_git_tree {
84 my $context = shift;
85
86 # build git command
87 my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp}, $context->{pkgname}, $context->{release} );
88 $context->{tmp_archive_file} = $archive_file;
89 my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
90
91 # run git command
92 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
93 system(@cmd) == 0 || croak "Export failed";
94 }
95
96 # ------------------------------------------------------------------
97
98 sub unpack_tree {
99 my $context = shift;
100
101 die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
102 my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
103
104 # run command
105 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
106 system(@cmd) == 0 || croak "Unpack failed";
107 }
108
109 # ------------------------------------------------------------------
110
111 sub make_version_script {
112 my $context = shift;
113
114 my $variant = substr( $context->{release}, length($context->{trelease}) );
115 if ( $context->{release} ne $context->{trelease} . $variant ) {
116 die "Broken version numbering, I'm buggy";
117 }
118
119 my $srcdir = File::Spec->catdir( $context->{release_tree}, 'src', 'src' );
120 chdir $srcdir or die "chdir $srcdir: $\n";
121
122 if ( -f "version.sh" ) {
123 print( "WARNING: version.sh already exists - leaving it in place\n" );
124 return;
125 }
126
127 my @cmd = ("../scripts/reversion", "release");
128 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
129 system(@cmd) == 0 || croak "reversion failed";
130
131 unlink "version.h";
132
133 -f "version.sh" or die "failed to create version.h";
134 }
135
136 # ------------------------------------------------------------------
137
138 sub build_html_documentation {
139 my $context = shift;
140
141 my $genpath = $context->{webgen_base} . '/script/gen.pl';
142 my $templates = $context->{webgen_base} . '/templates';
143 my $dir = File::Spec->catdir( $context->{release_tree}, 'html' );
144 my $spec = File::Spec->catfile( $context->{docbook}, 'spec.xml' );
145 my $filter = File::Spec->catfile( $context->{docbook}, 'filter.xml' );
146
147 mkdir($dir);
148
149 my @cmd = (
150 $genpath, '--spec', $spec, '--filter',
151 $filter, '--latest', $context->{trelease}, '--tmpl',
152 $templates, '--docroot', $dir, '--localstatic'
153 );
154
155 print "Executing ", join( ' ', @cmd ), "\n";
156 system(@cmd);
157
158 # move directory into right place
159 my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' );
160
161 rename(
162 File::Spec->catdir( $dir, sprintf( 'exim-html-%s', $context->{trelease} ) ),
163 File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) )
164 );
165 }
166
167 # ------------------------------------------------------------------
168
169 sub copy_docbook_files {
170 my $context = shift;
171
172 # where the generated docbook files can be found
173 my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
174
175 # where the website docbook source dir is - push files to here
176 my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} );
177 mkpath( $webdir, { verbose => ( $verbose || $debug ) } );
178
179 foreach my $file ( 'spec.xml', 'filter.xml' ) {
180 my $from = File::Spec->catfile( $docdir, $file );
181 my $to = File::Spec->catfile( $context->{docbook}, $file );
182 my $webto = File::Spec->catfile( $webdir, $file );
183 copy( $from, $to );
184 copy( $from, $webto );
185 }
186 }
187
188 # ------------------------------------------------------------------
189
190 sub build_documentation {
191 my $context = shift;
192
193 my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
194 # documentation building gets the truncated release, without RC
195 system("cd '$docdir' && ./OS-Fixups && make EXIM_VER=$context->{trelease} everything") == 0
196 || croak "Doc build failed";
197
198 copy_docbook_files($context);
199 build_html_documentation($context);
200 }
201
202 # ------------------------------------------------------------------
203
204 sub move_text_docs_into_pkg {
205 my $context = shift;
206
207 my $old_docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
208 my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' );
209 my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
210 mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
211
212 # move generated documents from docbook stuff
213 foreach my $file (qw/exim.8 spec.txt filter.txt/) {
214 move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
215 }
216
217 # move text documents across
218 foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) {
219
220 # skip a few we dont want
221 my $fn = ( File::Spec->splitpath($file) )[2];
222 next
223 if ( ( $fn eq 'ABOUT' )
224 || ( $fn eq 'ChangeLog.0' )
225 || ( $fn eq 'test-harness.txt' ) );
226 move( $file, File::Spec->catfile( $new_docdir, $fn ) );
227 }
228 }
229
230 # ------------------------------------------------------------------
231
232 sub build_pspdfinfo_directory {
233 my $context = shift;
234
235 ##foreach my $format (qw/pdf postscript texinfo info/) {
236 foreach my $format (qw/pdf postscript/) {
237 my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
238 mkpath( $target, { verbose => ( $verbose || $debug ) } );
239
240 # move documents across
241 foreach my $file (
242 glob(
243 File::Spec->catfile(
244 $context->{release_tree},
245 'doc',
246 'doc-docbook',
247 (
248 ( $format eq 'postscript' )
249 ? '*.ps'
250 : ( '*.' . $format )
251 )
252 )
253 )
254 )
255 {
256 move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
257 }
258 }
259 }
260
261 # ------------------------------------------------------------------
262
263 sub build_main_package_directory {
264 my $context = shift;
265
266 # build the exim package directory path
267 $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
268
269 # initially we move the exim-src directory to the new directory name
270 rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
271 || croak "Rename of src dir failed - $!";
272
273 # add Local subdirectory
274 mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
275
276 # now add the text docs
277 move_text_docs_into_pkg($context);
278 }
279
280 # ------------------------------------------------------------------
281
282 sub build_package_directories {
283 my $context = shift;
284
285 build_main_package_directory($context);
286 build_pspdfinfo_directory($context);
287 }
288
289 # ------------------------------------------------------------------
290
291 sub do_cleanup {
292 my $context = shift;
293
294 print "Cleaning up\n" if ($verbose);
295 rmtree( $context->{release_tree}, { verbose => $debug } );
296 rmtree( $context->{docbook}, { verbose => $debug } );
297 rmtree( $context->{pkgdirs}, { verbose => $debug } );
298 }
299
300 # ------------------------------------------------------------------
301
302 # We prefer gtar to tar if gtar exists in $PATH
303
304 sub fix_paths_tar {
305 my $context = shift;
306 my $tar = $context->{tar_cmd};
307
308 return unless $tar eq 'tar';
309
310 foreach my $d (File::Spec->path()) {
311 my $p = File::Spec->catfile($d, 'gtar');
312 if (-x $p) {
313 $context->{tar_cmd} = $p;
314 print "Switched tar command to: $p\n" if ($verbose);
315 return;
316 }
317 }
318 }
319
320 # ------------------------------------------------------------------
321
322 sub create_tar_files {
323 my $context = shift;
324
325 my $pkgs = $context->{pkgs};
326 my $pkgdirs = $context->{pkgdirs};
327 my $tar = $context->{tar_cmd};
328 if ($verbose) {
329 foreach my $c (keys %{ $context->{compressors} }) {
330 print "Compression: $c\t$context->{compressors}{$c}\n";
331 }
332 }
333
334 foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
335 my $dirname = ( File::Spec->splitdir($dir) )[-1];
336 if ($context->{compressors}{gzip}) {
337 print "Creating: ${pkgs}/${dirname}.tar.gz\n" if ($verbose || $debug);
338 system("$tar cf ${pkgs}/${dirname}.tar.gz --gzip -C ${pkgdirs} ${dirname}")
339 }
340 if ($context->{compressors}{bzip2}) {
341 print "Creating: ${pkgs}/${dirname}.tar.bz2\n" if ($verbose || $debug);
342 system("$tar cf ${pkgs}/${dirname}.tar.bz2 --bzip2 -C ${pkgdirs} ${dirname}")
343 }
344 if ($context->{compressors}{lzip}) {
345 print "Creating: ${pkgs}/${dirname}.tar.lz\n" if ($verbose || $debug);
346 system("$tar cf ${pkgs}/${dirname}.tar.lz --lzip -C ${pkgdirs} ${dirname}")
347 }
348 }
349 }
350
351 # ------------------------------------------------------------------
352 {
353 my $man;
354 my $help;
355 my $context = {
356 pkgname => 'exim',
357 orig_dir => File::Spec->curdir(),
358 tmp_dir => File::Temp->newdir(),
359 webgen_base => "$FindBin::Bin/../../../exim-website",
360 tar_cmd => 'tar',
361 compressors => {
362 gzip => 1,
363 bzip2 => 1,
364 lzip => 0,
365 },
366 };
367 my $delete;
368 my $cleanup = 1;
369 ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
370
371 unless (
372 GetOptions(
373 'directory=s' => \$context->{directory},
374 'webgen_base=s' => \$context->{webgen_base},
375 'tar' => \$context->{tar_cmd},
376 'lzip!' => \$context->{compressors}{lzip},
377 'verbose!' => \$verbose,
378 'debug!' => \$debug,
379 'help|?' => \$help,
380 'man!' => \$man,
381 'delete!' => \$delete,
382 'cleanup!' => \$cleanup,
383 )
384 )
385 {
386 pod2usage( -exitval => 1, -verbose => 0 );
387 }
388 pod2usage(0) if $help;
389 pod2usage( -verbose => 2 ) if $man;
390
391 get_and_check_version( shift, $context );
392 fix_paths_tar($context);
393 $context->{tag} = build_tag($context);
394 deal_with_working_directory( $context, $delete );
395 export_git_tree($context);
396 chdir( $context->{directory} ) || die;
397 unpack_tree($context);
398 make_version_script($context);
399 build_documentation($context);
400 build_package_directories($context);
401 create_tar_files($context);
402 do_cleanup($context) if ($cleanup);
403 }
404
405 1;
406
407 __END__
408
409 =head1 NAME
410
411 mk_exim_release.pl - Build an exim release
412
413 =head1 SYNOPSIS
414
415 mk_exim_release.pl [options] version
416
417 Options:
418 --debug force debug mode (SQL Trace)
419 --verbose force verbose mode
420 --help display this help and exits
421 --man displays man page
422 --tar=cmd command to use for tar
423 --directory=dir dir to package
424 --no-lzip do not create .tar.lz files
425 --delete Delete packaging directory at start
426
427 =head1 OPTIONS
428
429 =over 4
430
431 =item B<--debug>
432
433 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
434 to be output.
435
436 =item B<--tar>
437
438 Use to override the path to the tar command; without this, will search for
439 gtar, and if not found use tar. Need GNU tar for lzip, unless --no-lzip is
440 used.
441
442 =item B<--lzip>
443
444 Build the lzip tarballs.
445
446 =item B<--verbose>
447
448 Force verbose mode
449
450 =item B<--help>
451
452 Display help and exits
453
454 =item B<--man>
455
456 Display man page
457
458 =back
459
460 =head1 DESCRIPTION
461
462 Builds an exim release.
463
464 Starting in a populated git repo that has already been tagged for
465 release, build docs, build packages etc.
466
467 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
468
469 =head1 AUTHOR
470
471 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
472
473 =head1 COPYRIGHT
474
475 Copyright 2010 Exim Maintainers. All rights reserved.
476
477 =cut
478 # vim: set sw=4 et :