[BioRuby-cvs] bioruby/lib/bio/shell setup.rb, NONE, 1.1 irb.rb, NONE, 1.1 script.rb, NONE, 1.1 web.rb, 1.1, 1.2

Katayama Toshiaki k at dev.open-bio.org
Sun Dec 24 08:32:10 UTC 2006


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

Modified Files:
	web.rb 
Added Files:
	setup.rb irb.rb script.rb 
Log Message:
* New BioRuby shell with Ruby on Rails generator functionality is integrated.


Index: web.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/shell/web.rb,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** web.rb	27 Feb 2006 09:22:42 -0000	1.1
--- web.rb	24 Dec 2006 08:32:08 -0000	1.2
***************
*** 13,88 ****
  module Bio::Shell
  
!   private
  
!   def rails_directory_setup
!     server = "script/server"
!     unless File.exists?(server)
!       require 'fileutils'
!       basedir = File.dirname(__FILE__)
!       print "Copying web server files ... "
!       FileUtils.cp_r("#{basedir}/rails/.", ".")
!       puts "done"
      end
-   end
  
!   def rails_server_setup
!     require 'open3'
!     $web_server = Open3.popen3(server)
  
!     $web_error_log = File.open("log/web-error.log", "a")
!     $web_server[2].reopen($web_error_log)
  
!     while line = $web_server[1].gets
!       if line[/druby:\/\/localhost/]
!         uri = line.chomp
!         puts uri if $DEBUG
!         break
        end
      end
  
!     $web_access_log = File.open("log/web-access.log", "a")
!     $web_server[1].reopen($web_access_log)
! 
!     return uri
!   end
  
!   def web
!     return if $web_server
  
!     require 'drb/drb'
!     # $SAFE = 1   # disable eval() and friends
  
!     rails_directory_setup
!     #uri = rails_server_setup
!     uri = 'druby://localhost:81064' # baioroji-
  
!     $drb_server = DRbObject.new_with_uri(uri)
!     $drb_server.puts_remote("Connected")
  
!     puts "Connected to server #{uri}"
!     puts "Open http://localhost:3000/shell/"
  
!     io = IRB.conf[:MAIN_CONTEXT].io
  
!     io.class.class_eval do
!       alias_method :shell_original_gets, :gets
!     end
  
-     def io.gets
-       bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
-       vars = eval("local_variables", bind)
-       vars.each do |var|
-         next if var == "_"
-         if val = eval("#{var}", bind)
-           $drb_server[var] = val
-         else
-           $drb_server.delete(var)
-         end
-       end
-       line = shell_original_gets
-       line
-     end
-   end
-   
  end
  
--- 13,93 ----
  module Bio::Shell
  
!   class Web
  
!     def initialize
!       Bio::Shell.cache[:binding] = binding
!       Bio::Shell.cache[:results] ||= Results.new
!       install_rails
!       setup_rails
!       start_rails
      end
  
!     private
  
!     def setup_rails
!       puts
!       puts ">>>"
!       puts ">>>  open http://localhost:3000/bioruby"
!       puts ">>>"
!       puts
!       puts '(port # may differ if opts for rails are given "bioruby --web proj -- -p port")'
!       puts
!     end
  
!     def install_rails
!       unless File.exist?("script/generate")
!         puts "Installing Rails application for BioRuby shell ... "
!         system("rails .")
!         puts "done"
!       end
!       unless File.exist?("app/controllers/bioruby_controller.rb")
!         basedir = File.dirname(__FILE__)
!         puts "Installing Rails plugin for BioRuby shell ... "
!         FileUtils.cp_r("#{basedir}/rails/.", ".")
!         system("./script/generate bioruby shell")
!         puts "done"
        end
      end
  
!     def start_rails
!       begin
!         Bio::Shell.cache[:rails] = Thread.new {
!           require './config/boot'
!           require 'commands/server'
!         }
!       end
!     end
  
!     class Results
!       attr_accessor :number, :script, :result, :output
  
!       def initialize
!         @number = 0
!         @script = []
!         @result = []
!         @output = []
!       end
  
!       def store(script, result, output)
!         @number += 1
!         @script[@number] = script
!         @result[@number] = result
!         @output[@number] = output
!         return @number
!       end
  
!       def restore(number)
!         return @script[number], @result[number], @output[number]
!       end
!     end
  
!   end
  
!   private
  
!   # *TODO* stop irb and start rails?
!   #def web
!   #end
  
  end
  

--- NEW FILE: irb.rb ---
#
# = bio/shell/irb.rb - CUI for the BioRuby shell
#
# Copyright::   Copyright (C) 2006
#               Toshiaki Katayama <k at bioruby.org>
# License::     Ruby's
#
# $Id: irb.rb,v 1.1 2006/12/24 08:32:08 k Exp $
#

module Bio::Shell

  class Irb

    def initialize
      require 'irb'
      begin
        require 'irb/completion'
        Bio::Shell.cache[:readline] = true
      rescue LoadError
        Bio::Shell.cache[:readline] = false
      end
      IRB.setup(nil)
      setup_irb
      start_irb
    end

    def start_irb
      Bio::Shell.cache[:irb] = IRB::Irb.new

      # needed for method completion
      IRB.conf[:MAIN_CONTEXT] = Bio::Shell.cache[:irb].context

      # store binding for evaluation
      Bio::Shell.cache[:binding] = IRB.conf[:MAIN_CONTEXT].workspace.binding

      # overwrite gets to store history with time stamp
      io = IRB.conf[:MAIN_CONTEXT].io
      io.class.class_eval do
        alias_method :irb_original_gets, :gets
      end

      def io.gets
        line = irb_original_gets
        if line
          Bio::Shell.store_history(line)
        end
        return line
      end
    end

    def setup_irb
      # set application name
      IRB.conf[:AP_NAME] = 'bioruby'

      # change prompt for bioruby
      $_ = Bio::Shell.colors
      IRB.conf[:PROMPT][:BIORUBY_COLOR] = {
        :PROMPT_I => "bio#{$_[:ruby]}ruby#{$_[:none]}> ",
        :PROMPT_S => "bio#{$_[:ruby]}ruby#{$_[:none]}%l ",
        :PROMPT_C => "bio#{$_[:ruby]}ruby#{$_[:none]}+ ",
        :RETURN   => "  ==> %s\n"
      }
      IRB.conf[:PROMPT][:BIORUBY] = {
        :PROMPT_I => "bioruby> ",
        :PROMPT_S => "bioruby%l ",
        :PROMPT_C => "bioruby+ ",
        :RETURN   => "  ==> %s\n"
      }
      if Bio::Shell.config[:color]
        IRB.conf[:PROMPT_MODE] = :BIORUBY_COLOR
      else
        IRB.conf[:PROMPT_MODE] = :BIORUBY
      end

      # echo mode (uncomment to off by default)
      #IRB.conf[:ECHO] = Bio::Shell.config[:echo] || false

      # irb/input-method.rb >= v1.5 (not in 1.8.2)
      #IRB.conf[:SAVE_HISTORY] = 100000

      # not nicely works
      #IRB.conf[:AUTO_INDENT] = true
    end

  end # Irb

end


--- NEW FILE: script.rb ---
#
# = bio/shell/script.rb - script mode for the BioRuby shell
#
# Copyright::   Copyright (C) 2006
#               Toshiaki Katayama <k at bioruby.org>
# License::     Ruby's
#
# $Id: script.rb,v 1.1 2006/12/24 08:32:08 k Exp $
#

module Bio::Shell

  class Script

    def initialize(script)
      Bio::Shell.cache[:binding] = TOPLEVEL_BINDING
      Bio::Shell.load_session
      eval(File.read(File.join(Bio::Shell.script_dir, script)), TOPLEVEL_BINDING)
      exit
    end

  end # Script

end


--- NEW FILE: setup.rb ---
#
# = bio/shell/setup.rb - setup initial environment for the BioRuby shell
#
# Copyright::   Copyright (C) 2006
#               Toshiaki Katayama <k at bioruby.org>
# License::     Ruby's
#
# $Id: setup.rb,v 1.1 2006/12/24 08:32:08 k Exp $
#

require 'getoptlong'

class Bio::Shell::Setup

  def initialize
    check_ruby_version

    # command line options
    getoptlong

    # setup working directory
    setup_workdir

    # load configuration and plugins
    Dir.chdir(@workdir)
    Bio::Shell.configure
    Bio::Shell.cache[:workdir] = @workdir

    # set default to irb mode
    @mode ||= :irb

    case @mode
    when :web
      # setup rails server
      Bio::Shell::Web.new
    when :irb
      # setup irb server
      Bio::Shell::Irb.new
    when :script
      # run bioruby shell script
      Bio::Shell::Script.new(@script)
    end
  end

  def check_ruby_version
    if RUBY_VERSION < "1.8.2"
      raise "BioRuby shell runs on Ruby version >= 1.8.2"
    end
  end

  # command line argument (working directory or bioruby shell script file)
  def getoptlong
    opts = GetoptLong.new
    opts.set_options(
      [ '--rails',   '-r',  GetoptLong::NO_ARGUMENT ],
      [ '--web',     '-w',  GetoptLong::NO_ARGUMENT ],
      [ '--console', '-c',  GetoptLong::NO_ARGUMENT ],
      [ '--irb',     '-i',  GetoptLong::NO_ARGUMENT ]
    )
    opts.each_option do |opt, arg|
      case opt
      when /--rails/, /--web/
        @mode = :web
      when /--console/, /--irb/
        @mode = :irb
      end
    end
  end

  def setup_workdir
    arg = ARGV.shift

    # Options after the '--' argument are not parsed by GetoptLong and
    # are passed to irb or rails.  This hack preserve the first option
    # when working directory of the project is not given.
    if arg and arg[/^-/]
      ARGV.unshift arg
      arg = nil
    end
    
    if arg.nil?
      # run in current directory
      @workdir = current_workdir
    elsif File.directory?(arg)
      # run in existing directory
      @workdir = arg
    elsif File.file?(arg)
      # run file as a bioruby shell script
      @workdir = File.join(File.dirname(arg), "..")
      @script = File.basename(arg)
      @mode = :script
    else
      # run in new directory
      @workdir = install_workdir(arg)
    end
  end

  def current_workdir
    unless File.exists?(Bio::Shell.datadir)
      message = "Are you sure to start new session in this directory? [y/n] "
      unless Bio::Shell.ask_yes_or_no(message)
        exit
      end
    end
    return '.'
  end

  def install_workdir(workdir)
    FileUtils.mkdir_p(workdir)
    return workdir
  end

end # Setup




More information about the bioruby-cvs mailing list