Blame SOURCES/perl.req.stack

f523ca
#!/usr/bin/perl
f523ca
f523ca
# RPM (and its source code) is covered under two separate licenses.
f523ca
f523ca
# The entire code base may be distributed under the terms of the GNU
f523ca
# General Public License (GPL), which appears immediately below.
f523ca
# Alternatively, all of the source code in the lib subdirectory of the
f523ca
# RPM source code distribution as well as any code derived from that
f523ca
# code may instead be distributed under the GNU Library General Public
f523ca
# License (LGPL), at the choice of the distributor. The complete text
f523ca
# of the LGPL appears at the bottom of this file.
f523ca
f523ca
# This alternatively is allowed to enable applications to be linked
f523ca
# against the RPM library (commonly called librpm) without forcing
f523ca
# such applications to be distributed under the GPL.
f523ca
f523ca
# Any questions regarding the licensing of RPM should be addressed to
f523ca
# Erik Troan <ewt@redhat.com>.
f523ca
f523ca
# a simple makedepend like script for perl.
f523ca
f523ca
# To save development time I do not parse the perl grammar but
f523ca
# instead just lex it looking for what I want.  I take special care to
f523ca
# ignore comments and pod's.
f523ca
f523ca
# It would be much better if perl could tell us the dependencies of a
f523ca
# given script.
f523ca
f523ca
# The filenames to scan are either passed on the command line or if
f523ca
# that is empty they are passed via stdin.
f523ca
f523ca
# If there are strings in the file which match the pattern
f523ca
#     m/^\s*\$RPM_Requires\s*=\s*["'](.*)['"]/i
f523ca
# then these are treated as additional names which are required by the
f523ca
# file and are printed as well.
f523ca
f523ca
# I plan to rewrite this in C so that perl is not required by RPM at
f523ca
# build time.
f523ca
f523ca
# by Ken Estes Mail.com kestes@staff.mail.com
f523ca
f523ca
my $perl_req = "__SCL_NAME__";
f523ca
f523ca
$HAVE_VERSION = 0;
f523ca
eval { require version; $HAVE_VERSION = 1; };
f523ca
f523ca
f523ca
if ("@ARGV") {
f523ca
  foreach (@ARGV) {
f523ca
    process_file($_);
f523ca
  }
f523ca
} else {
f523ca
f523ca
  # notice we are passed a list of filenames NOT as common in unix the
f523ca
  # contents of the file.
f523ca
f523ca
  foreach (<>) {
f523ca
    process_file($_);
f523ca
  }
f523ca
}
f523ca
f523ca
f523ca
foreach $perlver (sort keys %perlreq) {
f523ca
  print "$perl_req >= $perlver\n";
f523ca
}
f523ca
foreach $module (sort keys %require) {
f523ca
  if (length($require{$module}) == 0) {
f523ca
    print "$perl_req($module)\n";
f523ca
  } else {
f523ca
f523ca
    # I am not using rpm3.0 so I do not want spaces around my
f523ca
    # operators. Also I will need to change the processing of the
f523ca
    # $RPM_* variable when I upgrade.
f523ca
f523ca
    print "$perl_req($module) >= $require{$module}\n";
f523ca
  }
f523ca
}
f523ca
f523ca
exit 0;
f523ca
f523ca
f523ca
f523ca
sub add_require {
f523ca
  my ($module, $newver) = @_;
f523ca
  my $oldver = $require{$module};
f523ca
  if ($oldver) {
f523ca
    $require{$module} = $newver
f523ca
      if ($HAVE_VERSION && $newver && version->new($oldver) < $newver);
f523ca
  }
f523ca
  else {
f523ca
    $require{$module} = $newver;
f523ca
  }
f523ca
}
f523ca
f523ca
sub process_file {
f523ca
f523ca
  my ($file) = @_;
f523ca
  chomp $file;
f523ca
f523ca
  if (!open(FILE, $file)) {
f523ca
    warn("$0: Warning: Could not open file '$file' for reading: $!\n");
f523ca
    return;
f523ca
  }
f523ca
f523ca
  while (<FILE>) {
f523ca
f523ca
    # skip the "= <<" block
f523ca
f523ca
    if (m/^\s*\$(?:.*)\s*=\s*<<\s*(["'`])(.+?)\1/ ||
f523ca
        m/^\s*\$(.*)\s*=\s*<<(\w+)\s*;/) {
f523ca
      $tag = $2;
f523ca
      while (<FILE>) {
f523ca
        chomp;
f523ca
        ( $_ eq $tag ) && last;
f523ca
      }
f523ca
      $_ = <FILE>;
f523ca
    }
f523ca
f523ca
    # skip q{} quoted sections - just hope we don't have curly brackets
f523ca
    # within the quote, nor an escaped hash mark that isn't a comment
f523ca
    # marker, such as occurs right here. Draw the line somewhere.
f523ca
    if ( m/^.*\Wq[qxwr]?\s*([{([#|\/])[^})\]#|\/]*$/ && ! m/^\s*(require|use)\s/ ) {
f523ca
      $tag = $1;
f523ca
      $tag =~ tr/{\(\[\#|\//})]#|\//;
f523ca
      $tag = quotemeta($tag);
f523ca
      while (<FILE>) {
f523ca
        ( $_ =~ m/$tag/ ) && last;
f523ca
      }
f523ca
    }
f523ca
f523ca
    # skip the documentation
f523ca
f523ca
    # we should not need to have item in this if statement (it
f523ca
    # properly belongs in the over/back section) but people do not
f523ca
    # read the perldoc.
f523ca
f523ca
    if (/^=(head[1-4]|pod|for|item)/) {
f523ca
      /^=cut/ && next while <FILE>;
f523ca
    }
f523ca
f523ca
    if (/^=over/) {
f523ca
      /^=back/ && next while <FILE>;
f523ca
    }
f523ca
f523ca
    # skip the data section
f523ca
    if (m/^__(DATA|END)__$/) {
f523ca
      last;
f523ca
    }
f523ca
f523ca
    # Each keyword can appear multiple times.  Don't
f523ca
    #  bother with datastructures to store these strings,
f523ca
    #  if we need to print it print it now.
f523ca
    #
f523ca
        # Again allow for "our".
f523ca
    if (m/^\s*(our\s+)?\$RPM_Requires\s*=\s*["'](.*)['"]/i) {
f523ca
      foreach $_ (split(/\s+/, $2)) {
f523ca
        print "$_\n";
f523ca
      }
f523ca
    }
f523ca
f523ca
    my $modver_re = qr/[.0-9]+/;
f523ca
f523ca
    if (
f523ca
f523ca
# ouch could be in a eval, perhaps we do not want these since we catch
f523ca
# an exception they must not be required
f523ca
f523ca
#   eval { require Term::ReadLine } or die $@;
f523ca
#   eval "require Term::Rendezvous;" or die $@;
f523ca
#   eval { require Carp } if defined $^S; # If error/warning during compilation,
f523ca
f523ca
f523ca
        (m/^(\s*)         # we hope the inclusion starts the line
f523ca
         (require|use)\s+(?!\{)     # do not want 'do {' loops
f523ca
         # quotes around name are always legal
f523ca
         ['"]?([^; '"\t#]+)['"]?[\t; ]
f523ca
         # the syntax for 'use' allows version requirements
f523ca
         # the latter part is for "use base qw(Foo)" and friends special case
f523ca
         \s*($modver_re|(qw\s*[(\/'"]\s*|['"])[^)\/"'\$]*?\s*[)\/"'])?
f523ca
         /x)
f523ca
       ) {
f523ca
      my ($whitespace, $statement, $module, $version) = ($1, $2, $3, $4);
f523ca
f523ca
      # we only consider require statements that are flushed against
f523ca
      # the left edge. any other require statements give too many
f523ca
      # false positives, as they are usually inside of an if statement
f523ca
      # as a fallback module or a rarely used option
f523ca
f523ca
      ($whitespace ne "" && $statement eq "require") && next;
f523ca
f523ca
      # if there is some interpolation of variables just skip this
f523ca
      # dependency, we do not want
f523ca
      #        do "$ENV{LOGDIR}/$rcfile";
f523ca
f523ca
      ($module =~ m/\$/) && next;
f523ca
f523ca
      # skip if the phrase was "use of" -- shows up in gimp-perl, et al.
f523ca
      next if $module eq 'of';
f523ca
f523ca
      # if the module ends in a comma we probably caught some
f523ca
      # documentation of the form 'check stuff,\n do stuff, clean
f523ca
      # stuff.' there are several of these in the perl distribution
f523ca
f523ca
      ($module  =~ m/[,>]$/) && next;
f523ca
f523ca
      # if the module name starts in a dot it is not a module name.
f523ca
      # Is this necessary?  Please give me an example if you turn this
f523ca
      # back on.
f523ca
f523ca
      #      ($module =~ m/^\./) && next;
f523ca
f523ca
      # if the module starts with /, it is an absolute path to a file
f523ca
      if ($module =~ m(^/)) {
f523ca
        print "$module\n";
f523ca
        next;
f523ca
      }
f523ca
f523ca
      # sometimes people do use POSIX qw(foo), or use POSIX(qw(foo)) etc.
f523ca
      # we can strip qw.*$, as well as (.*$:
f523ca
      $module =~ s/qw.*$//;
f523ca
      $module =~ s/\(.*$//;
f523ca
f523ca
      # if the module ends with .pm, strip it to leave only basename.
f523ca
      $module =~ s/\.pm$//;
f523ca
f523ca
      # some perl programmers write 'require URI/URL;' when
f523ca
      # they mean 'require URI::URL;'
f523ca
f523ca
      $module =~ s/\//::/;
f523ca
f523ca
      # trim off trailing parentheses if any.  Sometimes people pass
f523ca
      # the module an empty list.
f523ca
f523ca
      $module =~ s/\(\s*\)$//;
f523ca
f523ca
      if ( $module =~ m/^v?([0-9._]+)$/ ) {
f523ca
      # if module is a number then both require and use interpret that
f523ca
      # to mean that a particular version of perl is specified
f523ca
f523ca
      my $ver = $1;
f523ca
      if ($ver =~ /5.00/) {
f523ca
	$perlreq{"0:$ver"} = 1;
f523ca
        next;
f523ca
      }
f523ca
      else {
f523ca
	$perlreq{"1:$ver"} = 1;
f523ca
        next;
f523ca
      }
f523ca
f523ca
      };
f523ca
f523ca
      # ph files do not use the package name inside the file.
f523ca
      # perlmodlib documentation says:
f523ca
f523ca
      #       the .ph files made by h2ph will probably end up as
f523ca
      #       extension modules made by h2xs.
f523ca
f523ca
      # so do not expend much effort on these.
f523ca
f523ca
f523ca
      # there is no easy way to find out if a file named systeminfo.ph
f523ca
      # will be included with the name sys/systeminfo.ph so only use the
f523ca
      # basename of *.ph files
f523ca
f523ca
      ($module =~ m/\.ph$/) && next;
f523ca
f523ca
      # use base|parent qw(Foo) dependencies
f523ca
      if ($statement eq "use" && ($module eq "base" || $module eq "parent")) {
f523ca
        add_require($module, undef);
f523ca
        if ($version =~ /^qw\s*[(\/'"]\s*([^)\/"']+?)\s*[)\/"']/) {
f523ca
          add_require($_, undef) for split(' ', $1);
f523ca
        }
f523ca
        elsif ($version =~ /(["'])([^"']+)\1/) {
f523ca
          add_require($2, undef);
f523ca
        }
f523ca
        next;
f523ca
      }
f523ca
      $version = undef unless $version =~ /^$modver_re$/o;
f523ca
f523ca
      add_require($module, $version);
f523ca
    }
f523ca
f523ca
  }
f523ca
f523ca
  close(FILE) ||
f523ca
    die("$0: Could not close file: '$file' : $!\n");
f523ca
f523ca
  return;
f523ca
}