File cpanspec_obs of Package jw-env

#! /usr/bin/perl -w
#
# this integrates cpanspec with obs
# and works around the following bugs in cpanspec:
# - We prefer the full URL for sources, so that update_pkg may work.
# - We prefer the full URL for sources, the filename is more likely to be correct.
#   e.g. here the little 'v' is missing in the short name:
#   Source: http://www.cpan.org/authors/id/K/KC/KCLARK/Text-RecordParser-v%{version}.tar.gz
#   Source: %{cpan_name}-%{version}.tar.gz
#
# 2010-11-12, jw -- 0.2 nuke: BuildRequires:  perl(perl) >= 5.006001
# 2011-03-14, jw -- 0.3  pull in unzip for zip files.
# 2011-04-29, jw -- 0.4 added usage message and own specfile.
# 2011-05-03, jw -- 0.5 Summary should not ned in a dot.
# 2011-05-13, jw -- 0.6 AUTOMATED_TESTING, thanks to Thomas Fahle.
# 2011-05-14, jw -- 0.7 Summary should be capitalized.
# 2011-05-15, jw -- 0.8 removing empty .bs files before %perl_process_packlist
# 2011-06-22, jw -- 0.9 no more calls to bznew. It will fool adrians new source services
# 2011-09-08, jw -- 1.0 accept either bz2 or gz compressed files.

my $version = '1.0';

my $pkg = shift;
my $cpan_name;
my $obs_name;
$ENV{AUTOMATED_TESTING} = 1;

die qq{cpanspec_obs (V$version) Usage:
 cd YOUR_OBS_PROJECT;

 $0 CPAN::NAME
 $0 perl-CPAN-NAME

cpanspec_obs creates a perl package in the openSUSE BuildService.
It downloads the sources from cpan, runs cpanspec to create a suitable specfile, 
and also test-builds the new package.
It leaves a ready package directory for your review.  Ideally, you just need to say
 osc commit perl-CPAN-NAME
afterwards.

} unless $pkg;


if ($pkg =~ m{^perl-})
  {
    $obs_name = $pkg;
    $pkg =~ s{^perl-}{};
    $pkg =~ s{-}{::}g;
    $cpan_name = $pkg;
  }
else
  {
    $cpan_name = $pkg;
    $pkg =~ s{::}{-}g;
    $obs_name = "perl-$pkg";
  }

die "Not in a osc project checkout directory\n" unless -f '.osc/_project';
die "Won't create a package in a packace. Please do cd .. first.\n" if -f '.osc/_package';

run("osc search -V --package $obs_name");
run("osc mkpac $obs_name");
chdir $obs_name or die "osc mkpac did not create ./$obs_name\n";

## FIXME: can we use --follow here?
run("cpanspec -v $cpan_name");
# for my $gz (<*.gz>)
#   {
#     run("bznew $gz");
#   }

rename "$obs_name.spec", "$obs_name.spec.orig";
open I, "<", "$obs_name.spec.orig" or die "cannot read my $obs_name.spec.orig: $!\n";
open O, ">", "$obs_name.spec"      or die "cannot write   $obs_name.spec: $!\n";
my $http_source;
my $need_unzip = 0;
my $suggest_pod_coverage = 1;
while (defined (my $line = <I>))
  {
    chomp $line;
    if ($line =~ m{^#?\s*Source(\d*):\s*https?://})
      {
        $http_source = $1;
        $line =~ s{^#\s*}{};
	# $line =~ s{\.gz$}{.bz2};
	# $line =~ s{\.tgz$}{.tar.bz2};
	$line =~ s{\.tar\.zip$}{.zip};	# FIXME : cpanspec bug?
      }
    elsif ($line =~ m{^#?\s*Source(\d*):\s*})
      {
        my $src_nr = $1;
	# $line =~ s{\.gz$}{.bz2};
	# $line =~ s{\.tgz$}{.tar.bz2};
	$line =~ s{\.tar\.zip$}{.zip};	# FIXME : cpanspec bug?
	if ($src_nr eq $http_source)
	  {
	    $http_source = '';
	    $line =~ s{^S}{# S};
	  }
      }
    elsif ($line =~ m{^%(build|check)\b})
      {
        $line .= "\nexport AUTOMATED_TESTING=1"
      }
    elsif ($line =~ m{^%perl_process_packlist\b})
      {
        # W: zero-length /usr/lib/perl5/vendor_perl/5.12.3/i586-linux-thread-multi/auto/**.bs
        $line = "find %buildroot/%_prefix -name \*.bs -a -size 0 | xargs rm -f\n$line";
      }
    elsif ($line =~ m{^(Build)?Requires:\s*perl\(perl\)})
      {
        # nuke
        # BuildRequires:  perl(perl) >= 5.006001
        # Requires:  perl(perl) >= 5.006001
	next;
      }
    elsif ($line =~ m{^Summary:\s+(.*)$})
      {
        # W: summary-ended-with-dot (Badness: 89)
	# W: summary-not-capitalized (Badness: 63)
	# 
        # FIXME: this should be done inside cpanspec.
        my $summary = $1;
	$summary =~ s{\.+\s*$}{};
	$summary = ucfirst $summary;
	$line = "Summary: $summary";
      }
    if ($line =~ m{^BuildRequires:})
      {
        # FIXME: this should be done inside cpanspec.
	if ($need_unzip)
	  {
            print O "BuildRequires: unzip\n";
	    $need_unzip = 0;
	  }
	if ($suggest_pod_coverage)
	  {
            print O "# BuildRequires: perl(Test::Pod) perl(Test::Pod::Coverage)\n";
	    $suggest_pod_coverage = 0;
	  }
      }
    print O "$line\n";
  }
close O or die "failed to write $obs_name.spec";
run("osc build --local-package");
run("osc vc -m 'initial pull from CPAN with cpanspec_obs-$version'");
run("osc add *.spec *.changes");
run("osc add *.bz2") if <*.bz2>;
run("osc add *.gz") if <*.gz>;

print qq{
### suggested next commands:
cd $obs_name; osc ci
osc submitpac devel:languages:perl -m 'builds, works. Needed by myself'
};


exit 0;
######################
sub run
{
  my ($cmd) = @_;
  print STDERR "$cmd\n";
  system $cmd and warn "ERROR: $!\n";
}
openSUSE Build Service is sponsored by