Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / modules / installer / download.pm
blob37a9123fdf35579887cb6787ee286a236c6e1c97
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package installer::download;
21 use strict;
22 use warnings;
24 use File::Spec;
25 use installer::exiter;
26 use installer::files;
27 use installer::globals;
28 use installer::logger;
29 use installer::pathanalyzer;
30 use installer::remover;
31 use installer::systemactions;
33 BEGIN { # This is needed so that cygwin's perl evaluates ACLs
34 # (needed for correctly evaluating the -x test.)
35 if( $^O =~ /cygwin/i ) {
36 require filetest; import filetest "access";
40 ##################################################################
41 # Including the lowercase product name into the script template
42 ##################################################################
44 sub put_productname_into_script
46 my ($scriptfile, $variableshashref) = @_;
48 my $productname = $variableshashref->{'PRODUCTNAME'};
49 $productname = lc($productname);
50 $productname =~ s/\.//g; # openoffice.org -> openofficeorg
51 $productname =~ s/\s*//g;
53 my $infoline = "Adding productname $productname into download shell script\n";
54 push( @installer::globals::logfileinfo, $infoline);
56 for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
58 ${$scriptfile}[$i] =~ s/PRODUCTNAMEPLACEHOLDER/$productname/;
62 #########################################################
63 # Including the linenumber into the script template
64 #########################################################
66 sub put_linenumber_into_script
68 my ( $scriptfile ) = @_;
70 my $linenumber = $#{$scriptfile} + 2;
72 my $infoline = "Adding linenumber $linenumber into download shell script\n";
73 push( @installer::globals::logfileinfo, $infoline);
75 for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
77 ${$scriptfile}[$i] =~ s/LINENUMBERPLACEHOLDER/$linenumber/;
81 #########################################################
82 # Determining the name of the new scriptfile
83 #########################################################
85 sub determine_scriptfile_name
87 my ( $filename ) = @_;
89 $installer::globals::downloadfileextension = ".sh";
90 $filename = $filename . $installer::globals::downloadfileextension;
91 $installer::globals::downloadfilename = $filename;
93 my $infoline = "Setting download shell script file name to $filename\n";
94 push( @installer::globals::logfileinfo, $infoline);
96 return $filename;
99 #########################################################
100 # Saving the script file in the installation directory
101 #########################################################
103 sub save_script_file
105 my ($directory, $newscriptfilename, $scriptfile) = @_;
107 $newscriptfilename = $directory . $installer::globals::separator . $newscriptfilename;
108 installer::files::save_file($newscriptfilename, $scriptfile);
110 my $infoline = "Saving script file $newscriptfilename\n";
111 push( @installer::globals::logfileinfo, $infoline);
113 if ( ! $installer::globals::iswindowsbuild )
115 chmod 0775, $newscriptfilename;
118 return $newscriptfilename;
121 #########################################################
122 # Including checksum and size into script file
123 #########################################################
125 sub put_checksum_and_size_into_script
127 my ($scriptfile, $sumout) = @_;
129 my $checksum = "";
130 my $size = "";
132 if ( $sumout =~ /^\s*(\d+)\s+(\d+)\s*$/ )
134 $checksum = $1;
135 $size = $2;
137 else
139 installer::exiter::exit_program("ERROR: Incorrect return value from /usr/bin/sum: $sumout", "put_checksum_and_size_into_script");
142 my $infoline = "Adding checksum $checksum and size $size into download shell script\n";
143 push( @installer::globals::logfileinfo, $infoline);
145 for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
147 ${$scriptfile}[$i] =~ s/CHECKSUMPLACEHOLDER/$checksum/;
148 ${$scriptfile}[$i] =~ s/DISCSPACEPLACEHOLDER/$size/;
153 #########################################################
154 # Determining checksum and size of tar file
155 #########################################################
157 sub call_sum
159 my ($filename) = @_;
161 my $systemcall = "/usr/bin/sum $filename |";
163 my $sumoutput = "";
165 open (SUM, "$systemcall");
166 $sumoutput = <SUM>;
167 close (SUM);
169 my $returnvalue = $?; # $? contains the return value of the systemcall
171 my $infoline = "Systemcall: $systemcall\n";
172 push( @installer::globals::logfileinfo, $infoline);
174 if ($returnvalue)
176 $infoline = "ERROR: Could not execute \"$systemcall\"!\n";
177 push( @installer::globals::logfileinfo, $infoline);
179 else
181 $infoline = "Success: Executed \"$systemcall\" successfully!\n";
182 push( @installer::globals::logfileinfo, $infoline);
185 $sumoutput =~ s/\s+$filename\s$//;
186 return $sumoutput;
189 #########################################################
190 # Searching for the getuid.so
191 #########################################################
193 sub get_path_for_library
195 my $getuidlibrary = $ENV{'WORKDIR'} . '/LinkTarget/Library/libgetuid.so';
196 if ( ! -e $getuidlibrary ) { installer::exiter::exit_program("File $getuidlibrary does not exist!", "get_path_for_library"); }
197 return $getuidlibrary;
200 #########################################################
201 # Include the tar file into the script
202 #########################################################
204 sub include_tar_into_script
206 my ($scriptfile, $temporary_tarfile) = @_;
208 my $systemcall = "cat $temporary_tarfile >> $scriptfile && rm $temporary_tarfile";
209 my $returnvalue = system($systemcall);
211 my $infoline = "Systemcall: $systemcall\n";
212 push( @installer::globals::logfileinfo, $infoline);
214 if ($returnvalue)
216 $infoline = "ERROR: Could not execute \"$systemcall\"!\n";
217 push( @installer::globals::logfileinfo, $infoline);
219 else
221 $infoline = "Success: Executed \"$systemcall\" successfully!\n";
222 push( @installer::globals::logfileinfo, $infoline);
224 return $returnvalue;
227 #########################################################
228 # Create a tar file from the binary package
229 #########################################################
231 sub tar_package
233 my ( $installdir, $tarfilename, $getuidlibrary) = @_;
235 my $ldpreloadstring = "";
236 if ( $getuidlibrary ne "" ) { $ldpreloadstring = "LD_PRELOAD=" . $getuidlibrary; }
238 my $systemcall = "cd $installdir; $ldpreloadstring tar -cf - * > $tarfilename";
240 my $returnvalue = system($systemcall);
242 my $infoline = "Systemcall: $systemcall\n";
243 push( @installer::globals::logfileinfo, $infoline);
245 if ($returnvalue)
247 $infoline = "ERROR: Could not execute \"$systemcall\"!\n";
248 push( @installer::globals::logfileinfo, $infoline);
250 else
252 $infoline = "Success: Executed \"$systemcall\" successfully!\n";
253 push( @installer::globals::logfileinfo, $infoline);
256 chmod 0775, $tarfilename;
258 return ( -s $tarfilename );
261 #########################################################
262 # Setting installation languages
263 #########################################################
265 sub get_downloadname_language
267 my ($languagestringref) = @_;
269 my $languages = $$languagestringref;
271 if ( $installer::globals::added_english )
273 $languages =~ s/en-US_//;
274 $languages =~ s/_en-US//;
277 # do not list languages if there are too many
278 if ( length ($languages) > $installer::globals::max_lang_length )
280 $languages = '';
283 # do not list pure en-US, except for helppack and langpack
284 if ( ( $languages eq "en-US" ) &&
285 ( ! $installer::globals::languagepack ) &&
286 ( ! $installer::globals::helppack ) )
288 $languages = '';
291 return $languages;
294 #########################################################
295 # Setting download name
296 #########################################################
298 sub get_downloadname_productname
300 my ($allvariables) = @_;
302 my $start = "";
304 if ( $allvariables->{'PRODUCTNAME'} eq "LibreOffice" ) { $start = "LibreOffice"; }
306 elsif ( $allvariables->{'PRODUCTNAME'} eq "LibreOfficeDev" ) { $start = "LibreOfficeDev"; }
308 elsif ( $allvariables->{'PRODUCTNAME'} eq "OxygenOffice" ) { $start = "OOOP"; }
310 elsif ( $allvariables->{'PRODUCTNAME'} eq "" ) { $start = "LibreOffice"; }
312 else { $start = $allvariables->{'PRODUCTNAME'}; }
314 return $start;
317 #########################################################
318 # Setting download version
319 #########################################################
321 sub get_download_version
323 my ($allvariables) = @_;
325 my $version = "";
327 $version = $allvariables->{'PRODUCTVERSION'};
328 if (( $allvariables->{'PRODUCTEXTENSION'} ) && ( $allvariables->{'PRODUCTEXTENSION'} ne "" )) { $version = $version . $allvariables->{'PRODUCTEXTENSION'}; }
330 return $version;
333 #################################################################
334 # Setting the platform name for download
335 #################################################################
337 sub get_download_platformname
339 my $platformname = "";
341 if ( $installer::globals::islinuxbuild )
343 $platformname = "Linux";
345 elsif ( $installer::globals::issolarisbuild )
347 $platformname = "Solaris";
349 elsif ( $installer::globals::iswindowsbuild )
351 $platformname = "Win";
353 elsif ( $installer::globals::isfreebsdbuild )
355 $platformname = "FreeBSD";
357 elsif ( $installer::globals::ismacbuild )
359 $platformname = "MacOS";
361 else
363 $platformname = $installer::globals::os;
366 return $platformname;
369 #########################################################
370 # Setting the architecture for the download name
371 #########################################################
373 sub get_download_architecture
375 my $arch = "";
377 if ( $installer::globals::issolarissparcbuild )
379 $arch = "Sparc";
381 elsif ( $installer::globals::issolarisx86build )
383 $arch = "x86";
385 elsif ( $installer::globals::iswindowsbuild )
387 if ( $installer::globals::iswin64build )
389 $arch = "x64";
391 else
393 $arch = "x86";
396 elsif ( $installer::globals::cpuname eq 'INTEL' )
398 $arch = "x86";
400 elsif ( $installer::globals::cpuname eq 'POWERPC' )
402 $arch = "PPC";
404 elsif ( $installer::globals::cpuname eq 'POWERPC64' )
406 $arch = "PPC";
408 elsif ( $installer::globals::cpuname eq 'X86_64' )
410 $arch = "x86-64";
413 return $arch;
416 #########################################################
417 # Setting the content type for the download name
418 #########################################################
420 sub get_download_content
422 my ($allvariables) = @_;
424 my $content = "";
426 # content type included in the installer
427 if ( $installer::globals::isrpmbuild )
429 $content = "rpm";
431 elsif ( $installer::globals::isdebbuild )
433 $content = "deb";
435 elsif ( $installer::globals::packageformat eq "archive" )
437 $content = "archive";
440 return $content;
443 #########################################################
444 # Setting the functionality type for the download name
445 #########################################################
447 sub get_download_functionality
449 my ($allvariables) = @_;
451 my $functionality = "";
453 if ( $installer::globals::languagepack )
455 $functionality = "langpack";
457 elsif ( $installer::globals::helppack )
459 $functionality = "helppack";
461 elsif ( $allvariables->{'POSTVERSIONEXTENSION'} eq "SDK" )
463 $functionality = "sdk";
465 elsif ( $allvariables->{'POSTVERSIONEXTENSION'} eq "TEST" )
467 $functionality = "test";
469 elsif ( $allvariables->{'PRODUCTNAME'} eq "URE" )
471 $functionality = "ure";
474 return $functionality;
477 ###############################################################################################
478 # Setting the download file name
479 # Syntax:
480 # (PRODUCTNAME)_(VERSION)_(OS)_(ARCH)_(INSTALLTYPE)_(LANGUAGE).(FILEEXTENSION)
481 ###############################################################################################
483 sub set_download_filename
485 my ($languagestringref, $allvariables) = @_;
487 my $start = get_downloadname_productname($allvariables);
488 my $versionstring = get_download_version($allvariables);
489 my $platform = get_download_platformname();
490 my $architecture = get_download_architecture();
491 my $content = get_download_content($allvariables);
492 my $functionality = get_download_functionality($allvariables);
493 my $language = get_downloadname_language($languagestringref);
495 # Setting the extension happens automatically
497 my $filename = $start . "_" . $versionstring . "_" . $platform . "_" . $architecture . "_" . $content . "_" . $functionality . "_" . $language;
499 # get rid of duplicit "_" delimiters when some strings are empty
500 $filename =~ s/\_\_\_/\_/g;
501 $filename =~ s/\_\_/\_/g;
502 $filename =~ s/\_\s*$//;
504 $installer::globals::ooodownloadfilename = $filename;
506 return $filename;
510 #########################################################
511 # Creating a tar.gz file
512 #########################################################
514 sub create_tar_gz_file_from_directory
516 my ($installdir, $getuidlibrary, $downloaddir, $downloadfilename) = @_;
518 my $infoline = "";
520 my $packdir = $installdir;
521 installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$packdir);
522 my $changedir = $installdir;
523 installer::pathanalyzer::get_path_from_fullqualifiedname(\$changedir);
525 my $ldpreloadstring = "";
526 if ( $getuidlibrary ne "" ) { $ldpreloadstring = "LD_PRELOAD=" . $getuidlibrary; }
528 $installer::globals::downloadfileextension = ".tar.gz";
529 $installer::globals::downloadfilename = $downloadfilename . $installer::globals::downloadfileextension;
530 my $targzname = $downloaddir . $installer::globals::separator . $installer::globals::downloadfilename;
532 # fdo#67060 - install script is for RPM only
533 if ( -e "$installdir/install" && !$installer::globals::isrpmbuild )
535 unlink("$installdir/install");
538 my $systemcall = "cd $changedir; $ldpreloadstring tar -cf - $packdir | gzip > $targzname";
540 my $returnvalue = system($systemcall);
542 $infoline = "Systemcall: $systemcall\n";
543 push( @installer::globals::logfileinfo, $infoline);
545 if ($returnvalue)
547 $infoline = "ERROR: Could not execute \"$systemcall\"!\n";
548 push( @installer::globals::logfileinfo, $infoline);
550 else
552 $infoline = "Success: Executed \"$systemcall\" successfully!\n";
553 push( @installer::globals::logfileinfo, $infoline);
556 return $targzname;
559 #########################################################
560 # Setting the variables in the download name
561 #########################################################
563 sub resolve_variables_in_downloadname
565 my ($allvariables, $downloadname, $languagestringref) = @_;
567 # Typical name: soa-{productversion}-{extension}-bin-{os}-{languages}
569 my $productversion = "";
570 if ( $allvariables->{'PRODUCTVERSION'} ) { $productversion = $allvariables->{'PRODUCTVERSION'}; }
571 $downloadname =~ s/\{productversion\}/$productversion/;
573 my $packageversion = "";
574 if ( $allvariables->{'PACKAGEVERSION'} ) { $packageversion = $allvariables->{'PACKAGEVERSION'}; }
575 $downloadname =~ s/\{packageversion\}/$packageversion/;
577 my $extension = "";
578 if ( $allvariables->{'PRODUCTEXTENSION'} ) { $extension = $allvariables->{'PRODUCTEXTENSION'}; }
579 $extension = lc($extension);
580 $downloadname =~ s/\{extension\}/$extension/;
582 my $os = "";
583 if ( $installer::globals::iswindowsbuild ) { $os = "windows"; }
584 elsif ( $installer::globals::issolarissparcbuild ) { $os = "solsparc"; }
585 elsif ( $installer::globals::issolarisx86build ) { $os = "solia"; }
586 elsif ( $installer::globals::islinuxbuild ) { $os = "linux"; }
587 elsif ( $installer::globals::platformid eq 'macosx_x86_64' ) { $os = "macosxx"; }
588 else { $os = ""; }
589 $downloadname =~ s/\{os\}/$os/;
591 my $languages = $$languagestringref;
592 $downloadname =~ s/\{languages\}/$languages/;
594 $downloadname =~ s/\-\-\-/\-/g;
595 $downloadname =~ s/\-\-/\-/g;
596 $downloadname =~ s/\-\s*$//;
598 return $downloadname;
601 ##############################################################
602 # Returning the complete block in all languages
603 # for a specified string
604 ##############################################################
606 sub get_language_block_from_language_file
608 my ($searchstring, $languagefile) = @_;
610 my @language_block = ();
612 for ( my $i = 0; $i <= $#{$languagefile}; $i++ )
614 if ( ${$languagefile}[$i] =~ /^\s*\[\s*$searchstring\s*\]\s*$/ )
616 my $counter = $i;
618 push(@language_block, ${$languagefile}[$counter]);
619 $counter++;
621 while (( $counter <= $#{$languagefile} ) && (!( ${$languagefile}[$counter] =~ /^\s*\[/ )))
623 push(@language_block, ${$languagefile}[$counter]);
624 $counter++;
627 last;
631 return \@language_block;
634 ##############################################################
635 # Returning a specific language string from the block
636 # of all translations
637 ##############################################################
639 sub get_language_string_from_language_block
641 my ($language_block, $language) = @_;
643 my $newstring = "";
645 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
647 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
649 $newstring = $1;
650 last;
654 if ( $newstring eq "" )
656 $language = "en-US"; # defaulting to english
658 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
660 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
662 $newstring = $1;
663 last;
668 return $newstring;
671 ####################################################
672 # Creating download installation sets
673 ####################################################
675 sub create_download_sets
677 my ($installationdir, $includepatharrayref, $allvariableshashref, $downloadname, $languagestringref, $languagesarrayref) = @_;
679 my $infoline = "";
681 my $force = 1; # print this message even in 'quiet' mode
682 installer::logger::print_message( "\n******************************************\n" );
683 installer::logger::print_message( "... creating download installation set ...\n", $force );
684 installer::logger::print_message( "******************************************\n" );
686 installer::logger::include_header_into_logfile("Creating download installation sets:");
688 my $firstdir = $installationdir;
689 installer::pathanalyzer::get_path_from_fullqualifiedname(\$firstdir);
691 my $lastdir = $installationdir;
692 installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$lastdir);
694 if ( $installer::globals::iswindowsbuild && $lastdir =~ /\./ ) { $lastdir =~ s/\./_download_inprogress\./ }
695 else { $lastdir = $lastdir . "_download_inprogress"; }
697 # removing existing directory "_native_packed_inprogress" and "_native_packed_witherror" and "_native_packed"
699 my $downloaddir = $firstdir . $lastdir;
701 if ( -d $downloaddir ) { installer::systemactions::remove_complete_directory($downloaddir); }
703 my $olddir = $downloaddir;
704 $olddir =~ s/_inprogress/_witherror/;
705 if ( -d $olddir ) { installer::systemactions::remove_complete_directory($olddir); }
707 $olddir = $downloaddir;
708 $olddir =~ s/_inprogress//;
709 if ( -d $olddir ) { installer::systemactions::remove_complete_directory($olddir); }
711 # creating the new directory
713 installer::systemactions::create_directory($downloaddir);
715 $installer::globals::saveinstalldir = $downloaddir;
717 # evaluating the name of the download file
719 if ( $allvariableshashref->{'OOODOWNLOADNAME'} ) { $downloadname = set_download_filename($languagestringref, $allvariableshashref); }
720 else { $downloadname = resolve_variables_in_downloadname($allvariableshashref, $downloadname, $languagestringref); }
722 if ( ! $installer::globals::iswindowsbuild ) # Unix specific part
725 # getting the path of the getuid.so (only required for Solaris and Linux)
726 my $getuidlibrary = "";
727 if (( $installer::globals::issolarisbuild ) || ( $installer::globals::islinuxbuild )) { $getuidlibrary = get_path_for_library(); }
729 if ( $allvariableshashref->{'OOODOWNLOADNAME'} )
731 my $downloadfile = create_tar_gz_file_from_directory($installationdir, $getuidlibrary, $downloaddir, $downloadname);
733 else
735 # find and read setup script template
736 my $scriptfilename = $ENV{'SRCDIR'} . "/setup_native/scripts/downloadscript.sh";
738 if (! -f $scriptfilename) { installer::exiter::exit_program("ERROR: Could not find script file $scriptfilename!", "create_download_sets"); }
739 my $scriptfile = installer::files::read_file($scriptfilename);
741 $infoline = "Found script file $scriptfilename \n";
742 push( @installer::globals::logfileinfo, $infoline);
744 # add product name into script template
745 put_productname_into_script($scriptfile, $allvariableshashref);
747 # replace linenumber in script template
748 put_linenumber_into_script($scriptfile);
750 # create tar file
751 my $temporary_tarfile_name = $downloaddir . $installer::globals::separator . 'installset.tar';
752 my $size = tar_package($installationdir, $temporary_tarfile_name, $getuidlibrary);
753 installer::exiter::exit_program("ERROR: Could not create tar file $temporary_tarfile_name!", "create_download_sets") unless $size;
755 # calling sum to determine checksum and size of the tar file
756 my $sumout = call_sum($temporary_tarfile_name);
758 # writing checksum and size into scriptfile
759 put_checksum_and_size_into_script($scriptfile, $sumout);
761 # saving the script file
762 my $newscriptfilename = determine_scriptfile_name($downloadname);
763 $newscriptfilename = save_script_file($downloaddir, $newscriptfilename, $scriptfile);
765 installer::logger::print_message( "... including installation set into $newscriptfilename ... \n" );
766 # Append tar file to script
767 include_tar_into_script($newscriptfilename, $temporary_tarfile_name);
771 return $downloaddir;