csomh / source-git / rpm

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