| 1 | |||
| 2 | package App::FQStat::System; | ||
| 3 | # App::FQStat is (c) 2007-2009 Steffen Mueller | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use App::FQStat::Debug; | ||
| 11 | use App::FQStat::Config qw/get_config/; | ||
| 12 | use String::ShellQuote (); | ||
| 13 | |||
| 14 | use base 'Exporter'; | ||
| 15 | our %EXPORT_TAGS = ( | ||
| 16 | 'all' => [qw( | ||
| 17 | exec_local | ||
| 18 | run | ||
| 19 | run_local | ||
| 20 | run_capture | ||
| 21 | run_local_capture | ||
| 22 | )], | ||
| 23 | ); | ||
| 24 | our @EXPORT_OK = @{$EXPORT_TAGS{'all'}}; | ||
| 25 | |||
| 26 | sub exec_local { | ||
| 27 | warnenter if ::DEBUG; | ||
| 28 | warnline "Switching to running '@_' locally." if ::DEBUG > 1; | ||
| 29 | return exec(@_); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub run_local { | ||
| 33 | warnenter if ::DEBUG; | ||
| 34 | warnline "Running '@_' locally." if ::DEBUG > 1; | ||
| 35 | return system(@_); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub run_local_capture { | ||
| 39 | warnenter if ::DEBUG; | ||
| 40 | warnline "Running '@_' locally, capturing." if ::DEBUG > 1; | ||
| 41 | my $cmd = String::ShellQuote::shell_quote(@_); | ||
| 42 | return `$cmd`; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub run { | ||
| 46 | warnenter if ::DEBUG; | ||
| 47 | warnline "Running '@_'." if ::DEBUG > 1; | ||
| 48 | my $cmd = _make_system_call(@_); | ||
| 49 | return system($cmd); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub run_capture { | ||
| 53 | warnenter if ::DEBUG; | ||
| 54 | warnline "Running '@_' capturing." if ::DEBUG > 1; | ||
| 55 | my $cmd = _make_system_call(@_); | ||
| 56 | return `$cmd`; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _make_system_call { | ||
| 60 | warnenter if ::DEBUG > 1; | ||
| 61 | my $ssh = get_config("sshcommand"); | ||
| 62 | |||
| 63 | my $cmd = String::ShellQuote::shell_quote(@_); | ||
| 64 | if (defined $ssh and not $ssh eq '') { | ||
| 65 | if ($ssh =~ s/!COMMAND!/$cmd/g) { | ||
| 66 | $cmd = $ssh; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $cmd = "$ssh $cmd"; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | warnline "Generated shell commannd '$cmd'." if ::DEBUG > 1; | ||
| 74 | return $cmd; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | # lifted and modified from Module::Install::Can ((c) Brian Ingerson, Audrey Tang, Adam Kennedy, et al) | ||
| 79 | # check if we can run some command | ||
| 80 | sub module_install_can_run { | ||
| 81 | warnenter if ::DEBUG; | ||
| 82 | my $cmd = shift; | ||
| 83 | my $ssh = get_config("sshcommand"); | ||
| 84 | require ExtUtils::MakeMaker; | ||
| 85 | require Config; | ||
| 86 | require File::Spec; | ||
| 87 | my $_cmd = $cmd; | ||
| 88 | if (defined $ssh and $ssh ne '') { | ||
| 89 | # too slow... | ||
| 90 | return $_cmd; # err, right! | ||
| 91 | # my $cmd_escape = $cmd; | ||
| 92 | # $cmd_escape =~ s/'/\\'g/; # bad | ||
| 93 | # my $problem = run( | ||
| 94 | # 'perl', '-e', | ||
| 95 | # q|use ExtUtils::MakeMaker; if(-x '| | ||
| 96 | # . $cmd_escape | ||
| 97 | # . q|' or MM->maybe_command('| | ||
| 98 | # . $cmd_escape | ||
| 99 | # . q|'){exit(0)}else{exit(1)}| | ||
| 100 | # ); | ||
| 101 | # return $_cmd unless $problem; | ||
| 102 | |||
| 103 | } | ||
| 104 | else { | ||
| 105 | return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd)); | ||
| 106 | for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') { | ||
| 107 | my $abs = File::Spec->catfile($dir, $cmd); | ||
| 108 | return $abs if (-x $abs or $abs = MM->maybe_command($abs)); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 |