App-CPANtoRPM

 view release on metacpan or  search on metacpan

lib/App/CPANtoRPM.pm  view on Meta::CPAN

package App::CPANtoRPM;
# Copyright (c) 2012-2024 Sullivan Beck. All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

###############################################################################

use warnings;
use strict;
use locale;
use POSIX qw(locale_h);
use IO::File;

our($VERSION);
$VERSION="1.15";

$| = 1;

use vars qw($COM $DIR $ARCH $VERS);
use Config;
$ARCH  = $Config{'archname'};
$VERS  = $Config{'version'};

###############################################################################
# GLOBAL VARIABLES
###############################################################################

our $TMPDIR     = "/tmp/cpantorpm";

our %Macros     = (0 => {
                         '_optimize'  => '$RPM_OPT_FLAGS',
                         '_buildroot' => '$RPM_BUILD_ROOT',
                        },

                   1 => {
                         '_optimize'  => '%{optimize}',
                         '_buildroot' => '%{buildroot}',
                        }
                  );

our ($OUTPUT,@OUTPUT,%package,$MAN);
$package{'VERSION'} = $VERSION;

my $old_locale = setlocale(LC_TIME);
setlocale(LC_TIME, "C");
$package{'date'}    = POSIX::strftime("%a %b %d %Y",localtime());
setlocale(LC_TIME, $old_locale);

###############################################################################
###############################################################################

sub _new {
   my($class) = @_;

   my $self = {
               'add_provide'  => [],
               'add_require'  => [],
               'author'       => [],
               'build'        => [],
               'build_input'  => [],
               'build_rec'    => 0,
               'build_type'   => '',
               'clean_macros' => 0,
               'config'       => [],
               'config_input' => [],
               'cpan'         => 'cpanplus',
               'cwd'          => '',
               'debug'        => 0,
               'description'  => '',
               'disttag'      => '%{?dist}',
               'env'          => {},
               'epoch'        => '',
               'extracted'    => '',
               'file_path'    => '',
               'gpg_name'     => '',
               'gpg_passfile' => '',
               'gpg_passwd'   => '',
               'gpg_path'     => '',
               'group'        => 'Development/Libraries',
               'inst_base'    => '',
               'inst_type'    => '',
               'install'      => '',
               'macros'       => 0,
               'mainpod'      => '',
               'mandir'       => '',
               'name'         => '',
               'no_clean'     => 0,
               'no_deps'      => 0,
               'no_tests'     => 0,
               'package'      => '',

lib/App/CPANtoRPM.pm  view on Meta::CPAN

              ($$self{'no_deps'}   ? ("--nodeps")
                                   : ()),
              ($$self{'rpmbuild'}  ? ("--define","_topdir $$self{'rpmbuild'}")
                                   : ()),
              "$package{topdir}/SPECS/$package{specname}");

   my $cmd = join(' ',@cmd);
   $self->_log_message('INFO',"Attempting system command: $cmd");

   #
   # Build the RPM
   #

   if (system(@cmd) != 0) {
      $self->_log_message('ERR',"Unable to execute $rpmbuild");
   }
   chdir($TMPDIR);

   my $disttag = `rpm --eval '$$self{disttag}'`;
   chomp($disttag);

   $package{'rpmfile'} = "$package{topdir}/RPMS/$package{arch_val}/$package{rpmname}-$package{version}-$package{release}$disttag.$package{arch_val}.rpm";
   $package{'srpmfile'} = "$package{topdir}/SRPMS/$package{rpmname}-$package{version}-$package{release}$disttag.src.rpm";

   $self->_log_message('INFO',"RPM:  " . $package{'rpmfile'});
   $self->_log_message('INFO',"SRPM: " . $package{'srpmfile'});
}

############################################################################
############################################################################

# The process of generating the spec file is taken in large part from the
# cpanspec script.

sub _make_spec {
   my($self) = @_;

   $self->_log_message('HEAD',"Writing spec file: $package{name}");

   setlocale(LC_ALL, "en_US.UTF-8");

   #
   # A few more package values.
   #

   $package{'release'} = $$self{'release'};
   $package{'disttag'} = $$self{'disttag'};
   $package{'url'}     = ($package{'from'} eq 'url' ?
                          $package{'fromsrc'} :
                          "http://search.cpan.org/dist/$package{name}/");
   $package{'epoch'}   = $$self{'epoch'}  if ($$self{'epoch'} ne '');
   $package{'group'}   = $$self{'group'};
   $package{'license'} = ($package{'m_license'} ?
                          $package{'m_license'} :
                          'GPL+ or Artistic');
   $package{'source'} =
     ($package{'from'} eq 'CPAN' ?
      "http://search.cpan.org/authors/id/$package{cpandir}/$package{archive}" :
      $package{'fromsrc'} );

   foreach my $key (keys %{ $Macros{$$self{'macros'}} }) {
      my $val = $Macros{$$self{'macros'}}{$key};
      $package{$key} = $val;
   }

   #
   # Find out if there are is a post-build script.
   #

   $self->_post_build();

   #
   # Make sure we can run rpm.
   #

   my $cmd = 'rpm --version > /dev/null';
   $self->_log_message('INFO',"Attempting system command: $cmd");

   if (system($cmd) != 0) {
      $self->_log_message('ERR','Unable to run rpm.');
   }

   #
   # Make sure that the RPM build hierarchy exists.
   #

   $self->_check_rpm_build();
   $self->_log_message('INFO',
                       "SPEC file: $package{topdir}/SPECS/$package{specname}");

   #
   # Every package needs a packager.
   #

   if ($$self{'packager'}) {
      $package{'packager'} = $$self{'packager'};
   } else {
      my $tmp = `rpm --eval '\%packager'`;
      chomp($tmp);

      if (! $tmp  ||  $tmp eq "\%packager") {
         $self->_log_message('ERR','%packager not defined in ~/.rpmmacros.',
                           'Add it or use the --packager option.');
      }
      $package{'packager'} = $tmp;
   }

   #
   # Some values may contain email addresses which might be of the form:
   #    Name <Email>
   # and the brackets <> conflict with the SPEC file templated.
   #
   # The values where this can occur are:
   #    args     : this is already taken care of (< was turned to \<)
   #    packager : a single value
   #    author   : multiple values
   #
   # We need to escape the brackets in the unhandled cases.
   #

   foreach my $val ($package{'packager'},@{ $package{'author'} }) {
      $val =~ s/</\\</g;



( run in 0.862 second using v1.01-cache-2.11-cpan-bbe5e583499 )