Bump for 4.0-11
[LibreOffice.git] / bin / lo-pack-sources
blobde6a4be9abb400a4ec058b99ff0c30e2abd384f3
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"
17 # get libreoffice-build version from the given libreoffice-build sources
18 sub get_config_version($)
20 my ($lo_core_dir) = @_;
21 my $version;
23 open (CONFIGURE, "$lo_core_dir/configure.ac") ||
24 die "can't open \"$lo_core_dir/configure.ac\" for reading: $!\n";
26 while (my $line = <CONFIGURE>) {
27 chomp $line;
29 if ($line =~ /AC_INIT\s*\(\s*libreoffice-build\s*,\s*([\w\.]*)\)/) {
30 $version="$1";
33 close (CONFIGURE);
34 return $version;
37 # increment the version for a test build:
38 # + add 'a' if the version ended with a number
39 # + bump the letter otherwise
40 sub inc_test_version($)
42 my ($version) = @_;
44 my $lastchar = chop $version;
45 my $new_version;
47 if ($lastchar =~ /\d/) {
48 return "$version" . "$lastchar" . "a";
49 } elsif ($lastchar =~ /\w/) {
50 # select next letter alhabeticaly: a->b, b->c, ...
51 $lastchar =~ tr/0a-zA-Z/a-zA-Z0/;
52 return "$version" . "$lastchar";
53 } else {
54 die "Can't generate test version from \"$version$lastchar\n";
58 sub get_release_version($$$$)
60 my ($config_version, $state_config_version, $state_release_version, $inc_version) = @_;
61 my $release_version;
63 if (defined $state_config_version &&
64 defined $state_release_version &&
65 "$state_config_version" eq "$config_version") {
66 $release_version = "$state_release_version";
67 } else {
68 $release_version = "$config_version";
71 if ( defined $inc_version ) {
72 $release_version = inc_test_version($release_version);
75 return $release_version;
78 # copy files to temp dir; showing a progress; using a black list
79 sub copy_dir_filter_and_show_progress($$)
81 my ($source_dir, $target_dir) = @_;
83 print "Copying \"$source_dir\" -> \"$target_dir\"...";
84 # copy sources from git and show progress
85 system ("cd $source_dir && " .
86 "git archive --format=tar HEAD | " .
87 " tar -xf - -C $target_dir --checkpoint=500 --checkpoint-action=exec=\"echo -n .\"") &&
88 die "Error: copying failed: $!\n";
89 print "\n";
92 # copy files to temp dir; showing a progress; using a black list
93 sub remove_empty_submodules($)
95 my ($target_topdir) = @_;
97 foreach my $submodule (sort values %module_dirname) {
98 next unless ($submodule);
99 print "Removing empty submodule: $submodule...\n";
100 rmdir "$target_topdir/$submodule" || die "Error: Can't remove submodule directory: $target_topdir/$submodule";
104 # copy the source directory into a tmp directory
105 # omit the .git subdirectories
106 sub copy_lo_module_to_tempdir($$$)
108 my ($source_dir, $module, $lo_topdir_name) = @_;
109 my $tempdir = tempdir( 'libreoffice-XXXXXX', DIR => File::Spec->tmpdir );
111 mkdir "$tempdir/$lo_topdir_name" || die "Can't create directory \"$tempdir/$lo_topdir_name\": $!\n";
112 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});
114 copy_dir_filter_and_show_progress("$source_dir/$module_dirname{$module}", "$tempdir/$lo_topdir_name/$module_dirname{$module}");
115 remove_empty_submodules("$tempdir/$lo_topdir_name/") if ($module eq "core");
117 return $tempdir;
120 sub generate_lo_module_changelog($$$)
122 my ($source_dir, $lo_module_release_topdir, $module) = @_;
124 my $log_name = "ChangeLog";
125 $log_name .= "-$module_dirname{$module}" if ($module_dirname{$module});
126 print "Generating changelog for $module...\n";
127 # print "1:$lo_module_clone, 2:$lo_module_release_dir, 3:$module\n";
128 system ("cd $source_dir/$module_dirname{$module} && " .
129 "git log --date=short --pretty='format:@%cd %an <%ae> [%H]%n%n%s%n%n%e%b' | " .
130 " sed -e 's|^\([^@]\)|\t\1|' -e 's|^@||' >$lo_module_release_topdir/$log_name" ) &&
131 die "Error: generating failed: $!\n";
134 sub run_autogen($$)
136 my ($dir, $module) = @_;
138 print "Running autogen for $module...\n";
139 system ("cd $dir && " .
140 "NOCONFIGURE=1 ./autogen.sh && " .
141 "rm -rf autom4te.cache && " .
142 "cd - >/dev/null 2>&1") && die "Error: autogen failed: $!\n";
145 sub generate_sources_version_file($$)
147 my ($dir, $release_version) = @_;
149 open (VERFILE, ">$dir/sources.ver") || die "Can't open $dir/sources.ver: $!\n";
151 print VERFILE "lo_sources_ver=$release_version\n";
153 close VERFILE;
156 sub generate_tarball($$$)
158 my ($dir, $tarball, $tar_compress_option) = @_;
160 print "Creating $tarball...";
161 # generate the tarball in the current directory; avoid "./" prefix in the stored paths; show progress
162 system ("tar -c $tar_compress_option -f $tarball -C $dir --checkpoint=500 --checkpoint-action=exec=\"echo -n .\" --transform=\"s|^\./||\" .") &&
163 die "Error: releasing failed: $!\n";
164 print "\n";
167 sub generate_md5($)
169 my ($filename) = @_;
171 print "Generating MD5...\n";
172 system ("md5sum $filename >$filename.md5") &&
173 die "Error: releasing failed: $!\n";
176 sub default_releases_state_file($)
178 my ($lo_core_dir) = @_;
180 my $rootdir = $lo_core_dir;
181 $rootdir =~ s/^(.*?)\/?[^\/]+\/?$/$1/;
183 my $releases_state_file;
184 if ($rootdir) {
185 $releases_state_file = "$rootdir/.releases";
186 } else {
187 $releases_state_file = ".releases";
190 return "$releases_state_file";
193 sub load_releases_state($)
195 my ($releases_state_file) = @_;
197 my $state_config_version;
198 my $state_release_version;
200 if (open (STATE, "$releases_state_file")) {
202 while (my $line = <STATE>) {
203 chomp $line;
205 if ($line =~ /^\s*configure_version\s*=\s*(.*)$/) {
206 $state_config_version = "$1";
207 } elsif ($line =~ /^\s*released_version\s*=\s*(.*)$/) {
208 $state_release_version = "$1";
211 close (STATE);
214 return $state_config_version, $state_release_version;
217 sub save_releases_state($$$)
219 my ($releases_state_file, $config_version, $release_version) = @_;
221 open (STATE, '>', "$releases_state_file") ||
222 die "Can't open \"$releases_state_file\" for writing: $!\n";
224 print STATE "configure_version = $config_version\n";
225 print STATE "released_version = $release_version\n";
227 close (STATE);
230 sub remove_tempdir($)
232 my ($tempdir) = @_;
234 # print "Cleaning $tempdir...\n";
235 system ("rm -rf $tempdir") && die "Error: rm failed: $!\n";
238 sub check_if_file_exists($$)
240 my ($file, $force) = @_;
242 if (-e $file) {
243 if (defined $force) {
244 print "Warning: $file already exists and will be replaced!\n";
245 } else {
246 die "Error: $file alrady exists.\n".
247 " Use --force if you want to replace it.\n";
252 sub check_if_already_released($$$$$$)
254 my ($p_module_tarball_name, $force, $bzip2, $xz, $pack_lo_core, $pack_lo_modules) = @_;
256 foreach my $tarball_name ( sort values %{$p_module_tarball_name} ) {
257 check_if_file_exists("$tarball_name.tar.bz2", $force) if (defined $bzip2);
258 check_if_file_exists("$tarball_name.tar.xz", $force) if (defined $xz);
262 sub prepare_module_sources($$$$)
264 my ($source_dir, $release_version, $module, $lo_topdir_name) = @_;
266 # prepare sources
267 my $temp_dir = copy_lo_module_to_tempdir($source_dir, $module, $lo_topdir_name);
268 generate_lo_module_changelog($source_dir, "$temp_dir/$lo_topdir_name", $module);
269 run_autogen("$temp_dir/$lo_topdir_name", $module) if ($module eq 'core');
270 generate_sources_version_file("$temp_dir/$lo_topdir_name", $release_version) if ($module eq 'core');
272 return $temp_dir;
275 sub pack_module_sources($$$$)
277 my ($temp_dir, $md5, $tarball, $tar_compress_option) = @_;
279 generate_tarball($temp_dir, $tarball, $tar_compress_option);
280 generate_md5($tarball) if (defined $md5);
283 sub generate_module_tarball($$$$$$$$)
285 my ($source_dir, $release_version, $module, $md5, $bzip2, $xz, $lo_topdir_name, $module_tarball_name) = @_;
287 my $temp_dir = prepare_module_sources($source_dir, $release_version, $module, $lo_topdir_name);
288 pack_module_sources($temp_dir, $md5, "$module_tarball_name.tar.bz2", "--bzip2") if (defined $bzip2);
289 pack_module_sources($temp_dir, $md5, "$module_tarball_name.tar.xz", "--xz") if (defined $xz);
290 remove_tempdir($temp_dir);
294 sub generate_tarballs($$$$$$$$$)
296 my ($source_dir, $release_version, $md5, $bzip2, $xz, $lo_topdir_name, $p_module_tarball_name, $pack_lo_core, $pack_lo_modules) = @_;
298 foreach my $module (sort keys %{$p_module_tarball_name} ) {
299 print "\n--- Generating $module ---\n";
300 generate_module_tarball($source_dir, $release_version, $module, $md5, $bzip2, $xz, $lo_topdir_name, $p_module_tarball_name->{$module});
305 sub usage()
307 print "This tool helps to pack the libreoffice-build and module sources\n\n" .
309 "Usage:\n".
310 "\tlo-pack-sources [--help]\n" .
311 "\t [--force] [--md5] [--bzip2] [--xz]\n" .
312 "\t [--version][--set-version=<ver>] [--inc-version]\n" .
313 "\t [--no-submodule] [--module=<module>]\n" .
314 "\t [dir]\n\n" .
316 "Options:\n\n" .
317 "\t--help: print this help\n" .
318 "\t--force: replace an already existing release of the same version\n" .
319 "\t--md5: generate md5 sum for the final tarball\n" .
320 "\t--bzip2: generate tarballs compressed by bzip2\n" .
321 "\t--xz: generate tarballs compressed by xz (default)\n" .
322 "\t--version: just print version of the released package but do not\n" .
323 "\t\trelease it; the version is affected by the other options, e.g.\n" .
324 "\t\t--inc-version\n" .
325 "\t--set-version: force another version\n" .
326 "\t--inc-version: increment the latest version; there is a difference\n" .
327 "\t\tbetween test release (default) and final (not yet supported)\n" .
328 "\t--no-submodule: do not pack sources from git submodules\n" .
329 "\t--module=<module>: pack just a single module, use \"core\"\n" .
330 "\t\tfor the main git repo,\n" .
331 "\tdir: path of the source directory, either libreoffice-build or module\n";
335 my $module;
336 my $ptf;
337 my $md5;
338 my $bzip2;
339 my $xz;
340 my $inc_version;
341 my $config_version;
342 my $set_version;
343 my $get_config_version;
344 my $release_version;
345 my $pack_lo_core=1;
346 my $pack_lo_modules=1;
347 my $source_dir;
348 my $releases_state_file;
349 my $state_config_version;
350 my $state_release_version;
351 my $lo_core_tempdir;
352 my $force;
353 my $verbose=1;
354 my $lo_topdir_name;
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);