2 eval 'exec perl -wS $0 ${1+"$@"}'
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: packimages.pl,v $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
36 # packimages.pl - pack images into archives
43 use Archive
::Zip
qw(:ERROR_CODES :CONSTANTS);
47 my $img_global = '%GLOBALRES%'; # 'global' image prefix
48 my $img_module = '%MODULE%'; # 'module' image prefix
50 my $out_file; # path to output archive
51 my $tmp_out_file; # path to temporary output file
52 my $global_path; # path to global images directory
53 my $module_path; # path to module images directory
54 my $sort_file; # path to file containing sorting data
55 my @custom_path; # path to custom images directory
56 my @imagelist_path; # pathes to directories containing the image lists
57 my $verbose; # be verbose
58 my $extra_verbose; # be extra verbose
59 my $do_rebuild = 0; # is rebuilding zipfile required?
64 ( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
67 my $id_str = ' $Revision: 1.17 $ ';
68 $id_str =~ /Revision:\s+(\S+)\s+\$/
69 ?
($script_rev = $1) : ($script_rev = "-");
71 print "$script_name -- version: $script_rev\n";
76 my $image_lists_ref = get_image_lists
();
78 foreach ( @
{$image_lists_ref} ) {
79 $image_lists_hash{$_}="";
81 $do_rebuild = is_file_newer
(\
%image_lists_hash) if $do_rebuild == 0;
82 my ($global_hash_ref, $module_hash_ref, $custom_hash_ref) = iterate_image_lists
($image_lists_ref);
83 # custom_hash filled from filesystem lookup
84 find_custom
($custom_hash_ref);
85 my $zip_hash_ref = create_zip_list
($global_hash_ref, $module_hash_ref, $custom_hash_ref);
86 $do_rebuild = is_file_newer
($zip_hash_ref) if $do_rebuild == 0;
87 if ( $do_rebuild == 1 ) {
88 create_zip_archive
($zip_hash_ref);
89 replace_file
($tmp_out_file, $out_file);
90 print_message
("packing $out_file finished.");
92 print_message
("$out_file up to date. nothing to do.");
102 my $p = Getopt
::Long
::Parser
->new();
103 my @custom_path_list;
104 my $custom_path_extended;
105 my $success =$p->getoptions(
107 '-o=s' => \
$out_file,
108 '-g=s' => \
$global_path,
109 '-s=s' => \
$sort_file,
110 '-m=s' => \
$module_path,
111 '-c=s' => \
@custom_path_list,
112 '-e=s' => \
$custom_path_extended,
113 '-l=s' => \
@imagelist_path,
115 '-vv' => \
$extra_verbose
117 push @custom_path_list, $custom_path_extended if ($custom_path_extended);
118 if ( $opt_help || !$success || !$out_file || !$global_path
119 || !$module_path || !@custom_path_list || !@imagelist_path )
124 #define intermediate output file
125 $tmp_out_file="$out_file"."$$".$ENV{INPATH
};
128 # Check if out_file can be written.
129 my $out_dir = dirname
($out_file);
132 foreach ($out_dir, $global_path, $module_path, @imagelist_path) {
133 print_error
("no such directory: '$_'", 2) if ! -d
$_;
134 print_error
("can't search directory: '$_'", 2) if ! -x
$_;
136 print_error
("directory is not writable: '$out_dir'", 2) if ! -w
$out_dir;
138 # Use just the working paths
140 foreach (@custom_path_list) {
142 print_warning
("skipping non-existing directory: '$_'", 2);
145 print_error
("can't search directory: '$_'", 2);
148 push @custom_path, $_;
156 my $glob_imagelist_path;
158 foreach ( @imagelist_path ) {
159 $glob_imagelist_path = $_;
161 chomp( $glob_imagelist_path = qx{cygpath
-u
"$glob_imagelist_path"} ) if "$^O" eq "cygwin";
162 push @image_lists, glob("$glob_imagelist_path/*.ilst");
164 if ( !@image_lists ) {
165 print_error
("can't find any image lists in '@imagelist_path'", 3);
168 return wantarray ?
@image_lists : \
@image_lists;
171 sub iterate_image_lists
173 my $image_lists_ref = shift;
179 foreach my $i ( @
{$image_lists_ref} ) {
180 parse_image_list
($i, \
%global_hash, \
%module_hash, \
%custom_hash);
183 return (\
%global_hash, \
%module_hash, \
%custom_hash);
188 my $image_list = shift;
189 my $global_hash_ref = shift;
190 my $module_hash_ref = shift;
191 my $custom_hash_ref = shift;
193 print_message
("parsing '$image_list' ...") if $verbose;
195 open(IMAGE_LIST
, "< $image_list") or die "ERROR: can't open $image_list: $!";
196 while ( <IMAGE_LIST
> ) {
200 # clean up trailing whitespace
203 # clean up backslashes and double slashes
206 # hack "res" back into globals
207 if ( /^\Q$img_global\E\/(.*)$/o
) {
208 $global_hash_ref->{"res/".$1}++;
211 if ( /^\Q$img_module\E\/(.*)$/o
) {
212 $module_hash_ref->{$1}++;
215 # parse failed if we reach this point, bail out
217 print_error
("can't parse line $linecount from file '$image_list'", 4);
221 return ($global_hash_ref, $module_hash_ref, $custom_hash_ref);
226 my $custom_hash_ref = shift;
228 for my $path (@custom_path) {
229 find
({ wanted
=> \
&wanted
, no_chdir
=> 0 }, $path);
230 foreach ( @custom_list ) {
231 if ( /^\Q$path\E\/(.*)$/ ) {
233 if (!defined $custom_hash_ref->{$keep_back}) {
234 $custom_hash_ref->{$keep_back} = $path;
245 if ( $file =~ /.*\.png$/ && -f
$file ) {
246 push @custom_list, $File::Find
::name
;
252 my $global_hash_ref = shift;
253 my $module_hash_ref = shift;
254 my $custom_hash_ref = shift;
259 print_message
("assemble image list ...") if $verbose;
260 foreach ( keys %{$global_hash_ref} ) {
261 # check if in 'global' and in 'module' list and add to warn list
262 if ( exists $module_hash_ref->{$_} ) {
263 push(@warn_list, $_);
266 if ( exists $custom_hash_ref->{$_} ) {
267 $zip_hash{$_} = $custom_hash_ref->{$_};
270 # it's neither in 'module' nor 'custom', record it in zip hash
271 $zip_hash{$_} = $global_path;
273 foreach ( keys %{$module_hash_ref} ) {
274 if ( exists $custom_hash_ref->{$_} ) {
275 $zip_hash{$_} = $custom_hash_ref->{$_};
278 # it's not in 'custom', record it in zip hash
279 $zip_hash{$_} = $module_path;
283 foreach ( @warn_list ) {
284 print_warning
("$_ is duplicated in 'global' and 'module' list");
293 my $test_hash_ref = shift;
294 my $reference_stamp = 0;
296 print_message
("checking timestamps ...") if $verbose;
297 if ( -e
$out_file ) {
298 $reference_stamp = (stat($out_file))[9];
299 print_message
("found $out_file with $reference_stamp ...") if $verbose;
301 return 1 if $reference_stamp == 0;
303 foreach ( sort keys %{$test_hash_ref} ) {
304 my $path = $test_hash_ref->{$_};
305 $path .= "/" if "$path" ne "";
307 print_message
("checking '$path' ...") if $extra_verbose;
308 my $mtime = (stat($path))[9];
309 return 1 if $reference_stamp < $mtime;
314 sub optimize_zip_layout
($)
316 my $zip_hash_ref = shift;
318 if (!defined $sort_file) {
319 print_message
("no sort file - sorting alphabetically ...") if $verbose;
320 return sort keys %{$zip_hash_ref};
322 print_message
("sorting from $sort_file ...") if $verbose;
327 open ($orderh, $sort_file) || die "Can't open $sort_file: $!";
329 /^\#.*/ && next; # comments
333 if (!defined $zip_hash_ref->{$file}) {
334 print "unknown file '$file'\n" if ($extra_verbose);
337 $included{$file} = 1;
342 for my $img (sort keys %{$zip_hash_ref}) {
343 push @sorted, $img if (!$included{$img});
346 print_message
("done sort ...") if $verbose;
351 sub create_zip_archive
353 my $zip_hash_ref = shift;
355 print_message
("creating image archive ...") if $verbose;
356 my $zip = Archive
::Zip
->new();
358 # FIXME: test - $member = addfile ... $member->desiredCompressionMethod( COMPRESSION_STORED );
359 # any measurable performance win/loss ?
360 foreach ( optimize_zip_layout
($zip_hash_ref) ) {
361 my $path = $zip_hash_ref->{$_} . "/$_";
362 print_message
("zipping '$path' ...") if $extra_verbose;
363 my $member = $zip->addFile($path, $_);
365 print_error
("can't add file '$path' to image zip archive: $!", 5);
368 my $status = $zip->writeToFileNamed($tmp_out_file);
369 if ( $status != AZ_OK
) {
370 print_error
("write image zip archive '$tmp_out_file' failed. Reason: $status", 6);
377 my $source_file = shift;
378 my $dest_file = shift;
381 $result = unlink($dest_file) if -f
$dest_file;
382 if ( $result != 1 && -f
$dest_file ) {
384 print_error
("couldn't remove '$dest_file'",1);
386 if ( !rename($source_file, $dest_file)) {
388 print_error
("couldn't rename '$source_file'",1);
396 print STDERR
"Usage: packimages.pl [-h] -o out_file -g g_path -m m_path -c c_path -l imagelist_path\n";
397 print STDERR
"Creates archive of images\n";
398 print STDERR
"Options:\n";
399 print STDERR
" -h print this help\n";
400 print STDERR
" -o out_file path to output archive\n";
401 print STDERR
" -g g_path path to global images directory\n";
402 print STDERR
" -m m_path path to module images directory\n";
403 print STDERR
" -c c_path path to custom images directory\n";
404 print STDERR
" -s sort_file path to image sort order file\n";
405 print STDERR
" -l imagelist_path path to directory containing image lists (may appear mutiple times)\n";
406 print STDERR
" -v verbose\n";
407 print STDERR
" -vv very verbose\n";
414 print "$script_name: ";
423 print STDERR
"$script_name: ";
424 print STDERR
"WARNING $message\n";
431 my $error_code = shift;
433 print STDERR
"$script_name: ";
434 print STDERR
"ERROR: $message\n";
437 print STDERR
"\nFAILURE: $script_name aborted.\n";