aboutsummaryrefslogtreecommitdiffstats
path: root/lib/alaveteli_external_command.rb
diff options
context:
space:
mode:
authorseb <seb@seb-U36JC>2011-11-24 09:21:36 +0000
committerseb <seb@seb-U36JC>2011-11-24 09:21:36 +0000
commita4e533f1588a5d34dca4ab462b58ddd83eed37c5 (patch)
tree58bb3b57ec5664e094ae151c18b6c1915256e364 /lib/alaveteli_external_command.rb
parentd473fce1d0451c913d3ef697d3b45bd58c6fff54 (diff)
parente649c2a7f19d0a75206149d886ff47b3ccda4e91 (diff)
Merge branch 'develop' into xapian-dcabo
Conflicts: spec/models/xapian_spec.rb
Diffstat (limited to 'lib/alaveteli_external_command.rb')
-rw-r--r--lib/alaveteli_external_command.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/alaveteli_external_command.rb b/lib/alaveteli_external_command.rb
new file mode 100644
index 000000000..b967c89b5
--- /dev/null
+++ b/lib/alaveteli_external_command.rb
@@ -0,0 +1,33 @@
+require 'external_command'
+
+module AlaveteliExternalCommand
+ class << self
+ def run(program_name, *args)
+ # Run an external program, and return its output.
+ # Standard error is suppressed unless the program
+ # fails (i.e. returns a non-zero exit status).
+ opts = {}
+ if !args.empty? && args[-1].is_a?(Hash)
+ opts = args.pop
+ end
+
+ xc = ExternalCommand.new(program_name, *args)
+ if opts.has_key? :append_to
+ xc.out = opts[:append_to]
+ end
+ xc.run()
+ if xc.status != 0
+ # Error
+ $stderr.puts("Error from #{program_name} #{args.join(' ')}:")
+ $stderr.print(xc.err)
+ return nil
+ else
+ if opts.has_key? :append_to
+ opts[:append_to] << "\n\n"
+ else
+ return xc.out
+ end
+ end
+ end
+ end
+end