similar_count 0.1.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.
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in similar_count.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ similar_count (0.1.0)
5
+ thor (~> 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.1)
12
+ parallel (1.24.0)
13
+ parser (3.3.2.0)
14
+ ast (~> 2.4.1)
15
+ racc
16
+ racc (1.8.0)
17
+ rainbow (3.1.1)
18
+ rake (13.2.1)
19
+ regexp_parser (2.9.2)
20
+ rexml (3.2.8)
21
+ strscan (>= 3.0.9)
22
+ rspec (3.13.0)
23
+ rspec-core (~> 3.13.0)
24
+ rspec-expectations (~> 3.13.0)
25
+ rspec-mocks (~> 3.13.0)
26
+ rspec-core (3.13.0)
27
+ rspec-support (~> 3.13.0)
28
+ rspec-expectations (3.13.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.13.0)
31
+ rspec-mocks (3.13.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.13.0)
34
+ rspec-support (3.13.1)
35
+ rubocop (0.93.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 2.7.1.5)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8)
40
+ rexml
41
+ rubocop-ast (>= 0.6.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 2.0)
44
+ rubocop-ast (1.31.3)
45
+ parser (>= 3.3.1.0)
46
+ rubocop-performance (1.9.2)
47
+ rubocop (>= 0.90.0, < 2.0)
48
+ rubocop-ast (>= 0.4.0)
49
+ ruby-progressbar (1.13.0)
50
+ strscan (3.1.0)
51
+ thor (0.20.3)
52
+ unicode-display_width (1.8.0)
53
+
54
+ PLATFORMS
55
+ arm64-darwin-21
56
+
57
+ DEPENDENCIES
58
+ rake (~> 13.0)
59
+ rspec (~> 3.0)
60
+ rubocop (~> 0.93.1)
61
+ rubocop-performance (~> 1.9.2)
62
+ similar_count!
63
+
64
+ BUNDLED WITH
65
+ 2.2.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 kotarominami
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # SimilarCount
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/similar_count`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'similar_count'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install similar_count
22
+
23
+ ## Usage
24
+ ```bash
25
+ $ bundle init
26
+ $ bundle add similar_count
27
+ $ bundle install
28
+ $ bundle exec irb
29
+ ```
30
+ ```bash
31
+ $ mkdir "file_test"
32
+ $ echo -n "qwerty" > file_test/file_1.txt
33
+ $ echo -n "qwerty" > file_test/file_2.txt
34
+ $ echo -n "qwerty" > file_test/file_3.txt
35
+ $ echo -n "asdf" > file_test/file_4.txt
36
+ $ echo -n "asdf" > file_test/file_5.txt
37
+ ```
38
+
39
+ ```bash
40
+ irb(main):001:0> require('similar_count') # true
41
+ irb(main):002:0> path = "file_test"
42
+ irb(main):003:0> result = SimilarCount::Scanner.scan_directory(path) # {:text=>"qwerty 3", :content=>"qwerty", :max_count=>3, :max_hash=>"65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58"
43
+ irb(main):004:0> puts result[:text] # qwerty 3
44
+ ```
45
+
46
+
47
+
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/namakukingkong/similar_count.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'similar_count'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ require 'thor'
2
+ module SimilarCount
3
+ class CLI < Thor
4
+ desc 'scan DIRECTORY', 'Scan the DIRECTORY for files with the same content'
5
+ def scan(directory)
6
+ result = SimilarCount::Scanner.scan_directory(directory)
7
+ if result[:content]
8
+ puts "Content: #{result[:content]}, Count: #{result[:count]}"
9
+ else
10
+ puts 'No files found.'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,50 @@
1
+ require 'digest'
2
+ require 'find'
3
+
4
+ module SimilarCount
5
+ class Scanner
6
+ def self.file_hash(file_path)
7
+ sha256 = Digest::SHA256.new
8
+ File.open(file_path, 'rb') do |file|
9
+ buffer = ''
10
+ sha256.update(buffer) while file.read(4096, buffer)
11
+ end
12
+ sha256.hexdigest
13
+ end
14
+
15
+ def self.read_content(file_path)
16
+ content = File.read(file_path)
17
+ content
18
+ rescue => e
19
+ puts "Error reading #{file_path}: #{e.message}"
20
+ nil
21
+ end
22
+
23
+ def self.scan_directory(path)
24
+ hash_to_count = Hash.new(0)
25
+ hash_to_files = Hash.new { |hash, key| hash[key] = [] }
26
+
27
+ Find.find(path) do |file|
28
+ next if File.directory?(file) # Skip directories
29
+
30
+ begin
31
+ content_hash = file_hash(file)
32
+ hash_to_count[content_hash] += 1
33
+ hash_to_files[content_hash] << file
34
+ rescue => e
35
+ puts "Error processing #{file}: #{e.message}"
36
+ end
37
+ end
38
+
39
+ max_count = hash_to_count.values.max.to_i
40
+ max_hash = hash_to_count.key(max_count) || ''
41
+ files_with_max_content = hash_to_files[max_hash]
42
+ content = max_count > 0 ? read_content(files_with_max_content.first) : ''
43
+ { text: max_count > 0 ? "#{content} #{max_count}" : '',
44
+ content: content,
45
+ max_count: max_count,
46
+ max_hash: max_hash,
47
+ files_with_max_content: files_with_max_content }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimilarCount
4
+ VERSION = '0.1.0'
5
+ public_constant :VERSION
6
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'similar_count/version'
4
+ require_relative 'similar_count/scanner'
5
+ require_relative 'similar_count/cli'
6
+ module SimilarCount
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/similar_count/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'similar_count'
7
+ spec.version = SimilarCount::VERSION
8
+ spec.authors = ['kotarominami']
9
+ spec.email = ['namakukingkong@gmail.com']
10
+
11
+ spec.summary = 'A gem to count files with the same content in a directory'
12
+ spec.description = 'This gem scans a directory to find files with the same content and returns the most frequent content and its count.'
13
+ spec.homepage = 'http://example.com'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+ # spec.add_dependency "example-gem", "~> 1.0"
28
+
29
+ # For more information and examples about making a new gem, checkout our
30
+ # guide at: https://bundler.io/guides/creating_gem.html
31
+
32
+ spec.add_dependency 'thor', '~> 0.20.0'
33
+
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_development_dependency 'rubocop', '~> 0.93.1'
36
+ spec.add_development_dependency 'rubocop-performance', '~> 1.9.2'
37
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: similar_count
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kotarominami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.93.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.93.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-performance
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.9.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.2
69
+ description: This gem scans a directory to find files with the same content and returns
70
+ the most frequent content and its count.
71
+ email:
72
+ - namakukingkong@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/main.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - ".ruby-version"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/similar_count.rb
90
+ - lib/similar_count/cli.rb
91
+ - lib/similar_count/scanner.rb
92
+ - lib/similar_count/version.rb
93
+ - similar_count.gemspec
94
+ homepage: http://example.com
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.3.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.3.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: A gem to count files with the same content in a directory
117
+ test_files: []