-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_deb_remover.rb
executable file
·115 lines (93 loc) · 2.86 KB
/
multi_deb_remover.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
#!/usr/bin/env ruby
require 'fileutils'
require_relative 'libs/pkg_info_collector'
require_relative 'libs/user_response_parser'
def get_selections(collection)
collection_arr = collection.to_a # convert to arry for easy iteration
selections = [] # array to save selections
index = 0
while index < collection_arr.length
pkg = collection_arr[index][0]
debs = collection_arr[index][1]
total_debs = debs.length
puts "#{total_debs} version(s) found for #{pkg} #{debs.architecture}:"
debs.sort.each_with_index do |deb, index|
puts "#{index}: %-30s %10s" % [deb.version, deb.file_size]
end
puts 'Select index to remove, P(Previous), S(Stop), F(Finish)'
res = UserResponseParser.new(gets.chomp, (0...total_debs).to_a)
if res.response_type == :invalid
puts 'Invalid selection. Try again'
redo
elsif res.response_type == :command
case res.get_command
when :previous
index = (index-1) < 0 ? 0 : index-1
next
when :finish
break
when :stop
exit
else
index += 1
end
elsif res.response_type == :selection
if res.get_selection == :invalid
puts 'Invalid selection. Try again!'
redo
end
selections[index] = res.get_selection
index += 1
end # end
end # while
selections
end
def get_selected_files(collection, selections)
collection_arr = collection.to_a # convert to arry for easy iteration
files = []
index = 0
while index < selections.length
debs = collection_arr[index][1]
selection = selections[index]
selection.each do |i|
files << debs[i].path
end
index += 1
end
files
end
def remove_files(files)
files.each { |f| puts f }
puts 'These files are selected for removal. Are you sure? (Y/N)'
answer = gets.chomp.downcase
if answer == 'y'
dest_dir = 'to_delete'
FileUtils.mkdir(dest_dir) unless Dir.exist?(dest_dir)
files.each { |file|
FileUtils.move(file, dest_dir)
puts "#{File.basename file} -> #{File.realpath dest_dir}"
}
end
end
def sorted_by_size(collection, sizes)
# this method sorts the collection by using another
# size hash as guide
new_collection = { }
sizes_arr = sizes.to_a
sizes_arr.sort_by! { |pkg, max_deb_size| max_deb_size }
sizes_arr.reverse! # get reverse sort
sizes_arr.each do |pkg, max_size|
new_collection[pkg] = collection[pkg] unless collection[pkg].nil?
end
new_collection
end
if $0 == __FILE__
exclude_dirs = ['to_delete']
collector = PkgInfoCollector.new('.', exclude_dirs)
collection = collector.get_collection_with_multiples
sizes = collector.get_size_info
sorted_collection = sorted_by_size(collection, sizes)
selections = get_selections(sorted_collection) # while
files = get_selected_files(sorted_collection, selections)
remove_files(files) if files.length > 0
end