Skip to content

Commit

Permalink
Merge pull request #361 from BBC-News/wraith-3.1
Browse files Browse the repository at this point in the history
[ready to merge] Wraith 3.1
  • Loading branch information
ChrisBAshton committed Dec 24, 2015
2 parents a15cb1b + a251817 commit 2932a19
Show file tree
Hide file tree
Showing 48 changed files with 1,039 additions and 605 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "https://rubygems.org"

# Specify your gem's dependencies in ..gemspec
# Specify your gem's dependencies in wraith.gemspec
gemspec
181 changes: 108 additions & 73 deletions lib/wraith/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,137 +7,172 @@
require "wraith/thumbnails"
require "wraith/compare_images"
require "wraith/gallery"
require "wraith/validate"
require "wraith/version"
require "wraith/helpers/logger"
require "wraith/helpers/utilities"

class Wraith::CLI < Thor
include Thor::Actions
include Logging

attr_accessor :config_name

def self.source_root
File.expand_path("../../../", __FILE__)
end

# define internal methods which user should not be able to run directly
no_commands do
def within_acceptable_limits
yield
rescue CustomError => e
logger.error e.message
# other errors, such as SystemError, will not be caught nicely and will give a stack trace (which we'd need)
end

def check_for_paths(config_name)
spider = Wraith::Spidering.new(config_name)
spider.check_for_paths
end

def copy_old_shots(config_name)
create = Wraith::FolderManager.new(config_name)
create.copy_old_shots
end
end

desc "validate", "checks your configuration and validates that all required properties exist"
def validate(config_name)
within_acceptable_limits do
wraith = Wraith::Validate.new(config_name).validate
end
end

desc "setup", "creates config folder and default config"
def setup
directory("templates/configs", "configs")
directory("templates/javascript", "javascript")
within_acceptable_limits do
directory("templates/configs", "configs")
directory("templates/javascript", "javascript")
end
end

desc "reset_shots [config_name]", "removes all the files in the shots folder"
def reset_shots(config_name)
reset = Wraith::FolderManager.new(config_name)
reset.clear_shots_folder
within_acceptable_limits do
reset = Wraith::FolderManager.new(config_name)
reset.clear_shots_folder
end
end

desc "setup_folders [config_name]", "create folders for images"
def setup_folders(config_name)
create = Wraith::FolderManager.new(config_name)
create.create_folders
within_acceptable_limits do
create = Wraith::FolderManager.new(config_name)
create.create_folders
end
end

desc "copy_base_images [config_name]", "copies the required base images over for comparison with latest images"
def copy_base_images(config_name)
copy = Wraith::FolderManager.new(config_name)
copy.copy_base_images
end

desc "make_sure_base_shots_exists [config_name]", "warns user if config is missing base shots"
def make_sure_base_shots_exists(config_name)
wraith = Wraith::Wraith.new(config_name)
if wraith.history_dir.nil?
puts "You need to specify a `history_dir` property at #{config_name} before you can run `wraith latest`!"
exit 1
end
if !File.directory?(wraith.history_dir)
puts "You need to run `wraith history` at least once before you can run `wraith latest`!"
exit 1
end
end

no_commands do
def check_for_paths(config_name)
spider = Wraith::Spidering.new(config_name)
spider.check_for_paths
end

def copy_old_shots(config_name)
create = Wraith::FolderManager.new(config_name)
create.copy_old_shots
within_acceptable_limits do
copy = Wraith::FolderManager.new(config_name)
copy.copy_base_images
end
end

desc "save_images [config_name]", "captures screenshots"
def save_images(config_name, history = false)
puts "SAVING IMAGES"
save_images = Wraith::SaveImages.new(config_name, history)
save_images.save_images
within_acceptable_limits do
logger.info "SAVING IMAGES"
save_images = Wraith::SaveImages.new(config_name, history)
save_images.save_images
end
end

desc "crop_images [config_name]", "crops images to the same height"
def crop_images(config_name)
crop = Wraith::CropImages.new(config_name)
crop.crop_images
within_acceptable_limits do
crop = Wraith::CropImages.new(config_name)
crop.crop_images
end
end

desc "compare_images [config_name]", "compares images to generate diffs"
def compare_images(config_name)
puts "COMPARING IMAGES"
compare = Wraith::CompareImages.new(config_name)
compare.compare_images
within_acceptable_limits do
logger.info "COMPARING IMAGES"
compare = Wraith::CompareImages.new(config_name)
compare.compare_images
end
end

desc "generate_thumbnails [config_name]", "create thumbnails for gallery"
def generate_thumbnails(config_name)
puts "GENERATING THUMBNAILS"
thumbs = Wraith::Thumbnails.new(config_name)
thumbs.generate_thumbnails
within_acceptable_limits do
logger.info "GENERATING THUMBNAILS"
thumbs = Wraith::Thumbnails.new(config_name)
thumbs.generate_thumbnails
end
end

desc "generate_gallery [config_name]", "create page for viewing images"
def generate_gallery(config_name, multi = false)
puts "GENERATING GALLERY"
gallery = Wraith::GalleryGenerator.new(config_name, multi)
gallery.generate_gallery
within_acceptable_limits do
logger.info "GENERATING GALLERY"
gallery = Wraith::GalleryGenerator.new(config_name, multi)
gallery.generate_gallery
end
end

desc "capture [config_name]", "A full Wraith job"
desc "capture [config_name]", "Capture paths against two domains, compare them, generate gallery"
def capture(config, multi = false)
reset_shots(config)
check_for_paths(config)
setup_folders(config)
save_images(config)
crop_images(config)
compare_images(config)
generate_thumbnails(config)
generate_gallery(config, multi)
within_acceptable_limits do
logger.info Wraith::Validate.new(config).validate('capture')
reset_shots(config)
check_for_paths(config)
setup_folders(config)
save_images(config)
crop_images(config)
compare_images(config)
generate_thumbnails(config)
generate_gallery(config, multi)
end
end

desc "multi_capture [filelist]", "A Batch of Wraith Jobs"
def multi_capture(filelist)
config_array = IO.readlines(filelist)
config_array.each do |config|
capture(config.chomp, true)
within_acceptable_limits do
config_array = IO.readlines(filelist)
config_array.each do |config|
capture(config.chomp, true)
end
end
end

desc "history [config_name]", "Setup a baseline set of shots"
def history(config)
reset_shots(config)
check_for_paths(config)
setup_folders(config)
save_images(config)
copy_old_shots(config)
within_acceptable_limits do
logger.info Wraith::Validate.new(config).validate('history')
reset_shots(config)
check_for_paths(config)
setup_folders(config)
save_images(config)
copy_old_shots(config)
end
end

desc "latest [config_name]", "Capture new shots to compare with baseline"
def latest(config)
make_sure_base_shots_exists(config)
reset_shots(config)
save_images(config, true)
copy_base_images(config)
crop_images(config)
compare_images(config)
generate_thumbnails(config)
generate_gallery(config)
end
end
within_acceptable_limits do
logger.info Wraith::Validate.new(config).validate('latest')
reset_shots(config)
save_images(config, true)
copy_base_images(config)
crop_images(config)
compare_images(config)
generate_thumbnails(config)
generate_gallery(config)
end
end
end
6 changes: 4 additions & 2 deletions lib/wraith/compare_images.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "wraith"
require "wraith/helpers/logger"
require "image_size"
require "open3"
require "parallel"
require "shellwords"

class Wraith::CompareImages
include Logging
attr_reader :wraith

def initialize(config)
Expand All @@ -16,9 +18,9 @@ def compare_images
Parallel.each(files.each_slice(2), :in_processes => Parallel.processor_count) do |base, compare|
diff = base.gsub(/([a-zA-Z0-9]+).png$/, "diff.png")
info = base.gsub(/([a-zA-Z0-9]+).png$/, "data.txt")
puts "Comparing #{base} and #{compare}"
logger.info "Comparing #{base} and #{compare}"
compare_task(base, compare, diff, info)
puts "Saved diff"
logger.info "Saved diff"
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/wraith/crop.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "wraith"
require "wraith/helpers/logger"
require "image_size"
require "parallel"
require "shellwords"

class Wraith::CropImages
include Logging
attr_reader :wraith

def initialize(config)
Expand All @@ -14,7 +16,7 @@ def crop_images
files = Dir.glob("#{wraith.directory}/*/*.png").sort

Parallel.each(files.each_slice(2), :in_processes => Parallel.processor_count) do |base, compare|
puts "cropping images"
logger.info "cropping images"

width = image_dimensions(base)[0]
base_height = image_dimensions(base)[1]
Expand Down
10 changes: 6 additions & 4 deletions lib/wraith/folder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "wraith"
require "wraith/helpers/logger"

class Wraith::FolderManager
include Logging
attr_reader :wraith

def initialize(config)
Expand Down Expand Up @@ -35,17 +37,17 @@ def clear_shots_folder

def copy_old_shots
if history_dir.nil?
abort 'Error: no `history_dir` attribute found in config. Cannot copy files.'
logger.error 'no `history_dir` attribute found in config. Cannot copy files.'
else
FileUtils.cp_r("#{dir}/.", "#{history_dir}/")
end
end

def copy_base_images
puts "COPYING BASE IMAGES"
logger.info "COPYING BASE IMAGES"
wraith.paths.each do |path|
path = path[0]
puts "Copying #{history_dir}/#{path} to #{dir}"
logger.info "Copying #{history_dir}/#{path} to #{dir}"
FileUtils.cp_r(Dir.glob("#{history_dir}/#{path}"), dir)
end
end
Expand All @@ -60,7 +62,7 @@ def create_folders
FileUtils.mkdir_p("#{dir}/thumbnails/#{folder_label}")
FileUtils.mkdir_p("#{dir}/#{folder_label}")
end
puts "Creating Folders"
logger.info "Creating Folders"
end

def tidy_shots_folder(dirs)
Expand Down
Loading

0 comments on commit 2932a19

Please sign in to comment.