App-Chart

 view release on metacpan or  search on metacpan

devel/makefile-to-debs.pl  view on Meta::CPAN

#!/usr/bin/perl -w

# Copyright 2008, 2009, 2010, 2012, 2013, 2016 Kevin Ryde

# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart.  If not, see <http://www.gnu.org/licenses/>.


# Usage: ./makefile-to-debs.pl
#
# Look at Makefile.PL and compare the declared PREREQ_PM modules, and their
# versions, against what's in the Depends line of the debian/control file.
#

use strict;
use warnings;
use AptPkg::Config;
use Data::Dumper;
use File::Spec;
use FindBin;
use List::Util;
use Memoize;
use Module::CoreList;
use Module::Depends::Intrusive;
use Module::Util;
use Parse::DebControl;
use File::Slurp;
use version;

#use Smart::Comments;

# modules in perl from $minimum_perl_version onwards are considered core
my $minimum_perl_version = version->new('v5.10');

my $toplevel_dir = File::Spec->curdir;
my $makefile_filename = File::Spec->catdir ($toplevel_dir, 'Makefile.PL');
if (! -f $makefile_filename) {
  $toplevel_dir = File::Spec->catdir ($toplevel_dir, File::Spec->updir);
  $makefile_filename = File::Spec->catdir ($toplevel_dir, 'Makefile.PL');
  if (! -f $makefile_filename) {
    die "Cannot find Makefile.PL in current directory or parent directory\n";
  }
}
print "toplevel: $toplevel_dir\n";


my $makefile_contents = File::Slurp::slurp ($makefile_filename);
if ($makefile_contents =~ /^use (5\.[0-9.]+)/m) {
  $minimum_perl_version = version->new ($1);
  print "Makefile.PL: perl version $minimum_perl_version\n";
} else {
  print "Makefile.PL: no use perl version, leave at $minimum_perl_version\n";
}

my $deps = Module::Depends::Intrusive->new;
$deps->dist_dir($toplevel_dir);
$deps->find_modules;
if (my $err = $deps->error) {
  die "Cannot get Makefile.PL dependencies: $err";
}

$Data::Dumper::Sortkeys = 1;
my $requires = $deps->{'requires'}; # hashref
print "Module::Depends::Intrusive gives ", Dumper ($requires);


sub depends_to_hash {
  my ($str) = @_;
  my %hash;
  foreach my $elem (split (/, */, $str)) {
    my $package = $elem;
    my $version = 0;
    if ($elem =~ / \(>= (.*)\)/) {
      $package = $`;
      $version = $1;
    }
    $hash{$package} = $version;
  }
  return \%hash;
}

my $parser = Parse::DebControl->new;
my $control_filename = File::Spec->catfile ($toplevel_dir,'debian','control');
print "control file: $control_filename\n";
my $control = $parser->parse_file ($control_filename, {stripComments=>1});
print "Parse::DebControl gives ", Dumper ($control);
my $source = $control->[0];
my $binary = $control->[1];

my $control_depends = $binary->{'Depends'};
if (! defined $control_depends) {
  die "Oops, no Depends line\n";
}
print "Got Depends: $control_depends\n";
$control_depends = depends_to_hash ($control_depends);
{ ## no critic (RequireInterpolationOfMetachars)
  delete $control_depends->{'${misc:Depends}'};
}

my $build_depends = $source->{'Build-Depends'};
if (! defined $build_depends) {
  die "Oops, no Depends line\n";
}
print "Got Build-Depends: $build_depends\n";
$build_depends = depends_to_hash ($build_depends);


sub file_to_deb {
  my ($filename) = @_;
  ### file_to_deb(): $filename
  foreach my $prog (# 'dlocate',
                    # 'dloc',
                    'dpkg') {
    my $matches = `$prog -S $filename`;
    if ($matches =~ m{(.*): /usr/(share|lib)/perl/5.*/$filename}) {
      return 'perl';
    } elsif ($matches =~ m{(.*): /usr/(share|lib)/perl5/$filename}) {
      return $1;
    } else {
      print "Oops, $filename not found\n$prog gave:\n";
      print $matches;
    }



( run in 1.008 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )