[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Distribution installer that defaults to Debian?



Hi again,

Thanks for your advice Gregor, Jonathan and Robin!

I had a bit of time tonight and took a crack at writing a
bit of code. 
  
The script below outputs the results from:

   Module::Load::Conditional::check_install()
   apt-file find 
   Debian::AptContents::find_file_packages()
   Debian::AptContents::find_perl_module_packages()

With the -i flag, it installs a Debian candidate
if available, preferring the choice of find_perl_module_packages()
using 'sudo apt-get install'.

If there is no Debian candidate, it runs a cpan client
(cpanm if available, otherwise cpan) without 'sudo'
i.e. assuming local::lib.

I chose the preferred installation candidate based on the following
(really) small sample of empirical tests. 

For File::Find

    apt-file found:
    
        libfile-find-object-perl
        libfile-find-object-rule-perl
        libfile-find-rule-perl
        libfile-find-rule-perl-perl
        libfile-find-rule-vcs-perl
        libfile-finder-perl
        perl-doc
    
    Debian::AptContents::find_perl_module_package() found:
    
        perl

For DBIx::Class::Schema

    apt-file found:
    
        libdbix-class-perl
        libdbix-class-schema-loader-perl
    
    Debian::AptContents::find_perl_module_package() found:
    
        libdbix-class-perl


I'd appreciate any feedbacks, and also whether it might
be useful to anyone (besides me).

Best,

Joel

--cut--

#!/usr/bin/perl
use Debian::AptContents;
use Module::Load::Conditional 'check_install';
use YAML::Tiny;
use Getopt::Std;
use Modern::Perl;
no warnings 'uninitialized';

our ($opt_i);
getopt('i');

# flags: 
# -i - install

# input should be perl module name, i.e. DBIx::Class:Schema

my $want = shift;

my $href = check_install(module => $want);

if ( defined $href )
	{
		say qq(Module $want is already installed.);
		my $y = YAML::Tiny->new;
		$y->[0] = $href;
		my $output = $y->write_string();
		$output =~ s/^---.//s;
		say join "\n    ",'',split "\n",$output;
	}

my @apt_file_pkgs = split "\n",qx(apt-file -l find $want);
say_results("apt-file",@apt_file_pkgs);

my $c = Debian::AptContents->new( { homedir => '/tmp/.dh-make-perl' } );

my @apt_contents_file_pkgs = $c->find_file_packages($want);
say_results("Debian::AptContents::find_file_packages()",@apt_contents_file_pkgs);

my $apt_contents_perl_module_pkg = $c->find_perl_module_package($want);
say_results("Debian::AptContents::find_perl_module_package()",$apt_contents_perl_module_pkg);

# which to install

my @debian_available = (
	$apt_contents_perl_module_pkg, 
	@apt_contents_file_pkgs, 
	@apt_file_pkgs
); 

my $debian_pkg = shift @debian_available;

say "\nInstallation candidate for $want ", 
	($opt_i ? "is " : "would be\n"),
	$debian_pkg ? qq(Debian package "$debian_pkg") : "CPAN";

# stop here unless instructed to install (-i option)

say("\nUse -i flag to install."), exit unless $opt_i;

if ($debian_pkg)
	{
		system("sudo apt-get install $debian_pkg");
	} 
else
	{
		my $cpan_installer = qx(which cpanm) || qx(which cpan); # I prefer cpanm

		say("No installer found, neither 'cpan' nor 'cpanm'.  Aborting."),
			exit unless $cpan_installer;

		system("$cpan_installer $want");
	}

sub say_results {
	my ($utility, @results) = @_;
	@results = "(no packages)" unless @results;
	say join "\n    ","$utility found:\n", @results;
	say;
}
__END__


-- 
Joel Roth


Reply to: