| 1 |
#!/usr/bin/perl -lw |
| 2 |
# |
| 3 |
# $Id: cswproto 19813 2012-12-04 17:08:27Z dmichelsen $ |
| 4 |
# |
| 5 |
# Copyright 2006 Cory Omand <comand@blastwave.org> |
| 6 |
# All rights reserved. Use is subject to license terms. |
| 7 |
# |
| 8 |
# Redistribution and/or use, with or without modification, is |
| 9 |
# permitted. This software is without warranty of any kind. The |
| 10 |
# author(s) shall not be liable in the event that use of the |
| 11 |
# software causes damage. |
| 12 |
# |
| 13 |
# cswproto - Create package prototypes which adhere to CSW standards. |
| 14 |
# |
| 15 |
|
| 16 |
use strict; |
| 17 |
use File::Basename; |
| 18 |
use File::Spec::Functions qw/catfile/; |
| 19 |
use Getopt::Long qw/:config no_ignore_case/; |
| 20 |
use POSIX; |
| 21 |
|
| 22 |
use vars qw/ |
| 23 |
@XFORMS $Common $StdOwn $StdGrp $StdDirPerm $common $stamp $root |
| 24 |
/; |
| 25 |
|
| 26 |
# atime=8,mtime=9,ctime=10 |
| 27 |
use constant TIME_FIELD => 10; |
| 28 |
|
| 29 |
# Prototype defaults |
| 30 |
$StdOwn = 'root'; |
| 31 |
$StdGrp = 'bin'; |
| 32 |
$StdDirPerm = '0755'; |
| 33 |
|
| 34 |
# Path transforms |
| 35 |
@XFORMS = ( |
| 36 |
[ qr{^/opt/csw/man$}, q{/opt/csw/share/man} ], |
| 37 |
[ qr{^/opt/csw/doc$}, q{/opt/csw/share/doc} ], |
| 38 |
[ qr{^/opt/csw/info$}, q{/opt/csw/share/info} ], |
| 39 |
); |
| 40 |
|
| 41 |
# Print usage information and exit |
| 42 |
sub usage { |
| 43 |
print join(" ", @_) if @_; |
| 44 |
|
| 45 |
my $program = basename $0; |
| 46 |
print <<"_USAGE_"; |
| 47 |
Usage: $program [-c <commonpathes>] [-h] [-s <timestamp>] path1[=path1] ... pathN[=pathN] |
| 48 |
|
| 49 |
-c Filename containing common pathes not to include in the package |
| 50 |
|
| 51 |
-s Timestamp source. |
| 52 |
The path to a file to be used as the base timestamp for prototype |
| 53 |
operations. If this is specified, all source file creation times |
| 54 |
are compared to the creation time of this file. Files created |
| 55 |
*before* this time will be excluded from the prototype. |
| 56 |
|
| 57 |
-h Display brief usage. |
| 58 |
|
| 59 |
pathN The remainder of arguments to this command will be specified |
| 60 |
directly to pkgproto. These arguments specify which paths are to |
| 61 |
be included in the prototype. This can be specified as |
| 62 |
pathX=pathY to use a different prefix for files in the prototype |
| 63 |
than were present on the build system. |
| 64 |
_USAGE_ |
| 65 |
|
| 66 |
exit 1; |
| 67 |
} |
| 68 |
|
| 69 |
# Returns true if the file should be excluded, false otherwise. |
| 70 |
sub exclude { |
| 71 |
my $path = shift; |
| 72 |
|
| 73 |
return 0 unless $stamp; |
| 74 |
return 1 unless $path; |
| 75 |
|
| 76 |
die "Path $path is not readable by current user!\n" unless -r $path or -l $path; |
| 77 |
|
| 78 |
my $time = (lstat($path))[TIME_FIELD]; |
| 79 |
return ($time >= $stamp) ? 0 : 1; |
| 80 |
} |
| 81 |
|
| 82 |
# |
| 83 |
# MAIN EXECUTION |
| 84 |
# |
| 85 |
|
| 86 |
# Process command line arguments |
| 87 |
my $test; |
| 88 |
GetOptions( |
| 89 |
'root=s' => \$root, |
| 90 |
'stamp=s' => \$stamp, |
| 91 |
'common=s' => \$common, |
| 92 |
'help' => \&usage, |
| 93 |
) or usage; |
| 94 |
|
| 95 |
usage "Error: timestamp '$stamp' not readable" if $stamp and not -r $stamp; |
| 96 |
usage "Error: one or more pkgproto patterns required" unless @ARGV; |
| 97 |
|
| 98 |
$stamp = $stamp ? (stat($stamp))[TIME_FIELD] : 0; |
| 99 |
|
| 100 |
if( $common ) { |
| 101 |
# Load common path contents |
| 102 |
my %alldirs = ('/' => 1); |
| 103 |
open F, $common || die "Couldn't open $common"; |
| 104 |
while (<F>) { |
| 105 |
chomp; next if /^\s*$/ or /^#/; |
| 106 |
s/\+/\\+/g; |
| 107 |
s/\-/\\-/g; |
| 108 |
my @c = split( m!/! ); |
| 109 |
my @pc = map { join( '/', @c[0..$_] ) } 1..$#c; |
| 110 |
$alldirs{$_} = 1 foreach (@pc); |
| 111 |
} |
| 112 |
close F; |
| 113 |
my $re = '^(' . join( '|', keys %alldirs ) . ')$'; |
| 114 |
$Common = qr /$re/; |
| 115 |
} |
| 116 |
|
| 117 |
my @prototype; |
| 118 |
|
| 119 |
foreach my $protopat (@ARGV) { |
| 120 |
my ($actual, $virtual) = split /=/, $protopat; |
| 121 |
|
| 122 |
my @pproto = `/usr/bin/pkgproto $protopat`; |
| 123 |
die "Failed to generate prototype" |
| 124 |
unless WIFEXITED($?) and WEXITSTATUS($?) == 0; |
| 125 |
|
| 126 |
SPECLINE: |
| 127 |
foreach my $entry (@pproto) { |
| 128 |
chomp $entry; |
| 129 |
do { print $entry; next } if $entry =~ /^(?:i|\!)/; |
| 130 |
|
| 131 |
my @F = split /\s+/, $entry; |
| 132 |
my ($lhs, $rhs) = split /=/, $F[2]; |
| 133 |
|
| 134 |
# Find the real path |
| 135 |
my $realpath; |
| 136 |
if ($F[0] eq 's' or $F[0] eq 'l') { |
| 137 |
$realpath = substr($lhs, 0, 1) ne '/' |
| 138 |
? catfile($actual, $lhs) |
| 139 |
: $lhs; |
| 140 |
|
| 141 |
} |
| 142 |
else { |
| 143 |
$F[2] = $lhs if $lhs and $rhs; |
| 144 |
$realpath = $rhs |
| 145 |
? $rhs |
| 146 |
: (substr($lhs, 0, 1) ne '/' ? catfile($actual, $lhs) : $lhs); |
| 147 |
} |
| 148 |
|
| 149 |
# Then do path transforms |
| 150 |
foreach my $xform (@XFORMS) { $F[2] =~ s/$xform->[0]/$xform->[1]/g } |
| 151 |
$F[2] =~ s/$root// if $root; |
| 152 |
next unless $F[2]; |
| 153 |
|
| 154 |
# Then process any excludes |
| 155 |
next SPECLINE if( $Common && $F[2] =~ /$Common/ ); |
| 156 |
next if exclude($realpath); |
| 157 |
|
| 158 |
# Fix up dir permissions/file ownership. |
| 159 |
$F[3] = $StdDirPerm if $F[0] eq 'd'; |
| 160 |
($F[4], $F[5]) = ( $StdOwn, $StdGrp ) |
| 161 |
unless $F[0] eq 's' or $F[0] eq 'l'; |
| 162 |
|
| 163 |
push @prototype, [ grep { defined $_ } @F ]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
print join " " => @$_ foreach (sort { $a->[2] cmp $b->[2] } @prototype); |
| 168 |
|
| 169 |
# End of file |