bump product version to 5.0.4.1
[LibreOffice.git] / bin / lo-pack-sources
blob3571638cc45f83195eea12166758f10634ea2f44
1 #!/usr/bin/perl
2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
4 #!/usr/bin/perl
6 use strict;
7 use File::Copy;
8 use File::Temp qw/ tempfile tempdir /;
10 my %module_dirname = (
11 "core" => "",
12 "dictionaries" => "dictionaries",
13 "help" => "helpcontent2",
14 "translations" => "translations"
16 my $lo_topdir_name;
18 # get libreoffice-build version from the given libreoffice-build sources
19 sub get_config_version($)
21 my ($lo_core_dir) = @_;
22 my $version;
24 open (CONFIGURE, "$lo_core_dir/configure.ac") ||
25 die "can't open \"$lo_core_dir/configure.ac\" for reading: $!\n";
27 while (my $line = <CONFIGURE>) {
28 chomp $line;
30 if ($line =~ /AC_INIT\s*\(\s*libreoffice-build\s*,\s*([\w\.]*)\)/) {
31 $version="$1";
34 close (CONFIGURE);
35 return $version;
38 # increment the version for a test build:
39 # + add 'a' if the version ended with a number
40 # + bump the letter otherwise
41 sub inc_test_version($)
43 my ($version) = @_;
45 my $lastchar = chop $version;
46 my $new_version;
48 if ($lastchar =~ /\d/) {
49 return "$version" . "$lastchar" . "a";
50 } elsif ($lastchar =~ /\w/) {
51 # select next letter alhabeticaly: a->b, b->c, ...
52 $lastchar =~ tr/0a-zA-Z/a-zA-Z0/;
53 return "$version" . "$lastchar";
54 } else {
55 die "Can't generate test version from \"$version$lastchar\n";
59 sub get_release_version($$$$)
61 my ($config_version, $state_config_version, $state_release_version, $inc_version) = @_;
62 my $release_version;
64 if (defined $state_config_version &&
65 defined $state_release_version &&
66 "$state_config_version" eq "$config_version") {
67 $release_version = "$state_release_version";
68 } else {
69 $release_version = "$config_version";
72 if ( defined $inc_version ) {
73 $release_version = inc_test_version($release_version);
76 return $release_version;
79 # copy files to temp dir; showing a progress; using a black list
80 sub copy_dir_filter_and_show_progress($$)
82 my ($source_dir, $target_dir) = @_;
84 print "Copying \"$source_dir\" -> \"$target_dir\"...";
85 # copy sources from git and show progress
86 system ("cd $source_dir && " .
87 "git archive --format=tar HEAD | " .
88 " tar -xf - -C $target_dir --checkpoint=500 --checkpoint-action=exec=\"echo -n .\"") &&
89 die "Error: copying failed: $!\n";
90 print "\n";
93 # copy files to temp dir; showing a progress; using a black list
94 sub remove_empty_submodules($)
96 my ($target_topdir) = @_;
98 foreach my $submodule (sort values %module_dirname) {
99 next unless ($submodule);
100 print "Removing empty submodule: $submodule...\n";
101 rmdir "$target_topdir/$submodule" || die "Error: Can't remove submodule directory: $target_topdir/$submodule";
105 # copy the source directory into a tmp directory
106 # omit the .git subdirectories
107 sub copy_lo_module_to_tempdir($$$)
109 my ($source_dir, $module, $lo_topdir_name) = @_;
110 my $tempdir = tempdir( 'libreoffice-XXXXXX', DIR => File::Spec->tmpdir );
112 mkdir "$tempdir/$lo_topdir_name" || die "Can't create directory \"$tempdir/$lo_topdir_name\": $!\n";
113 mkdir "$tempdir/$lo_topdir_name/$module_dirname{$module}" || die "Can't create directory \"$tempdir/$lo_topdir_name/$module_dirname{$module}\": $!\n" if ($module_dirname{$module});
115 copy_dir_filter_and_show_progress("$source_dir/$module_dirname{$module}", "$tempdir/$lo_topdir_name/$module_dirname{$module}");
116 remove_empty_submodules("$tempdir/$lo_topdir_name/") if ($module eq "core");
118 return $tempdir;
121 sub generate_lo_module_changelog($$$)
123 my ($source_dir, $lo_module_release_topdir, $module) = @_;
125 my $log_name = "ChangeLog";
126 $log_name .= "-$module_dirname{$module}" if ($module_dirname{$module});
127 print "Generating changelog for $module...\n";
128 # print "1:$lo_module_clone, 2:$lo_module_release_dir, 3:$module\n";
129 system ("cd $source_dir/$module_dirname{$module} && " .
130 "git log --date=short --pretty='format:@%cd %an <%ae> [%H]%n%n%s%n%n%e%b' | " .
131 " sed -e 's|^\([^@]\)|\t\1|' -e 's|^@||' >$lo_module_release_topdir/$log_name" ) &&
132 die "Error: generating failed: $!\n";
135 sub run_autogen($$)
137 my ($dir, $module) = @_;
139 print "Running autogen for $module...\n";
140 system ("cd $dir && " .
141 "NOCONFIGURE=1 ./autogen.sh && " .
142 "rm -rf autom4te.cache && " .
143 "cd - >/dev/null 2>&1") && die "Error: autogen failed: $!\n";
146 sub generate_sources_version_file($$)
148 my ($dir, $release_version) = @_;
150 open (VERFILE, ">$dir/sources.ver") || die "Can't open $dir/sources.ver: $!\n";
152 print VERFILE "lo_sources_ver=$release_version\n";
154 close VERFILE;
157 sub generate_tarball($$$)
159 my ($dir, $tarball, $tar_compress_option) = @_;
161 print "Creating $tarball...";
162 # generate the tarball in the current directory; avoid "./" prefix in the stored paths; show progress
163 system ("tar -c $tar_compress_option -f $tarball -C $dir --checkpoint=500 --checkpoint-action=exec=\"echo -n .\" $lo_topdir_name") &&
164 die "Error: releasing failed: $!\n";
165 print "\n";
168 sub generate_md5($)
170 my ($filename) = @_;
172 print "Generating MD5...\n";
173 system ("md5sum $filename >$filename.md5") &&
174 die "Error: releasing failed: $!\n";
177 sub default_releases_state_file($)
179 my ($lo_core_dir) = @_;
181 my $rootdir = $lo_core_dir;
182 $rootdir =~ s/^(.*?)\/?[^\/]+\/?$/$1/;
184 my $releases_state_file;
185 if ($rootdir) {
186 $releases_state_file = "$rootdir/.releases";
187 } else {
188 $releases_state_file = ".releases";
191 return "$releases_state_file";
194 sub load_releases_state($)
196 my ($releases_state_file) = @_;
198 my $state_config_version;
199 my $state_release_version;
201 if (open (STATE, "$releases_state_file")) {
203 while (my $line = <STATE>) {
204 chomp $line;
206 if ($line =~ /^\s*configure_version\s*=\s*(.*)$/) {
207 $state_config_version = "$1";
208 } elsif ($line =~ /^\s*released_version\s*=\s*(.*)$/) {
209 $state_release_version = "$1";
212 close (STATE);
215 return $state_config_version, $state_release_version;
218 sub save_releases_state($$$)
220 my ($releases_state_file, $config_version, $release_version) = @_;
222 open (STATE, '>', "$releases_state_file") ||
223 die "Can't open \"$releases_state_file\" for writing: $!\n";
225 print STATE "configure_version = $config_version\n";
226 print STATE "released_version = $release_version\n";
228 close (STATE);
231 sub remove_tempdir($)
233 my ($tempdir) = @_;
235 # print "Cleaning $tempdir...\n";
236 system ("rm -rf $tempdir") && die "Error: rm failed: $!\n";
239 sub check_if_file_exists($$)
241 my ($file, $force) = @_;
243 if (-e $file) {
244 if (defined $force) {
245 print "Warning: $file already exists and will be replaced!\n";
246 } else {
247 die "Error: $file alrady exists.\n".
248 " Use --force if you want to replace it.\n";
253 sub check_if_already_released($$$$$$)
255 my ($p_module_tarball_name, $force, $bzip2, $xz, $pack_lo_core, $pack_lo_modules) = @_;
257 foreach my $tarball_name ( sort values %{$p_module_tarball_name} ) {
258 check_if_file_exists("$tarball_name.tar.bz2", $force) if (defined $bzip2);
259 check_if_file_exists("$tarball_name.tar.xz", $force) if (defined $xz);
263 sub prepare_module_sources($$$$)
265 my ($source_dir, $release_version, $module, $lo_topdir_name) = @_;
267 # prepare sources
268 my $temp_dir = copy_lo_module_to_tempdir($source_dir, $module, $lo_topdir_name);
269 generate_lo_module_changelog($source_dir, "$temp_dir/$lo_topdir_name", $module);
270 run_autogen("$temp_dir/$lo_topdir_name", $module) if ($module eq 'core');
271 generate_sources_version_file("$temp_dir/$lo_topdir_name", $release_version) if ($module eq 'core');
273 return $temp_dir;
276 sub pack_module_sources($$$$)
278 my ($temp_dir, $md5, $tarball, $tar_compress_option) = @_;
280 generate_tarball($temp_dir, $tarball, $tar_compress_option);
281 generate_md5($tarball) if (defined $md5);
284 sub generate_module_tarball($$$$$$$$)
286 my ($source_dir, $release_version, $module, $md5, $bzip2, $xz, $lo_topdir_name, $module_tarball_name) = @_;
288 my $temp_dir = prepare_module_sources($source_dir, $release_version, $module, $lo_topdir_name);
289 pack_module_sources($temp_dir, $md5, "$module_tarball_name.tar.bz2", "--bzip2") if (defined $bzip2);
290 pack_module_sources($temp_dir, $md5, "$module_tarball_name.tar.xz", "--xz") if (defined $xz);
291 remove_tempdir($temp_dir);
295 sub generate_tarballs($$$$$$$$$)
297 my ($source_dir, $release_version, $md5, $bzip2, $xz, $lo_topdir_name, $p_module_tarball_name, $pack_lo_core, $pack_lo_modules) = @_;
299 foreach my $module (sort keys %{$p_module_tarball_name} ) {
300 print "\n--- Generating $module ---\n";
301 generate_module_tarball($source_dir, $release_version, $module, $md5, $bzip2, $xz, $lo_topdir_name, $p_module_tarball_name->{$module});
306 sub usage()
308 print "This tool helps to pack the libreoffice-build and module sources\n\n" .
310 "Usage:\n".
311 "\tlo-pack-sources [--help]\n" .
312 "\t [--force] [--md5] [--bzip2] [--xz]\n" .
313 "\t [--version][--set-version=<ver>] [--inc-version]\n" .
314 "\t [--no-submodule] [--module=<module>]\n" .
315 "\t [dir]\n\n" .
317 "Options:\n\n" .
318 "\t--help: print this help\n" .
319 "\t--force: replace an already existing release of the same version\n" .
320 "\t--md5: generate md5 sum for the final tarball\n" .
321 "\t--bzip2: generate tarballs compressed by bzip2\n" .
322 "\t--xz: generate tarballs compressed by xz (default)\n" .
323 "\t--version: just print version of the released package but do not\n" .
324 "\t\trelease it; the version is affected by the other options, e.g.\n" .
325 "\t\t--inc-version\n" .
326 "\t--set-version: force another version\n" .
327 "\t--inc-version: increment the latest version; there is a difference\n" .
328 "\t\tbetween test release (default) and final (not yet supported)\n" .
329 "\t--no-submodule: do not pack sources from git submodules\n" .
330 "\t--module=<module>: pack just a single module, use \"core\"\n" .
331 "\t\tfor the main git repo,\n" .
332 "\tdir: path of the source directory, either libreoffice-build or module\n";
336 my $module;
337 my $ptf;
338 my $md5;
339 my $bzip2;
340 my $xz;
341 my $inc_version;
342 my $config_version;
343 my $set_version;
344 my $get_config_version;
345 my $release_version;
346 my $pack_lo_core=1;
347 my $pack_lo_modules=1;
348 my $source_dir;
349 my $releases_state_file;
350 my $state_config_version;
351 my $state_release_version;
352 my $lo_core_tempdir;
353 my $force;
354 my $verbose=1;
355 my %module_tarball_name;
357 ###################
358 # Arguments parsing
359 ###################
361 for my $arg (@ARGV) {
362 if ($arg eq '--help' || $arg eq '-h') {
363 usage;
364 exit 0;
365 } elsif ($arg eq '--force') {
366 $force=1;
367 } elsif ($arg eq '--md5') {
368 $md5=1;
369 } elsif ($arg eq '--bzip2') {
370 $bzip2=1;
371 } elsif ($arg eq '--xz') {
372 $xz=1;
373 } elsif ($arg eq '--version') {
374 $get_config_version=1;
375 $verbose = undef;
376 } elsif ($arg eq '--inc-version') {
377 $inc_version=1
378 } elsif ($arg =~ m/--set-version=(.*)/) {
379 $set_version="$1";
380 } elsif ($arg eq '--no-submodule') {
381 $module = "core";
382 } elsif ($arg =~ m/--module=(.*)/) {
383 # process just one module and do not pack libreoffice-build
384 die("Error: unknown module: $1") unless (defined $module_dirname{$1});
385 $module = $1;
386 } elsif ($arg =~ /^-/ ) {
387 die "Error: unknown option: $arg\n";
388 } else {
389 if (! defined $source_dir) {
390 $source_dir = $arg;
391 } else {
392 die "Error: Too many arguments $arg\n";
397 # ugly hack; we want only one module
398 if ($module) {
399 my $name = $module_dirname{$module};
400 %module_dirname = ();
401 $module_dirname{$module} = $name;
404 ###################
405 # Initial checks
406 ###################
408 unless ( defined $source_dir ) {
409 die "Error: undefined source directory, try --help\n";
412 unless ( -d "$source_dir" ) {
413 die "Error: is not a directory: $source_dir\n";
416 # check if it is a valid libreoffice-core directory
417 unless (-f "$source_dir/autogen.sh" && -f "$source_dir/config_host.mk.in") {
418 die "Error: \"$source_dir\" is not a valid libreoffice-core directory\n";
421 if (defined $set_version && defined $inc_version) {
422 die "Error: --set-version and --inc-version options can't be used together\n";
425 # default compression
426 $xz = 1 unless (defined $xz || defined $bzip2);
429 ###################
430 # Main logic
431 ###################
434 print "Source: $source_dir\n" if ($verbose);
436 # detect some paths
437 $releases_state_file = default_releases_state_file($source_dir) unless (defined $releases_state_file);
439 # detect versions
440 $config_version = get_config_version($source_dir);
441 ($state_config_version, $state_release_version) = load_releases_state($releases_state_file);
442 if (defined $set_version) {
443 $release_version = "$set_version";
444 } else {
445 $release_version = get_release_version($config_version, $state_config_version, $state_release_version, $inc_version);
448 # define tarball names
449 print "Detected module:\n";
450 foreach my $module (sort keys %module_dirname) {
451 if (-e "$source_dir/$module_dirname{$module}/.git") {
452 print " $module\n";
453 if ($module eq "core") {
454 $module_tarball_name{$module} = "libreoffice-$release_version";
455 } else {
456 $module_tarball_name{$module} = "libreoffice-$module-$release_version";
458 } else {
459 print "did not found: $source_dir/$module_dirname{$module}/.git\n";
460 print "Warning: $module sources are not available -> skipping\n";
464 # top directory inside the source tarballs
465 $lo_topdir_name = "libreoffice-$release_version";
467 print "Default version : $config_version\n" if ($verbose && defined $config_version);
468 print "Last used version : $state_release_version\n" if ($verbose && defined $state_release_version);
469 print "New version : $release_version\n" if ($verbose);
471 # do the real job
472 if ( defined $get_config_version ) {
473 print "$release_version\n";
474 } else {
475 check_if_already_released(\%module_tarball_name, $force, $bzip2, $xz, $pack_lo_core, $pack_lo_modules);
477 # give a chance to stop the process
478 print ("\nWaiting 3 seconds...\n");
479 sleep 3;
481 generate_tarballs($source_dir, $release_version, $md5, $bzip2, $xz, $lo_topdir_name, \%module_tarball_name, $pack_lo_core, $pack_lo_modules);
483 if ( defined $releases_state_file ) {
484 save_releases_state($releases_state_file, $config_version, $release_version);