how to check if file exists in all subdirectories? - ruby

How to check if the file exists in all subdirectories within chef recipe (not_if guard).

There is no specific answer other than "write some Ruby code to check what you want". You'll probably want to do a Dir glob and then compare the various outputs but maybe you just need an all? and File.exist?.

Related

How to recursively change file permissions only in a ruby script

I have been using the function FileUtils.chmod_R to recursively change files and directories permissions under a given path but now want to change only the file permissions and leave the directories as they are. Looking at the man page for this function I can't see how to do this and I would prefer not to do this with a bash script. Please can someone tell me if this is possible with the FileUtils.chmod_R function or would I have to write additional code to iterate over every file that exist under a given path (recursive) and then FileUtils.chmod it to the desire permission? I am a ruby newbie so please point me someplace if I am asking anything obvious
You could do something like below - this will change permissions of the list of files matched by Dir.glob.
FileUtils.chmod 0400, Dir.glob('/path/to/dir/**/*')
As mentioned in this thread,
Dir.glob("**/*/") # will return list of all directories
Dir.glob("**/*") # will return list of all files

Recursively copy file-types from directory tree

I'm trying to find a way to copy all *.exe files (and more, *.dtd, *.obj, etc.) from a directory structure to another path.
For example I might have:
Code
\classdirA
\bin
\classA.exe
\classdirB
\bin
\classB.exe
\classdirC
\bin
\classC.exe
\classdirD
\bin
\classD.exe
And I want to copy all *.exe files into a single directory, say c:\bins
What would be the best way to do this?
Constraints for my system are:
Windows
Can be Perl, Ruby, or .cmd
Anyone know what I should be looking at here?
Just do in Ruby, using method Dir::glob :
# this will give you all the ".exe" files recursively from the directory "Code".
Dir.glob("c:/Code/**/*.exe")
** - Match all directories recursively. This is used to descend into the directory tree and find all files in sub-directories of the current directory, rather than just files in the current directory. This wildcard is explored in the example code.
* - Match zero or more characters. A glob consisting of only the asterisk and no other characters or wildcards will match all files in the current directory. The asterisk is usually combined with a file extension, if not more characters to narrow down the search.
Nice blog Using Glob with Directories.
Now to copy the files to your required directory, you need to look into the method, FileUtils.cp_r :
require 'fileutils'
FileUtils.cp_r Dir.glob("c:/Code/**/*.exe"), "c:\\bins"
I just have tested, that FileUtils.cp method will also work, in this case :
require 'fileutils'
FileUtils.cp Dir.glob("c:/Code/**/*.exe"), "c:\\bins"
My preference here is to use ::cp method. Because Dir::glob is actually collecting all the files having .exe extensions recursively, and return them as an array. Now cp method is enough here, now just taking each file from the array and coping it to the target file.
Why I am not liking in such a situation, the method ::cp_r ?
Okay, let me explain it here also. As the method name suggests, it will copy all the files recursively from the source to target directory. If there is a need to copy specific files recursively, then ::cp_r wouldn't be able to do this by its own power ( as it can't do selections by itself, which ::glob can do ). Thus in such a situation, you have to give it the specific file lists, it would then copy then to the target directory. If this is the only task, I have to do, then I think we should go with ::cp, rather than ::cp_r.
Hope my explanation helps.
From cmd command line
for /r "c:\code" %f in (*.exe) do copy "%~ff" "c:\bins"
For usage inside a batch file, double the percent signs (%% instead of %)
Windows shell (cmd) command:
for /r code %q in (*.exe) do copy "%q" c:\bin
Double the % characters if you place this in a batch file.

Ruby - Search and collect files in all directorys

I'm trying to search for a certain file type within all directories on my unix system using a ruby script. I understand the following code will search all files ending with .pdf within the current directory:
my_pdfs = Dir['*pdf']
As well as:
my_pdfs = Dir.glob('*.pdf').each do |f|
puts f
end
But how about searching all directories and sub-directories for files with the .pdf extension?
Check out the Find module:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/find/rdoc/Find.html
Using Dir.glob is less than ideal since globbing doesn't handle recursion nearly as well as something like find.
Also if you're on a *nix box try using the find command. Its pretty amazingly useful for one liners.
Maybe something like:
pdfs=Dir['/**/*.pdf']
?
Not using Linux right now, so don't know if that will work. The ** syntax implies recursive listing.

RUBYLIB Environment Path

So currently I have included the following in my .bashrc file.
export RUBYLIB=/home/git/project/app/helpers
I am trying to run rspec with a spec that has
require 'output_helper'
This file is in the helpers directory. My question is that when I change the export line to:
export RUBYLIB=/home/git/project/
It no longer finds the helper file. I thought that ruby should search the entire path I supply, and not just the outermost directory supplied? Is this the correct way to think about it? And if not, how can I make it so RUBY will search through all subdirectories and their subdirectories, etc?
Thanks,
Robin
Similar to PATH, you need to explicitly name the directory under which to look for libraries. However, this will not include any child directories within, so you will need to list any child sub-directories as well, delimiting them with a colon.
For example:
export RUBYLIB=/home/git/project:/home/git/project/app/helpers
As buruzaemon mentions, Ruby does not search subdirectories, so you need to include all the directories you want in your search path. However, what you probably want to do is:
require 'app/helpers/output_helper'
This way you aren't depending on the RUBYLIB environment variable being set a certain way. When you're deploying code to production, or collaborating with others, these little dependencies can make for annoying debugging sessions.
Also as a side note, you can specify . as a search path, rather than using machine-specific absolute paths.

What a safe and easy way to delete a dir in Ruby?

I'd like to delete a directory that may or may not contain files or other directories. Looking in the Ruby docs I found Dir.rmdir but it won't delete non-empty dir. Is there a convenience method that let's me do this? Or do I need to write a recursive method to examine everything below the directory?
require 'fileutils'
FileUtils.rm_rf(dir)
A pure Ruby way:
require 'fileutils'
FileUtils.rm_rf("/directory/to/go")
If you need thread safety: (warning, changes working directory)
FileUtils.rm_rf("directory/to/go", :secure=>true)
If some looking for answer on #ferventcoder comment -
Just a note, I found that with Windows and Ruby 1.9.3 (at least) FileUtils.rm_rf will follow links (symlinks in this case) and delete those files and folders as well. This was found based on creating a symlink with CreateSymbolicLinkW and then running FileUtils.rm_rf against a parent directory the symlinks are in. Not exactly expected behavior.
– ferventcoder
Safest way is to iterate path and delete it manually.
def rec_del(path):
if path is file call FileUtils.rm_rf - it is safe to delete even in windows
if dir call Dir.rmdir which will succeed for empty dir and dir symlink. else will get ENOTEMPTY for regular non empty dir.
iterate the dir and call rec_del for each item.
now call again Dir.rmdir which will succeed
The laziest way is:
def delete_all(path)
`rm -rf "#{path}"`
end

Resources