2004-05-21 07:46:13 -05:00
|
|
|
:
|
|
|
|
eval 'exec perl -wS $0 ${1+"$@"}'
|
|
|
|
if 0;
|
|
|
|
#*************************************************************************
|
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# OpenOffice.org - a multi-platform office productivity suite
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# $RCSfile: packimages.pl,v $
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# $Revision: 1.12 $
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# last change: $Author: rt $ $Date: 2005-09-07 22:12:42 $
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# The Contents of this file are made available subject to
|
|
|
|
# the terms of GNU Lesser General Public License Version 2.1.
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# GNU Lesser General Public License Version 2.1
|
|
|
|
# =============================================
|
|
|
|
# Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
# 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License version 2.1, as published by the Free Software Foundation.
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
2005-09-07 16:12:43 -05:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
# MA 02111-1307 USA
|
2004-05-21 07:46:13 -05:00
|
|
|
#
|
|
|
|
#*************************************************************************
|
|
|
|
|
|
|
|
#
|
|
|
|
# packimages.pl - pack images into archives
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Getopt::Long;
|
2004-11-19 04:41:35 -06:00
|
|
|
use File::Find;
|
2004-05-21 07:46:13 -05:00
|
|
|
use File::Basename;
|
|
|
|
use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
|
|
|
|
|
|
|
|
#### globals ####
|
|
|
|
|
2004-11-19 04:41:35 -06:00
|
|
|
my $img_global = '%GLOBALRES%'; # 'global' image prefix
|
2004-05-21 07:46:13 -05:00
|
|
|
my $img_module = '%MODULE%'; # 'module' image prefix
|
|
|
|
|
|
|
|
my $out_file; # path to output archive
|
2004-09-20 02:35:32 -05:00
|
|
|
my $tmp_out_file; # path to temporary output file
|
2004-05-21 07:46:13 -05:00
|
|
|
my $global_path; # path to global images directory
|
|
|
|
my $module_path; # path to module images directory
|
2005-01-18 07:34:45 -06:00
|
|
|
my @custom_path; # path to custom images directory
|
2004-11-19 04:41:35 -06:00
|
|
|
my @imagelist_path; # pathes to directories containing the image lists
|
2004-05-21 07:46:13 -05:00
|
|
|
my $verbose; # be verbose
|
|
|
|
my $extra_verbose; # be extra verbose
|
2004-09-20 02:35:32 -05:00
|
|
|
my $do_rebuild = 0; # is rebuilding zipfile required?
|
2004-05-21 07:46:13 -05:00
|
|
|
|
2004-11-19 04:41:35 -06:00
|
|
|
my @custom_list;
|
2004-05-21 07:46:13 -05:00
|
|
|
#### script id #####
|
|
|
|
|
|
|
|
( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
|
|
|
|
|
|
|
|
my $script_rev;
|
2005-09-07 16:12:43 -05:00
|
|
|
my $id_str = ' $Revision: 1.12 $ ';
|
2004-05-21 07:46:13 -05:00
|
|
|
$id_str =~ /Revision:\s+(\S+)\s+\$/
|
|
|
|
? ($script_rev = $1) : ($script_rev = "-");
|
|
|
|
|
|
|
|
print "$script_name -- version: $script_rev\n";
|
|
|
|
|
|
|
|
#### main #####
|
|
|
|
|
|
|
|
parse_options();
|
|
|
|
my $image_lists_ref = get_image_lists();
|
2004-09-20 02:35:32 -05:00
|
|
|
my %image_lists_hash;
|
|
|
|
foreach ( @{$image_lists_ref} ) {
|
|
|
|
$image_lists_hash{$_}="";
|
|
|
|
}
|
|
|
|
$do_rebuild = is_file_newer(\%image_lists_hash) if $do_rebuild == 0;
|
2004-05-21 07:46:13 -05:00
|
|
|
my ($global_hash_ref, $module_hash_ref, $custom_hash_ref) = iterate_image_lists($image_lists_ref);
|
2004-11-19 04:41:35 -06:00
|
|
|
# custom_hash filled from filesystem lookup
|
|
|
|
find_custom($custom_hash_ref);
|
2004-05-21 07:46:13 -05:00
|
|
|
my $zip_hash_ref = create_zip_list($global_hash_ref, $module_hash_ref, $custom_hash_ref);
|
2004-09-20 02:35:32 -05:00
|
|
|
$do_rebuild = is_file_newer($zip_hash_ref) if $do_rebuild == 0;
|
|
|
|
if ( $do_rebuild == 1 ) {
|
|
|
|
create_zip_archive($zip_hash_ref);
|
|
|
|
replace_file($tmp_out_file, $out_file);
|
|
|
|
print_message("packing $out_file finished.");
|
|
|
|
} else {
|
|
|
|
print_message("$out_file up to date. nothing to do.");
|
|
|
|
}
|
2004-05-21 07:46:13 -05:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
#### subroutines ####
|
|
|
|
|
|
|
|
sub parse_options
|
|
|
|
{
|
|
|
|
my $opt_help;
|
|
|
|
my $p = Getopt::Long::Parser->new();
|
|
|
|
my $success =$p->getoptions(
|
|
|
|
'-h' => \$opt_help,
|
|
|
|
'-o=s' => \$out_file,
|
|
|
|
'-g=s' => \$global_path,
|
|
|
|
'-m=s' => \$module_path,
|
2005-01-18 07:34:45 -06:00
|
|
|
'-c=s' => \@custom_path,
|
2004-11-19 04:41:35 -06:00
|
|
|
'-l=s' => \@imagelist_path,
|
2004-05-21 07:46:13 -05:00
|
|
|
'-v' => \$verbose,
|
|
|
|
'-vv' => \$extra_verbose
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $opt_help || !$success || !$out_file || !$global_path
|
2005-01-18 07:34:45 -06:00
|
|
|
|| !$module_path || !@custom_path || !@imagelist_path )
|
2004-05-21 07:46:13 -05:00
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-09-20 02:35:32 -05:00
|
|
|
#define intermediate output file
|
|
|
|
$tmp_out_file="$out_file"."$$".$ENV{INPATH};
|
2004-05-21 07:46:13 -05:00
|
|
|
# Sanity checks.
|
|
|
|
|
|
|
|
# Check if out_file can be written.
|
|
|
|
my $out_dir = dirname($out_file);
|
|
|
|
print_error("no such directory: '$out_dir'", 2) if ! -d $out_dir;
|
|
|
|
print_error("can't search directory: '$out_dir'", 2) if ! -x $out_dir;
|
|
|
|
print_error("directory is not writable: '$out_dir'", 2) if ! -w $out_dir;
|
|
|
|
|
|
|
|
# Check paths.
|
2005-01-18 07:34:45 -06:00
|
|
|
foreach ($global_path, $module_path, @custom_path, @imagelist_path) {
|
2004-05-21 07:46:13 -05:00
|
|
|
print_error("no such directory: '$_'", 2) if ! -d $_;
|
|
|
|
print_error("can't search directory: '$_'", 2) if ! -x $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_image_lists
|
|
|
|
{
|
2004-11-19 04:41:35 -06:00
|
|
|
my @image_lists;
|
2005-01-28 09:05:32 -06:00
|
|
|
my $glob_imagelist_path;
|
|
|
|
|
2004-11-19 04:41:35 -06:00
|
|
|
foreach ( @imagelist_path ) {
|
2005-01-28 09:05:32 -06:00
|
|
|
$glob_imagelist_path = $_;
|
|
|
|
# cygwin perl
|
|
|
|
chomp( $glob_imagelist_path = qx{cygpath -u "$glob_imagelist_path"} ) if "$^O" eq "cygwin";
|
|
|
|
push @image_lists, glob("$glob_imagelist_path/*.ilst");
|
2004-11-19 04:41:35 -06:00
|
|
|
}
|
2004-05-21 07:46:13 -05:00
|
|
|
if ( !@image_lists ) {
|
2004-11-19 04:41:35 -06:00
|
|
|
print_error("can't find any image lists in '@imagelist_path'", 3);
|
2004-05-21 07:46:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return wantarray ? @image_lists : \@image_lists;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub iterate_image_lists
|
|
|
|
{
|
|
|
|
my $image_lists_ref = shift;
|
|
|
|
|
|
|
|
my %global_hash;
|
|
|
|
my %module_hash;
|
|
|
|
my %custom_hash;
|
|
|
|
|
2004-11-19 04:41:35 -06:00
|
|
|
foreach my $i ( @{$image_lists_ref} ) {
|
|
|
|
parse_image_list($i, \%global_hash, \%module_hash, \%custom_hash);
|
2004-05-21 07:46:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (\%global_hash, \%module_hash, \%custom_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_image_list
|
|
|
|
{
|
|
|
|
my $image_list = shift;
|
|
|
|
my $global_hash_ref = shift;
|
|
|
|
my $module_hash_ref = shift;
|
|
|
|
my $custom_hash_ref = shift;
|
|
|
|
|
|
|
|
print_message("parsing '$image_list' ...") if $verbose;
|
|
|
|
my $linecount = 0;
|
|
|
|
open(IMAGE_LIST, "< $image_list");
|
|
|
|
while ( <IMAGE_LIST> ) {
|
|
|
|
$linecount++;
|
|
|
|
next if /^\s*#/;
|
|
|
|
next if /^\s*$/;
|
|
|
|
# clean up trailing whitespace
|
|
|
|
tr/\r\n//d;
|
|
|
|
s/\s+$//;
|
|
|
|
# clean up backslashes and double slashes
|
|
|
|
tr{\\}{/}s;
|
|
|
|
tr{/}{}s;
|
2004-11-19 04:41:35 -06:00
|
|
|
# hack "res" back into globals
|
2004-05-21 07:46:13 -05:00
|
|
|
if ( /^\Q$img_global\E\/(.*)$/o ) {
|
2004-11-19 04:41:35 -06:00
|
|
|
$global_hash_ref->{"res/".$1}++;
|
2004-05-21 07:46:13 -05:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( /^\Q$img_module\E\/(.*)$/o ) {
|
|
|
|
$module_hash_ref->{$1}++;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# parse failed if we reach this point, bail out
|
|
|
|
close(IMAGE_LIST);
|
|
|
|
print_error("can't parse line $linecount from file '$image_list'", 4);
|
|
|
|
}
|
|
|
|
close(IMAGE_LIST);
|
|
|
|
|
|
|
|
return ($global_hash_ref, $module_hash_ref, $custom_hash_ref);
|
|
|
|
}
|
|
|
|
|
2004-11-19 04:41:35 -06:00
|
|
|
sub find_custom
|
|
|
|
{
|
|
|
|
my $custom_hash_ref = shift;
|
|
|
|
my $keep_back;
|
2005-01-18 07:34:45 -06:00
|
|
|
for my $path (@custom_path) {
|
|
|
|
find({ wanted => \&wanted, no_chdir => 0 }, $path);
|
2004-11-19 04:41:35 -06:00
|
|
|
foreach ( @custom_list ) {
|
2005-02-25 02:47:13 -06:00
|
|
|
if ( /^\Q$path\E\/(.*)$/ ) {
|
2005-01-18 07:34:45 -06:00
|
|
|
$keep_back=$1;
|
|
|
|
if (!defined $custom_hash_ref->{$keep_back}) {
|
|
|
|
$custom_hash_ref->{$keep_back} = $path;
|
2004-11-19 04:41:35 -06:00
|
|
|
}
|
2005-01-18 07:34:45 -06:00
|
|
|
}
|
|
|
|
}
|
2004-11-19 04:41:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub wanted
|
|
|
|
{
|
|
|
|
my $file = $_;
|
|
|
|
|
|
|
|
if ( $file =~ /.*\.png$/ && -f $file ) {
|
|
|
|
push @custom_list, $File::Find::name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-21 07:46:13 -05:00
|
|
|
sub create_zip_list
|
|
|
|
{
|
|
|
|
my $global_hash_ref = shift;
|
|
|
|
my $module_hash_ref = shift;
|
|
|
|
my $custom_hash_ref = shift;
|
|
|
|
|
|
|
|
my %zip_hash;
|
|
|
|
my @warn_list;
|
|
|
|
|
|
|
|
print_message("assemble image list ...") if $verbose;
|
|
|
|
foreach ( keys %{$global_hash_ref} ) {
|
|
|
|
# check if in 'global' and in 'module' list and add to warn list
|
|
|
|
if ( exists $module_hash_ref->{$_} ) {
|
|
|
|
push(@warn_list, $_);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( exists $custom_hash_ref->{$_} ) {
|
2005-01-18 07:34:45 -06:00
|
|
|
$zip_hash{$_} = $custom_hash_ref->{$_};
|
2004-05-21 07:46:13 -05:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
# it's neither in 'module' nor 'custom', record it in zip hash
|
|
|
|
$zip_hash{$_} = $global_path;
|
|
|
|
}
|
|
|
|
foreach ( keys %{$module_hash_ref} ) {
|
|
|
|
if ( exists $custom_hash_ref->{$_} ) {
|
2005-01-18 07:34:45 -06:00
|
|
|
$zip_hash{$_} = $custom_hash_ref->{$_};
|
2004-05-21 07:46:13 -05:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
# it's not in 'custom', record it in zip hash
|
|
|
|
$zip_hash{$_} = $module_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( @warn_list ) {
|
|
|
|
foreach ( @warn_list ) {
|
|
|
|
print_warning("$_ is duplicated in 'global' and 'module' list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return \%zip_hash
|
|
|
|
}
|
|
|
|
|
2004-09-20 02:35:32 -05:00
|
|
|
sub is_file_newer
|
|
|
|
{
|
|
|
|
my $test_hash_ref = shift;
|
|
|
|
my $reference_stamp = 0;
|
|
|
|
|
|
|
|
print_message("checking timestamps ...") if $verbose;
|
|
|
|
if ( -e $out_file ) {
|
|
|
|
$reference_stamp = (stat($out_file))[9];
|
|
|
|
print_message("found $out_file with $reference_stamp ...") if $verbose;
|
|
|
|
}
|
|
|
|
return 1 if $reference_stamp == 0;
|
|
|
|
|
|
|
|
foreach ( sort keys %{$test_hash_ref} ) {
|
2004-10-01 10:01:16 -05:00
|
|
|
my $path = $test_hash_ref->{$_};
|
|
|
|
$path .= "/" if "$path" ne "";
|
|
|
|
$path .= "$_";
|
2004-09-20 02:35:32 -05:00
|
|
|
print_message("checking '$path' ...") if $extra_verbose;
|
|
|
|
my $mtime = (stat($path))[9];
|
|
|
|
return 1 if $reference_stamp < $mtime;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-21 07:46:13 -05:00
|
|
|
sub create_zip_archive
|
|
|
|
{
|
|
|
|
my $zip_hash_ref = shift;
|
|
|
|
|
|
|
|
print_message("creating image archive ...") if $verbose;
|
|
|
|
my $zip = Archive::Zip->new();
|
|
|
|
|
|
|
|
foreach ( sort keys %{$zip_hash_ref} ) {
|
|
|
|
my $path = $zip_hash_ref->{$_} . "/$_";
|
|
|
|
print_message("zipping '$path' ...") if $extra_verbose;
|
|
|
|
if ( !$zip->addFile($path, $_) ) {
|
|
|
|
print_error("can't add file '$path' to image zip archive: $!", 5);
|
|
|
|
}
|
|
|
|
}
|
2004-09-20 02:35:32 -05:00
|
|
|
my $status = $zip->writeToFileNamed($tmp_out_file);
|
2004-05-21 07:46:13 -05:00
|
|
|
if ( $status != AZ_OK ) {
|
2004-09-20 02:35:32 -05:00
|
|
|
print_error("write image zip archive '$tmp_out_file' failed. Reason: $status", 6);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub replace_file
|
|
|
|
{
|
|
|
|
my $source_file = shift;
|
|
|
|
my $dest_file = shift;
|
2004-10-01 10:01:16 -05:00
|
|
|
my $result = 0;
|
2004-09-20 02:35:32 -05:00
|
|
|
|
2004-10-01 10:01:16 -05:00
|
|
|
$result = unlink($dest_file) if -f $dest_file;
|
2004-09-20 02:35:32 -05:00
|
|
|
if ( $result != 1 && -f $dest_file ) {
|
|
|
|
unlink $source_file;
|
|
|
|
print_error("couldn't remove '$dest_file'",1);
|
|
|
|
} else {
|
|
|
|
if ( !rename($source_file, $dest_file)) {
|
|
|
|
unlink $source_file;
|
|
|
|
print_error("couldn't rename '$source_file'",1);
|
|
|
|
}
|
2004-05-21 07:46:13 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub usage
|
|
|
|
{
|
|
|
|
print STDERR "Usage: packimages.pl [-h] -o out_file -g g_path -m m_path -c c_path -l imagelist_path\n";
|
|
|
|
print STDERR "Creates archive of images\n";
|
|
|
|
print STDERR "Options:\n";
|
|
|
|
print STDERR " -h print this help\n";
|
|
|
|
print STDERR " -o out_file path to output archive\n";
|
|
|
|
print STDERR " -g g_path path to global images directory\n";
|
|
|
|
print STDERR " -m m_path path to module images directory\n";
|
|
|
|
print STDERR " -c c_path path to custom images directory\n";
|
2004-11-19 04:41:35 -06:00
|
|
|
print STDERR " -l imagelist_path path to directory containing image lists (may appear mutiple times)\n";
|
2004-05-21 07:46:13 -05:00
|
|
|
print STDERR " -v verbose\n";
|
|
|
|
print STDERR " -vv very verbose\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_message
|
|
|
|
{
|
|
|
|
my $message = shift;
|
|
|
|
|
|
|
|
print "$script_name: ";
|
|
|
|
print "$message\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_warning
|
|
|
|
{
|
|
|
|
my $message = shift;
|
|
|
|
|
|
|
|
print STDERR "$script_name: ";
|
|
|
|
print STDERR "WARNING $message\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_error
|
|
|
|
{
|
|
|
|
my $message = shift;
|
|
|
|
my $error_code = shift;
|
|
|
|
|
|
|
|
print STDERR "$script_name: ";
|
|
|
|
print STDERR "ERROR: $message\n";
|
|
|
|
|
|
|
|
if ( $error_code ) {
|
|
|
|
print STDERR "\nFAILURE: $script_name aborted.\n";
|
|
|
|
exit($error_code);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|