Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

A hack to automatically install all dependencies for a given script

by mascip (Pilgrim)
on Feb 12, 2014 at 00:14 UTC ( #1074554=perlquestion: print w/ replies, xml ) Need Help??
mascip has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I wondered if there's any value for such a script, if it's silly, or if there's something better that already exists.

Today I tried using scan-prereqs-cpanfile for the first time, and was disappointed that it didn't detect all of the dependencies of a project. So I had to install quite a few dependencies manually. Hence this little hack to never need to do this again - hopefully.

Warning: this code hasn't been tested at all - I'm at a friend's on Windows, but I really want to get feedback on this idea, and I won't be near my computer for two days. Here is the very simple pseudo code:

### THE SCRIPT:
my $cmd_from_user = join ' ', @ARGV;
while( my $dep = return_a_missing_dependency_for_running($cmd_from_user) ) {
    install_a_dependency($dep);
}

### FUNCTIONS:
use Sysadm::Install qw(tap); # tap($cmd): To run a bash command and read the output

sub install_a_dependency {
    my $dep = shift;

    # TODO: Possibly, prompt me (yes/no) to ask if I want to install $dep by running this command:
    my $cmd_to_install_dep = "cpanm $dep"; # Adapt this to your own needs
    my ($stdout, $stderr, $exit_code) = tap($cmd_to_install_dep);
    
    die "Could not install module $dep" if could_not_install_dep($stderr); # Some regex in here
}

sub return_a_missing_dependency_for_running {
    my $cmd_from_user = shift;

    my ($stdout, $stderr, $exit_code) = tap($cmd_from_user);
    
    return extract_missing_dependency_from_output($stderr); # some regex in here. Returns an empty String if no dependencies are missing.
}    

This could easily be adapted to any dependency management system. For example for Carton you would do:

# Add the dependency to the cpanfile, and install it with Carton
my $cmd_to_install_dep = $qq{echo "requires '$dep';" >> cpanfile; carton install}; 

Obviously I'm thinking: if it worked as expected and was useful, it would bloody well exist already. Either I didn't look in the right place, or there is a reason why this is a bad idea.

What do you think?

Comment on A hack to automatically install all dependencies for a given script
Re: A hack to automatically install all dependencies for a given script (scandeps)
by Anonymous Monk on Feb 12, 2014 at 00:27 UTC

      Cheers, I'll try it out =)

      I'm not sure why scan-prereqs-cpanfile didn't find all of my dependencies. In two days I'll see if scandeps does.

Re: A hack to automatically install all dependencies for a given script
by Anonymous Monk on Feb 12, 2014 at 02:58 UTC
      Cheers :-) Well, I kind of knew I was re-inventing the wheel. It was fun!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1074554]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-25 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls