Class: PubDriver::Pub

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/pub_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops = {}) ⇒ Pub

Returns a new instance of Pub.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/it_tools/pub_driver.rb', line 12

def initialize( ops = {} )
  @ops = {}
  @ops.merge! ops
  @log = Logger.new('log.txt')
  if level = @ops[:debug_level]
    @log.level = level
    @log.debug "Set logger to debug."
  else
    @log.level = Logger::INFO
    @log.info "Set logger to info."
  end
end

Instance Attribute Details

#opsObject

Returns the value of attribute ops.



11
12
13
# File 'lib/it_tools/pub_driver.rb', line 11

def ops
  @ops
end

Instance Method Details

#count_files_in_dir(options, dirs, extension = []) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/it_tools/pub_driver.rb', line 69

def count_files_in_dir options, dirs, extension = []
  file_count = {}
  ext_count = {}
  dirs.each do |dir|
    file_count[dir] = Dir.entries(options[dir]).size - 2
    next unless extension.size > 0
    files_with_extension_count = {}
    extension.each do |curr_ext|
      ext_count[curr_ext] = 0
    end
    Dir.entries(options[dir]).each do |file|
      full_path = File.join options[dir], file
      next unless File.exists? full_path
      next if File.directory? full_path
      extension.each do |curr_ext|
        if (File.extname file) == curr_ext
          ext_count[curr_ext] += 1
        end
      end
    end
    return [file_count,ext_count]
  end
end

#get_local_pub_optionsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/it_tools/pub_driver.rb', line 122

def get_local_pub_options
  options = {}
  src = { :short => '-s', :long => '--src_dir DIR', :mesg => 'Directory of source markdown files.'}
  solr = { :short => '-w', :long => '--solr_write_dir DIR', :mesg => 'Directory to write solr files.'}
  stat = { :short => '-t', :long => '--static_html_dir DIR', :mesg => 'Directory to write static html files.'}
  solr_search = { :short => '-l', :long => '--solr_search_files_dir DIR', :mesg => 'Directory to read solr search files from.'}

  values = { :src_dir => src, :solr_dir => solr, :static_dir => stat, :ssf => solr_search }
  optparse = OptionParser.new do |opts|
    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
    curr_opt = values[:src_dir]
    opts.on( curr_opt[:short], curr_opt[:long], curr_opt[:mesg] ) do |dir|
      @log.debug "Setting source directory for markdown files."
      raise "Directory not specified! " + curr_opt[:mesg] if dir.nil?
      raise "Can't find markdown source dir as specified #{dir}" unless File.directory? dir
      options[:src_dir] = dir
    end
    curr_opt = values[:solr_dir]
    opts.on( curr_opt[:short], curr_opt[:long], curr_opt[:mesg] ) do |dir|
      raise "Directory not specified! " + curr_opt[:mesg] if dir.nil?
      FileUtils.mkdir dir unless File.exists? dir
      options[:solr_dir] = dir
    end
    curr_opt = values[:static_dir]
    opts.on( curr_opt[:short], curr_opt[:long], curr_opt[:mesg] ) do |dir|
      raise "Directory not specified! " + curr_opt[:mesg] if dir.nil?
      FileUtils.mkdir dir unless File.exists? dir
      options[:static_dir] = dir
    end
    curr_opt = values[:ssf]
    opts.on( curr_opt[:short], curr_opt[:long], curr_opt[:mesg] ) do |dir|
      raise "Directory not specified! " + curr_opt[:mesg] if dir.nil?
      FileUtils.mkdir dir unless File.exists? dir
      options[:solr_search_files_dir] = dir
    end

    mesg = "Specify the search category: 'public', 'work', etc..."
    opts.on( '-c', '--index_category CATEGORY', mesg ) do |category|
      raise "Missing Field: " + mesg if category.nil?
      options[:search_category] = category
    end

    mesg = "Set to 'info' or 'debug'"
    opts.on( '-d', '--debug_level LEVEL', mesg ) do |level|
      raise "Debug level not specified! #{mesg}" if level.nil?
      case level
        when "info"
        options[:debug_level] = Logger::INFO
        @log.level = Logger::INFO
        when "debug"
        options[:debug_level] = Logger::DEBUG
        @log.level = Logger::DEBUG
        else
        raise "Debug level not specified properly! #{mesg}"
      end
    end
  end
  optparse.parse!
  validate(options, [:src_dir, :solr_dir, :static_dir], values)
  @ops.merge! options
  return options
end


64
65
66
67
68
# File 'lib/it_tools/pub_driver.rb', line 64

def print_counts hash, printer
  hash.each do |key,value|
    printer.call key, value, hash
  end
end

#publish_locallyObject



53
54
55
56
57
# File 'lib/it_tools/pub_driver.rb', line 53

def publish_locally
  solr_indexer = "http://127.0.0.1:8983/solr/"
  converter = Website::MarkdownConverter.new :indexer_url => solr_indexer
  converter.go
end

#publish_locally2Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/it_tools/pub_driver.rb', line 31

def publish_locally2
  options = get_local_pub_options
  solr_search_files = [ "search.html",
                        "search.js",
                        "ajax-loader.gif",
                        "help.png" ]

  parameters = { 
    :solr_base_url => "http://searcher:8983/solr/",
    :style_sheet => "inputStyles.css",
    :solr_search_files => solr_search_files,
    :src_dir => options[:src_dir],
    :target_dir => options[:solr_dir],
    :static_dir => options[:static_dir],
    :debug_level => options[:debug_level],
    :solr_search_files_dir => options[:solr_search_files_dir],
    :search_category => options[:search_category] }
  
  @log.debug "Params: " + parameters.to_s
  publisher = Publisher::Markdown.new parameters
  publisher.process_files
end

#publish_remote(params) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/it_tools/pub_driver.rb', line 100

def publish_remote params
  vpnTools = NetworkTools::VpnTools.new
  rsync_ops = {}
  rsync_ops[:from_dir] = params[:doc]
  rsync_ops[:as_user] = params[:as_user]
  if vpnTools.on_vpn
    @log.debug "On VPN, so sync'ing regular docs AND work docs"
    rsync_ops[:to_host] = params[:int_host]

    rsync_ops[:to_dir] = params[:int_docs]
    rsync_it rsync_ops

    rsync_ops[:from_dir] = params[:wdoc]
    rsync_ops[:to_dir] = params[:int_wdocs]
    rsync_it rsync_ops
  else
    @log.debug "On VPN, so only sync'ing regular docs."
    rsync_ops[:to_host] = params[:ext_host]
    rsync_ops[:to_dir] = params[:ext_docs]
    rsync_it rsync_ops
  end
end

#rsync_it(ops) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/it_tools/pub_driver.rb', line 92

def rsync_it ops
  command = "rsync -avP --stats #{ops[:from_dir]} "
  command += "#{ops[:as_user]}@" if ops[:as_user]
  command += "#{ops[:to_host]}:#{ops[:to_dir]} "
  p "[command]: " + command
  system command
end

#validate(options, required, values) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/it_tools/pub_driver.rb', line 24

def validate( options, required, values ) 
  missing_msg = "Missing required parameter: '"
  required.each do |entry|
    value = values[entry]
    raise missing_msg + value[:short] + "', or '" + value[:long] + "', which specifies the " + value[:mesg] unless options[entry]
  end
end

#validate_local_publish(options) ⇒ Object



58
59
60
61
62
63
# File 'lib/it_tools/pub_driver.rb', line 58

def validate_local_publish options
  dir_counts, ext_counts = count_files_in_dir options, [:src_dir, :solr_dir, :static_dir], ['.mmd']
  dir_count_printer = lambda { |key,count,hash| p hash[key] + ": [#{count}]" }
  print_counts dir_counts, dir_count_printer
  print_counts ext_counts, options
end