[BioRuby-cvs] bioruby/lib/bio/shell core.rb,1.24,1.25

Katayama Toshiaki k at dev.open-bio.org
Tue Jun 26 01:41:35 UTC 2007


Update of /home/repository/bioruby/bioruby/lib/bio/shell
In directory dev.open-bio.org:/tmp/cvs-serv14986/lib/bio/shell

Modified Files:
	core.rb 
Log Message:
* changed to Dir.chdir only on mkdir/save instead of start up time


Index: core.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/shell/core.rb,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** core.rb	5 Apr 2007 23:35:41 -0000	1.24
--- core.rb	26 Jun 2007 01:41:33 -0000	1.25
***************
*** 11,14 ****
--- 11,16 ----
  module Bio::Shell::Core
  
+   # chdir
+   # change to File.join for / ended dirs
    SAVEDIR = "shell/session/"
    CONFIG  = "config"
***************
*** 41,45 ****
  
    def datadir
!     DATADIR
    end
  
--- 43,47 ----
  
    def datadir
!     File.join(@cache[:workdir], DATADIR)
    end
  
***************
*** 49,57 ****
  
    def object_file
!     SAVEDIR + OBJECT
    end
  
    def history_file
!     SAVEDIR + HISTORY
    end
  
--- 51,59 ----
  
    def object_file
!     File.join(@cache[:workdir], SAVEDIR, OBJECT)
    end
  
    def history_file
!     File.join(@cache[:workdir], SAVEDIR, HISTORY)
    end
  
***************
*** 88,94 ****
    ### save/restore the environment
  
!   def configure
      @config = {}
!     @cache  = {}
      create_save_dir
      load_config
--- 90,100 ----
    ### save/restore the environment
  
!   def configure(workdir)
!     savedir = File.join(Dir.pwd, workdir)
      @config = {}
!     @cache  = {
!       :workdir => workdir,
!       :savedir => savedir,
!     }
      create_save_dir
      load_config
***************
*** 115,119 ****
        save_config
      end
!     STDERR.puts "Leaving directory '#{@cache[:workdir]}'"
      STDERR.puts "History is saved in '#{@cache[:workdir]}/#{SAVEDIR + HISTORY}'"
    end
--- 121,125 ----
        save_config
      end
!     #STDERR.puts "Leaving directory '#{@cache[:workdir]}'"
      STDERR.puts "History is saved in '#{@cache[:workdir]}/#{SAVEDIR + HISTORY}'"
    end
***************
*** 128,136 ****
  
    def create_save_dir_ask
!     if File.directory?(SAVEDIR)
!       @cache[:save] = true
      end
!     if @cache[:save].nil?
!       if ask_yes_or_no("Save session in '#{SAVEDIR}' directory? [y/n] ")
          create_real_dir(SAVEDIR)
          create_real_dir(PLUGIN)
--- 134,144 ----
  
    def create_save_dir_ask
!     Dir.chdir(@cache[:workdir]) do |path|
!       if File.directory?(SAVEDIR)
!         @cache[:save] = true
!       end
      end
!     unless @cache[:save]
!       if ask_yes_or_no("Save session in '#{@cache[:workdir]}/#{SAVEDIR}' directory? [y/n] ")
          create_real_dir(SAVEDIR)
          create_real_dir(PLUGIN)
***************
*** 146,156 ****
  
    def create_real_dir(dir)
!     unless File.directory?(dir)
!       begin
!         STDERR.print "Creating directory (#{dir}) ... "
!         FileUtils.mkdir_p(dir)
!         STDERR.puts "done"
!       rescue
!         warn "Error: Failed to create directory (#{dir}) : #{$!}"
        end
      end
--- 154,166 ----
  
    def create_real_dir(dir)
!     Dir.chdir(@cache[:workdir]) do |path|
!       unless File.directory?(dir)
!         begin
!           STDERR.print "Creating directory (#{path}/#{dir}) ... "
!           FileUtils.mkdir_p(dir)
!           STDERR.puts "done"
!         rescue
!           warn "Error: Failed to create directory (#{path}/#{dir}) : #{$!}"
!         end
        end
      end
***************
*** 160,177 ****
  
    def create_flat_dir(dbname)
!     dir = BIOFLAT + dbname.to_s.strip
!     unless File.directory?(dir)
!       FileUtils.mkdir_p(dir)
      end
!     return dir
    end
  
    def find_flat_dir(dbname)
!     dir = BIOFLAT + dbname.to_s.strip
!     if File.exists?(dir)
!       return dir
!     else
!       return nil
      end
    end
  
--- 170,195 ----
  
    def create_flat_dir(dbname)
!     flatdb = ""
!     Dir.chdir(@cache[:workdir]) do |path|
!       dir = BIOFLAT + dbname.to_s.strip
!       unless File.directory?(dir)
!         FileUtils.mkdir_p(dir)
!       end
!       flatdb = "#{path}/#{dir}"
      end
!     return flatdb
    end
  
    def find_flat_dir(dbname)
!     flatdb = ""
!     Dir.chdir(@cache[:workdir]) do |path|
!       dir = BIOFLAT + dbname.to_s.strip
!       if File.exists?(dir)
!         flatdb = "#{path}/#{dir}"
!       else
!         flatdb = nil
!       end
      end
+     return flatdb
    end
  
***************
*** 183,192 ****
  
    def load_config_file(file)
!     if File.exists?(file)
!       STDERR.print "Loading config (#{file}) ... "
!       if hash = YAML.load(File.read(file))
!         @config.update(hash)
        end
-       STDERR.puts "done"
      end
    end
--- 201,212 ----
  
    def load_config_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       if File.exists?(file)
!         STDERR.print "Loading config (#{path}/#{file}) ... "
!         if hash = YAML.load(File.read(file))
!           @config.update(hash)
!         end
!         STDERR.puts "done"
        end
      end
    end
***************
*** 197,208 ****
  
    def save_config_file(file)
!     begin
!       STDERR.print "Saving config (#{file}) ... "
!       File.open(file, "w") do |f|
!         f.puts @config.to_yaml
        end
-       STDERR.puts "done"
-     rescue
-       warn "Error: Failed to save (#{file}) : #{$!}"
      end
    end
--- 217,230 ----
  
    def save_config_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       begin
!         STDERR.print "Saving config (#{path}/#{file}) ... "
!         File.open(file, "w") do |f|
!           f.puts @config.to_yaml
!         end
!         STDERR.puts "done"
!       rescue
!         warn "Error: Failed to save (#{path}/#{file}) : #{$!}"
        end
      end
    end
***************
*** 259,267 ****
  
    def load_plugin_dir(dir)
!     if File.directory?(dir)
!       Dir.glob("#{dir}/*.rb").sort.each do |file|
!         STDERR.print "Loading plugin (#{file}) ... "
!         load file
!         STDERR.puts "done"
        end
      end
--- 281,291 ----
  
    def load_plugin_dir(dir)
!     Dir.chdir(@cache[:workdir]) do |path|
!       if File.directory?(dir)
!         Dir.glob("#{dir}/*.rb").sort.each do |file|
!           STDERR.print "Loading plugin (#{path}/#{file}) ... "
!           load file
!           STDERR.puts "done"
!         end
        end
      end
***************
*** 286,306 ****
  
    def load_object_file(file)
!     if File.exists?(file)
!       STDERR.print "Loading object (#{file}) ... "
!       begin
!         bind = Bio::Shell.cache[:binding]
!         hash = Marshal.load(File.read(file))
!         hash.each do |k, v|
!           begin
!             Thread.current[:restore_value] = v
!             eval("#{k} = Thread.current[:restore_value]", bind)
!           rescue
!             STDERR.puts "Warning: object '#{k}' couldn't be loaded : #{$!}"
            end
          end
!       rescue
!         warn "Error: Failed to load (#{file}) : #{$!}"
        end
-       STDERR.puts "done"
      end
    end
--- 310,332 ----
  
    def load_object_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       if File.exists?(file)
!         STDERR.print "Loading object (#{path}/#{file}) ... "
!         begin
!           bind = Bio::Shell.cache[:binding]
!           hash = Marshal.load(File.read(file))
!           hash.each do |k, v|
!             begin
!               Thread.current[:restore_value] = v
!               eval("#{k} = Thread.current[:restore_value]", bind)
!             rescue
!               STDERR.puts "Warning: object '#{k}' couldn't be loaded : #{$!}"
!             end
            end
+         rescue
+           warn "Error: Failed to load (#{path}/#{file}) : #{$!}"
          end
!         STDERR.puts "done"
        end
      end
    end
***************
*** 311,340 ****
  
    def save_object_file(file)
!     begin
!       STDERR.print "Saving object (#{file}) ... "
!       File.rename(file, "#{file}.old") if File.exist?(file)
!       File.open(file, "w") do |f|
!         bind = Bio::Shell.cache[:binding]
!         list = eval("local_variables", bind)
!         list -= ["_"]
!         hash = {}
!         list.each do |elem|
!           value = eval(elem, bind)
!           if value
!             begin
!               Marshal.dump(value)
!               hash[elem] = value
!             rescue
!               # value could not be dumped.
              end
            end
          end
!         Marshal.dump(hash, f)
!         @config[:marshal] = MARSHAL
        end
-       STDERR.puts "done"
-     rescue
-       File.rename("#{file}.old", file) if File.exist?("#{file}.old")
-       warn "Error: Failed to save (#{file}) : #{$!}"
      end
    end
--- 337,368 ----
  
    def save_object_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       begin
!         STDERR.print "Saving object (#{path}/#{file}) ... "
!         File.rename(file, "#{file}.old") if File.exist?(file)
!         File.open(file, "w") do |f|
!           bind = Bio::Shell.cache[:binding]
!           list = eval("local_variables", bind)
!           list -= ["_"]
!           hash = {}
!           list.each do |elem|
!             value = eval(elem, bind)
!             if value
!               begin
!                 Marshal.dump(value)
!                 hash[elem] = value
!               rescue
!                 # value could not be dumped.
!               end
              end
            end
+           Marshal.dump(hash, f)
+           @config[:marshal] = MARSHAL
          end
!         STDERR.puts "done"
!       rescue
!         File.rename("#{file}.old", file) if File.exist?("#{file}.old")
!         warn "Error: Failed to save (#{file}) : #{$!}"
        end
      end
    end
***************
*** 343,347 ****
  
    def open_history
!     @cache[:histfile] = File.open(SAVEDIR + HISTORY, "a")
      @cache[:histfile].sync = true
    end
--- 371,377 ----
  
    def open_history
!     Dir.chdir(@cache[:workdir]) do |path|
!       @cache[:histfile] = File.open(SAVEDIR + HISTORY, "a")
!     end
      @cache[:histfile].sync = true
    end
***************
*** 363,371 ****
  
    def load_history_file(file)
!     if File.exists?(file)
!       STDERR.print "Loading history (#{file}) ... "
!       File.open(file).each do |line|
!         unless line[/^# /]
!           Readline::HISTORY.push line.chomp
          end
        end
--- 393,403 ----
  
    def load_history_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       if File.exists?(file)
!         STDERR.print "Loading history (#{path}/#{file}) ... "
!         File.open(file).each do |line|
!           unless line[/^# /]
!             Readline::HISTORY.push line.chomp
!           end
          end
        end
***************
*** 382,393 ****
  
    def save_history_file(file)
!     begin
!       STDERR.print "Saving history (#{file}) ... "
!       File.open(file, "w") do |f|
!         f.puts Readline::HISTORY.to_a
        end
-       STDERR.puts "done"
-     rescue
-       warn "Error: Failed to save (#{file}) : #{$!}"
      end
    end
--- 414,427 ----
  
    def save_history_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       begin
!         STDERR.print "Saving history (#{path}/#{file}) ... "
!         File.open(file, "w") do |f|
!           f.puts Readline::HISTORY.to_a
!         end
!         STDERR.puts "done"
!       rescue
!         warn "Error: Failed to save (#{path}/#{file}) : #{$!}"
        end
      end
    end
***************
*** 428,441 ****
    def save_script
      if @script_begin and @script_end and @script_begin <= @script_end
!       if File.exists?(SCRIPT)
!         message = "Overwrite script file (#{SCRIPT})? [y/n] "
!       else
!         message = "Save script file (#{SCRIPT})? [y/n] "
        end
-       if ask_yes_or_no(message)
-         save_script_file(SCRIPT)
-       else
-         STDERR.puts " ... save aborted."
-       end 
      elsif @script_begin and @script_end and @script_begin - @script_end == 1
        STDERR.puts " ... script aborted."
--- 462,477 ----
    def save_script
      if @script_begin and @script_end and @script_begin <= @script_end
!       Dir.chdir(@cache[:workdir]) do |path|
!         if File.exists?(SCRIPT)
!           message = "Overwrite script file (#{path}/#{SCRIPT})? [y/n] "
!         else
!           message = "Save script file (#{path}/#{SCRIPT})? [y/n] "
!         end
!         if ask_yes_or_no(message)
!           save_script_file(SCRIPT)
!         else
!           STDERR.puts " ... save aborted."
!         end 
        end
      elsif @script_begin and @script_end and @script_begin - @script_end == 1
        STDERR.puts " ... script aborted."
***************
*** 446,461 ****
  
    def save_script_file(file)
!     begin
!       STDERR.print "Saving script (#{file}) ... "
          File.open(file, "w") do |f|
!         f.puts "#!/usr/bin/env bioruby"
!         f.puts
!         f.puts Readline::HISTORY.to_a[@script_begin.. at script_end]
!         f.puts
        end
-       STDERR.puts "done"
-     rescue
-       @script_begin = nil
-       warn "Error: Failed to save (#{file}) : #{$!}"
      end
    end
--- 482,499 ----
  
    def save_script_file(file)
!     Dir.chdir(@cache[:workdir]) do |path|
!       begin
!         STDERR.print "Saving script (#{path}/#{file}) ... "
          File.open(file, "w") do |f|
!           f.puts "#!/usr/bin/env bioruby"
!           f.puts
!           f.puts Readline::HISTORY.to_a[@script_begin.. at script_end]
!           f.puts
!         end
!         STDERR.puts "done"
!       rescue
!         @script_begin = nil
!         warn "Error: Failed to save (#{path}/#{file}) : #{$!}"
        end
      end
    end




More information about the bioruby-cvs mailing list