-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.rb
executable file
·238 lines (193 loc) · 7.97 KB
/
stats.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env ruby
# encoding: utf-8
#
# usage example: DEBUG=true bundle exec ruby stats.rb --takeout-path "/path/to/extracted/takeout/archive/Takeout/" --checksums-archive-file data/files_by_checksum_and_filesize-`date "+%Y%m%d"`
require_relative File.join('lib', 'cli_toolkit', 'cli_toolkit.rb')
extend CliToolkit::CliOptions
require 'easy_logging'
log_path = (cli_option('--log-file') || nil)
if log_path
FileUtils.mkdir_p(File.dirname(log_path)) unless File.exist?(File.dirname(log_path))
EasyLogging.log_destination = log_path
end
EasyLogging.level = ENV['DEBUG'] ? Logger::DEBUG : Logger::Warn
include EasyLogging
logger.info "Log level: #{EasyLogging.level}"
require 'pathname'
require 'pry'
require 'yaml'
require_relative 'lib/data_storage/data_storage.rb'
include DataStorage
class FileDetails
RE_COUNTER = /\((\d+)\)$/
require 'filesize'
require 'mimemagic'
require 'magic'
require 'digest'
attr_reader :size, :human_size, :mime_type, :path, :extensions, :mime_by_path
def initialize(filepath)
@path = filepath
@size = File.size(filepath)
@human_size = Filesize.from(@size.to_s).pretty
@basename = File.basename(filepath)
@mime_by_path = MimeMagic.by_path(path)
@extensions = basename.split('.')[1..-1]
end
def extensions
return @extensions unless @extensions.empty?
@extensions = basename.split('.')[1..-1] #FIXME: Because I don't feel like re-generating my local dump
end
def basename(include_counter: true)
return include_counter ? @basename : @basename.gsub(RE_COUNTER, '')
end
def expanded_path(include_counter: true)
@expanded_path ||= File.expand_path(path)
return include_counter ? @expanded_path : @expanded_path.gsub(RE_COUNTER, '')
end
def full_path(include_counter: true)
expanded_path(include_counter: include_counter)
end
def complete_extension(include_counter: true)
extension(amount: 0, include_counter: include_counter)
end
def counter
extension(include_counter: true).match(RE_COUNTER)&.values_at(1)
end
def extension(amount: 1, include_counter: false)
if amount == 0
ext = extensions.join('.')
else
ext = extensions.reverse[0,amount].join('.')
end
return include_counter ? ext : ext.gsub(RE_COUNTER, '')
end
def human_size
@human_size ||= Filesize.from(self.size.to_s).pretty
end
def magic
return @magic ||= File.magic(path)
end
def mime_by_magic
return @mime_by_magic ||= File.mime(path)
end
def type_by_magic
return @type_by_magic ||= File.type(path)
end
def mime
[mime_by_path.to_s, type_by_magic].uniq.join(' detected as ')
end
def md5
@md5 ||= Digest::MD5.hexdigest(File.read(path))
# logger.debug "MD5: #{@md5}, #{path}"
return @md5
end
end
takeout_dir = Pathname.new(cli_option('--takeout-path'))
all_files = Dir.glob(takeout_dir.join('**','*'))
detailed_files = all_files.reject{|f|File.directory?(f)}.map{|f| FileDetails.new(f)}
detailed_files_by_extension = detailed_files.group_by(&:extension)
detailed_files_by_filename = detailed_files.group_by{|fd| fd.basename(include_counter: false).split('.').first}
# puts "Found #{detailed_files.size} files, and #{detailed_files_by_extension.keys.size} extensions (#{detailed_files_by_extension.keys.join(', ')}):"
# detailed_files_by_extension.each do |ext, fds|
# puts "== #{ext}"
# fds[0..10].each do |fd|
# puts "#{fd.human_size}: #{fd.full_path} (#{fd.mime})"
# end
# end
files_by_checksum_and_filesize_data_file = (cli_option('--checksums-archive-file')||'data/files_by_checksum_and_filesize')
detailed_files_by_checksum_and_filesize = read_data_file(filepath: files_by_checksum_and_filesize_data_file, format: 'yaml')
if detailed_files_by_checksum_and_filesize.nil? || detailed_files_by_checksum_and_filesize.empty?
file_counter = 1
total_file_count = detailed_files.size
detailed_files_by_checksum_and_filesize = detailed_files.group_by do |fd|
puts "Calculating md5 for file #{fd.path} [#{file_counter}/#{total_file_count}]"
file_counter += 1
[fd.size,fd.md5].join('+')
end
save_data_file(filepath: files_by_checksum_and_filesize_data_file, data: detailed_files_by_checksum_and_filesize, format: :yaml)
end
class UniqueFileEntry
attr_reader :md5, :file_details_entries
def initialize(md5:, file_details_entries: [])
@md5 = md5
file_details_entries = [file_details_entries] if file_details_entries.kind_of?(FileDetails)
raise ArgumentError.new("file_details_entries: needs to be an (array of) FileDetails item(s)") if file_details_entries.any?{|fd|!fd.kind_of?(FileDetails)}
@file_details_entries = file_details_entries
end
def filesize
file_details_entries.first.size
end
def human_filesize
Filesize.from(filesize.to_s).pretty
end
def files
file_details_entries.map{|fd| fd.expanded_path}
end
def file_count
file_details_entries.size
end
def total_size
file_count * filesize
end
def human_total_size
Filesize.from(total_size.to_s).pretty
end
def total_size_from_file_details
file_details_entries.inject(0){|total, fd| total + fd.size}
end
def total_wasted_space
total_size - filesize
end
def human_total_wasted_space
Filesize.from(total_wasted_space.to_s).pretty
end
def to_s
formatted(human: true, files_format: :oneline)
end
def formatted(human: true, files_format: :list)
if files_format == :list
list = files.map{|f|" - #{f}"}.join("\n")
elsif files_format == :oneline
list = files.join(', ')
elsif files_format == :hidden
list = nil
else
files
end
"#{md5}+#{filesize} (#{file_details_entries.first.basename}): #{file_count} files @ #{human ? human_filesize : filesize} = #{human ? human_total_size : total_size} (#{human ? human_total_wasted_space : total_wasted_space} wasted):#{list&.index("\n") ? "\n#{list}" : list}"
end
end
def report(unique_file_entries:, files_format: :formatted, count: 5, subheader: '', sort_orders: {file_count: 'most amount of duplicates', total_size: 'largest total size', total_wasted_space: 'largest total wasted space'})
str = "The top #{count} unique file details entries#{subheader}, ordered by:\n\n"
sort_orders.each do |sort_order, description|
str += " - #{description}:\n"
str += unique_file_entries.sort_by{|ufe| ufe.send(sort_order)}.reverse[0,count].map{|ufe| ' - ' + ufe.formatted(files_format: files_format)}.join("\n")
str += "\n\n"
end
total_used_space = unique_file_entries.inject(0){|total, ufe| total + ufe.total_size}
total_wasted_space = unique_file_entries.inject(0){|total, ufe| total + ufe.total_wasted_space}
total_actually_required_space = unique_file_entries.inject(0){|total, ufe| total + ufe.filesize}
str += "Total used space#{subheader}: #{Filesize.from(total_used_space.to_s).pretty}\n"
str += "Total actually required space#{subheader}: #{Filesize.from(total_actually_required_space.to_s).pretty}\n"
str += "Total wasted space#{subheader}: #{Filesize.from(total_wasted_space.to_s).pretty}\n"
return str
end
unique_file_entries = detailed_files_by_checksum_and_filesize.map do |checksum, fds|
UniqueFileEntry.new(md5: fds.first.md5, file_details_entries: fds)
end
unique_file_entries_by_extension = unique_file_entries.group_by{|ufe|ufe.file_details_entries.first.extension}
puts "Found #{unique_file_entries.size} unique files with identical checksum+filesize combinations:"
puts report(unique_file_entries: unique_file_entries, files_format: :hidden)
unique_file_entries_by_extension.each do |extension, ufes|
puts
puts "## #{extension}"
puts report(unique_file_entries: ufes, files_format: :hidden, subheader: " for files with file extension #{extension}",sort_orders: {total_wasted_space: 'largest total wasted space'})
end
# puts "Found #{detailed_files_by_filename.keys.size} files with unique names:"
# detailed_files_by_filename.each do |filename, fds|
# puts "== #{filename}"
# fds[0..10].each do |fd|
# puts "#{fd.human_size}: #{fd.full_path} (#{fd.mime})"
# end
# end
binding.pry