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