Trying to identify directories


(Leam Hall) #1

Trying to figure out why this doesn't put just the directory names.
There are directories in options[:source] and if I start an irb
session in that directory, pick a directory name like "docs", and do:
  File.directory?('docs')
it returns true.

Besides a brain, a clue. and a life, what am I missing?

Thanks!

Leam

···

###
Dir.entries(options[:source]).each {|node|
  if File.directory?(node)
    puts " node is: #{node}."
  end
}

# output, even though there are directories present:
  node is: ..
  node is: ...

# If I remove the "File.directory?() it puts all the files and directories.


(Gonzalo Garramuno) #2

You are missing the full path to the directory or Dir.chdir to the directory you call the script from

···

El 30/07/18 a las 08:46, leam hall escribió:

Trying to figure out why this doesn't put just the directory names.
There are directories in options[:source] and if I start an irb
session in that directory, pick a directory name like "docs", and do:
   File.directory?('docs')
it returns true.

Besides a brain, a clue. and a life, what am I missing?

--
Gonzalo Garramuño


(Ken D'Ambrosio) #3

Besides a brain, a clue. and a life, what am I missing?

Your problem almost certainly isn't with the code you show, which works fine, but rather, with what's in options[:source] .
I'm betting there's something in there (perhaps even something dumb, like a <CR>) that's not what you think it should be.
Or, possibly, it's an unfully qualified directory. But the code directly below is spot-on; no changes required. Instead, add a line as I did, and see what's going on.

Thanks!

Leam

###

puts "And here is what we think our PWD is: #{Dir.pwd} and our source directory: #{options[:source]}."
Dir.entries(options[:source]).each {|node|
   if File.directory?(node)
     puts " node is: #{node}."
   end
}

···

On 2018-07-30 07:46, leam hall wrote:

# output, even though there are directories present:
  node is: ..
  node is: ...

# If I remove the "File.directory?() it puts all the files and directories.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Leam Hall) #4

Besides a brain, a clue. and a life, what am I missing?

Your problem almost certainly isn't with the code you show, which works
fine, but rather, with what's in options[:source] .
I'm betting there's something in there (perhaps even something dumb, like a
<CR>) that's not what you think it should be.
Or, possibly, it's an unfully qualified directory. But the code directly
below is spot-on; no changes required. Instead, add a line as I did, and see
what's going on.

Thanks, I'll give that a look The <CR> is likely the culprit.

In this case the command set options[:source] to the directory the
code was in, but used the explicit path "/home/me/lang/ruby".

···

On Mon, Jul 30, 2018 at 8:32 AM, Ken D'Ambrosio <ken@jots.org> wrote:

On 2018-07-30 07:46, leam hall wrote:

###

puts "And here is what we think our PWD is: #{Dir.pwd} and our source
directory: #{options[:source]}."
Dir.entries(options[:source]).each {|node|
  if File.directory?(node)
    puts " node is: #{node}."
  end
}

# output, even though there are directories present:
  node is: ..
  node is: ...

# If I remove the "File.directory?() it puts all the files and
directories.


(Leam Hall) #5

And this was it. I need to spend some time figuring out how to handle
this with recursion, but it's the root cause.

Thanks!

Leam

···

On Mon, Jul 30, 2018 at 8:26 AM, Gonzalo Garramuño <ggarra13@gmail.com> wrote:

You are missing the full path to the directory or Dir.chdir to the directory
you call the script from

--
Gonzalo Garramuño


(Leam Hall) #6

Updated, and full code. Still working on the final product, but this is a big step forward. I'm learning to use Ruby on files and file-systems. This task is to find all the '.git' directories so I can grab the repository URLs and clone them into a new directory.

Leam

···

On 07/30/2018 06:39 PM, leam hall wrote:

On Mon, Jul 30, 2018 at 8:26 AM, Gonzalo Garramuño <ggarra13@gmail.com> wrote:

You are missing the full path to the directory or Dir.chdir to the directory
you call the script from

And this was it. I need to spend some time figuring out how to handle
this with recursion, but it's the root cause.

####

# ruby source_dest_dir -s /home/me/lang/git

require 'optparse'
git_dirs = []
options = Hash.new{}

option_parser = OptionParser.new {|opts|
   opts.on('-s DIR', '--source', "Source") {|s|
     options[:source] = s
   }
   opts.on('-d DIR', '--dest', "Destination") {|d|
     options[:dest] = d
   }
}
option_parser.parse!

begin
   options[:dest] = Dir.pwd unless options.has_key?(:dest)
   options[:source] = Dir.pwd unless options.has_key?(:source)
   raise "Cannot read #{options[:source]}." unless File.readable?(options[:source])
   raise "Can't write to #{options[:dest]}." unless File.writable?(options[:dest])
rescue RuntimeError => e
   puts "Access failure: #{e}."
end

def walk_dir(dir, git_dirs)
   tree_dirs = ['.', '..']
   Dir.entries(dir).each {|node|
     next if tree_dirs.include?(node)
     node_ref = "#{dir}" + '/' + node
     next unless File.executable?(node_ref)
     if File.directory?(node_ref) and File.readable?(node_ref)
       if node == '.git'
         git_dirs << node_ref
       else
         walk_dir(node_ref, git_dirs)
       end
     end
   }
end

walk_dir(options[:source], git_dirs)
puts git_dirs


(Toshihiko Ichida) #7

Hi,

This
task is to find all the '.git' directories so I can grab the repository

URLs

and clone them into a new directory.

You can search all .git files in a directory and subdirectories like

irb

dir = '.'

=> "."

git_dirs = Dir["#{dir}/**/.git"]

=> ["./database/projectdb/.git", ...

Regards,
Toshihiko Ichida

···

2018-07-31 9:57 GMT+09:00 Leam Hall <leamhall@gmail.com>: