Ruby on Rails rescuing from 'Errno::ENOENT' - ruby-on-rails

I need to rescue from 'Errno::ENOENT' in a Ruby on Rails 3.0.4 application. I currently have controller with the following code:
begin
`lame #{parameters}`
rescue Errno::ENOENT
logger.info "command 'lame' not found: ensure LAME is installed"
end
However, the log code is never called, but the logs show:
script/rails: No such file or directory - lame ...
If I create a ruby script with same snippet, the exception is rescued.

In Ruby 1.8, Errno::ENOENT is not raised by shell execution / back-ticks - the error you're seeing is standard error, printed by the shell. If you want to detect this, I'd recommend looking for an exit code of 127:
`lame #{parameters} 2>&1`
if $?.exitstatus == 127
logger.info "command 'lame' not found: ensure LAME is installed"
end
However, this will raise Errno::ENOENT in Ruby 1.9.
You might consider checking the output from which lame instead:
lame_installed = system("which lame >/dev/null")
# or even better
lame_available = !(lame_path = `which lame`.strip).empty? && File.executable?(lame_path)
Further reading:
http://www.faqs.org/docs/abs/HTML/exitcodes.html

Related

Ruby GPGME::Error> Inappropriate ioctl for device

I'm unable to import a key file. I'm using ruby 1.9.3 and ruby-gpgme
https://github.com/ueno/ruby-gpgme
require "gpgme"
begin
key = GPGME::Key.import(File.open("private.key"), {:password => "redacted"})
rescue GPGME::Error => g
puts g.inspect
puts g.message
puts g.code
puts g.source
end
Output
Inappropriate ioctl for device
More info:
/usr/local/opt/rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/gpgme-2.0.2/lib/gpgme/ctx.rb:43:in new': Inappropriate ioctl for device (GPGME::Error)
from /usr/local/opt/rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/gpgme-2.0.2/lib/gpgme/key.rb:120:inimport'
from gpgme.rb:17:in `'
Here is the ctx.rb file, but I don't get what could be causing the issue...
https://github.com/ueno/ruby-gpgme/blob/master/lib/gpgme/ctx.rb
The error was a result of some issue with gpgme. So I reinstalled gpgme. I used "brew gpgme". now it seems to be working.

ExecJS::RuntimeError running the Getting Started with Rails blog on cygwin (windows7)

I am trying to run the Getting Started with Rails blog on cygwin (windows7). I get the following error message:
ExecJS:: RuntimeError in Welcome#index
module.js:340
throw err;
^
Error: Cannot find module 'C:\tmp\execjs20130903-50672-1vn7gqc.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
(in /usr/lib/ruby/gems/1.9.1/gems/turbolinks-1.3.0/lib/assets/javascripts /turbolinks.js.coffee)
node is installed.
This is after
$rails generate controller welcome index
$rails s
I am running Rails 4.0 on cygwin
Any ideas why this might be happening?
Thanks
umbregachoong
I encountered this error and it had to do with the path to the temp file being wrong. I was able to fix it by changing the following two files in \gems\[ruby version]\gems\execjs-2.0.2\lib\execjs.
(Possibly found in \usr\lib\ruby\, but that depends on how your Ruby is installed. I'm using RVM so mine is different.)
external_runtime.rb
compile_to_tempfile(source) do |file|
extract_result(#runtime.send(:exec_runtime, file.path))
end
end
should change to
compile_to_tempfile(source) do |file|
filepath = file.path
if ExecJS.cygwin? && #runtime.name == "JScript"
IO.popen("cygpath -m " + file.path) { |f| filepath = f.read }
filepath = filepath.gsub("\n","")
end
extract_result(#runtime.send(:exec_runtime, filepath))
end
end
module.rb
Add this right before the last two ends.
def cygwin?
#cygwin ||= RbConfig::CONFIG["host_os"] =~ /cygwin/
end
After this restart your Rails server and with any luck it should work.
Source: https://github.com/sstephenson/execjs/issues/78
don't do anything just go to application/assets/javascript/application.js and remove
'//'
from
//=require turbolinks
to
=require turbolinks
that will basically uncomment the line from the file
This step made my work done when i faced similar error in Default rails server going through RAILS Tutorial. I am using Windows 10 PC and this solved my case

Paperclip File Not Found Error

I just switched from a paperclip rails plugin to a paperclip gem. The project is a rails 2.3 application and I am using paperclip 2.7.2 gem.
I am getting the following odd error:
identify: unable to open image `file': No such file or directory # error/blob.c/OpenBlob/2617.
identify: no decode delegate for this image format `file' # error/constitute.c/ReadImage/544.
Seems like paperclip is looking for a file called 'file' but I am not sure why. I didn't change any of the code we had before. It used to work, all I did was upgrade to a newer version and use a gem over a plugin.
Any ideas?
Update
It to be a bug in paper clip where it does not parse the content of the command properly. I dug deep into the paperclip source to find:
def run(cmd, arguments = "", local_options = {})
if options[:image_magick_path]
Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
end
command_path = options[:command_path] || options[:image_magick_path]
Cocaine::CommandLine.path = [Cocaine::CommandLine.path, command_path].flatten.compact.uniq
local_options = local_options.merge(:logger => logger) if logging? && (options[:log_command] || local_options[:log_command])
Cocaine::CommandLine.new(cmd, arguments, local_options).run
end
and
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
def self.from_file file
file_path = file.respond_to?(:path) ? file.path : file
raise(Paperclip::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
geometry = begin
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
rescue Cocaine::ExitStatusError
""
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
end
parse(geometry) ||
raise(NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
end
The Paperclip.run command fails to replace the :file placeholder for some reason. I verified this by running the following on the commandline:
identify :file
Monkey patching the replacement by hand yields other errors where a similar thing happens.
Ok I managed to solve it.
It was a problem with Cocaine. Seems paperclip has a dependency on cocaine that only says it must be Cociane > 0.02. The latest version of Cocaine 0.4.2 (at the time of this writing) has a new API which is not backward compatible. You need to downgrade to Cocaine 0.3.2.
Simply put this in your Gemfile before paperclip:
gem "cocaine", "0.3.2"

Running soffice (libreoffice or openoffice) from command line in ruby on rails

I have an application which I would like to use libreoffice to batch convert files to html. I have the following method:
def set_versioned_odt
condition = %x(soffice --invisible --convert-to html /path/to/file.odt --outdir /path/to/dir/)
if condition
Rails.logger.info "This has been finished"
else
Rails.logger.info "This failed..."
end
end
When I run this method in the application. It will return a failing statement; however, if I run this command in the rails console, soffice will process correctly. Does anyone know what I am doing wrong in the rails app?
Thanks.
Best guess is that soffice isn't in the path. try specifying the full path to soffice.

Rails 3.1 rake test:profile deprecation warning and empty output files

When runnign rake test:profile I get:
DEPRECATION WARNING: read_csv_fixture_files is deprecated and will be
removed from Rails 3.2. (called from block in autorun at
/Users/hade/.rvm/rubies/ruby-1.9.2-p290-perf/lib/ruby/1.9.1/minitest/unit.rb:508)
Unit.rb around line 508:
def self.autorun
at_exit {
next if $! # don't run if there was an exception
# the order here is important. The at_exit handler must be
# installed before anyone else gets a chance to install their
# own, that way we can be assured that our exit will be last
# to run (at_exit stacks).
exit_code = nil
at_exit { exit false if exit_code && exit_code != 0 }
exit_code = MiniTest::Unit.new.run(ARGV) <------ LINE 508
} unless ##installed_at_exit
##installed_at_exit = true
end
Is MiniTest calling this read_csv_fixture_files method? Is this normal behaviour with my setup? How can I remove this?
My environment:
Rails 3.1.0
Ruby 1.9.2-p290 with GC-patch
My tests seem to run ok, but my output files are useless. In my output files I only have this line:
ActiveSupport::Testing::Performance::Profiler#run
But when I run rails profiler 'User.all' I get nice console output and output files with some content.
Any help would be really appreciated. I have tried different Ruby versions with GC-patch, but no help there.

Resources