Class: Websitary::App
- Inherits:
-
Object
- Object
- Websitary::App
- Defined in:
- lib/websitary.rb
Overview
Constant Summary collapse
- MINUTE_SECS =
60
- HOUR_SECS =
MINUTE_SECS * 60
- DAY_SECS =
HOUR_SECS * 24
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
The configurator.
-
#difftext ⇒ Object
readonly
Hash: The output of the diff commands for each url.
-
#tdiff_min ⇒ Object
readonly
Secs until next update.
Instance Method Summary collapse
- #cmdline_arg_add(configuration, url) ⇒ Object
- #copy(from, to) ⇒ Object
- #copy_move(method, from, to) ⇒ Object
- #ensure_dir(dir, fatal_nondir = true) ⇒ Object
- #execute_add ⇒ Object
-
#execute_aggregate ⇒ Object
Aggregate data for later review (see #execute_show).
-
#execute_configuration ⇒ Object
Show the currently configured URLs.
-
#execute_downdiff(show_output = true, rebuild = false, &accumulator) ⇒ Object
Process the sources in @configuration.url as defined by profiles and command-line options.
-
#execute_edit ⇒ Object
Edit currently chosen profiles.
-
#execute_latest ⇒ Object
Show the current version of all urls.
- #execute_ls ⇒ Object
-
#execute_rebuild ⇒ Object
Rebuild the report from the already downloaded copies.
-
#execute_review ⇒ Object
Show the latest report.
-
#execute_show ⇒ Object
Show data collected by #execute_aggregate.
-
#execute_unroll ⇒ Object
Restore previous backups.
- #format_tdiff(secs) ⇒ Object
-
#initialize(args = []) ⇒ App
constructor
- args
-
Array of command-line (like) arguments.
- #move(from, to) ⇒ Object
-
#process ⇒ Object
Run the command stored in @execute.
Constructor Details
#initialize(args = []) ⇒ App
- args
-
Array of command-line (like) arguments.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/websitary.rb', line 66 def initialize(args=[]) @configuration = Websitary::Configuration.new(self, args) @difftext = {} @tdiff_min = nil ensure_dir(@configuration.cfgdir) css = File.join(@configuration.cfgdir, 'websitary.css') unless File.exists?(css) $logger.info "Copying default css file: #{css}" @configuration.write_file(css, 'w') do |io| io.puts @configuration.opt_get(:page, :css) end end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
The configurator
59 60 61 |
# File 'lib/websitary.rb', line 59 def configuration @configuration end |
#difftext ⇒ Object (readonly)
Hash: The output of the diff commands for each url.
56 57 58 |
# File 'lib/websitary.rb', line 56 def difftext @difftext end |
#tdiff_min ⇒ Object (readonly)
Secs until next update.
62 63 64 |
# File 'lib/websitary.rb', line 62 def tdiff_min @tdiff_min end |
Instance Method Details
#cmdline_arg_add(configuration, url) ⇒ Object
121 122 123 |
# File 'lib/websitary.rb', line 121 def cmdline_arg_add(configuration, url) configuration.to_do url end |
#copy(from, to) ⇒ Object
305 306 307 308 |
# File 'lib/websitary.rb', line 305 def copy(from, to) # copy_move(:copy, from, to) copy_move(:cp, from, to) end |
#copy_move(method, from, to) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/websitary.rb', line 311 def copy_move(method, from, to) if File.exists?(from) $logger.debug "Overwrite: #{from} -> #{to}" if File.exists?(to) lst = File.lstat(from) FileUtils.send(method, from, to) File.utime(lst.atime, lst.mtime, to) @configuration.mtimes.set(from, lst.mtime) @configuration.mtimes.set(to, lst.mtime) end end |
#ensure_dir(dir, fatal_nondir = true) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/websitary.rb', line 334 def ensure_dir(dir, fatal_nondir=true) if File.exist?(dir) unless File.directory?(dir) if fatal_nondir $logger.fatal "Not a directory: #{dir}" exit 5 else $logger.info "Not a directory: #{dir}" return false end end else parent = Pathname.new(dir).parent.to_s ensure_dir(parent, fatal_nondir) unless File.directory?(parent) Dir.mkdir(dir) end return true end |
#execute_add ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/websitary.rb', line 126 def execute_add if @configuration.quicklist_profile quicklist = @configuration.profile_filename(@configuration.quicklist_profile, false) $logger.info "Use quicklist file: #{quicklist}" if quicklist @configuration.write_file(quicklist, 'a') do |io| @configuration.todo.each do |url| io.puts %{source #{url.inspect}} end end return 0 end end $logger.fatal 'No valid quick-list profile defined' exit 5 end |
#execute_aggregate ⇒ Object
Aggregate data for later review (see #execute_show)
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/websitary.rb', line 191 def execute_aggregate rv = execute_downdiff(false) do |url, difftext, opts| if difftext and !difftext.empty? aggrbase = @configuration.encoded_filename('aggregate', url, true, 'md5') aggrext = Digest::MD5.hexdigest(Time.now.to_s) aggrfile = [aggrbase, aggrext].join('_') @configuration.write_file(aggrfile) {|io| io.puts difftext} end end clean_diffs rv end |
#execute_configuration ⇒ Object
Show the currently configured URLs
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/websitary.rb', line 100 def execute_configuration keys = @configuration..keys urls = @configuration.todo # urls = @configuration.todo..sort {|a,b| @configuration.url_get(a, :title, a) <=> @configuration.url_get(b, :title, b)} urls.each_with_index do |url, i| data = @configuration.urls[url] text = [ "<b>URL</b><br/>#{url}<br/>", "<b>current</b><br/>#{CGI.escapeHTML(@configuration.latestname(url, true))}<br/>", "<b>backup</b><br/>#{CGI.escapeHTML(@configuration.oldname(url, true))}<br/>", *((data.keys | keys).map do |k| v = @configuration.url_get(url, k).inspect "<b>:#{k}</b><br/>#{CGI.escapeHTML(v)}<br/>" end) ] accumulate(url, text.join("<br/>")) end return show end |
#execute_downdiff(show_output = true, rebuild = false, &accumulator) ⇒ Object
Process the sources in @configuration.url as defined by profiles and command-line options. The differences are stored in @difftext (a Hash).
- show_output
-
If true, show the output with the defined viewer.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/websitary.rb', line 252 def execute_downdiff(show_output=true, rebuild=false, &accumulator) if @configuration.todo.empty? $logger.error 'Nothing to do' return 5 end @configuration.todo.each do |url| opts = @configuration.urls[url] $logger.debug "Source: #{@configuration.url_get(url, :title, url)}" diffed = @configuration.diffname(url, true) $logger.debug "diffname: #{diffed}" if File.exists?(diffed) $logger.warn "Reuse old diff: #{@configuration.url_get(url, :title, url)} => #{diffed}" difftext = File.read(diffed) accumulate(url, difftext, opts) else latest = @configuration.latestname(url, true) $logger.debug "latest: #{latest}" next unless rebuild or !skip_url?(url, latest, opts) older = @configuration.oldname(url, true) $logger.debug "older: #{older}" begin if rebuild or download(url, opts, latest, older) difftext = diff(url, opts, latest, older) if difftext @configuration.write_file(diffed, 'wb') {|io| io.puts difftext} # $logger.debug "difftext: #{difftext}" #DBG# if accumulator accumulator.call(url, difftext, opts) else accumulate(url, difftext, opts) end end end rescue Exception => e $logger.error e.to_s $logger.info e.backtrace.join("\n") end end end return show_output ? show : @difftext.empty? ? 0 : 1 end |
#execute_edit ⇒ Object
Edit currently chosen profiles
160 161 162 163 |
# File 'lib/websitary.rb', line 160 def execute_edit @configuration.edit_profile exit 0 end |
#execute_latest ⇒ Object
Show the current version of all urls
174 175 176 177 178 179 180 181 |
# File 'lib/websitary.rb', line 174 def execute_latest @configuration.todo.each do |url| latest = @configuration.latestname(url) text = File.read(latest) accumulate(url, text) end return show end |
#execute_ls ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/websitary.rb', line 205 def execute_ls rv = 0 @configuration.todo.each do |url| opts = @configuration.urls[url] name = @configuration.url_get(url, :title, url) $logger.debug "Source: #{name}" aggrbase = @configuration.encoded_filename('aggregate', url, true, 'md5') aggrfiles = Dir["#{aggrbase}_*"] aggrn = aggrfiles.size if aggrn > 0 puts "%3d - %s" % [aggrn, name] rv = 1 end end rv end |
#execute_rebuild ⇒ Object
Rebuild the report from the already downloaded copies.
185 186 187 |
# File 'lib/websitary.rb', line 185 def execute_rebuild execute_downdiff(true, true) end |
#execute_review ⇒ Object
Show the latest report
167 168 169 170 |
# File 'lib/websitary.rb', line 167 def execute_review @configuration.view_output 0 end |
#execute_show ⇒ Object
Show data collected by #execute_aggregate
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/websitary.rb', line 224 def execute_show @configuration.todo.each do |url| opts = @configuration.urls[url] $logger.debug "Source: #{@configuration.url_get(url, :title, url)}" aggrbase = @configuration.encoded_filename('aggregate', url, true, 'md5') difftext = [] aggrfiles = Dir["#{aggrbase}_*"] aggrfiles.each do |file| difftext << File.read(file) end difftext.compact! difftext.delete('') unless difftext.empty? joindiffs = @configuration.url_get(url, :joindiffs, lambda {|t| t.join("\n")}) difftext = @configuration.call_cmd(joindiffs, [difftext], :url => url) if joindiffs accumulate(url, difftext, opts) end aggrfiles.each do |file| File.delete(file) end end show end |
#execute_unroll ⇒ Object
Restore previous backups
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/websitary.rb', line 145 def execute_unroll @configuration.todo.each do |url| latest = @configuration.latestname(url, true) backup = @configuration.oldname(url, true) if File.exist?(backup) $logger.warn "Restore: #{url}" $logger.debug "Copy: #{backup} => #{latest}" copy(backup, latest) end end return 0 end |
#format_tdiff(secs) ⇒ Object
323 324 325 326 327 328 329 330 331 |
# File 'lib/websitary.rb', line 323 def format_tdiff(secs) d = (secs / DAY_SECS).to_i if d > 0 return "#{d}d" else d = (secs / HOUR_SECS).to_i return "#{d}h" end end |
#move(from, to) ⇒ Object
299 300 301 302 |
# File 'lib/websitary.rb', line 299 def move(from, to) # copy_move(:rename, from, to) # ftools copy_move(:mv, from, to) # FileUtils end |
#process ⇒ Object
Run the command stored in @execute.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/websitary.rb', line 83 def process begin m = "execute_#{@configuration.execute}" if respond_to?(m) exit_code = send(m) else $logger.fatal "Unknown command: #{@configuration.execute}" exit_code = 5 end ensure @configuration.mtimes.swap_out end return exit_code end |