970N/A#! /usr/perl5/bin/perl
970N/A#
1339N/A# Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
970N/A#
970N/A# Permission is hereby granted, free of charge, to any person obtaining a
970N/A# copy of this software and associated documentation files (the "Software"),
970N/A# to deal in the Software without restriction, including without limitation
970N/A# the rights to use, copy, modify, merge, publish, distribute, sublicense,
970N/A# and/or sell copies of the Software, and to permit persons to whom the
970N/A# Software is furnished to do so, subject to the following conditions:
970N/A#
970N/A# The above copyright notice and this permission notice (including the next
970N/A# paragraph) shall be included in all copies or substantial portions of the
970N/A# Software.
970N/A#
970N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
970N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
970N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
970N/A# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
970N/A# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
970N/A# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
970N/A# DEALINGS IN THE SOFTWARE.
970N/A#
970N/A
970N/A#
970N/A# Script to generate a package manifest for use with pkg(5) from a
970N/A# temporary proto area
970N/A#
970N/A# Usage: pkg-manifest-generate.pl [<attribute>=<value>...]
970N/A#
970N/A# If <manifest input> exists, it is merged into the output.
970N/A# If <manifest input> does not exist, <license file> is included in the output
970N/A# The result is written to <manifest output>, or stdout if not specified
970N/A
970N/Ause strict;
970N/Ause warnings;
1339N/Ause POSIX qw(strftime);
970N/A
970N/Amy %options;
970N/A
970N/Aforeach my $arg (@ARGV) {
970N/A my ($attr, $val) = split /=/, $arg, 2;
970N/A
970N/A push @{$options{$attr}}, $val;
970N/A}
970N/A
970N/Asub required_option_list {
970N/A my ($opt, $message) = @_;
970N/A
970N/A if (!exists $options{$opt}) {
970N/A die ("Must specify $opt $message\n");
970N/A }
970N/A return $options{$opt};
970N/A}
970N/A
970N/Asub required_option {
970N/A my ($opt, $message) = @_;
970N/A
970N/A my $opt_list_ref = required_option_list(@_);
970N/A
970N/A if (scalar(@{$opt_list_ref}) != 1) {
970N/A die ("Must specify only one value for $opt $message\n");
970N/A }
970N/A return $opt_list_ref->[0];
970N/A}
970N/A
970N/Amy %actions_seen = ();
970N/Amy @manifest_header;
970N/A
1172N/Asub next_action_seen {
1172N/A my ($FH) = @_;
1172N/A my $action = "";
1172N/A
1172N/A while (my $l = <$FH>) {
1172N/A chomp($l);
1265N/A if ($l =~ m{^\#}) { # comment
1265N/A $action .= $l;
1265N/A return $action;
1265N/A } elsif ($l =~ m{^(.*)\\$}) { # Line continues
1172N/A $action .= $1 . " ";
1172N/A } else {
1172N/A $action .= $l;
1172N/A
1172N/A $action =~ s{\s+}{ }g;
1172N/A
1172N/A next if $action =~ m{^\s*$};
1172N/A
1172N/A if ($action =~ m{ path=(\S+)}) {
1172N/A $actions_seen{$1} = $action;
1172N/A } else {
1172N/A $actions_seen{$action} = $action;
1172N/A }
1172N/A
1172N/A return $action;
1172N/A }
1172N/A }
1172N/A
1172N/A return undef;
1172N/A}
1172N/A
970N/A# Check if there is an existing manifest to merge with
970N/Aif (exists $options{'input_manifest'}) {
970N/A foreach my $mf (@{$options{'input_manifest'}}) {
970N/A next if (! -f $mf);
970N/A open my $INPUT_MF, '<', $mf or die "Cannot open input_manifest $mf: $!\n";
970N/A my $action = "";
1172N/A while (my $action = next_action_seen($INPUT_MF)) {
970N/A push @manifest_header, $action;
970N/A }
970N/A close $INPUT_MF;
970N/A }
970N/A}
970N/A
1172N/A# Manifest listing entries not to include in manifest
1172N/Aif (exists $options{'exception_manifest'}) {
1172N/A foreach my $mf (@{$options{'exception_manifest'}}) {
1172N/A next if (! -f $mf);
1172N/A open my $EXCEPT_MF, '<', $mf or
1172N/A die "Cannot open exception_manifest $mf: $!\n";
1172N/A while (my $action = next_action_seen($EXCEPT_MF)) {
1172N/A # Just adding it to action_seen, nothing more to do
1172N/A }
1172N/A close $EXCEPT_MF;
1172N/A }
1172N/A}
1172N/A
970N/A# Generate a manifest header if not merging into an existing file
970N/Aif (!@manifest_header) {
970N/A
970N/A my $manifest_license_listref = required_option_list
970N/A ('manifest_license', 'when not merging with existing manifest.');
970N/A
1339N/A # substitute the current year for @YEARS@ in the license template
1339N/A my $current_year = strftime '%Y', localtime;
1339N/A
970N/A foreach my $lf (@{$options{'manifest_license'}}) {
970N/A open my $LICENSE, '<', $lf or die "Cannot open manifest_license $lf: $!\n";
970N/A while (my $ll = <$LICENSE>) {
970N/A chomp($ll);
970N/A if ($ll !~ m{^\#}) {
970N/A $ll = '# ' . $ll;
970N/A }
1339N/A $ll =~ s{\@YEARS\@}{$current_year};
970N/A push @manifest_header, $ll;
970N/A }
970N/A close $LICENSE;
970N/A }
970N/A
970N/A my $pkg_name = required_option ('pkg_name',
970N/A 'when not merging with existing manifest.');
970N/A
1088N/A my $pkg_vers = '$(PKGVERS)';
1088N/A if (exists $options{'version'}) {
1088N/A $pkg_vers = $options{'version'}->[0];
1088N/A }
1088N/A
970N/A push @manifest_header, join('', 'set name=pkg.fmri value=pkg:/',
1088N/A $pkg_name, '@', $pkg_vers);
970N/A
970N/A push @manifest_header, 'set name=pkg.description ' .
970N/A 'value="XXX: Please provide a descriptive paragraph for the package."';
970N/A
970N/A my $pkg_summary = '';
1339N/A if (exists $options{'summary'}) {
1339N/A $pkg_summary = $options{'summary'}->[0];
1339N/A }
970N/A
1339N/A if ($pkg_summary =~ m{^\s*$}) {
970N/ASDIR: foreach my $sdir (@{$options{'source_dir'}}) {
970N/A foreach my $bdir (glob("build-*/$sdir")) {
970N/A # First try looking in a README file for a short summary
970N/A my $rf = join('/', $bdir, 'README');
970N/A if (open my $README, '<', $rf) {
970N/A while (my $readme = <$README>) {
970N/A chomp($readme);
970N/A last if $readme =~ m{^[\-=\s]*$};
970N/A $readme =~ s{[\t\n]+}{ }g;
970N/A if ($pkg_summary =~ m{\S$}) {
970N/A $pkg_summary .= ' ';
970N/A }
970N/A $pkg_summary .= $readme;
970N/A }
970N/A close $README;
970N/A last SDIR if $pkg_summary ne '';
970N/A }
970N/A
970N/A # Then try looking in man pages
970N/A my @manpage = glob("$bdir/man/*.man");
970N/A push @manpage, glob("$bdir/*.man");
970N/A
970N/A foreach my $manpage (@manpage) {
970N/A if ($manpage && (-f $manpage)) {
970N/A my $desc;
970N/A
970N/A open(my $MANPAGE, '<', $manpage) or warn "Cannot read $manpage\n";
970N/A
970N/A while (my $l = <$MANPAGE>) {
970N/A if ($l =~ /^.SH NAME/) {
970N/A $desc = <$MANPAGE>;
970N/A last;
970N/A }
970N/A }
970N/A close($MANPAGE);
970N/A
970N/A chomp($desc);
970N/A
970N/A # Remove backslashes, such as \- instead of -
970N/A $desc =~ s/\\//g;
970N/A
970N/A if ($sdir =~ /^xf86-(input|video)-(.*)-.*$/) {
970N/A $desc =~ s/driver$/driver for the Xorg X server/;
970N/A }
970N/A
970N/A if ($desc !~ m{^\s*$}) {
970N/A $pkg_summary = $desc;
970N/A last SDIR;
970N/A }
970N/A }
970N/A }
970N/A }
1339N/A }
970N/A }
970N/A
970N/A if ($pkg_summary eq '') {
970N/A $pkg_summary =
970N/A 'XXX: Please provide a short name for the package';
970N/A } else {
970N/A $pkg_summary =~ s{^\s+}{};
970N/A $pkg_summary =~ s{\s+$}{};
970N/A }
970N/A
970N/A push @manifest_header, join('', 'set name=pkg.summary ',
970N/A 'value="', $pkg_summary, '"');
970N/A
970N/A if (exists $options{'classification'}) {
970N/A my $pkg_class = $options{'classification'};
970N/A
970N/A push @manifest_header, join('', 'set name=info.classification ',
970N/A 'value="org.opensolaris.category.2008:',
970N/A $pkg_class, '"');
970N/A }
970N/A}
970N/A
970N/A## Contents
970N/A
970N/Amy @manifest_contents;
970N/A
970N/Amy $subdir64 = required_option('subdir_64',
970N/A 'with the name used for the subdirectory containing 64-bit objects');
970N/A
970N/Amy $proto_area_ref = required_option_list('proto_area',
970N/A 'for files to include in manifest');
970N/A
970N/Aforeach my $proto_area (@{$proto_area_ref}) {
970N/A my $pkgsend_cmd = join(' ', 'pkgsend', 'generate', $proto_area);
970N/A open my $PKGSEND, '-|', $pkgsend_cmd or die "Cannot run $pkgsend_cmd: $!\n";
970N/A
970N/A while (my $ps = <$PKGSEND>) {
970N/A chomp($ps);
970N/A
970N/A # Skip -uninstalled.pc files, since those are only used during build
970N/A next if $ps =~ m{^file\b.*\bpath=.*-uninstalled.pc\b};
970N/A
970N/A # Convert 64-bit subdirectories to platform-independent form
970N/A $ps =~ s{([=/])${subdir64}\b}{$1\$(ARCH64)}g;
970N/A
970N/A # Don't add duplicates of actions we've already got
970N/A my $action = $ps;
970N/A if ($ps =~ m{ path=(\S+)}) {
970N/A $action = $1;
970N/A } else {
970N/A $action =~ s{\s+}{ }g;
970N/A }
970N/A next if exists $actions_seen{$action};
970N/A $actions_seen{$action} = $ps;
970N/A
970N/A # Drop file path from file actions, so we always use the path attribute
970N/A $ps =~ s{^file (\S+) (.* path=\1)}{file $2};
970N/A
970N/A # Drop attributes that will be generated during package build
970N/A push @manifest_contents,
970N/A join(" ", grep (! /^(mode|group|owner|pkg.size|timestamp)=/,
970N/A split /\s+/, $ps));
970N/A }
970N/A close $PKGSEND;
970N/A}
970N/A
970N/Amy $pkgfmt_cmd = 'pkgfmt';
970N/Aif (exists $options{'pkgfmt'}) {
970N/A $pkgfmt_cmd = join(' ', @{$options{'pkgfmt'}});
970N/A}
970N/Aopen my $PKGFMT, '|-', $pkgfmt_cmd or die "Cannot run $pkgfmt_cmd: $!\n";
970N/Aforeach my $line (@manifest_header, @manifest_contents) {
970N/A print $PKGFMT $line, "\n";
970N/A}
970N/Aclose $PKGFMT;