csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
#!/usr/bin/perl
2ff057
2ff057
# RPM (and it's 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 alternative 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 script to print the proper name for perl libraries.
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 proper name 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 lines in the file which match the pattern
2ff057
#      (m/^\s*\$VERSION\s*=\s+/)
2ff057
# then these are taken to be the version numbers of the modules.
2ff057
# Special care is taken with a few known idioms for specifying version
2ff057
# numbers of files under rcs/cvs control.
2ff057
2ff057
# If there are strings in the file which match the pattern
2ff057
#     m/^\s*\$RPM_Provides\s*=\s*["'](.*)['"]/i
2ff057
# then these are treated as additional names which are provided 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
if ("@ARGV") {
2ff057
  foreach (@ARGV) {
2ff057
    next if !/\.pm$/;
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
    next if !/\.pm$/;
2ff057
    process_file($_);
2ff057
  }
2ff057
}
2ff057
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 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
  my ($package, $version, $incomment, $inover) = ();
2ff057
2ff057
  while (<FILE>) {
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 (m/^=(head[1-4]|pod|for|item)/) {
2ff057
      $incomment = 1;
2ff057
    }
2ff057
2ff057
    if (m/^=(cut)/) {
2ff057
      $incomment = 0;
2ff057
      $inover = 0;
2ff057
    }
2ff057
2ff057
    if (m/^=(over)/) {
2ff057
      $inover = 1;
2ff057
    }
2ff057
2ff057
    if (m/^=(back)/) {
2ff057
      $inover = 0;
2ff057
    }
2ff057
2ff057
    if ($incomment || $inover || m/^\s*#/) {
2ff057
       next;
2ff057
    }
2ff057
2ff057
    # skip the data section
2ff057
    if (m/^__(DATA|END)__$/) {
2ff057
      last;
2ff057
    }
2ff057
2ff057
    # not everyone puts the package name of the file as the first
2ff057
    # package name so we report all namespaces except some common
2ff057
    # false positives as if they were provided packages (really ugly).
2ff057
2ff057
    if (m/^\s*package\s+([_:a-zA-Z0-9]+)\s*(?:v?([0-9_.]+)\s*)?[;{]/) {
2ff057
      $package = $1;
2ff057
      $version = $2;
2ff057
      if ($package eq 'main') {
2ff057
        undef $package;
2ff057
      } else {
2ff057
        # If $package already exists in the $require hash, it means
2ff057
        # the package definition is broken up over multiple blocks.
2ff057
        # In that case, don't stomp a previous $VERSION we might have
2ff057
        # found.  (See BZ#214496.)
2ff057
        $require{$package} = $version unless (exists $require{$package});
2ff057
      }
2ff057
    }
2ff057
2ff057
    # after we found the package name take the first assignment to
2ff057
    # $VERSION as the version number. Exporter requires that the
2ff057
    # variable be called VERSION so we are safe.
2ff057
2ff057
    # here are examples of VERSION lines from the perl distribution
2ff057
2ff057
    #FindBin.pm:$VERSION = $VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
2ff057
    #ExtUtils/Install.pm:$VERSION = substr q$Revision: 1.9 $, 10;
2ff057
    #CGI/Apache.pm:$VERSION = (qw$Revision: 1.9 $)[1];
2ff057
    #DynaLoader.pm:$VERSION = $VERSION = "1.03";     # avoid typo warning
2ff057
    #General.pm:$Config::General::VERSION = 2.33;
2ff057
    #
2ff057
    # or with the new "our" pragma you could (read will) see:
2ff057
    #
2ff057
    #    our $VERSION = '1.00'
2ff057
    if ($package && m/^\s*(our\s+)?\$(\Q$package\E::)?VERSION\s*=\s+/) {
2ff057
2ff057
      # first see if the version string contains the string
2ff057
      # '$Revision' this often causes bizarre strings and is the most
2ff057
      # common method of non static numbering.
2ff057
2ff057
      if (m/\$Revision: (\d+[.0-9]+)/) {
2ff057
        $version = $1;
2ff057
      } elsif (m/=\s*['"]?(\d+[._0-9]+)['"]?/) {
2ff057
2ff057
        # look for a static number hard coded in the script
2ff057
2ff057
        $version = $1;
2ff057
      }
2ff057
      $require{$package} = $version;
2ff057
    }
2ff057
2ff057
    # Allow someone to have a variable that defines virtual packages
2ff057
    # The variable is called $RPM_Provides.  It must be scoped with
2ff057
    # "our", but not "local" or "my" (just would not make sense).
2ff057
    #
2ff057
    # For instance:
2ff057
    #
2ff057
    #     $RPM_Provides = "blah bleah"
2ff057
    #
2ff057
    # Will generate provides for "blah" and "bleah".
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
    if (m/^\s*(our\s+)?\$RPM_Provides\s*=\s*["'](.*)['"]/i) {
2ff057
      foreach $_ (split(/\s+/, $2)) {
2ff057
        print "$_\n";
2ff057
      }
2ff057
    }
2ff057
2ff057
  }
2ff057
2ff057
  close(FILE) ||
2ff057
    die("$0: Could not close file: '$file' : $!\n");
2ff057
2ff057
  return;
2ff057
}