Skip to content

Commit

Permalink
Merge pull request #5 from /issues/4
Browse files Browse the repository at this point in the history
Issues/4
  • Loading branch information
isonic1 authored Aug 4, 2016
2 parents a750897 + 13c3029 commit 04883db
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bin/flick
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_relative '../lib/flick'
require 'commander/import'

program :version, '0.2.0'
program :version, '0.2.1'
program :description, 'A CLI to capture screenshots, video, logs, and device info for Android (Devices & Emulators) and iOS (Devices).'

command :video do |c|
Expand Down
2 changes: 1 addition & 1 deletion flick.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", '~> 3.4', '>= 3.4.0'
spec.add_dependency "parallel", '~> 1.6', '>= 1.6.2'
spec.add_dependency "colorize", "~> 0.7.7"
spec.add_dependency "colorize", "~> 0.8.1"
spec.add_dependency "commander", '~> 4.4', '>= 4.4.0'
spec.add_dependency "json", '~> 1.8', '>= 1.8.3'
spec.add_dependency "wannabe_bool", "~> 0.5.0"
Expand Down
22 changes: 13 additions & 9 deletions lib/flick/android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ class Android

def initialize options
Flick::Checker.system_dependency "adb"
self.flick_dir = "#{Dir.home}/.flick"
self.dir_name = "sdcard/flick"
self.udid = options.fetch(:udid, get_device_udid(options))
self.name = options.fetch(:name, self.udid)
self.flick_dir = "#{Dir.home}/.flick/#{udid}"
self.dir_name = "sdcard/flick"
self.name = remove_bad_characters(options.fetch(:name, self.udid))
self.outdir = options.fetch(:outdir, Dir.pwd)
self.unique = options.fetch(:unique, true).to_b
self.limit = options.fetch(:limit, 180)
self.specs = options.fetch(:specs, false)
create_flick_dirs
end

def remove_bad_characters string
string.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
end

def create_flick_dirs
Flick::System.setup_system_dir flick_dir
%x(adb -s #{udid} shell 'mkdir #{dir_name}')
end

def clear_files
Flick::System.clean_system_dir flick_dir, udid
Flick::System.clean_system_dir flick_dir
%x(adb -s #{udid} shell rm '#{dir_name}/*')
end

Expand All @@ -41,8 +45,7 @@ def check_for_devices
end

def get_device_udid opts_hash
devices_connected?
return unless opts_hash[:udid].nil?
check_for_devices
if devices.size == 1
devices.first
else
Expand Down Expand Up @@ -109,7 +112,8 @@ def log name
end

def recordable?
%x(adb -s #{udid} shell "ls /system/bin/screenrecord").strip == "/system/bin/screenrecord"
#%x(adb -s #{udid} shell "ls /system/bin/screenrecord").strip == "/system/bin/screenrecord"
!(udid.include? "emulator" or info[:manufacturer] == "Genymotion")
end

def screenrecord name
Expand All @@ -126,7 +130,7 @@ def unique_files type
else
command = "md5sum"
end
files = %x(adb -s #{udid} shell "#{command} #{dir_name}/#{type}-#{udid}*")
files = %x(adb -s #{udid} shell "#{command} #{dir_name}/#{type}*")
hash = files.split("\r\n").map { |file| { md5: file.match(/(.*) /)[1].strip, file: file.match(/ (.*)/)[1].strip } }
hash.uniq! { |e| e[:md5] }
hash.map { |file| file[:file] }
Expand All @@ -136,7 +140,7 @@ def pull_files type
if unique
files = unique_files type
else
files = %x(adb -s #{udid} shell "ls #{dir_name}/#{type}-#{udid}*").split("\r\n")
files = %x(adb -s #{udid} shell "ls #{dir_name}/#{type}*").split("\r\n")
end
return if files.empty?
Parallel.map(files, in_threads: 10) { |file| pull_file file, flick_dir }
Expand Down
24 changes: 13 additions & 11 deletions lib/flick/info.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Info

attr_accessor :platform, :driver, :save

def initialize options
Flick::Checker.platform options[:platform]
self.platform = options[:platform]
Expand All @@ -14,20 +14,22 @@ def initialize options
end
self.save = options[:save].to_b
end

def save_device_data info
info.each do |k,v|
open("#{driver.outdir}/info-#{driver.udid}.log", 'a') do |file|
file << "#{k}: #{v}\n"
end
end
end


def info
ap driver.info
if save
puts "Saving to #{driver.outdir}/info-#{driver.name}.log"
save_device_data driver.info
end
end

private

def save_device_data info
info.each do |k,v|
open("#{driver.outdir}/info-#{driver.name}.log", 'a') do |file|
file << "#{k}: #{v}\n"
end
end
end
end
12 changes: 8 additions & 4 deletions lib/flick/ios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ class Ios
attr_accessor :flick_dir, :udid, :name, :outdir, :todir, :specs

def initialize options
Flick::Checker.system_dependency "idevice_id"
self.flick_dir = "#{Dir.home}/.flick"
self.udid = options.fetch(:udid, get_device_udid(options))
self.name = options.fetch(:name, self.udid)
self.flick_dir = "#{Dir.home}/.flick/#{udid}"
self.name = remove_bad_characters(options.fetch(:name, self.udid))
self.todir = options.fetch(:todir, self.flick_dir)
self.outdir = options.fetch(:outdir, Dir.pwd)
self.specs = options.fetch(:specs, false)
create_flick_dirs
end

def remove_bad_characters string
string.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
end

def create_flick_dirs
Flick::System.setup_system_dir flick_dir
end

def devices
Flick::Checker.system_dependency "idevice_id"
(`idevice_id -l`).split.uniq.map { |d| d }
end

Expand Down Expand Up @@ -58,7 +62,7 @@ def recordable?
end

def clear_files
Flick::System.clean_system_dir flick_dir, udid
Flick::System.clean_system_dir flick_dir
end

def screenshot name
Expand Down
20 changes: 11 additions & 9 deletions lib/flick/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ def initialize options
self.udid = self.driver.udid
end

def android
platform == "android"
end

def ios
platform == "ios"
end

def run
self.send(action)
end
Expand All @@ -45,8 +37,18 @@ def log
$0 = "flick-log-#{udid}"
SimpleDaemon.daemonize!
command = -> do
driver.log driver.name
driver.log "#{driver.name}"
end
command.call
end

private

def android
platform == "android"
end

def ios
platform == "ios"
end
end
8 changes: 4 additions & 4 deletions lib/flick/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def initialize options
setup
end

def android
platform == "android"
end

def screenshot
puts "Saving to #{driver.outdir}/#{driver.name}.png"
driver.screenshot driver.name
Expand All @@ -27,6 +23,10 @@ def screenshot

private

def android
platform == "android"
end

def setup
driver.clear_files
end
Expand Down
6 changes: 3 additions & 3 deletions lib/flick/simple_daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def self.daemonize! out = '/dev/null', err = '/dev/null', safe = true
raise 'Second fork failed' if (pid = fork) == -1
exit unless pid.nil?
kill_pid
@file = Tempfile.new
@file = Tempfile.new("flick-temp")
@file.write Process.pid
@file.rewind
unless safe
Expand Down Expand Up @@ -115,9 +115,9 @@ def self.kill_pid
rescue TypeError
$stdout.puts "#{pidfile} was empty: TypeError"
rescue Errno::ENOENT
#$stdout.puts "#{pidfile} did not exist: Errno::ENOENT"
$stdout.puts "#{pidfile} did not exist: Errno::ENOENT"
rescue Errno::ESRCH
#$stdout.puts "The process #{opid} did not exist: Errno::ESRCH"
$stdout.puts "The process #{opid} did not exist: Errno::ESRCH"
rescue Errno::EPERM
raise "Lack of privileges to manage the process #{opid}: Errno::EPERM"
rescue ::Exception => e
Expand Down
6 changes: 2 additions & 4 deletions lib/flick/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ def self.setup_system_dir dir_name
Dir.mkdir dir_name unless File.exists? dir_name
end

def self.clean_system_dir dir_name, udid
Dir.glob("#{dir_name}/*#{udid}*").each do |file|
File.delete file
end
def self.clean_system_dir dir_name
Dir.glob("#{dir_name}/*").each { |file| File.delete file }
end

def self.find_pid string
Expand Down
2 changes: 1 addition & 1 deletion lib/flick/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Flick
VERSION = "0.2.0"
VERSION = "0.2.1"
end
41 changes: 25 additions & 16 deletions lib/flick/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,19 @@ def initialize options
self.format = options[:format]
end

def android
platform == "android"
end

def ios
platform == "ios"
end

def run
self.send(action)
end

def start
driver.clear_files
puts "\nStarting Recoder!!!"
if driver.recordable?
if extended
puts "In extended mode."
Flick::Checker.system_dependency "mp4box"
loop_record
else
puts "Device is NOT Recordable!!!"
start_record
end
else
Expand All @@ -63,12 +55,20 @@ def stop

private

def android
platform == "android"
end

def ios
platform == "ios"
end

def start_record
Flick::System.kill_process "video", udid
$0 = "flick-video-#{udid}"
SimpleDaemon.daemonize!
command = -> do
driver.screenrecord "video-#{udid}-single"
driver.screenrecord "video-single"
end
command.call
end
Expand All @@ -81,7 +81,7 @@ def loop_record
count = "%03d" % 1
loop do
unless Flick::System.process_running? "#{udid}-"
driver.screenrecord "video-#{udid}-#{count}"
driver.screenrecord "video-#{count}"
count.next!
end
end
Expand All @@ -93,7 +93,7 @@ def stop_record
Flick::System.kill_process "video", udid
sleep 5 #wait for video process to finish
driver.pull_files "video"
files = Dir.glob("#{driver.flick_dir}/video-#{udid}*.mp4")
files = Dir.glob("#{driver.flick_dir}/video*.mp4")
return if files.empty?
files.each { |file| system("mp4box -cat #{file} #{driver.flick_dir}/#{driver.name}.mp4") }
puts "Saving to #{driver.outdir}/#{driver.name}.#{format}"
Expand All @@ -113,7 +113,7 @@ def start_screenshot_record
count = "%03d" % 1
loop do
if count.to_i <= image_count
driver.screenshot "screenshot-#{udid}-#{count}"
driver.screenshot "screenshot-#{count}"
count.next!; sleep seconds
else
stop_screenshot_recording
Expand All @@ -126,8 +126,9 @@ def start_screenshot_record

def stop_screenshot_recording
driver.pull_files "screenshot" if android
puts "Saving to #{driver.outdir}/#{driver.name}.#{format}"
self.send(format)
Flick::System.kill_process "screenshot", udid
puts "Saving to #{driver.outdir}/#{driver.name}.#{format}"
end

def gif
Expand All @@ -140,13 +141,21 @@ def mp4
File.rename "#{driver.flick_dir}/#{driver.name}.mp4", "#{driver.outdir}/#{driver.name}.mp4" unless format == "gif"
end

def wait_for_file file
start = Time.now
until File.exists? file
sleep 1; break if Time.now - start > 30
end
end

def convert_images_to_mp4
remove_zero_byte_images
%x(ffmpeg -loglevel quiet -framerate 1 -pattern_type glob -i '#{driver.flick_dir}/screenshot-#{udid}*.png' -c:v libx264 -pix_fmt yuv420p #{driver.flick_dir}/#{driver.name}.mp4)
%x(ffmpeg -loglevel quiet -framerate 1 -pattern_type glob -i '#{driver.flick_dir}/screenshot*.png' -c:v libx264 -pix_fmt yuv420p #{driver.flick_dir}/#{driver.name}.mp4)
wait_for_file "#{driver.flick_dir}/#{driver.name}.mp4"
end

def remove_zero_byte_images
Dir.glob("#{driver.flick_dir}/screenshot-#{udid}*.png").each do |f|
Dir.glob("#{driver.flick_dir}/screenshot*.png").each do |f|
File.delete f if File.zero? f
end
end
Expand Down

0 comments on commit 04883db

Please sign in to comment.