rdupes 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/rdupes +34 -0
- data/lib/rdupes/version.rb +3 -0
- data/lib/rdupes.rb +108 -0
- data/rdupes.gemspec +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c0f75ec8d82ddbdc1319fdfdcfcfad7696d7fccd
|
4
|
+
data.tar.gz: b74a50547365ff3370009416d6927262b29341e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad19c096fc4d671d14c3e8a5752b02e960f203abd702e0a851bb2c0abc71591b3abb85aac082ec4fc7fa65bd66f8e4d552337642205d2192fc560b7511eb31a5
|
7
|
+
data.tar.gz: 4a8a9eb52b3e0c006a532452c78dc937da9c64b728a953fbd872d798355d6dfb165af55e8b3926d236087d7fe83a515b427daa837704b7c40277e5cd12ac4054
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Harold Waterkeyn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Rdupes
|
2
|
+
|
3
|
+
Ruby wrapper for fdupes.
|
4
|
+
This gem can be used to find and delete duplicates.
|
5
|
+
|
6
|
+
# Dependencies
|
7
|
+
This gem requires `fdupes` to be installed.
|
8
|
+
On a Mac, this can be done with `brew install fdupes`.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'rdupes'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install rdupes
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
TODO: Write usage instructions here
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hwaterke/rdupes.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rdupes"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/rdupes
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rdupes"
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
rdupe = Rdupes::Finder.new
|
7
|
+
|
8
|
+
opt_parser = OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: rdupes [options] path"
|
10
|
+
|
11
|
+
opts.on('-r', '--reference DIRECTORY', "Add DIRECTORY to the reference directories.", "No files will ever be touched in this directory.") do |directory|
|
12
|
+
rdupe.add_reference_directory directory
|
13
|
+
end
|
14
|
+
|
15
|
+
opts.on("-q", "--quiet", "Runs quietly") do |q|
|
16
|
+
rdupe.quiet!
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on("-k", "--keep", "Keep the report generated by fdupes") do |q|
|
20
|
+
rdupe.keep!
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on("-l", "--logging", "Enables verbose logging") do |l|
|
24
|
+
rdupe.logger.level = Logger::DEBUG
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-h", "--help", "Prints this help") do
|
28
|
+
puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
opt_parser.parse!(ARGV)
|
34
|
+
rdupe.process(ARGV)
|
data/lib/rdupes.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require "rdupes/version"
|
2
|
+
require 'logger'
|
3
|
+
require 'shellwords'
|
4
|
+
require 'tmpdir'
|
5
|
+
|
6
|
+
module Rdupes
|
7
|
+
class Finder
|
8
|
+
attr_reader :logger
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@logger = Logger.new(STDOUT)
|
12
|
+
@logger.level = Logger::WARN
|
13
|
+
@reference_directories = []
|
14
|
+
@search_directories = []
|
15
|
+
@counters = Hash.new(0)
|
16
|
+
@quiet = false
|
17
|
+
@keep = false
|
18
|
+
end
|
19
|
+
|
20
|
+
def quiet!
|
21
|
+
@quiet = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def keep!
|
25
|
+
@keep = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_reference_directory(directory)
|
29
|
+
directory_path = File.expand_path(directory)
|
30
|
+
@logger.debug "Adding #{directory_path} to the reference directories"
|
31
|
+
raise "#{directory_path} does not exist" unless Dir.exist?(directory_path)
|
32
|
+
@reference_directories << directory_path
|
33
|
+
@reference_directories.uniq!
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_search_directory(directory)
|
37
|
+
directory_path = File.expand_path(directory)
|
38
|
+
@logger.debug "Adding #{directory_path} to the search directories"
|
39
|
+
raise "#{directory_path} does not exist" unless Dir.exist?(directory_path)
|
40
|
+
@search_directories << directory_path
|
41
|
+
@search_directories.uniq!
|
42
|
+
end
|
43
|
+
|
44
|
+
def process(directories)
|
45
|
+
@search_directories.clear
|
46
|
+
directories.each { |d| add_search_directory(d) }
|
47
|
+
raise 'No directories provided for duplicate search' if @search_directories.empty?
|
48
|
+
raise 'fdupes needs to be installed' unless command? 'fdupes'
|
49
|
+
|
50
|
+
unless @quiet
|
51
|
+
puts "Processing #{@search_directories}"
|
52
|
+
puts "Reference directory: #{@reference_directories}"
|
53
|
+
end
|
54
|
+
|
55
|
+
Dir.mktmpdir do |dir|
|
56
|
+
fdupe_output = File.join(dir, 'duplicates.log')
|
57
|
+
# Redirect to file
|
58
|
+
cmd = "fdupes -rq #{directories_for_search.shelljoin} > #{fdupe_output}"
|
59
|
+
@logger.debug "Executing: #{cmd}"
|
60
|
+
r = system cmd
|
61
|
+
raise "fdupe crashed " unless r
|
62
|
+
process_fdupes_result(fdupe_output)
|
63
|
+
FileUtils.cp fdupe_output, "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_duplicates.log" if @keep
|
64
|
+
puts "Deleted #{@counters[:deleted]} files" unless @quiet
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def command?(command)
|
71
|
+
system("which #{ command} > /dev/null 2>&1")
|
72
|
+
end
|
73
|
+
|
74
|
+
def directories_for_search
|
75
|
+
(@reference_directories + @search_directories).uniq
|
76
|
+
end
|
77
|
+
|
78
|
+
def process_fdupes_result(fdupes_output_file)
|
79
|
+
@logger.debug "Handling fdupes result #{fdupes_output_file}"
|
80
|
+
current_group = []
|
81
|
+
File.open(fdupes_output_file).each do |line|
|
82
|
+
line.chomp!
|
83
|
+
if line.empty?
|
84
|
+
handle_duplicate_group(current_group)
|
85
|
+
current_group = []
|
86
|
+
else
|
87
|
+
current_group << line
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def handle_duplicate_group(duplicate_group)
|
93
|
+
@logger.debug "Handling group of #{duplicate_group.size} duplicates"
|
94
|
+
reference_files, duplicate_files = duplicate_group.partition do |f|
|
95
|
+
@reference_directories.any? { |rf| File.expand_path(f).start_with?(rf) }
|
96
|
+
end
|
97
|
+
# Keep the first duplicate if there is no reference file.
|
98
|
+
duplicate_files.shift if reference_files.empty?
|
99
|
+
duplicate_files.each { |dp| handle_duplicate_file(dp) }
|
100
|
+
end
|
101
|
+
|
102
|
+
def handle_duplicate_file(duplicate_file)
|
103
|
+
@counters[:deleted] += 1
|
104
|
+
@logger.debug "Handling duplicate #{duplicate_file}"
|
105
|
+
File.delete(duplicate_file)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/rdupes.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rdupes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rdupes"
|
8
|
+
spec.version = Rdupes::VERSION
|
9
|
+
spec.authors = ["Harold Waterkeyn"]
|
10
|
+
spec.email = ["hwaterke@users.noreply.github.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Wrapper around fdupes}
|
13
|
+
spec.description = %q{Find and delete duplicate files}
|
14
|
+
spec.homepage = "https://github.com/hwaterke/rdupes"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rdupes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harold Waterkeyn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Find and delete duplicate files
|
56
|
+
email:
|
57
|
+
- hwaterke@users.noreply.github.com
|
58
|
+
executables:
|
59
|
+
- rdupes
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- exe/rdupes
|
73
|
+
- lib/rdupes.rb
|
74
|
+
- lib/rdupes/version.rb
|
75
|
+
- rdupes.gemspec
|
76
|
+
homepage: https://github.com/hwaterke/rdupes
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.5.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Wrapper around fdupes
|
100
|
+
test_files: []
|