1999-09-08 03:30:45 +04:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Netscape Public
|
|
|
|
# License Version 1.1 (the "License"); you may not use this file
|
|
|
|
# except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at http://www.mozilla.org/NPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
# implied. See the License for the specific language governing
|
|
|
|
# rights and limitations under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is Mozilla Communicator client code, released
|
|
|
|
# March 31, 1998.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is Netscape
|
|
|
|
# Communications Corporation. Portions created by Netscape are
|
|
|
|
# Copyright (C) 1998-1999 Netscape Communications Corporation. All
|
|
|
|
# Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Jonathan Granrose (granrose@netscape.com)
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# pkgcp.pl -
|
|
|
|
#
|
|
|
|
# Parse a package file and copy the specified files for a component
|
|
|
|
# from the given source directory into the given destination directory
|
|
|
|
# for packaging by the install builder.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# load modules
|
1999-09-08 03:30:45 +04:00
|
|
|
use Cwd;
|
|
|
|
use File::Basename;
|
|
|
|
use File::Copy;
|
|
|
|
use File::Find;
|
|
|
|
use File::Path;
|
1999-09-09 04:25:25 +04:00
|
|
|
use Getopt::Long;
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# initialize variables
|
1999-09-08 03:30:45 +04:00
|
|
|
$saved_cwd = cwd();
|
|
|
|
$component = "";
|
|
|
|
$altdest = "";
|
|
|
|
$file = "";
|
1999-09-09 04:25:25 +04:00
|
|
|
$srcdir = "";
|
|
|
|
$destdir = "";
|
|
|
|
$package = "";
|
|
|
|
$os = "";
|
1999-09-08 03:30:45 +04:00
|
|
|
$lineno = 0;
|
|
|
|
$batch = 0;
|
1999-09-09 04:25:25 +04:00
|
|
|
$help = 0;
|
|
|
|
$debug = 0;
|
|
|
|
|
|
|
|
|
|
|
|
# get command line options
|
|
|
|
$return = GetOptions( "source|src|s=s", \$srcdir,
|
|
|
|
"destination|dest|d=s", \$destdir,
|
|
|
|
"file|f=s", \$package,
|
|
|
|
"os|o=s", \$os,
|
|
|
|
"help|h", \$help,
|
|
|
|
"debug=i", \$debug,
|
|
|
|
"<>", \&do_badargument
|
|
|
|
);
|
|
|
|
|
|
|
|
# set debug level
|
|
|
|
$debug = abs ($debug);
|
|
|
|
if ($debug) {
|
|
|
|
print "debug level: $debug\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# check Usage
|
|
|
|
if (! $return)
|
|
|
|
{
|
|
|
|
die "Error: couldn't parse command line options. See \'$0 --help' for options.\nExiting...\n";
|
|
|
|
} else {
|
|
|
|
check_arguments();
|
|
|
|
}
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# main loop
|
|
|
|
#
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
open (MANIFEST,"<$package") ||
|
|
|
|
die "Error: couldn't open file $package for reading: $!. Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
|
|
|
|
LINE: while (<MANIFEST>) {
|
|
|
|
$file = "";
|
|
|
|
$altdest = "";
|
|
|
|
$lineno++;
|
|
|
|
|
|
|
|
s/\;.*//; # it's a comment, kill it.
|
|
|
|
s/^\s+//; # nuke leading whitespace
|
|
|
|
s/\s+$//; # nuke trailing whitespace
|
|
|
|
s/\/$//; # strip any trailing '/'
|
|
|
|
|
|
|
|
|
|
|
|
# it's a blank line, skip it.
|
|
|
|
/^$/ && do {
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# it's a new component
|
|
|
|
/^\[/ && do {
|
|
|
|
$component = $_;
|
|
|
|
do_component ();
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# make sure a component is defined before doing any copies or deletes.
|
|
|
|
( $component eq "") &&
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: item $_ outside a component ($package, $lineno). Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
|
|
|
|
# delete the file or directory following the '-'
|
|
|
|
/^-/ && do {
|
|
|
|
s/^-//; # strip leading '-'
|
|
|
|
do_delete ("$destdir/$component/$_");
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# file/directory being copied to different target location
|
|
|
|
/\,/ && do {
|
|
|
|
/.*\,.*\,.*/ &&
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: multiple commas not allowed ($package, $lineno): $_.\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
($file, $altdest) = split (/\s*\,\s*/, $_, 2);
|
|
|
|
$file =~ s/\/*$//; # strip any trailing '/'
|
|
|
|
$altdest =~ s/\/*$//; # strip any trailing '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
($file eq "") && ($file = $_); # if $file not set, set it.
|
|
|
|
|
|
|
|
# if it has wildcards, do recursive copy.
|
|
|
|
/(?:\*|\?)/ && do {
|
|
|
|
do_batchcopy ("$srcdir/$file");
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# if it's a directory, do recursive copy.
|
|
|
|
(-d "$srcdir/$file") && do {
|
|
|
|
do_batchcopy ("$srcdir/$file");
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# if it's a single file, copy it.
|
|
|
|
( -f "$srcdir/$file" ) && do {
|
|
|
|
do_copyfile ();
|
|
|
|
$file = "";
|
|
|
|
next LINE;
|
|
|
|
};
|
|
|
|
|
|
|
|
# if we hit this, dunno what it is. abort! abort!
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: $_ unrecognized ($package, $lineno). Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
|
|
|
|
} # LINE
|
|
|
|
|
|
|
|
close (MANIFEST);
|
|
|
|
chdir ($saved_cwd);
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Delete the given file or directory
|
|
|
|
#
|
|
|
|
sub do_delete
|
|
|
|
{
|
|
|
|
local ($target) = $_[0];
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "do_delete:\n";
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
if (-f $target) {
|
|
|
|
!(-w $target) &&
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: delete failed: $target not writeable ($package, $component, $lineno). Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
unlink ($target) ||
|
|
|
|
die "Error: unlink() failed: $!. Exiting...\n";
|
|
|
|
} elsif (-d $target) {
|
|
|
|
!(-w $target) &&
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: delete failed: $target not writeable ($package, $component, $lineno). Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
rmtree ($target, 0, 0) ||
|
|
|
|
die "Error: rmtree() failed: $!. Exiting...\n";
|
|
|
|
} else {
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: delete failed: $target is not a file or directory ($package, $component, $lineno). Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Copy an individual file from the srcdir to the destdir.
|
|
|
|
# This is called by both the individual and batch/recursive copy routines,
|
|
|
|
# using $batch to check if called from do_batchcopy.
|
|
|
|
#
|
|
|
|
sub do_copyfile
|
|
|
|
{
|
|
|
|
local ($path) = "";
|
|
|
|
my ($srcfile) = "";
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "do_copyfile:\n";
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
# set srcfile correctly depending on how called
|
|
|
|
if ($batch) {
|
|
|
|
$srcfile = $File::Find::name;
|
|
|
|
} else {
|
|
|
|
$srcfile = "$srcdir/$file";
|
|
|
|
}
|
|
|
|
# check that source file is readable
|
|
|
|
(!( -r $srcfile )) &&
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: file $srcfile is not readable ($package, $component, $lineno).\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
|
|
|
|
# set the destination path, if alternate destination given, use it.
|
|
|
|
if ($altdest ne "") {
|
|
|
|
if ($batch) {
|
|
|
|
$path = "$destdir/$component/$altdest/$File::Find::dir";
|
|
|
|
$path =~ s/$srcdir\/$file\///; # rm info added by find
|
|
|
|
$basefile = basename ($File::Find::name);
|
|
|
|
# print "recursive find w/altdest: $path $basefile\n";
|
|
|
|
} else {
|
|
|
|
$path = dirname ("$destdir/$component/$altdest");
|
|
|
|
$basefile = basename ($altdest);
|
|
|
|
# print "recursive find w/altdest: $path $basefile\n";
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
if ($batch) {
|
|
|
|
$path = "$destdir/$component/$File::Find::dir";
|
|
|
|
$path =~ s/$srcdir\///; # rm $srcdir added by find()
|
|
|
|
$basefile = basename ($File::Find::name);
|
|
|
|
# print "recursive find w/o altdest: $path $basefile\n";
|
|
|
|
} else {
|
|
|
|
$path = dirname ("$destdir/$component/$file");
|
|
|
|
$basefile = basename ($file);
|
|
|
|
# print "recursive find w/o altdest: $path $basefile\n";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# create the directory path to the file if not there yet
|
|
|
|
if (!( -d $path)) {
|
|
|
|
mkpath ($path, 0, 0755) ||
|
|
|
|
die "Error: mkpath() failed: $!. Exiting...\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
if (-f $srcfile) { # don't copy if it's a directory
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 5) && print "\tcopy\t$srcfile => $path/$basefile\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
copy ("$srcfile", "$path/$basefile") ||
|
1999-09-09 04:25:25 +04:00
|
|
|
die "Error: copy of file $srcdir/$file failed ($package, $component, $lineno): $!. Exiting...\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
#
|
|
|
|
# Expand any wildcards, and recursively copy files and directories specified.
|
|
|
|
#
|
|
|
|
sub do_batchcopy
|
|
|
|
{
|
|
|
|
my ($entry) = $_[0];
|
|
|
|
my (@list) = ();
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "do_batchcopy:\n";
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
if ($entry =~ /(?:\*|\?)/) { # it's a wildcard,
|
|
|
|
@list = glob($entry); # expand it, and
|
|
|
|
foreach $entry (@list) {
|
|
|
|
do_batchcopy($entry); # recursively copy results.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$batch = 1; # flag for do_copyfile
|
|
|
|
find (\&do_copyfile, $entry);
|
|
|
|
$batch = 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Handle new component
|
|
|
|
#
|
|
|
|
sub do_component
|
|
|
|
{
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "do_component:\n";
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
( $component =~ /^\[.*(?:\s|\[|\])+.*\]/ ) && # no brackets or ws
|
|
|
|
die "Error: malformed component $component. Exiting...\n";
|
|
|
|
$component =~ s/^\[(.*)\]/$1/; # strip []
|
|
|
|
|
|
|
|
# create component directory
|
1999-09-09 04:25:25 +04:00
|
|
|
if ( -d "$destdir/$component" ) {
|
|
|
|
warn "Warning: component directory \"$component\" already exists in \"$destdir\".\n";
|
|
|
|
} else {
|
|
|
|
mkdir ("$destdir/$component", 0755) ||
|
|
|
|
die "Error: couldn't create component directory \"$component\": $!. Exiting...\n";
|
|
|
|
};
|
1999-09-08 03:30:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Check that arguments to script are valid.
|
|
|
|
#
|
|
|
|
sub check_arguments
|
|
|
|
{
|
1999-09-09 04:25:25 +04:00
|
|
|
my ($exitval) = 0;
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "check_arguments:\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# if --help print usage
|
|
|
|
if ($help) {
|
|
|
|
print_usage();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# make sure required variables are set:
|
|
|
|
# check source directory
|
|
|
|
if ($srcdir eq "") {
|
|
|
|
print "Error: source directory (--source) not specified.\n";
|
|
|
|
$exitval += 8;
|
|
|
|
} elsif (!(-d $srcdir) || !(-r $srcdir)) {
|
|
|
|
print "Error: source directory \"$srcdir\" is not a directory or is unreadable.\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
$exitval = 1;
|
|
|
|
}
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# check destination directory
|
|
|
|
if ($destdir eq "") {
|
|
|
|
print "Error: destination directory (--destination) not specified.\n";
|
|
|
|
$exitval += 8;
|
|
|
|
} elsif (!(-d $destdir) || !(-w $destdir)) {
|
|
|
|
print "Error: destination directory \"$destdir\" is not a directory or is not writeable.\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
$exitval += 2;
|
|
|
|
}
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# check package file
|
|
|
|
if ($package eq "") {
|
|
|
|
print "Error: package file (--file) not specified.\n";
|
|
|
|
$exitval += 8;
|
|
|
|
} elsif (!(-f $package) || !(-r $package)) {
|
|
|
|
print "Error: package file \"$package\" is not a file or is unreadable.\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
$exitval += 4;
|
|
|
|
}
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
# check OS == {mac|unix|dos}
|
|
|
|
if ($os eq "") {
|
|
|
|
print "Error: OS type (--os) not specified.\n";
|
|
|
|
$exitval += 8;
|
|
|
|
} elsif ( $os =~ /mac/i ) {
|
|
|
|
$os = "MacOS";
|
|
|
|
} elsif ( $os =~ /dos/i ) {
|
|
|
|
$os = "MSDOS";
|
|
|
|
} elsif ( $os =~ /unix/i ) { # null because Unix is default for
|
|
|
|
$os = ""; # fileparse_set_fstype()
|
|
|
|
} else {
|
|
|
|
print "Error: OS type \"$os\" unknown.\n";
|
|
|
|
$exitval += 16;
|
|
|
|
}
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
if ($exitval) {
|
1999-09-09 04:25:25 +04:00
|
|
|
print "\See '$0 --help\' for more information.\n";
|
1999-09-08 03:30:45 +04:00
|
|
|
print "Exiting...\n";
|
|
|
|
exit ($exitval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
#
|
|
|
|
# This is called by GetOptions when there are extra command line arguments
|
|
|
|
# it doesn't understand.
|
|
|
|
#
|
|
|
|
sub do_badargument
|
|
|
|
{
|
|
|
|
warn "Warning: unknown command line option specified: @_.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
#
|
|
|
|
# display usage information
|
|
|
|
#
|
|
|
|
sub print_usage
|
|
|
|
{
|
1999-09-09 04:25:25 +04:00
|
|
|
($debug >= 2) && print "print_usage:\n";
|
|
|
|
|
1999-09-08 03:30:45 +04:00
|
|
|
print <<EOC
|
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
$0
|
|
|
|
Copy files from the source directory to component directories
|
|
|
|
in the destination directory as specified by the package file.
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
Options:
|
|
|
|
--source <source directory>
|
|
|
|
Specifies the directory from which to copy the files
|
|
|
|
specified in the file passed via --file. Can also be
|
|
|
|
abbreviated to: --src, --s.
|
|
|
|
Required.
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
--destination <destination directory>
|
|
|
|
Specifies the directory in which to create the component
|
|
|
|
directories and copy the files specified in the file passed
|
|
|
|
via --file. Can also be abbreviated to: --dest, --d.
|
|
|
|
Required.
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
--file <package file>
|
|
|
|
Specifies the file listing the components to be created in
|
|
|
|
the destination directory and the files to copy from the
|
|
|
|
source directory to each component directory in the
|
|
|
|
destination directory. Can also be abbreviated to: --f.
|
|
|
|
Required.
|
|
|
|
|
|
|
|
--os [dos|mac|unix]
|
|
|
|
Specifies which type of system this is. Used for parsing file
|
|
|
|
specifications from the package file. Can also be abbreviated
|
|
|
|
to: --o.
|
|
|
|
Required.
|
|
|
|
|
|
|
|
--help
|
|
|
|
Prints this information. Can also be abbreviated to: --h
|
|
|
|
Optional.
|
|
|
|
|
|
|
|
--debug [1-10]
|
|
|
|
Controls verbosity of debugging output, 10 being most verbose.
|
|
|
|
Optional.
|
1999-09-08 03:30:45 +04:00
|
|
|
|
1999-09-09 04:25:25 +04:00
|
|
|
e.g.
|
|
|
|
|
|
|
|
$0 --os unix --source /builds/mozilla/dist --destination /h/lithium/install --file packages-win
|
|
|
|
|
|
|
|
Note: options can be specified by either a leading '--' or '-'.
|
|
|
|
|
|
|
|
EOC
|
1999-09-08 03:30:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# EOF
|