Skip to content

Instantly share code, notes, and snippets.

@ecavazos
Created November 1, 2010 14:01
Show Gist options
  • Save ecavazos/658210 to your computer and use it in GitHub Desktop.
Save ecavazos/658210 to your computer and use it in GitHub Desktop.
A ruby script to zip up a code directory
#!/usr/bin/env ruby
require 'date'
# Arco - (Ar)chive (Co)de
class Arco
def initialize(arguments)
@start = Time.now
@source = arguments[0]
@target = arguments[1]
valid_unless('Source directory must be provided') { @source }
valid_unless "Source '#{@source}' must be a directory" do
File.directory?(@source)
end
valid_unless('Target directory must be provided') { @target }
valid_unless "Target (#{@target}) must be a directory" do
File.directory?(@target)
end
# TODO
# 1. growl notify
end
def run
archive = "#{@source}_#{timestamp()}.tar.gz"
puts "archiving #{@source}:"
sleep 1
if system("tar -cvzf #{archive} #{@source}")
p_line
mv_archive(archive)
else
exit
end
puts results
end
private
def mv_archive(archive)
puts "moving #{archive} to #{@target}:"
%x{mv -vf #{archive} #{@target}}
end
def p_line
60.times { print '-'}
puts "\n\n"
end
def results
elapsed = Time.now - @start
hms = seconds_to_hms(elapsed)
"\nArchive finished in #{hms[:hours]} hour(s)," +
" #{hms[:minutes]} minute(s) and #{hms[:seconds]} second(s)"
end
def valid_unless(error_message)
raise ArgumentError, error_message unless yield
end
def timestamp
DateTime.now.strftime('%Y%m%d%H%M%S')
end
def seconds_to_hms(seconds)
div = lambda { |x| (x/60).to_i }
mod = lambda { |x| (x%60).to_i }
h = m = 0
s = mod.call(seconds)
if seconds >= 60 then
m = div.call(seconds)
if m >= 60 then
h = div.call(m)
m = mod.call(m)
end
end
{:hours => h, :minutes => m, :seconds => s}
end
end
# Create and run the application
arco = Arco.new(ARGV)
arco.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment