Ruby - FileUtils.cp deletes file and fails - ruby

I am trying to copy a file in ruby using FileUtils#cp
Unfortunately, Ruby is deleting the file and then is unable to copy it because it is missing.
Is this a known bug or something I am doing wrong with the cp method.
src = "/var/tmp/myfile"
dest = "/usr/bin/myfile"
FileUtils.cp(src, dest)
It always complains that src file is missing but when I check it has been deleted. If I recreate the file and set permissions to 777 the file is present, after running the script it is gone and the copy fails

Place the following in a copy_myfile.rb, then run with: sudo ruby copy_myfile.rb
require 'fileutils'
src = "/var/tmp/myfile"
dest = "/usr/bin"
FileUtils.cp(src, dest)

It seems to work for me in Ruby 1.9.3:
my file permission: -rw-rw-r--
require 'fileutils'
=> true
irb(main):002:0> FileUtils.cp 'test.txt', 'text1.txt'
=> nil
The file does get copied.

Related

rubyzip 2.3.2 read fails on macOS 10.15.7, ruby 3.1.1p18

This fails:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
contents = zip.read(filename)
with this error:
Errno::ENOENT: No such file or directory - content.xml
This succeeds:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
while zip.size == 0
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
end
contents = zip.read(filename)
Why?
Note that #archive_io is a StringIO instance. 'filename' is an entry in the zip file known to exist of 6253 uncompressed bytes. When using Pry as a debugger, I can simply retype/rerun the open_buffer method and suddenly the zip read works. This lead to me adding the loop to re-attempt the open_buffer - a kludge of a solution.

Copy Folder Contents to Parent Directory in Rake (on Windows)

I have a set of files in a folder ../SomeFolder/AndAnother/dist
the dist folder contains a bunch of files and folders that I want to move up a level in a Rake task.
So
../SomeFolder/AndAnother/dist/subFolder/a.txt becomes ../SomeFolder/AndAnother/subFolder/a.txt
I can do this on linux by
task :lift_to_parent do
sh('mv', '../SomeFolder/AndAnother/dist/*', '../SomeFolder/AndAnother')
end
but this Rake task also runs on Windows and on that OS i get Errno::EACCES: Permission denied # unlink_internal
I'm hoping that FileUtils.mv will work on both linux and windows...
but if I
task :lift_to_parent do
FileUtils.mv '../SomeFolder/AndAnother/dist', '../SomeFolder/AndAnother', :force => true
end
I get ArgumentError: same file: ../SomeFolder/AndAnother/dist and ../SomeFolder/AndAnother/dist so I'm clearly missing something to allow FileUtils.mv to copy up a level (or going about this the wrong way)
So, how do I fix my FileUtils version or otherwise use a Rake task to copy a folder structure to its parent?
I've ended up doing this
task : lift_to_parent do
copied_by_jenkins = '../SomeFolder/AndAnother/dist'
copy_pattern = "#{copied_by_jenkins}/**/*"
target_directory = '../SomeFolder/AndAnother/Public'
next unless File.exists? copied_by_jenkins
FileList[copy_pattern].each do |file|
file_path = File.dirname(file).sub! copied_by_jenkins, ''
file_name = File.basename(file)
target_directory = File.join(target_directory, file_path)
destination = File.join(target_directory, file_name)
FileUtils.mkdir_p target_directory
FileUtils.copy_file(file, destination) unless File.directory? file
end
FileUtils.remove copied_by_jenkins
end
but that seems like a lot of the typing to achieve my goal

Create Directory if it doesn't exist with Ruby

I am trying to create a directory with the following code:
Dir.mkdir("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")
unless File.exists?("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")
However, I'm receiving this error:
No such file or directory - /Users/Luigi/Desktop/Survey_Final/Archived/Survey/test (Errno::ENOENT)
Why is this directory not being created by the Dir.mkdir statement above?
You are probably trying to create nested directories. Assuming foo does not exist, you will receive no such file or directory error for:
Dir.mkdir 'foo/bar'
# => Errno::ENOENT: No such file or directory - 'foo/bar'
To create nested directories at once, FileUtils is needed:
require 'fileutils'
FileUtils.mkdir_p 'foo/bar'
# => ["foo/bar"]
Edit2: you do not have to use FileUtils, you may do system call (update from #mu is too short comment):
> system 'mkdir', '-p', 'foo/bar' # worse version: system 'mkdir -p "foo/bar"'
=> true
But that seems (at least to me) as worse approach as you are using external 'tool' which may be unavailable on some systems (although I can hardly imagine system without mkdir, but who knows).
Simple way:
directory_name = "name"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
Another simple way:
Dir.mkdir('tmp/excel') unless Dir.exist?('tmp/excel')
How about just Dir.mkdir('dir') rescue nil ?

Ruby - FileUtils copy_file Permission denied on Windows

I'm making gem that copy files from /template directory (inside the gem) into the current directory of the console.
Here's what it looks like:
require "fileutils"
# Get the console's current directory
destination_dir = Dir.pwd
# Home directory of my gem, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0
home_dir = File.expand_path( "..", File.dirname(__FILE__) )
# Template directory, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
template_dir = File.join( home_dir, "template" )
FileUtils.copy_file( template_dir, destination_dir )
And I got this error:
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:1370:in `initialize': Permission denied -
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template (Errno::
EACCES)
I have checked that the directory does exists by running Dir[template_dir].
Any solution? Thanks
UPDATE to answer comments below
#Babai
I added this line before copy_file, but still doesn't work. Am I doing it right?
FileUtils.chmod(0777, template_dir)
#mudasobwa
Here's the result of the code
# puts "#{template_dir} \n #{destination_dir}"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
C:/Users/myname/Documents/Test
My bad. My template directory contains another folders. So I need to use cp_r instead of copy_file
FileUtils.cp_r( template_dir, destination_dir )

Require all files in sub-directory

I have the following directory tree.
- app.rb
- folder/
- one/
- one.rb
- two/
- two.rb
I want to be able to load Ruby files in the folder/ directory, even the ones in the sub-directories. How would I do so?
Jekyll does something similar with its plugins. Something like this should do the trick:
Dir[File.join(".", "**/*.rb")].each do |f|
require f
end
With less code, but still working on Linux, OS X and Windows:
Dir['./**/*.rb'].each{ |f| require f }
The '.' is needed for Ruby 1.9.2 where the current directory is no longer part of the path.
Try this:
Dir.glob(File.join(".", "**", "*.rb")).each do |file|
require file
end
In my project this evaluates to ["./fixset.rb", "./spec/fixset_spec.rb", "./spec/spec_helper.rb"] which causes my specs to run twice. Therefore, here's a tweaked version:
Dir[File.join(".", "**/*.rb")].each { |f| require f unless f[/^\.\/spec\//]}
This'll safely ignore all *.rb files in ./spec/

Resources