Project

General

Profile

Download (2.74 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / base.rb @ ee942928

1
# Copyright (c) 2013 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25

    
26
require 'hammer_cli'
27
require 'katello_api'
28
require 'json'
29
require 'csv'
30

    
31
module HammerCLICsv
32
  class BaseCommand < HammerCLI::AbstractCommand
33

    
34
    HEADERS = {'Accept' => 'version=2,application/json'}
35

    
36
    option ["-v", "--verbose"], :flag, "be verbose"
37
    option ['--threads'], 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
38
    option ['--csv-file'], 'FILE_NAME', 'CSV file to name', :required => true
39
    option ['--csv-export'], :flag, 'Export current data instead of importing'
40

    
41
    def initialize(*args)
42
      @init_options = { :base_url => HammerCLI::Settings.get(:katello, :host),
43
                        :username => HammerCLI::Settings.get(:katello, :username),
44
                        :password => HammerCLI::Settings.get(:katello, :password) }
45
    end
46

    
47
    def get_lines(filename)
48
      file = File.open(filename ,'r')
49
      contents = file.readlines
50
      file.close
51
      contents
52
    end
53

    
54
    def namify(name_format, number)
55
      if name_format.index('%')
56
        name_format % number
57
      else
58
        name_format
59
      end
60
    end
61

    
62
    def thread_import
63
      csv = get_lines(csv_file)[1..-1]
64
      lines_per_thread = csv.length/threads.to_i + 1
65
      splits = []
66

    
67
      threads.to_i.times do |current_thread|
68
        start_index = ((current_thread) * lines_per_thread).to_i
69
        finish_index = ((current_thread + 1) * lines_per_thread).to_i
70
        lines = csv[start_index...finish_index].clone
71
        splits << Thread.new do
72
          lines.each do |line|
73
            if line.index('#') != 0
74
              yield line
75
            end
76
          end
77
        end
78
      end
79

    
80
      splits.each do |thread|
81
        thread.join
82
      end
83

    
84
    end
85
  end
86
end