toreriklinnerud-clean-files 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +46 -0
- data/bin/clean_files +6 -0
- data/lib/clean_files.rb +92 -0
- data/lib/cleaner.rb +58 -0
- data/spec/cleaner_spec.rb +117 -0
- data/spec/spec_helper.rb +10 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 AlphaSights Ltd
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
== Synopsis
|
2
|
+
|
3
|
+
Executable to delete files fitting certain criteria.
|
4
|
+
|
5
|
+
The intended purpose is to delete backups older than a certain date,
|
6
|
+
whilst keeping hourly, daily or weekly and so on backups.
|
7
|
+
|
8
|
+
Specifying an option like --hourly will keep the first file of the hour
|
9
|
+
and delete the rest of the files created within the same hour,
|
10
|
+
provided that the those files were created before the threshold date.
|
11
|
+
|
12
|
+
The executable does not itself create any backups, it is only
|
13
|
+
intended for cleaning up existing ones.
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
clean-files file_paths [options]
|
18
|
+
|
19
|
+
For help use: clean-files -h
|
20
|
+
== Options
|
21
|
+
|
22
|
+
-v, --verbose Print name of files deleted
|
23
|
+
-p, --pretend Implies -v, only prints what files would have been deleted
|
24
|
+
-r, --recursive Delete directories as well as files
|
25
|
+
-t, --threshold Time ago in days for when to start deleting files
|
26
|
+
File newer than this date are never deleted.
|
27
|
+
The default is 30 days.
|
28
|
+
|
29
|
+
For example:
|
30
|
+
|
31
|
+
-t 10 or --threshold=30
|
32
|
+
|
33
|
+
-H, --hourly Keep hourly files
|
34
|
+
-D, --daily Keep daily files
|
35
|
+
-W, --weekly Keep weekly files
|
36
|
+
-M, --monthly Keep monthly files
|
37
|
+
-Y, --yearly Keep yearly files
|
38
|
+
|
39
|
+
== Examples
|
40
|
+
|
41
|
+
clean_files /backups/sql/*.sql --threshold 60 --daily
|
42
|
+
clean_files /Users/me/Downloads/* --pretend --verbose --recursive -t 10
|
43
|
+
|
44
|
+
== Copyright
|
45
|
+
|
46
|
+
Copyright (c) 2009 AlphaSights Ltd. See LICENSE for details.
|
data/bin/clean_files
ADDED
data/lib/clean_files.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'rdoc/usage'
|
3
|
+
require 'active_support'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
require 'cleaner'
|
7
|
+
|
8
|
+
class CleanFiles
|
9
|
+
VERSION = '0.1.0'
|
10
|
+
|
11
|
+
attr_reader :options
|
12
|
+
|
13
|
+
def initialize(arguments)
|
14
|
+
@arguments = arguments
|
15
|
+
|
16
|
+
# Set defaults
|
17
|
+
@options = Hash.new
|
18
|
+
@options[:threshold] = 1.month.ago
|
19
|
+
end
|
20
|
+
|
21
|
+
# Parse options, check arguments, then process the command
|
22
|
+
def run
|
23
|
+
|
24
|
+
if parsed_options? && arguments_valid?
|
25
|
+
Cleaner.new(@options.delete(:paths), @options).start
|
26
|
+
else
|
27
|
+
output_usage
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def parsed_options?
|
35
|
+
|
36
|
+
# Specify options
|
37
|
+
opts = OptionParser.new
|
38
|
+
opts.on('-h', '--help') { output_help }
|
39
|
+
opts.on('-v', '--verbose') { @options[:verbose] = true }
|
40
|
+
opts.on('-d', '--pretend') { @options[:pretend] = true
|
41
|
+
@options[:verbose] = true }
|
42
|
+
opts.on('-r', '--recursive') {@options[:recursive] = true }
|
43
|
+
|
44
|
+
opts.on('-t', '--treshold [DAYS]') do |days|
|
45
|
+
raise OptionParser::InvalidOption unless days.to_i > 0 || days == "0"
|
46
|
+
@options[:threshold] = days.to_i.send(:days).ago
|
47
|
+
end
|
48
|
+
|
49
|
+
Cleaner::VALID_INTERVALS.each do |interval|
|
50
|
+
opts.on("-#{interval.to_s.first.upcase}", "--#{interval}") {@options[interval] = true}
|
51
|
+
end
|
52
|
+
return false unless opts.parse!(@arguments)
|
53
|
+
|
54
|
+
@options[:paths] = @arguments.dup
|
55
|
+
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def arguments_valid?
|
60
|
+
@options[:paths].present?
|
61
|
+
end
|
62
|
+
|
63
|
+
def output_help
|
64
|
+
rdoc
|
65
|
+
exit -1
|
66
|
+
end
|
67
|
+
|
68
|
+
def output_usage
|
69
|
+
rdoc('Usage', 'Options')
|
70
|
+
exit -1
|
71
|
+
end
|
72
|
+
|
73
|
+
def rdoc(*sections)
|
74
|
+
readme_path = File.join(File.dirname(__FILE__), '/../README.rdoc')
|
75
|
+
comment = File.read(readme_path)
|
76
|
+
|
77
|
+
markup = SM::SimpleMarkup.new
|
78
|
+
flow_convertor = SM::ToFlow.new
|
79
|
+
flow = markup.convert(comment, flow_convertor)
|
80
|
+
format = "plain"
|
81
|
+
|
82
|
+
unless sections.empty?
|
83
|
+
flow = RDoc.extract_sections(flow, sections)
|
84
|
+
end
|
85
|
+
|
86
|
+
options = RI::Options.instance
|
87
|
+
|
88
|
+
formatter = options.formatter.new(options, "")
|
89
|
+
formatter.display_flow(flow)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
data/lib/cleaner.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class Cleaner
|
2
|
+
|
3
|
+
attr_reader :paths, :options
|
4
|
+
|
5
|
+
VALID_OPTIONS = [:threshold, :pretend, :verbose, :recursive]
|
6
|
+
VALID_INTERVALS = [:hourly, :daily, :weekly, :monthly, :yearly]
|
7
|
+
TIME_INTERVALS = [:hour, :day, :cweek, :month, :year]
|
8
|
+
|
9
|
+
def initialize(paths, options = {})
|
10
|
+
@paths = [paths].flatten
|
11
|
+
@options = options.reverse_merge(:threshold => 30.days.ago)
|
12
|
+
options.assert_valid_keys(VALID_OPTIONS + VALID_INTERVALS)
|
13
|
+
given_intervals = VALID_INTERVALS & options.keys
|
14
|
+
if given_intervals.present?
|
15
|
+
lowest_selected_position = VALID_INTERVALS.index(given_intervals.first)
|
16
|
+
@key_intervals = TIME_INTERVALS[lowest_selected_position..-1]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def start
|
21
|
+
files = files_to_delete.map(&:path)
|
22
|
+
puts files.join("\n") if options[:verbose]
|
23
|
+
FileUtils.rm_rf(files, :noop => !!options[:pretend])
|
24
|
+
end
|
25
|
+
|
26
|
+
def files
|
27
|
+
@_files ||= paths.map do |file_path|
|
28
|
+
begin
|
29
|
+
File.new(file_path)
|
30
|
+
rescue Errno::EOPNOTSUPP
|
31
|
+
nil
|
32
|
+
rescue Errno::ENOENT => e
|
33
|
+
puts e.to_s
|
34
|
+
exit -2
|
35
|
+
end
|
36
|
+
end.compact.select do |file|
|
37
|
+
file.stat.file? || (options[:recursive] && file.stat.directory?)
|
38
|
+
end.sort_by(&:ctime)
|
39
|
+
end
|
40
|
+
|
41
|
+
def files_to_delete
|
42
|
+
files_before_threshold - files_to_preserve
|
43
|
+
end
|
44
|
+
|
45
|
+
def files_before_threshold
|
46
|
+
files.select{|file| file.ctime < options[:threshold]}
|
47
|
+
end
|
48
|
+
|
49
|
+
def files_to_preserve
|
50
|
+
return [] if @key_intervals.nil?
|
51
|
+
files_before_threshold.group_by do |file|
|
52
|
+
cdate = file.ctime.to_datetime
|
53
|
+
@key_intervals.map{|interval| cdate.send(interval)}
|
54
|
+
end.map do |_, files|
|
55
|
+
files.first
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '/spec_helper')
|
2
|
+
|
3
|
+
describe Cleaner do
|
4
|
+
|
5
|
+
it 'find files older than threshold' do
|
6
|
+
cleaner = Cleaner.new('/path', :threshold => 5.days.ago)
|
7
|
+
cleaner.should_receive(:files).and_return(mock_files(7.days.ago, 4.days.ago))
|
8
|
+
cleaner.files_before_threshold.should == mock_files(7.days.ago)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'deletes files' do
|
12
|
+
cleaner = Cleaner.new('/path')
|
13
|
+
files = mock_files(3.days.ago, 2.days.ago, 7.days.ago)
|
14
|
+
cleaner.should_receive(:files_to_delete).and_return(files)
|
15
|
+
FileUtils.should_receive(:rm_rf).with(files.map(&:path), :noop => false)
|
16
|
+
cleaner.start
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not delete any files if specifying pretend => true' do
|
20
|
+
cleaner = Cleaner.new('/path', :pretend => true)
|
21
|
+
files = mock_files(3.days.ago, 2.days.ago, 7.days.ago)
|
22
|
+
cleaner.should_receive(:files_to_delete).and_return(files)
|
23
|
+
FileUtils.should_receive(:rm_rf).with(files.map(&:path), :noop => true)
|
24
|
+
cleaner.start
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has has files to delete' do
|
28
|
+
cleaner = Cleaner.new('/path')
|
29
|
+
cleaner.should_receive(:files_before_threshold).and_return(mock_files('12:00', '13:00'))
|
30
|
+
cleaner.should_receive(:files_to_preserve).and_return(mock_files('13:00'))
|
31
|
+
cleaner.files_to_delete.should == mock_files('12:00')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'prints name of file to be deleted if specifying verbose => true' do
|
35
|
+
cleaner = Cleaner.new('/path', :verbose => true)
|
36
|
+
files = mock_files('00:50', '13:30')
|
37
|
+
cleaner.should_receive(:files_to_delete).and_return(files)
|
38
|
+
FileUtils.should_receive(:rm_rf).with(files.map(&:path), :noop => false)
|
39
|
+
cleaner.should_receive(:puts).with("01 May 00:50\n01 May 13:30")
|
40
|
+
cleaner.start
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'preserves the first file of the hour' do
|
44
|
+
should_preserve(:hourly,
|
45
|
+
'12:05' => true,
|
46
|
+
'12:10' => false,
|
47
|
+
'13:15' => true,
|
48
|
+
'13:16' => false)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'preserves the first file of the day' do
|
52
|
+
should_preserve(:daily,
|
53
|
+
'01 10:00' => true,
|
54
|
+
'01 12:00' => false,
|
55
|
+
'02 00:00' => true,
|
56
|
+
'02 00:01' => false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'preserves the first file of the week' do
|
60
|
+
should_preserve(:weekly,
|
61
|
+
'Sun Jan 02 03:40 00' => true,
|
62
|
+
'Tue Jan 04 03:40 00' => true,
|
63
|
+
'Wed Jan 05 03:40 00' => false)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'preserves the first file of the month' do
|
67
|
+
should_preserve(:monthly,
|
68
|
+
'Sun Jan 02 12:00 00' => true,
|
69
|
+
'Tue Feb 01 12:00 00' => true,
|
70
|
+
'Wed Feb 02 12:00 00' => false)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'preserves the first file of the year' do
|
74
|
+
should_preserve(:yearly,
|
75
|
+
'Sun Jan 02 03:40 01' => true,
|
76
|
+
'Tue Jan 04 03:40 02' => true,
|
77
|
+
'Wed Jan 05 03:40 02' => false)
|
78
|
+
end
|
79
|
+
|
80
|
+
MockFile = Struct.new(:ctime) do
|
81
|
+
def ==(other)
|
82
|
+
(ctime.to_i - other.ctime.to_i) < 2
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect
|
86
|
+
ctime.to_s(:short)
|
87
|
+
end
|
88
|
+
|
89
|
+
def stat
|
90
|
+
stat = Object.new
|
91
|
+
def stat.file?
|
92
|
+
true
|
93
|
+
end
|
94
|
+
stat
|
95
|
+
end
|
96
|
+
|
97
|
+
alias path inspect
|
98
|
+
end
|
99
|
+
|
100
|
+
def mock_files(*ctimes)
|
101
|
+
ctimes.map do |ctime|
|
102
|
+
if ctime.is_a?(String)
|
103
|
+
ctime = "1 #{ctime}" unless ctime =~ /\d\d /
|
104
|
+
ctime = "May #{ctime} 2009" unless ctime =~ /\d{2}/
|
105
|
+
ctime = Time.parse(ctime)
|
106
|
+
end
|
107
|
+
MockFile.new(ctime)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def should_preserve(interval, files)
|
112
|
+
cleaner = Cleaner.new('/path', interval => true)
|
113
|
+
cleaner.should_receive(:files_before_threshold).and_return(mock_files(*files.keys.sort))
|
114
|
+
expected_files = files.select{|_, select| select == true}.map{|file, _| file}
|
115
|
+
cleaner.files_to_preserve.to_set.should == mock_files(*expected_files).to_set
|
116
|
+
end
|
117
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toreriklinnerud-clean-files
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tor Erik Linnerud
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-05 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: torerik.linnerud@alphasights.com
|
27
|
+
executables:
|
28
|
+
- clean_files
|
29
|
+
- clean_files
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
files:
|
36
|
+
- bin/clean_files
|
37
|
+
- lib/clean_files.rb
|
38
|
+
- lib/cleaner.rb
|
39
|
+
- spec/cleaner_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/alphasights/clean-files
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Executable to delete files which fit certain criteria
|
69
|
+
test_files:
|
70
|
+
- spec/cleaner_spec.rb
|
71
|
+
- spec/spec_helper.rb
|