wolfe 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f8eca5f25a417d9be289940dbd9293b12d37a05a
4
- data.tar.gz: 680d98ac9ff0c28edcb0cea1b3388a0707ee8b07
2
+ SHA256:
3
+ metadata.gz: 9ce9fa333a51ce60859ae813dbfb4308c90eadbd41ee179a3e3eadf743ecf109
4
+ data.tar.gz: db8bcfc7a54747e164a9e045c8eec8b0867b1b00039f0232568dd174e8bfd353
5
5
  SHA512:
6
- metadata.gz: 40901c950e77768b131b859fc80bc66e71ca9d5c73ed5b0932efdfd352de01a9a4c7e5e22c9344d64b28a59ee925a19263f1c764849537eb52d1ae84ed73d5d1
7
- data.tar.gz: 04073e08e15428e1ce63711fa42bb6ca59ab2429b02d74ad8212e6d6bceb90601e3afdbfd086055af842d652a439d8eb286cc7c99a28802c076f35af7a9fe4bb
6
+ metadata.gz: 7d7377bdcd4332ce20d928137a308fe390e4f4714856c2c9f4f5ec32d39d177339a29e9c4ea6bd0bbd6d5b376c269720ec373db5073e658627442bdac688f5a3
7
+ data.tar.gz: 78888639767e3bd22287db106dde4d08d8ba5ad45ca5eddc0e3b86e8c811eedc6d4ad0d43165e176ae3edcc5dfc7e1ec8698c0ff11a60ed3cb3a6c703ff51f30
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.2.0 (2018-09-05)
2
+
3
+ Changes:
4
+
5
+ - modify gem not to clean backups if the last backup is empty
6
+ - modify gem to have the option of not keeping one backup
7
+ - improve readability
8
+
1
9
  ## 1.1.1 (2015-10-16)
2
10
 
3
11
  Changes:
data/lib/wolfe/cleanup.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require "active_support"
2
2
  require "active_support/core_ext"
3
3
  require "fileutils"
4
+ require "wolfe/timespan_from_configuration"
4
5
 
5
6
  module Wolfe
6
7
  class Cleanup
7
8
  attr_accessor :configuration
8
9
 
10
+ BACKUP_FIRST_CUTOFF_DATE = Date.today - 5.years
11
+
9
12
  def initialize(configuration)
10
13
  @configuration = configuration
11
14
  validate_configuration
@@ -49,32 +52,42 @@ module Wolfe
49
52
  # cleanup
50
53
  #
51
54
 
52
- def cleanup( config )
53
- daily_date = Date.today - eval( config['one_per_day_timespan'] )
54
- monthly_date = Date.today - eval( config['one_per_month_timespan'] )
55
- first_relevant_date = Date.today - 5.years
55
+ def cleanup(config)
56
+ daily_date = Date.today - TimespanFromConfiguration.new(config['one_per_day_timespan']).timespan
57
+ monthly_date = calculate_monthly_date(config['one_per_month_timespan'])
58
+ keep_one = TimespanFromConfiguration.new(config['one_per_month_timespan']).keep_one_backup?
56
59
 
57
60
  if File.directory?(config['path'])
58
- clean_monthly( monthly_date, daily_date, config )
59
- clean_yearly( first_relevant_date, monthly_date, config )
61
+ clean_monthly(monthly_date, daily_date, config, keep_one)
62
+ clean_yearly(monthly_date, config, keep_one) if keep_one
60
63
  else
61
64
  puts "Path '#{config['path']}' is not a directory."
62
65
  end
63
66
  end
64
67
 
65
- def clean_monthly( monthly_date, daily_date, config )
66
- monthly_date.upto( daily_date ) do |date|
67
- delete_but_keep_one_per_month( config['path'], config['filename'], date )
68
+ def calculate_monthly_date(one_per_month_timespan)
69
+ timespan = TimespanFromConfiguration.new(one_per_month_timespan).timespan
70
+
71
+ if Date.today - timespan == Date.today
72
+ BACKUP_FIRST_CUTOFF_DATE
73
+ else
74
+ Date.today - timespan
68
75
  end
69
76
  end
70
77
 
71
- def clean_yearly( first_relevant_date, monthly_date, config )
72
- first_relevant_date.upto( monthly_date ) do |date|
73
- delete_but_keep_one_per_year( config['path'], config['filename'], date )
78
+ def clean_monthly(monthly_date, daily_date, config, keep_one)
79
+ monthly_date.upto(daily_date) do |date|
80
+ delete_monthly(config['path'], config['filename'], date, keep_one)
74
81
  end
75
82
  end
76
83
 
77
- def delete_but_keep_one_per_month( path, filename, date )
84
+ def clean_yearly(monthly_date, config, keep_one)
85
+ BACKUP_FIRST_CUTOFF_DATE.upto(monthly_date) do |date|
86
+ delete_yearly(config['path'], config['filename'], date, keep_one)
87
+ end
88
+ end
89
+
90
+ def delete_monthly(path, filename, date, keep_one)
78
91
  filename_month = filename % { year: date.strftime('%Y'),
79
92
  month: date.strftime('%m'),
80
93
  day: '*',
@@ -83,10 +96,11 @@ module Wolfe
83
96
  month: date.strftime('%m'),
84
97
  day: date.strftime('%d'),
85
98
  hour: '*' }
86
- delete_but_keep_one( full_path( path, filename_month ), full_path( path, filename_day ) )
99
+
100
+ select_file_for_deletion(keep_one, path, filename_day, date, filename_month, nil)
87
101
  end
88
102
 
89
- def delete_but_keep_one_per_year( path, filename, date )
103
+ def delete_yearly(path, filename, date, keep_one)
90
104
  filename_year = filename % { year: date.strftime('%Y'),
91
105
  month: '*',
92
106
  day: '*',
@@ -95,20 +109,69 @@ module Wolfe
95
109
  month: date.strftime('%m'),
96
110
  day: date.strftime('%d'),
97
111
  hour: '*' }
98
- delete_but_keep_one( full_path( path, filename_year ), full_path( path, filename_day ) )
112
+
113
+ select_file_for_deletion(keep_one, path, filename_day, date, nil, filename_year)
114
+ end
115
+
116
+ def select_file_for_deletion(keep_one, path, filename_day, date, filename_month, filename_year)
117
+ if keep_one
118
+ select_file(full_path(path, filename_month), delete_path: full_path(path, filename_day)) if filename_month
119
+ select_file(full_path(path, filename_year), delete_path: full_path(path, filename_day)) if filename_year
120
+ else
121
+ select_file(delete_path: full_path( path, filename_day))
122
+ end
99
123
  end
100
124
 
101
- def full_path( path, filename )
125
+ def full_path(path, filename)
102
126
  File.expand_path(File.join(path, filename))
103
127
  end
104
128
 
105
- def delete_but_keep_one( keep_path, delete_path )
129
+ def select_file(keep_path=nil, delete_path:)
106
130
  Dir.glob(delete_path).each do |f|
107
- if Dir.glob(keep_path).count > 1
108
- puts "Delete: #{f}"
109
- FileUtils.rm(f)
131
+ if File.size(Dir.glob(month_path(f)).sort.last) > 0
132
+ if keep_path
133
+ delete_but_keep_one(f, keep_path)
134
+ else
135
+ delete_without_keeping_one(f)
136
+ end
110
137
  end
111
138
  end
112
139
  end
140
+
141
+ def delete_but_keep_one(file, keep_path)
142
+ if Dir.glob(keep_path).count > 1
143
+ delete(file)
144
+ end
145
+ end
146
+
147
+ def delete_without_keeping_one(file)
148
+ if File.size(Dir.glob(next_file_path(file)).last) > 0
149
+ delete(file)
150
+ end
151
+ end
152
+
153
+ def delete(file)
154
+ puts "Delete: #{file}"
155
+ FileUtils.rm(file)
156
+ end
157
+
158
+ def month_path(file)
159
+ file_splitted = file.split('-')
160
+ file_splitted[-2] = "*"
161
+ file_splitted[-1] = "*"
162
+ file_splitted.join('-')
163
+ end
164
+
165
+ def date_from_file(file)
166
+ file_splitted = file.split('-')
167
+ file_splitted[-4..-2].join('.').to_date
168
+ end
169
+
170
+ def next_file_path(file_path)
171
+ next_file = file_path.dup
172
+ date = date_from_file(file_path)
173
+ next_file[-13..-4] = date.next.to_s
174
+ next_file
175
+ end
113
176
  end
114
177
  end
@@ -0,0 +1,33 @@
1
+ module Wolfe
2
+ class TimespanFromConfiguration
3
+ SUPPORTED_TIME_UNITS = %w(hours days months years).freeze
4
+
5
+ def initialize(config)
6
+ config = split_duration_and_unit(config)
7
+
8
+ @duration = Integer(config['duration'])
9
+ @unit = config['unit']
10
+ @unit = (@unit.to_s << 's').to_sym unless @unit.to_s.end_with? 's'
11
+
12
+ raise ArgumentError.new("Invalid time unit #{config['unit'].inspect}, expected one of #{SUPPORTED_TIME_UNITS.join(', ')}") unless supported_time_unit?
13
+ end
14
+
15
+ def timespan
16
+ @duration.send(@unit)
17
+ end
18
+
19
+ def keep_one_backup?
20
+ @duration.to_i != 0
21
+ end
22
+
23
+ private
24
+
25
+ def supported_time_unit?
26
+ SUPPORTED_TIME_UNITS.include?(@unit.to_s)
27
+ end
28
+
29
+ def split_duration_and_unit(config)
30
+ { 'duration' => config.split('.')[0].to_i, 'unit' => config.split('.')[1].to_sym }
31
+ end
32
+ end
33
+ end
data/lib/wolfe/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Wolfe
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolfe
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Emhofer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-10-16 00:00:00.000000000 Z
12
+ date: 2018-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -120,6 +120,7 @@ files:
120
120
  - lib/wolfe.rb
121
121
  - lib/wolfe/cleanup.rb
122
122
  - lib/wolfe/cli.rb
123
+ - lib/wolfe/timespan_from_configuration.rb
123
124
  - lib/wolfe/version.rb
124
125
  - wolfe.gemspec
125
126
  homepage: http://github.com/lomography/wolfe
@@ -142,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  version: '0'
143
144
  requirements: []
144
145
  rubyforge_project:
145
- rubygems_version: 2.4.5
146
+ rubygems_version: 2.7.6
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: Cleanup (backup) files by date.