Ruby - FileUtils copy_file Permission denied on Windows - ruby

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 )

Related

Difference between '.' and File.dirname(__FILE__) in RUBY

I am working my way through a Lynda.com Ruby course and building a small app.
In order to make the file portable, the teacher uses this form below.
APP_ROOT = File.dirname(__FILE__)
I understand the purpose of the constant FILE as it always resolves the current file no matter what its name.
When I type p APP_ROOT it resolves to a . # => '.' which I understand represents this current directory.
What I don't get is why, if File.dirname(__FILE__) always == '.', why not just use '.' ?
For example the output of:
$LOAD_PATH.unshift( File.join(APP_ROOT, 'lib'))
p $:
is equal to
$LOAD_PATH.unshift( File.join('.', 'lib'))
p $:
when I p $: I get the same exact results for either line. What is the value of File.dirname(__FILE__) over '.' ?
Also, I searched all over but I'm not sure.
If I am in directory /home/one/apps
If I enter '.'
is that equal to the apps directory OR does that mean the entire absolute path including the final directory? so . is really /home/ones/apps not just /apps
Thanks in advance.
File.dirname(__FILE__) isn’t always the same as the current directory. It will be the same if you start your Ruby program from the same directory as the file, but will be different if you start from a different directory.
For example if you have a subdirectory subdir containing foo.rb with the contents:
puts File.dirname(__FILE__)
then running from parent directory you get this:
$ ruby subdir/foo.rb
subdir
but if you cd into the directory and then run the file you get this:
$ cd subdir
$ ruby foo.rb
.

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

how to read file using path in ruby by function IO.readlines("path")[0]

i want to read first line of file by using following function in ruby
IO.readlines("path")[0]
But file is not in current directory, so i use path there
puts IO.readlines("Home/Documents/vikas/SHIF.doc")
but it is giving error as
a1.rb:1:in `readlines': No such file or directory # rb_sysopen - Home/Documents/vikas/SHIF.doc (Errno::ENOENT)
from a1.rb:1:in `<main>'
You can also open a file and read only the first line instead of the entire file
File.open("Home/Documents/vikas/SHIF.doc").readline
You can use File.expand_path:
puts IO.readlines(File.expand_path("Home/Documents/vikas/SHIF.doc", __FILE__))
Note however that it will create path relatively to a file directory, not to a root directory.
If you are using rails, you could use:
puts IO.readlines(Rails.root.join 'Home', 'Documents', 'vikas', 'SHIF.doc')

Ruby - FileUtils.cp deletes file and fails

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.

Rake and current directory

How do I get the directory where the rakefile.rb is located?
I want to use this as my root directory to locate everything off.
use __FILE__ to get the file name then you can get the directory from there:
in test.rb
puts __FILE__
output:
/users/foo/test.rb
__FILE__ resolves to the full path of the file it is in.
Use this to get the dir name:
File.dirname(__FILE__)
You can get it by calling application.original_dir method. In task you can achieve application object using application method on task object.
Why not just use Dir.pwd
?
As of Ruby 2 you can use __dir__ instead of File.dirname(__FILE__) to get the directory that contains the current script.
If this is a RoR app your Rakefile.rb should be in your RAILS_ROOT directory. So in any script you can specify file location like
config.load_paths += %W( #{RAILS_ROOT}/extras )

Resources