Copying files using "ftools.rb" library

Copying files using "ftools.rb" library

Postby mgate » Wed Apr 19, 2017 11:57 am

I aim to copy a SketchUp file from one folder to another. I'm using the following method from the "ftools.rb" library:
Code: Select all
def copy(from, to, verbose = false)
    $stderr.print from, " -> ", catname(from, to), "\n" if verbose
    syscopy from, to
  end

  alias cp copy

when running my plugin, the file is copied correctly from the original folder to the destination folder but SketchUp crashes afterwards.

SketchUp Application has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.

I tried different options, i.e. keeping the same "to" file name as the "from", also changing it but SketchUp always crashes.
However, when using the move method of the "ftools.rb", it works perfectly with no problem even changing the "to" name:
Code: Select all
  def move(from, to, verbose = false)
    to = catname(from, to)
    $stderr.print from, " -> ", to, "\n" if verbose

    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
      unlink to
    end
    fstat = stat(from)
    begin
      rename from, to
    rescue
      begin
        symlink readlink(from), to and unlink from
      rescue
   from_stat = stat(from)
   syscopy from, to and unlink from
   utime(from_stat.atime, from_stat.mtime, to)
   begin
     chown(fstat.uid, fstat.gid, to)
   rescue
   end
      end
    end
  end

  alias mv move

Any idea where the problem is? Thanks for your time.
Note: I use SU v8
0

mgate 
 

Re: Copying files using "ftools.rb" library

Postby Dan Rathbun » Fri Apr 21, 2017 3:03 am

(1) SU v8 uses Ruby 1.8.x which does not have Unicode character support on Windows. So any unicode characters in the pathnames could cause issues.

(2) Symbolic links require being logged into an administrator account on newer Windows version (I believe v6.0 [Vista] and higher.)

Lastly, you haven't said what kind of file you are copying. ?

Is the file currently in use by SketchUp or your extension ?
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Copying files using "ftools.rb" library

    Postby mgate » Fri Apr 21, 2017 10:00 am

    The goal is to create a copy of two existing template files (not opened) from "Resource" folder to a different path, the target files should have new names too (previously defined in a inputbox).
    templates:
    ...\Resources\XXX AT Template.skp
    ...\Resources\XXX AT Template.xlsx

    Here it is part of the script of my plugin, at the moment just trying to copy the skp file though:
    Code: Select all
    require 'ftools.rb'
    ...
    ...
    folder      = File.dirname( __FILE__ )
    resources   = "Resources"
    resource_name= "XXXXXX AT Template.skp"
    resource_file   = File.join(folder, resources, resource_name).tr("\\","/") # the script will be modified to select the directory via a UI.savepanel dialogue
    file_name   = "123456 AT aa.bb.skp"  # the real name will be picked via an inputbox
    desti_file   = File.join(folder, file_name).tr("\\","/")

    if File.exist?(resource_file)
     File.copy(resource_file, desti_file, verbose = true)
    else
     puts "SKETCHUP TEMPLATE FILE NOT FOUND!"
     return nil
    end
    ...
    ...

    The above script certainly creates the copy in the correct folder but SU crashes afterwards.

    As the move() doesn't crash, I tried to modify it as follow:
    Code: Select all
      def move(from, to, verbose = false)
        to = catname(from, to)
        $stderr.print from, " -> ", to, "\n" if verbose

    #    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
    #      unlink to
    #    end
        fstat = stat(from)
        begin
    #     rename from, to
    #  rescue
          begin
          ln from, to and unlink from
          rescue
       from_stat = stat(from)
       syscopy from, to  and unlink from
       utime(from_stat.atime, from_stat.mtime, to)
       begin
         chown(fstat.uid, fstat.gid, to)
       rescue
       end
          end
        end
      end

      alias mv move

    by using above move(), the resource_file is kept and the desti_file is created but SU crashes too.

    I tried to use Fileutils.rb instead but there are lots of errors when open SU v.8 (I'm using !AdditionalPluginFolders.rb) and below doesn't work:
    Code: Select all
    FileUtils.copy_file(resource_file, desti_file, preserve = false, dereference = true)
    0

    mgate 
     

    Re: Copying files using "ftools.rb" library

    Postby Dan Rathbun » Tue Apr 25, 2017 2:54 pm

    Is the above code within a method? Using local variablesin the TOPLEVEL_BINDING of a script will not work. (They are removed from memory when the script ends.) They should be instance or class variables instead.

    mgate wrote:require 'ftools.rb'


    Where did "ftools.rb" come from ?

    mgate wrote:I tried to use Fileutils.rb instead but there are lots of errors when open SU v.8

    Where did you get the "FileUtils.rb" file from ?

    I have a properly versioned Ruby 1.8.6-p287 standard Ruby library packaged as an RBZ extension for SketchUp v8..13 here:

    https://github.com/DanRathbun/sketchup- ... ses/latest

    mgate wrote: (I'm using "!AdditionalPluginFolders.rb") and below doesn't work:
    FileUtils.copy_file(resource_file, desti_file, preserve = false, dereference = true)


    Using this just adds complexity to the issue at hand. I'd suggest removing it from the equation. And fix the underlying issue.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Copying files using "ftools.rb" library

      Postby Dan Rathbun » Tue Apr 25, 2017 3:30 pm

      mgate wrote:
      Code: Select all
      require 'ftools.rb'
      ...
      folder      = File.dirname( __FILE__ )
      resources   = "Resources"
      resource_name= "XXXXXX AT Template.skp"
      resource_file   = File.join(folder, resources, resource_name).tr("\\","/")
      ...

      Problem here.

      The "Resources" folder is not in a sub-folder of the "Plugins" folder, nor in a sub-folder of YOUR extension sub-folder (which should be in a sub-folder of "Plugins".)

      This should return the correct path:
      resource_path = Sketchup.find_support_file("Resources")

      The above script certainly creates the copy in the correct folder but SU crashes afterwards.[/quote]
      Do you get a BugSplat!, or is a Windows Error Report (WER) generated (ie, check Event Viewer) ?

      Secondly, your rescue clauses are not telling you what error is happening. Do something like this:
      Code: Select all
      begin
        # file operation
      rescue => e
        puts e.inspect # or UI.messagebox(e.inspect)
      else # change to more meaningful message:
        puts "file operation success!"
      end


      Thirdly, we used to tell users to run SketchUp as administrator because all users needed read and write permissions on folder in the SketchUp application's program path. (This can cause file drag and drop to stop working for SketchUp.)
      The alternative is to set permissions for SketchUp and all it's program sub-folders to allow all users full permissions.
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        SketchUcation One-Liner Adverts

        by Ad Machine » 5 minutes ago



        Ad Machine 
        Robot
         



         

        Return to Developers' Forum

        Who is online

        Users browsing this forum: gdesign21, ianko31, Mr Potato and 4 guests

        Visit our sponsors: