dfm 0.0.1

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.
Files changed (7) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +21 -0
  3. data/README.md +4 -0
  4. data/bin/dfm +43 -0
  5. data/lib/dfm.rb +76 -0
  6. data/lib/dfm/version.rb +6 -0
  7. metadata +49 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTg4NWZlYWEwYWMxZTI4ZmE3YmUwZTljYzVjNmI3NTljZjBhYTQ2MA==
5
+ data.tar.gz: !binary |-
6
+ NjhjOTFjNGM4NTBjZTc3YmM5NmU4NTE2ZWM1NjI5YjMxODNlOGJhMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmU2ZTZjMDBkMjAzYmJiMTcxNjhkMmE5YmFiZTViNzRkZTUwNmY0MjEzOGI2
10
+ M2U3NDY5NGI4ZGNiMmMwODgxOTU3MDM5NWVhZDQ2MzE1YzY1ZTJkNjZkZWE2
11
+ ZDRmN2RiY2EyZmJlNmZkYWI1NWI2MmU4YWRmOWUzYTYyZDY2ZDU=
12
+ data.tar.gz: !binary |-
13
+ NTIzZmNiZjczZmQxNmQ0NTEzOGU1NDM0Y2Y0ZDcxMjVkZjJjNTkyNDdkYzBi
14
+ MWE2YjhkM2I0MTNkOTZjYWZjZmJmYTEzYmVkNDg2MmY2YTM0OGE1NjhhMGYw
15
+ ODliNWE1Njg0NzM2ODU5ZWUyMWI1YmFiZmQyZTAyYWI4YzYyMDg=
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Daniel P. Clark
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.
@@ -0,0 +1,4 @@
1
+ dfm
2
+ ===
3
+
4
+ Duplicate File Manager
data/bin/dfm ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'dfm'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'dfm'
8
+ end
9
+ require 'optparse'
10
+
11
+
12
+ options = {}
13
+ printers = Array.new
14
+
15
+ OptionParser.new do |opts|
16
+ opts.banner = "Usage: dfm [options] [path]\nDefaults: dfm -x -d ." + File::SEPARATOR
17
+ opts.on("-f", "--filters FILTERS", Array, "File extension filters") do |filters|
18
+ options[:filters] = filters
19
+ end
20
+ opts.on("-x", "--duplicates-hex", "Prints duplicate files by MD5 hexdigest") do |dh|
21
+ printers << "dh"
22
+ end
23
+ opts.on("-d", "--duplicates-name", "Prints duplicate files by file name") do |dh|
24
+ printers << "dn"
25
+ end
26
+ opts.on("-s", "--singles-hex", "Prints non-duplicate files by MD5 hexdigest") do |dh|
27
+ printers << "sh"
28
+ end
29
+ opts.on("-n", "--singles-name", "Prints non-duplicate files by file name") do |dh|
30
+ printers << "sn"
31
+ end
32
+ end.parse!
33
+ path = ARGV.select {|i| File.directory? i }[0]
34
+ program = DFM.new( options.update( { :path => path } ) )
35
+ if printers.empty?
36
+ program.print_duplicates
37
+ program.print_duplicates( "name" )
38
+ else
39
+ if printers.include? "dh"; program.print_duplicates; end
40
+ if printers.include? "dn"; program.print_duplicates( "name" ); end
41
+ if printers.include? "sh"; program.print_singles; end
42
+ if printers.include? "sn"; program.print_singles( "name" ); end
43
+ end
@@ -0,0 +1,76 @@
1
+ require 'digest'
2
+ require 'json'
3
+ require 'dfm/version'
4
+
5
+ class DFM
6
+ attr_accessor :filters
7
+
8
+ def initialize( params = {} )
9
+ @files_by_hexdigest = {}
10
+ @files_by_name = {}
11
+ @filters = Array( params.fetch( :filters, nil ) )
12
+ @path = ( ( params.fetch( :path, '.' ) ) || '.' ) + File::SEPARATOR
13
+ @hashFunc = Digest::MD5.new
14
+ recurse_path( @path )
15
+ end
16
+
17
+ def print_singles( opt = "hex" )
18
+ print_match( { :type => opt, :duplicates => false } )
19
+ end
20
+
21
+ def print_duplicates( opt = "hex" )
22
+ print_match( { :type => opt } )
23
+ end
24
+
25
+ def hex( duplicates = true )
26
+ select_duplicates( { :hash => @files_by_hexdigest, :duplicates => duplicates } )
27
+ end
28
+
29
+ def name( duplicates = true )
30
+ select_duplicates( { :hash => @files_by_name, :duplicates => duplicates } )
31
+ end
32
+
33
+ def recurse( path )
34
+ @files_by_hexdigest = {}
35
+ @files_by_name = {}
36
+ recurse_path( path )
37
+ end
38
+
39
+ private
40
+
41
+ def insert_file( file )
42
+ # hex
43
+ file_io = File.open( file, "rb" ) { |io| io.read }
44
+ hex_file = @hashFunc.hexdigest( file_io )
45
+ @files_by_hexdigest[ hex_file ] = Array( @files_by_hexdigest[ hex_file ] ) << file
46
+ # name
47
+ @files_by_name[ File.basename( file ) ] = Array( @files_by_name[ File.basename( file ) ] ) << file
48
+ end
49
+
50
+ def recurse_path( path = @path )
51
+ @filters.empty? ? ( filters = "" ) : ( filters = @filters.join( "," ).prepend( ".{" ).<<( "}" ) )
52
+ Dir.glob( path + '**' + File::SEPARATOR + '*' + filters ).each { |file|
53
+ if File.file? file
54
+ insert_file file
55
+ end
56
+ }
57
+ end
58
+
59
+ def select_duplicates( opt = { :hash => @files_by_hexdigest, :duplicates => true } )
60
+ if opt[ :duplicates ]
61
+ opt[ :hash ].select { |k,v| v.length > 2 }
62
+ else
63
+ opt[ :hash ].select { |k,v| v.length == 1 }
64
+ end
65
+ end
66
+
67
+ def print_match( opt = { :type => "hex", :duplicates => true } )
68
+ if !!opt[ :type ][ "hex" ] or !!opt[ :type ][ "name" ]
69
+ print_json send( opt[ :type ], *Array( opt[ :duplicates ] ) )
70
+ end
71
+ end
72
+
73
+ def print_json( hash )
74
+ puts JSON.pretty_generate( hash )
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+ class DFM
2
+ VERSION = "0.0.1"
3
+ end
4
+
5
+ # Version 0.1.0
6
+ # * Complete Release
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dfm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel P. Clark / 6ftDan(TM)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parse duplicate files to process!
14
+ email: webmaster@6ftdan.com
15
+ executables:
16
+ - dfm
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - bin/dfm
23
+ - lib/dfm.rb
24
+ - lib/dfm/version.rb
25
+ homepage: https://github.com/danielpclark/dfm
26
+ licenses:
27
+ - The MIT License (MIT)
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.2.2
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Duplicate File Manager!
49
+ test_files: []