FileUtils.cp_r - how to prune

I am doing
    FileUtils.cp_r( "source/.", "generated")
to copy a directory tree.

How can I prune out any sub_tree with /.svn/ ?

I started to dig into File.find with File.prune but it seems to be getting
somewhat messy, and maybe there is an easier way.

Thanks!

Hi,

I am doing
    FileUtils.cp_r( "source/.", "generated")
to copy a directory tree.

How can I prune out any sub_tree with /.svn/ ?

If you want a tree without the .svn dirs,
one way would be to remove the .svn subtrees afterwards with something like

(these use *nix utils so if you need it to work in pure ruby, replace finds with corresponding results = ; Find.find{|filename| if filename.is_bad? then Find.prune else results << filename end })

subdirs = `find . -type d -name .svn`.split(/\n/) # or the Find.find equivalent
subdirs.each{|sd| FileUtils.rm_r(sd)}

Or the hard way:
cd to source dir and

dirs = `find . -type d -false -name .svn`.split(/\n/)
files = `find . -type f`.split(/\n/).find_all{|f| dirs.include? File.dirname(f)}
files.each{|f|
   # f should be relative to source dir root
   FileUtils.mkdir_p(File.join(target_dir, File.dirname(f)))
   FileUtils.cp(f, File.join(target_dir, f))
}

(maybe there's yet another easier way, I don't know)

-Ilmari

···

On 19.12.2004, at 04:57, itsme213 wrote:

Thanks, I'll keep that as a backup. But I should have been more explicit ...

> I am doing
> FileUtils.cp_r( "source/.", "generated")
> to copy a directory tree.
>
> How can I prune out any sub_tree with /.svn/ ?

How can I prune out any sub_tree with /.svn/ in its path from even being
copied over in the first place?

See the hard way. It collects a list of directories that don't have .svn in the path, then collects all files that are in that list of directories, then...
oops, forgot to make it first create the directories (now empty dirs aren't created.)
Anyhow.
1. mkdir every directory in the list of directories to target dir (and set permissions to match)
2. copy every file from list of files to target dir

Or did you want to do something different..?

-Ilmari

···

On 19.12.2004, at 06:37, itsme213 wrote:

Thanks, I'll keep that as a backup. But I should have been more explicit ...

I am doing
    FileUtils.cp_r( "source/.", "generated")
to copy a directory tree.

How can I prune out any sub_tree with /.svn/ ?

How can I prune out any sub_tree with /.svn/ in its path from even being
copied over in the first place?

The easiest way is probably just to modify FileUtils.cp_r() to add an
":ignore => %r{}" option (and adjust fu_copy_dir() accordingly). A quick
glance looks like only a handful of lines would be affected/added.

andrew

···

On Sun, 19 Dec 2004 04:35:26 GMT, itsme213 <itsme213@hotmail.com> wrote:

Thanks, I'll keep that as a backup. But I should have been more explicit ...

> I am doing
> FileUtils.cp_r( "source/.", "generated")
> to copy a directory tree.
>
> How can I prune out any sub_tree with /.svn/ ?

How can I prune out any sub_tree with /.svn/ in its path from even being
copied over in the first place?

--
Andrew L. Johnson http://www.siaris.net/
      It's kinda hard trying to remember Perl syntax *and* Occam's
      razor at the same time :slight_smile:
          -- Graham Patterson

"Ilmari Heikkinen" <kig@misfiring.net>

See the hard way.

It works just fine, thanks so much. I only tried the 1st versionn earlier
....

See the hard way.

Spoke too soon. The Find.find version (on XP) seems to need absolute
pathname arguments, and passing absolute pathnames into itsblock. This is
what makes things messy.

Ok, this was not as messy or as complicated as I thought ... once I figured
out how to avoid getting into absolute pathnames for Find.find.

I did:
Dir.chdir(an_absolute_path) do # block executed with process directory set
    Find.find(".") do |f| # now I can work with relative paths
        Find.prune if f =~ /.svn/
        target = File.join(target_dir, f) # easy with relative paths
        # either FileUtils.mkdir(target) or FileUtils.cp(f,target)
    end
end

Thanks for the suggestions.

"Andrew Johnson" <ajohnson@cpan.org> wrote in message
news:tkixd.524094$Pl.259735@pd7tw1no...

> Thanks, I'll keep that as a backup. But I should have been more explicit

....

···

On Sun, 19 Dec 2004 04:35:26 GMT, itsme213 <itsme213@hotmail.com> wrote:
>
>> > I am doing
>> > FileUtils.cp_r( "source/.", "generated")
>> > to copy a directory tree.
>> >
>> > How can I prune out any sub_tree with /.svn/ ?
>
> How can I prune out any sub_tree with /.svn/ in its path from even being
> copied over in the first place?

The easiest way is probably just to modify FileUtils.cp_r() to add an
":ignore => %r{}" option (and adjust fu_copy_dir() accordingly). A quick
glance looks like only a handful of lines would be affected/added.

andrew

--
Andrew L. Johnson http://www.siaris.net/
      It's kinda hard trying to remember Perl syntax *and* Occam's
      razor at the same time :slight_smile:
          -- Graham Patterson

itsme213 wrote:

See the hard way.

Spoke too soon. The Find.find version (on XP) seems to need absolute
pathname arguments, and passing absolute pathnames into itsblock. This is
what makes things messy.

Messy like, Dir.getwd.concat( "path/to/file" ) messy?

Zach