module Typingpool::Utility::Test::Script

Public Instance Methods

assign_default() click to toggle source
# File lib/typingpool/utility/test/script.rb, line 210
def assign_default
  Hash[
       :template => 'interview/phone',
       :deadline => '5h',
       :lifetime => '10h',
       :approval => '10h',
       :qualify => ['approval_rate >= 90', 'hits_approved > 10'],
       :keyword => ['test', 'mp3', 'typingpooltest']
      ]
end
audio_files(subdir='mp3') click to toggle source
# File lib/typingpool/utility/test/script.rb, line 205
def audio_files(subdir='mp3')
  dir = File.join(audio_dir, subdir)
  Dir.entries(dir).reject{|entry| entry.match(/^\./) }.map{|entry| File.join(dir, entry)}.select{|path| File.file?(path) }
end
call_script(script_name, *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 112
def call_script(script_name, *args)
  out, err, status = Open3.capture3(path_to_script(script_name), *args)
  if status.success?
    return [out.to_s.chomp, err.to_s.chomp]
  else
    if err
      raise Error::Shell, err.chomp
    else
      raise Error::Shell
    end
  end
end
config_path(dir) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 79
def config_path(dir)
  File.join(dir, project_default[:config_filename])   
end
copy_fixtures_to_project_dir(fixture_prefix, project_path) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 345
def copy_fixtures_to_project_dir(fixture_prefix, project_path)
  copies = 0
  with_fixtures_in_project_dir(fixture_prefix, project_path) do |source_fixture_path, project_fixture_path|
    if File.exist? project_fixture_path
      FileUtils.mv(project_fixture_path, File.join(File.dirname(project_fixture_path), "orig_#{File.basename(project_fixture_path)}"))
    end
    FileUtils.cp(source_fixture_path, project_fixture_path)
    copies += 1
  end
  copies > 0 or raise Error, "No fixtures to copy with prefix #{fixture_prefix}"
  copies
end
copy_tp_assign_fixtures(dir, fixture_prefix, config_path=config_path(dir), project_title=project_default[:title]) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 243
def copy_tp_assign_fixtures(dir, fixture_prefix, config_path=config_path(dir), project_title=project_default[:title])
  project = Project.new(project_title, Config.file(config_path))
  if Typingpool::Test.record
    project_time(project, Time.now)
    with_fixtures_in_transcripts_dir(dir, "#{fixture_prefix}_", project_title) do |fixture_path, project_path|
      FileUtils.cp(project_path, fixture_path)
    end
  elsif not(Typingpool::Test.live)
    copy_fixtures_to_project_dir("#{fixture_prefix}_", File.join(dir, project_title))
    reconfigure_project_csv(project)
  end
end
do_later() { || ... } click to toggle source

Intended to be overriden by some classes that mixin this module

# File lib/typingpool/utility/test/script.rb, line 85
def do_later
  at_exit{ yield }
end
make_fixture_transcripts_dir(name) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 326
def make_fixture_transcripts_dir(name)
  dir = File.join(fixtures_dir, name)
  if File.exist? dir
    raise Error::Test, "Fixture transcript dir already exists for #{name} at #{dir}"
  end
  Dir.mkdir(dir)
  dir
end
path_to_script(script_name) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 107
def path_to_script(script_name)
  File.join(Utility.app_dir, 'bin', script_name)
end
project_time(project, time=nil) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 310
def project_time(project, time=nil)
  file = project.local.file('data', 'time.txt')
  if time
    file.write(time.to_i)
  else
    time = Time.at(file.read.to_i)
  end
  time
end
project_transcript_count(project, which_csv) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 368
def project_transcript_count(project, which_csv)
  project.local.file('data', which_csv).as(:csv).reject{|assignment| assignment['transcript'].to_s.empty?}.size
end
readymade_project_path() click to toggle source
# File lib/typingpool/utility/test/script.rb, line 69
def readymade_project_path
  unless @@readymade_project_path
    transcripts_dir = @@readymade_project_path = Dir.mktmpdir('typingpool_')
    do_later{ FileUtils.remove_entry_secure(transcripts_dir) }
    write_testing_config_for_transcripts_dir(transcripts_dir, reconfigure_for_s3(self.config))
    tp_make(transcripts_dir, config_path(transcripts_dir), 'mp3', true)
  end
  @@readymade_project_path
end
reconfigure_for_s3(config) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 35
def reconfigure_for_s3(config)
  unless s3_credentials?(config)
    raise Error::Test, "No S3 credentials available"
  end
  config.to_hash.delete('sftp')
  config
end
reconfigure_for_testing(config) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 49
def reconfigure_for_testing(config)
  config['assign']['reward'] = '0.02'
  config.assign.to_hash.delete('qualify')
  config
end
reconfigure_for_transcripts_dir(config, transcripts_dir) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 43
def reconfigure_for_transcripts_dir(config, transcripts_dir)
  config.transcripts = transcripts_dir
  config.cache = File.join(transcripts_dir, '.cache')
  config
end
reconfigure_project(project) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 89
def reconfigure_project(project)
  #rewrite URLs in assignment.csv according to config at config_path
  File.delete(project.local.file('data', 'id.txt'))
  project.local.create_id
  reconfigure_project_csv(project)
  project
end
reconfigure_project_csv(project) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 97
def reconfigure_project_csv(project)
  assignments = project.local.file('data', 'assignment.csv').as(:csv)
  urls = project.create_remote_names(assignments.map{|assignment| Project.local_basename_from_url(assignment['audio_url']) }).map{|file| project.remote.file_to_url(file) }
  assignments.each! do |assignment|
    assignment['audio_url'] = urls.shift
    assignment['project_id'] = project.local.id
  end
  assignments
end
restore_project_dir_from_fixtures(fixture_prefix, project_path) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 358
def restore_project_dir_from_fixtures(fixture_prefix, project_path)
  with_fixtures_in_project_dir(fixture_prefix, project_path) do |source_fixture_path, project_fixture_path|
    FileUtils.rm(project_fixture_path) if File.exists? project_fixture_path
    path_to_orig = File.join(File.dirname(project_fixture_path), "orig_#{File.basename(project_fixture_path)}")
    if File.exist?(path_to_orig)
      FileUtils.mv(path_to_orig, project_fixture_path)
    end
  end #with_fixtures_in_transctips_dir
end
script_with_vcr(dir, fixture_name, config_path=config_path(dir), can_ever_run_live=true, are_recording=Typingpool::Test.record, choices=nil) { |dir, config_path, project_default, choices, *vcr_args(fixture_name, can_ever_run_live, are_recording)| ... } click to toggle source
# File lib/typingpool/utility/test/script.rb, line 188
def script_with_vcr(dir, fixture_name, config_path=config_path(dir), can_ever_run_live=true, are_recording=Typingpool::Test.record, choices=nil)
  yield([dir, config_path, project_default[:title], choices, *vcr_args(fixture_name, can_ever_run_live, are_recording)])
end
simulate_failed_audio_upload_in(dir, config_path=config_path(dir)) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 320
def simulate_failed_audio_upload_in(dir, config_path=config_path(dir))
  project = Typingpool::Project.new(project_default[:title], Config.file(config_path))
  csv = project.local.file('data', 'assignment.csv').as(:csv)
  csv.each!{|a| a['audio_uploaded'] = 'maybe'}
end
split_reviews(output) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 372
def split_reviews(output)
  output.split(/Transcript for\b/)
end
tp_assign(dir, config_path=config_path(dir), project_title=project_default[:title], *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 221
def tp_assign(dir, config_path=config_path(dir), project_title=project_default[:title], *args)
  call_script(
              'tp-assign',
              '--sandbox',
              project_title,
              assign_default[:template],
              '--config', config_path,
              *[:deadline, :lifetime, :approval].map{|param| ["--#{param}", assign_default[param]] }.flatten,
              *[:qualify, :keyword].map{|param| assign_default[param].map{|value| ["--#{param}", value] } }.flatten,
              *args)
  
end
tp_assign_with_vcr(dir, fixture_name, config_path=config_path(dir), project_title=project_default[:title]) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 234
def tp_assign_with_vcr(dir, fixture_name, config_path=config_path(dir), project_title=project_default[:title])
  project = Project.new(project_default[:title], Typingpool::Config.file(config_path))
  args = [dir, config_path, project_title, *vcr_args(fixture_name)]
  unless (Typingpool::Test.live || Typingpool::Test.record)
    args.push('--testtime', project_time(project).to_i.to_s)
  end
  tp_assign(*args)
end
tp_collect_with_fixture(dir, fixture_name, are_recording=false) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 256
def tp_collect_with_fixture(dir, fixture_name, are_recording=false)
  fixture_handle = File.join(vcr_dir, fixture_name)
  args = ['tp-collect', '--sandbox', '--testfixture', fixture_handle, '--config', config_path(dir)]
  if are_recording
    delete_vcr_fixture(fixture_name)
    args.push('--testfixturerecord')
  end
  call_script(*args)
end
tp_config_with_input(args, input) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 296
def tp_config_with_input(args, input)
  output = {}
  Open3.popen3(path_to_script('tp-config'), *args) do |stdin, stdout, stderr, wait_thr|
    input.each do |sending|
      stdin.puts(sending)
    end
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end #Open3.popen3...
  output
end
tp_finish(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 151
def tp_finish(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  tp_finish_inside_sandbox(dir, config_path, project_title, choices, *args)
  tp_finish_outside_sandbox(dir, config_path, project_title, choices, *args)
end
tp_finish_inside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 157
def tp_finish_inside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  tp_finish_outside_sandbox(dir, config_path, project_title, choices, '--sandbox', *args)
end
tp_finish_inside_sandbox_with_vcr(*args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 200
def tp_finish_inside_sandbox_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish_inside_sandbox(*new_args) }
end
tp_finish_outside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 161
def tp_finish_outside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  output = {}
  args = [
          File.join(Utility.app_dir, 'bin', 'tp-finish'),
          project_title,
          '--config',
          config_path,
          *args
         ]
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    choices.each{|choice| stdin.puts(choice) } if choices
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end
  if output[:status] == 0
    return [output[:out].to_s.chomp, output[:err].to_s.chomp]
  else
    if output[:err]
      raise Error::Shell, output[:err].chomp
    else
      raise Error::Shell
    end
  end #if output[:status] != 0
end
tp_finish_outside_sandbox_with_vcr(*args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 196
def tp_finish_outside_sandbox_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish_outside_sandbox(*new_args) }
end
tp_finish_with_vcr(*args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 192
def tp_finish_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish(*new_args) }
end
tp_make(in_dir, config=config_path(in_dir), audio_subdir='mp3', devtest_mode_skipping_upload=false, *args) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 125
def tp_make(in_dir, config=config_path(in_dir), audio_subdir='mp3', devtest_mode_skipping_upload=false, *args)
  commands = [
              '--config', config, 
              '--chunks', project_default[:chunks],
              *[:title, :subtitle].map{|param| ["--#{param}", project_default[param]] }.flatten,
              *[:voice, :unusual].map{|param| project_default[param].map{|value| ["--#{param}", value] } }.flatten,
              *audio_files(audio_subdir).map{|path| ['--file', path]}.flatten, 
              *args
             ]
  commands.push('--testnoupload', '--testkeepmergefile') if devtest_mode_skipping_upload
  call_script('tp-make', *commands)
end
tp_make_with_vcr(dir, fixture_name, config_path=config_path(dir)) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 138
def tp_make_with_vcr(dir, fixture_name, config_path=config_path(dir))
  tp_make(dir, config_path, 'mp3', false, *vcr_args(fixture_name))
end
tp_review_with_fixture(transcripts_dir, fixture_name, choices, are_recording=false, project_name=nil) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 266
def tp_review_with_fixture(transcripts_dir, fixture_name, choices, are_recording=false, project_name=nil)
  fixture_handle = File.join(vcr_dir, fixture_name)
  output = {}
  args = [
          File.join(Utility.app_dir, 'bin', 'tp-review'),
          '--sandbox',
          '--config', config_path(transcripts_dir),
          '--testfixture', fixture_handle
         ]
  args.push(project_name) if project_name
  if are_recording
    delete_vcr_fixture(fixture_name)
    args.push('--testfixturerecord')
  end
  
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    choices.each do |choice|
      stdin.puts(choice)
      if choice.strip.match(/^r/i)
        stdin.puts("No reason - this is a test")
      end
    end
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end
  output
end
vcr_args(fixture_name, can_ever_run_live=true, are_recording=Typingpool::Test.record) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 142
def vcr_args(fixture_name, can_ever_run_live=true, are_recording=Typingpool::Test.record)
  args = []
  if fixture = cleared_vcr_fixture_path_for(fixture_name, can_ever_run_live, are_recording)
    args.push('--testfixture', fixture)
    args.push('--testfixturerecord') if are_recording
  end #if fixture = ...
  args
end
with_fixtures_in_project_dir(fixture_prefix, project_path) { |source_fixture_path, project_fixture_path| ... } click to toggle source
# File lib/typingpool/utility/test/script.rb, line 335
def with_fixtures_in_project_dir(fixture_prefix, project_path)
  fixtures = Dir.entries(fixtures_dir).select{|entry| entry.include?(fixture_prefix) && entry.index(fixture_prefix) == 0 }.select{|entry| File.file?(File.join(fixtures_dir, entry)) }
  fixtures.map!{|fixture| fixture[fixture_prefix.size .. -1] }
  fixtures.each do |fixture|
    project_fixture_path = File.join(project_path, 'data', fixture)
    source_fixture_path = File.join(fixtures_dir, [fixture_prefix, fixture].join )
    yield(source_fixture_path, project_fixture_path)
  end
end
with_temp_readymade_project() { |transcripts_dir| ... } click to toggle source
# File lib/typingpool/utility/test/script.rb, line 16
def with_temp_readymade_project
  Dir.mktmpdir('typingpool_') do |transcripts_dir|
    FileUtils.cp_r(File.join(readymade_project_path, '.'), transcripts_dir)
    config = reconfigure_for_transcripts_dir(
                                             Config.file(config_path(transcripts_dir)),
                                             transcripts_dir)
    write_config(transcripts_dir, config)
    reconfigure_project(Project.new(project_default[:title], config))
    yield(transcripts_dir)
  end
end
with_temp_transcripts_dir() { |transcripts_dir| ... } click to toggle source
# File lib/typingpool/utility/test/script.rb, line 28
def with_temp_transcripts_dir
  Dir.mktmpdir('typingpool_') do |transcripts_dir|
    write_testing_config_for_transcripts_dir(transcripts_dir, self.config)
    yield(transcripts_dir)
  end
end
write_config(dir, config, filename=project_default[:config_filename]) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 55
def write_config(dir, config, filename=project_default[:config_filename])
  path = File.join(dir, filename)
  File.write(path, YAML.dump(config.to_hash))
  path
end
write_testing_config_for_transcripts_dir(transcripts_dir, config=self.config) click to toggle source
# File lib/typingpool/utility/test/script.rb, line 61
def write_testing_config_for_transcripts_dir(transcripts_dir, config=self.config)
  write_config(
               transcripts_dir,
               reconfigure_for_transcripts_dir(reconfigure_for_testing(config), transcripts_dir),
               project_default[:config_filename])
end