solaris Application Packaging 編寫的小工具

發表於2012-07-24

本小工具用於將目錄或檔案封裝為流檔案 pkg格式

以下為原始碼:

[@more@]

#!/usr/bin/perl
###########################################
#
# Automated processes to create SUN packages
# You can run this script after you did a 'make install' in the chrooted
# environment. Run it from the /packagename-1.0/usr/local/ directory
#
#Author: Tony Liu Date:2012/07/14
#History:

############################################
# rm exist already pkg info

$find = "/usr/bin/find";
$pkgproto = "/usr/bin/pkgproto";
$pkgmk = "/usr/bin/pkgmk";
$pkgtrans = "/usr/bin/pkgtrans";
$temp = "/tmp/prototype$$";
$prototype = "prototype";
$pkginfo = "pkginfo";
$rootapth = "/";
$pkgdir="/var/spool/pkg";


if (-e $pkgdir) {
system("rm -R $pkgdir");
system("mkdir $pkgdir");
}else
{
system("mkdir $pkgdir");
}
if (-e "/$pkginfo") {
unlink("/$pkginfo");
}
if (-e "/$prototype") {
unlink("/$prototype");
}
# Sanitycheck

$pwd = `pwd`;
if ($pwd =~ '/usr/local') {
$pwd = $`;
}
$propwd= `pwd`;
if ($propwd =~ '/usr/local') {
$propwd = $`;
}
chomp $propwd;
#$propwd=substr($propwd,1,length($propwd));
die "Wrong location, please cd to /usr/local/ and run again.n" if ($pwd eq "");

system ("$find . -print | $pkgproto > $temp");
open (PREPROTO,"< $temp") || die "Unable to read prototype information ($!)n";
open (PROTO,"> $prototype") || die "Unable to write file prototype ($!)n";
print PROTO "i pkginfo=./$pkginfon";


while () {
# Read the prototype information from /tmp/prototype$$
chomp;
$thisline = $_;
if ($thisline =~ " prototype ") {
# We don't need that line
} elsif ($thisline =~ "^[fd] ") {
# Change the ownership for files and directories
($dir, $none, $file, $mode, $user, $group) = split / /,$thisline;

print PROTO "$dir $none $propwd/$file $mode $user $groupn";
} else {
# Symlinks and other stuff should be printed as well ofcourse
print PROTO "$thislinen";
}
}
close PROTO;
close PREPROTO;

# Clean up
unlink $temp || warn "Unable to remove tempfile ($!)n";

# Now we can start building the package
#
# First get some info

$thispackage = `basename $pwd`;
if ($thispackage =~ '-') {
$default{"name"} = $`;
$default{"version"} = $';
chomp $default{"version"};
} else {
$default{"name"} = $thispackage;
chomp $default{"name"};
$default{"version"} = "1.0";
}
$default{"pkg"} = "UMC" . substr($default{"name"},0,4);
$default{"arch"} = `uname -m`;
chomp $default{"arch"};
$default{"category"} = "application";
$default{"vendor"} = "GNU";
#$default{"email"} = ;
$default{"desc"} = "$pkg";
$login = getlogin();
($user, $passwd, $uid, $gid, $quota, $default{"pstamp"}, $userInfo, $userHome, $loginShell) = getpwnam ($login);
$default{"pstamp"} = "Jasper Aukes" if ($default{"pstamp"} eq "");
$os = `uname -r`;
$os =~ '.';
$os = "sol$'";
chomp $os;
$default{"basedir"} = "/";

# Check for correctness of guessed values by userinput

%questions = (
pkg => "Please give the name for this package",
name => "Now enter the real name for this package",
arch => "What architecture did you build the package on?",
version => "Enter the version number of the package",
category => "What category does this package belong to?",
vendor => "Who is the vendor of this package?",
#email => "Enter the email adress for contact",
desc => "Please write this package description info",
pstamp => "Enter your own name",
#basedir => "What is the basedir this package will install into?",
packagename => "How should i call the packagefile?",
);

@vars = qw(pkg name arch version category vendor desc pstamp packagename);
foreach $varname (@vars) {
$default{"$varname"} = "$name-$version-$os-$arch" if ($varname eq "packagename");
getvar($varname);
}
$classes = "none";

# Create the pkginfo file
print "nNow creating $pkginfo filen";
open (PKGINFO,"> $pkginfo") || die "Unable to open $pkginfo for writingi ($!)n";
print PKGINFO "PKG="$pkg"n";
print PKGINFO "NAME="$name"n";
print PKGINFO "ARCH="$arch"n";
print PKGINFO "VERSION="$version"n";
print PKGINFO "CATEGORY="$category"n";
print PKGINFO "VENDOR="$vendor"n";
#print PKGINFO "EMAIL="$email"n";
print PKGINFO "DESC="$desc"n";
print PKGINFO "PSTAMP="$pstamp"n";
print PKGINFO "BASEDIR="$basedir"n";
print PKGINFO "CLASSES="$classes"n";
close PKGINFO;
print "Done.n";


# Build the package
system ("mv $prototype $rootapth");
system ("mv $pkginfo $rootapth");
print "Building packagen";
system ("(cd /;$pkgmk -r $rootapth)");
system ("(cd /var/spool/pkg;pkgchk -vd . $pkg;$pkgtrans -s `pwd` /tmp/$packagename.pkg)");
print "Done. (/tmp/$packagename.pkg)n";

if (-e $pkgdir) {
system("rm -R $pkgdir");
system("mkdir $pkgdir");
}else
{
system("mkdir $pkgdir");
}
if (-e "/$pkginfo") {
unlink("/$pkginfo");
}
if (-e "/$prototype") {
unlink("/$prototype");
}

# The subroutines

sub getvar {
my $questionname = "@_";
print "$questions{$questionname} [$default{"$questionname"}]: ";
my $answer = ;
chomp $answer;
$$questionname = $answer;
$$questionname = $default{$questionname} if ($$questionname eq "");
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24870090/viewspace-1058996/,如需轉載,請註明出處,否則將追究法律責任。

相關文章