Sha256: 8a3cae31daaebd4e132f40c12579a2c4db87207b14a637516d5730ca231368f8

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'rubygems'
require 'open4'

class HglibClient
  def initialize(repository_url)
    @repository_url = repository_url
    @py_script = File.dirname(__FILE__) + '/server.py'
  end

  def start
    @pid, @stdin, @stdout, @stderr = Open4::popen4 "python #{@py_script}"
    open_repository
  end

  def open_repository
    send_command("REPO_OPEN\t#{@repository_url}")
  end

  def cat_file(revision, file)
    begin
      send_command("CAT_FILE\t#{revision}\t#{file}")
    rescue RuntimeError => e
      if e.message =~ /not found in manifest/
        return nil # File does not exist.
      else
        raise
      end
    end
  end

  def parent_tokens(revision)
    send_command("PARENT_TOKENS\t#{revision}").split("\t")
  end

  def send_command(cmd)
    # send the command
    @stdin.puts cmd
    @stdin.flush

    # get status on stderr, first letter indicates state,
    # remaing value indicates length of the file content
    status = @stderr.read(10)
    flag = status[0,1]
    size = status[1,9].to_i
    if flag == 'F'
      return nil
    elsif flag == 'E'
      error = @stdout.read(size)
      raise RuntimeError.new("Exception in server process\n#{error}")
    end

    # read content from stdout
    return @stdout.read(size)
  end

  def shutdown
    send_command("QUIT")
    Process.waitpid(@pid, Process::WNOHANG)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ohloh_scm-2.0.1 lib/ohloh_scm/adapters/hglib/client.rb
ohloh_scm-2.0.0 lib/ohloh_scm/adapters/hglib/client.rb