module Commands

Public Class Methods

download(options=nil) click to toggle source

TODO the p4ruby extconf.rb file mechanism has some logic to search the ftp site for things. We might also want to use HTTP

# File lib/commands/download.rb, line 10
def Commands.download(options=nil)
  version = 'r14.2'
  binary = 'p4d'

  if options and options.params and !options.params.empty?

    op = OptionParser.new do |op|
      op.on('-v VERSION', '--version VERSION', 'ftp.perforce.com version') do |v|
        version = v
      end
    end

    op.parse!(options.params)

    if !options.params.empty?
      binary = options.params.first
    end
  end

  case binary
    when 'p4'
      download_p4_via_ftp(version)
    when 'p4d'
      download_p4d_via_ftp(version)
    when 'p4api'
      download_p4api_via_ftp(version)
    else
      raise "Don't know how to download #{binary}, check 'p4util help download'"
  end
end
help(options) click to toggle source
# File lib/commands/help.rb, line 4
def Commands.help(options)
  if !options.params.empty? && Commands.respond_to?(options.params.first.to_sym)
    method_name = "print_#{options.params.first}_help".to_sym
    Commands.method(method_name).call()
  else
    print_general_help
  end
end
init(options=nil) click to toggle source
# File lib/commands/init.rb, line 14
def Commands.init(options=nil)
  init_dir = 'p4init'

  p4port = '127.0.0.1:1666'
  version = nil
  auto = false

  if options and !options.params.empty?

    op = OptionParser.new do |op|
      op.on('-p PORT', '--port PORT', 'P4PORT setting') { |x| p4port = x }
      op.on('-a', '--auto', 'Force charset to be auto') { |x| auto = true }
      op.on('-v VERSION', '--version VERSION', 'ftp.perforce.com version') do |v|
        version = v
      end
    end

    op.parse!(options.params)

    if !options.params.empty?
      init_dir = options.params.first
    end
  end

  if options and !options.params.empty?
    init_dir = options.params.shift
  end

  initialize_p4d(init_dir, p4port, auto)
end
kill(options=nil) click to toggle source
# File lib/commands/kill.rb, line 5
def Commands.kill(options=nil)
  list_process_names.select{|p| p =~ /p4d/}.each do |p|
    pid = pid_for_process(p)
    next if pid.nil?
    begin
      puts "killing p4d #{p} at #{pid}"
      Process.kill('TERM', pid)
    rescue Exception => e
      puts "Problem killing #{p}: #{e.message}"
      puts e.backtrace.join('\n')
    end
  end

  is_running = true
  while is_running
    is_running = p4d_running?
    if is_running
      sleep 0.2
    end
  end
end
list_process_names() click to toggle source
# File lib/commands/util.rb, line 9
def self.list_process_names
  if OsUtil.osx? or OsUtil.linux?
    return `ps aux | awk '{print $11}'`.split(/\n/).drop(1)
  else
    # TODO investigate using tasklist just for this
    raise 'No support for windows just yet'
  end
end
p4d_available?(port=':1666') click to toggle source
# File lib/commands/util.rb, line 31
def Commands.p4d_available?(port=':1666')
  begin
    p4 = P4.new
    p4.port = port
    p4.connect
    p4.disconnect
    true
  rescue
    false
  end
end
p4d_running?() click to toggle source
# File lib/commands/util.rb, line 5
def Commands.p4d_running?
  list_process_names.select { |p| p =~ /p4d/ }.empty? == false
end
pid_for_process(process) click to toggle source
# File lib/commands/util.rb, line 18
def self.pid_for_process(process)
  if OsUtil.osx? or OsUtil.linux?
    line = `ps aux | awk '{print $2,$11}'`.split(/\n/).drop(1).find { |p| p =~ /#{process}/ }
    unless line.nil?
      return line.split('\n').first.to_i
    else
      return nil
    end
  else
    raise 'NO support for windows yet'
  end
end
print_download_help() click to toggle source
print_init_help() click to toggle source
print_kill_help() click to toggle source
print_start_help() click to toggle source
rake(options) click to toggle source
# File lib/commands/rake.rb, line 4
def Commands.rake(options)
  print_rake_help
end
spawn_p4d() click to toggle source
# File lib/commands/start.rb, line 12
def Commands.spawn_p4d
  pid = Process.spawn("#{OsUtil.p4d_path} -r #{Conventions.p4droot_dir} "+
                      "-v server=1 -L #{Conventions.p4d_log_path}")
  Process.detach(pid)

  while !p4d_running?
    sleep 0.2
  end

  while !p4d_available?
    sleep 0.1
  end
end
start(options=nil) click to toggle source
# File lib/commands/start.rb, line 4
def Commands.start(options=nil)
  if !File.exists?(OsUtil.p4d_path)
    Commands.download(options)
  end
  Conventions.init_p4droot_dir
  spawn_p4d
end
unicode_upgrade() click to toggle source
# File lib/commands/util.rb, line 43
def Commands.unicode_upgrade
  system("#{OsUtil.p4d_path} -r #{Conventions.p4droot_dir} "+
             "-v server=1 -L #{Conventions.p4d_log_path} " +
             "-xi")

end
version(options) click to toggle source
# File lib/commands/version.rb, line 6
def Commands.version(options)
  print_version_help
end

Private Class Methods

download_p4_via_ftp(version) click to toggle source
# File lib/commands/download.rb, line 63
def Commands.download_p4_via_ftp(version)
  download_via_ftp(version, OsUtil.p4_executable, OsUtil.p4_path)

  if !File.executable?(OsUtil.p4_path)
    File.chmod(0755, OsUtil.p4_path)
  end
end
download_p4api_via_ftp(version) click to toggle source
# File lib/commands/download.rb, line 79
def Commands.download_p4api_via_ftp(version)
  download_via_ftp(version, OsUtil.p4api_file, OsUtil.p4api_path)

  # um, probably should expand this guy out
end
download_p4d_via_ftp(version) click to toggle source
# File lib/commands/download.rb, line 71
def Commands.download_p4d_via_ftp(version)
  download_via_ftp(version, OsUtil.p4d_executable, OsUtil.p4d_path)

  if !File.executable?(OsUtil.p4d_path)
    File.chmod(0755, OsUtil.p4d_path)
  end
end
download_via_ftp(version, filename, output_path) click to toggle source
# File lib/commands/download.rb, line 87
def Commands.download_via_ftp(version, filename, output_path)
  ftp = Net::FTP.new('ftp.perforce.com')
  ftp.login

  dir = OsUtil.ftp_download_dir(version)
  ftp.chdir(dir)

  ftp.passive=true

  Conventions.init_working_dir

  ftp.getbinaryfile(filename, output_path)
ensure
  ftp.close if ftp and !ftp.closed?
end
initialize_p4d(init_dir, p4port, auto) click to toggle source
# File lib/commands/init.rb, line 208
def Commands.initialize_p4d(init_dir, p4port, auto)
  # Go through our init_dir, and evaluate each script as if it were defined
  # in the Commands::Init module.
  if File.directory?(init_dir)
    Dir.glob("#{init_dir}/**/*.rb") do |file|
      contents = IO.read(file)
      Commands::Init.class_eval(contents, file)
    end
  elsif File.file?(init_dir)
    contents = IO.read(init_dir)
    Commands::Init.class_eval(contents, init_dir)
  end

  # Note that nothing is actually done until this line. This allows classes
  # to re-define methods and do fancy shit, like, 'oh in security_settings 0
  # this guy actually doesn't have a password'.
  Commands::Init::InitModel.run(p4port, auto)
end
print_general_help() click to toggle source
print_rake_help() click to toggle source
print_version_help() click to toggle source