Module: DeeBee::Helpers

Included in:
Backup, CloudSync, FileRotation
Defined in:
lib/dee_bee/helpers.rb

Constant Summary collapse

ONE_MONTH =
60 * 60 * 24 * 30
DEFAULT_DAYS_TO_KEEP_ORPHANS =

Delete files orphaned on the remote after 30 days

30
DEFAULT_DAYS_TO_KEEP_DAILY_FILES =
7

Instance Method Summary collapse

Instance Method Details

#age_in_days(timestamp) ⇒ Object



73
74
75
# File 'lib/dee_bee/helpers.rb', line 73

def age_in_days (timestamp)
  ((Time.now - timestamp)/60/60/24).floor
end

#copy_files_of_pattern_to_directoy(opts) ⇒ Object



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

def copy_files_of_pattern_to_directoy (opts)
  files = Dir.glob("#{opts[:directory]}/#{opts[:pattern]}").select { |fn| File.file?(fn) }

  # make the destination directory
  FileUtils.mkdir_p(opts[:new_directory]) unless File.directory? opts[:new_directory]

  files.each do |project_file|
    new_filepath = File.join([opts[:new_directory], File.basename(project_file)])
    if !!opts[:pretend]
      puts "  pretend: cp #{project_file}, #{new_filepath}" unless File.exists?(new_filepath)
    else
      unless File.exists?(new_filepath)
        FileUtils.cp(project_file, new_filepath)
        puts "  cp #{project_file}, #{new_filepath}" #NOTE: don't use verbose in FileUtils with FakeFS
      end
      FileUtils.rm(project_file) if opts[:remove_original]
    end
  end
end

#move_files_of_pattern_to_directoy(opts) ⇒ Object



53
54
55
# File 'lib/dee_bee/helpers.rb', line 53

def move_files_of_pattern_to_directoy (opts)
  copy_files_of_pattern_to_directoy (opts.merge(:remove_original => true))
end

#remove_files_not_containing_substrings(opts) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dee_bee/helpers.rb', line 57

def remove_files_not_containing_substrings (opts)
  substrings = opts[:substrings].is_a?(String) ? [opts[:substrings]] : opts[:substrings]
  files = Dir.glob("#{opts[:directory]}/**/*").select { |fn| File.file?(fn) }

  files.each do |filename|
    unless substrings.any?{ |substring| File.basename(filename) =~ /#{substring}/ }
      if !!opts[:pretend]
        puts "pretend: rm #{filename}"
      else
        FileUtils.rm(filename)
        puts "  rm #{filename}"
      end
    end
  end
end

#run_command(command) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dee_bee/helpers.rb', line 16

def run_command (command)
  stdin, stdout, stderr = Open3.popen3(command)
  errors = stderr.readlines
  unless errors.empty?
    raise "Command failed with errors: \n#{errors.join("\n")}"
  end
end

#symbolize_keys(original_hash) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/dee_bee/helpers.rb', line 24

def symbolize_keys(original_hash)
  original_hash.inject({}) do |acc, (k,v)|
    key = String === k ? k.to_sym : k
    value = Hash === v ? v.symbolize_keys : v
    acc[key] = value
    acc
  end
end

#time_elapsed_for(name) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dee_bee/helpers.rb', line 77

def time_elapsed_for(name)
  start_time = Time.now
  result = yield
  elapsed_time = Time.now - start_time
  print(green, "#{name} completed in #{ "%0.2f" % elapsed_time } seconds", reset, "\n")
  result
end