-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.rb
88 lines (66 loc) · 1.65 KB
/
cleanup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env ruby
require 'optparse'
require 'shellwords'
dry_run = false
verbose = false
limit = 0
OptionParser.new do |opts|
opts.banner = "Usage: ruby cleanup.rb [options] <paths>"
opts.on('-n', '--dry-run', "Don't delete anything") do |n|
dry_run = n
end
opts.on('-v', '--verbose', "Be verbose") do |v|
verbose = v
end
opts.on('-l', '--limit LIMIT', "Limit size") do |l|
l.match(/^(\d+)([kmgt]?)$/i) do |m|
limit = m[1].to_i
multipliers = {
"t" => 1024**4,
"g" => 1024**3,
"m" => 1024**2,
"k" => 1024
}
limit *= multipliers[m[2].downcase] unless m[2].empty?
end
end
end.parse!
if limit < 1
puts "Need a --limit"
exit!
end
def dir_by_date(path)
paths = {}
Dir.new(path).each do |sub_path|
next if sub_path =~ /^..?$/
full_path = path + File::SEPARATOR + sub_path
paths[full_path] = File.mtime(full_path).to_i
end
paths.sort_by { |path, mtime| mtime }.collect { |name, time| name }
end
def get_size(path)
return File.size(path) if !FileTest.directory? path
total = 0
Dir.new(path).each do |sub_path|
next if sub_path =~ /^..?$/
total += get_size(path + File::SEPARATOR + sub_path)
end
total
end
ARGV.each do |path|
path = path.chomp('/')
puts "#{path} does not exist or is not a directory" unless Dir.exist? path
total = 0
dir_by_date(path).reverse().each do |sub_path|
total += get_size(sub_path)
if total > limit
if !dry_run
puts "Deleting #{sub_path}" if verbose
system "rm -rf #{sub_path.shellescape}"
# todo
else
puts "Would delete #{sub_path}"
end
end
end
end