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'      => '',
               'packager'     => '',
               'patch'        => '',
               'patch_dir'    => '',
               'prefix'       => 'perl-',
               'release'      => 1,
               'rem_provide'  => [],
               'rem_require'  => [],
               'repl_provide' => [],
               'repl_require' => [],
               'rpmbuild'     => '',
               'runtime_rec'  => 0,
               'script'       => '',
               'script_dir'   => '',
               'sign'         => 0,
               'spec_only'    => 0,
               'summary'      => '',
               'test_rec'     => 0,
               'version'      => '',
               'yum'          => '',
              };

   my $cwd             = `pwd`;
   chomp($cwd);
   $$self{'cwd'}       = $cwd;

   $COM                = $0;
   $COM                =~ s/^.*\///;
   $DIR                = $0;
   $DIR                =~ s/\/?$COM$//;
   $DIR                = "."  if (! $DIR);
   chdir $DIR;
   $DIR                = `pwd`;
   chomp($DIR);

   bless $self, $class;

   $package{'CMD'}     = $COM;
   $package{'command'} = $0;
   $package{'args'}    = $self->_args();
   $package{'self'}    = $self;

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

   return $self;
}

sub _args {
   my($self) = @_;
   my @args = @ARGV;
   map {
      s/([<>\*\|`&\$!#\(\)\[\]\{\}:'"\ \	])/\\$1/g;
   } @args;
   return join(' ',@args);
}

# %package =
#   # Argument parsing step:             Notes
#   # ------------------------------     --------------------------------
#   CMD        => cpantorpm              This script
#   VERSION    => 1.00                   Version of this script
#   command    => STRING                 The command executed
#   args       => STRING                 The command line arguments
#                                        This must only be used in a comment
#                                        in the SPEC file.
#   date       => STRING                 Current timestamp
#   incl_tests => 0/1                    1 if we'll be adding tests to SPEC
#   incl_deps  => 0/1                    1 if we'll be included requires
#                                        in the SPEC
#   incl_compat=> 0/1                    1 if we'll be including the MODULE_COMPAT
#                                        line in the SPEC
#
#   # Init step:
#   # ------------------------------     --------------------------------
#   TMP        => $TMPDIR                Directory where we'll store things
#
#   # Package retrieval step
#   # ------------------------------     --------------------------------
#   from       => file/dir/url/CPAN
#   fromsrc    => Foo::Bar               how it was passed in
#   DIR        => $TMPDIR/Foo-Bar-1.0
#   dir        => Foo-Bar-1.0
#   dist       => Foo-Bar
#   vers       => 1.0
#   archive    => Foo-Bar-1.0.tgz        Set for URL/File/CPAN
#   ext        => tgz                    Set for URL/File/CPAN
#   filetype   => tar.gz                 Set for URL/File/CPAN
#                                        One of: tar.gz, tar.bz2, zip
#   cpandir    => S/SB/SBECK             Set for CPAN
#
#   # Reading metadata step
#   # ------------------------------     --------------------------------
#   build      => Build.PL               The Build.PL file (if one exists)
#   make       => Makefile.PL            The Makefile.PL file (if one exists)
#   m_name     => Foo-Bar                Should be the same as 'dist'
#   m_version  => 1.0                    Should be the same as 'vers'
#   m_abstract => Summary text
#   m_description => Description
#   m_keywords => [ WORD, WORD, ... ]
#   m_author   => [ AUTHOR, AUTHOR, ... ]
#   m_provides => { FEATURE => { version => VERS,
#                                file    => FILE } ... }
#                                        FILE = lib/Foo/Bar.pm

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

         $self->_log_message('ERR','Unable to locate rpmbuild command');
      }

      my @out = `$rpm -ba 2>&1`;
      chomp(@out);
      my @tmp = grep(/unknown option/,@out);
      if (@tmp) {
         $self->_log_message('ERR',
                             'Unable to locate rpm command that supports -ba');
      }

      $rpmbuild = $rpm;
   }

   if ($$self{'no_tests'} == 1) {
      $ENV{'RPMBUILD_NOTESTS'} = 1;
   }

   my @cmd = ($rpmbuild,"-ba",
              ($$self{'no_clean'}  ? ("--noclean")
                                   : ("--clean")),
              ($$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);



( run in 0.976 second using v1.01-cache-2.11-cpan-ceb78f64989 )