require 'optparse'
require 'P4'
class MSF
attr_accessor :root
def initialize(root=Dir.pwd())
@root = root
@options = {}
end
def parse_opts()
@options[:add] = false
@options[:binary] = false
@options[:volume] = 1
@options[:size] = "1K"
@options[:transaction] = false
@options[:fill] = false
@options[:wlineend] = false
@options[:llineend] = false
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-a", "--[no-]add", "Add to p4 server") do |a|
@options[:add] = a
end
opts.on("-t", "--[no-]transaction", "Separate transaction mode, Each file submitted as a changelist") do |t|
@options[:transaction] = t
end
opts.on("-b", "--[no-]binary", "Make binaries") do |b|
@options[:binary] = b
end
opts.on("-v", "--volume NUM", Integer, "Make NUM files") do |v|
@options[:volume] = v
end
opts.on("-s", "--size SIZE", "Make files SIZE big (can take B K M G, K assumed") do |s|
@options[:size] = s
end
opts.on("-f", "--[no-]fill", "Fill with data") do |f|
@options[:fill] = f
end
opts.on("-w", "--[no-]wlineendings", "Add win line endings every 50 chars") do |l|
@options[:wlineend] = l
end
opts.on("-l", "--[no-]lineendings", "Add linux line endings every 50 chars") do |l|
@options[:llineend] = l
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!(ARGV)
p @options
end
def write_file(fname, write_type)
f = File.open(fname, write_type)
if @options[:fill] and @options[:llineend]
format_size(@options[:size].dup).times do |val|
if val % 50 == 0
f.write("\n")
else
f.write('b')
end
f.write("\n")
end
elsif @options[:fill] and @options[:wlineend]
format_size(@options[:size].dup).times do |val|
if val % 50 == 0
f.write("\r\n")
else
f.write('b')
end
f.write("\r\n")
end
elsif @options[:fill] and !(@options[:llineend] or @options[:wlineend])
format_size(@options[:size].dup).times do |val|
f.write('b')
end
else
f.seek(format_size(@options[:size].dup) - 1, IO::SEEK_SET)
f.write('a')
end
f.close
puts "\nFILE :: " + fname +
"\nType :: " + write_type +
"\nSize :: " + @options[:size] + "\n"
end
def format_size(size)
unit_hash = {"B" => 1,
"K" => 1024,
"M" => (1024*1024),
"G" => (1024*1024*1024)}
unit = size[-1,1]
if unit.is_a? Integer
size = size.to_i * 1024
else
size.slice!(-1,1)
size = size.to_i * unit_hash[unit.upcase]
end
return size
end
def add_to_p4()
if @options[:add]
p4 = P4.new
p4.connect
if @options[:transaction]
for i in 0...@options[:volume] do
begin
p4.run_add(@root+'\file'+i.to_s())
p4.run_submit("-d", '"add single' + " file of size " + @options[:size] + '"')
puts "Uploaded :: file" + i.to_s
rescue P4Exception
p4.errors.each { |e| $stdout.puts( e ) }
end
end
else
begin
for x in 0...@options[:volume] do
p4.run_add(@root+'\file'+x.to_s())
puts "Added :: file" + x.to_s
end
p4.run_submit("-d", '"add ' + @options[:volume].to_s + " files of size " + @options[:size] + '"')
puts "Submitted :: file0 -> file" + @options[:volume].to_s
rescue P4Exception
p4.errors.each { |e| $stdout.puts( e ) }
end
end
end
end
def run()
parse_opts()
if @options[:binary]
write_type = 'wb'
else
write_type = 'w'
end
for i in 0...@options[:volume] do
write_file(@root+'\file'+i.to_s(), write_type)
end
add_to_p4()
return "Done"
end
end
msf = MSF.new()
puts msf.run()