Rgrep 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4
+ gem 'thor'
File without changes
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib_dir = File.expand_path('lib',File.dirname(__FILE__))
3
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
4
+ require "Rgrep/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "Rgrep"
8
+ s.version = Rgrep::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.license = "MIT"
11
+ s.authors = ["Ernest Khasanzhinov"]
12
+ s.email = ["khasanzhinov@gmail.com"]
13
+ s.homepage = ""
14
+ s.summary = %q{A sample gem}
15
+ s.description = %q{A sample gem. Gem like grep in linux, but ruby.}
16
+
17
+ s.add_runtime_dependency "launchy"
18
+ s.add_runtime_dependency 'thor'
19
+ s.add_runtime_dependency 'os'
20
+ s.add_development_dependency "rspec", "~>2.5.0"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,161 @@
1
+ # encoding: UTF-8
2
+ require "thor"
3
+ require "os"
4
+ require File.join(File.dirname(__FILE__), 'Rgrep', 'Description.rb')
5
+
6
+ class Rgrep < Thor
7
+ include Description
8
+
9
+ map "-l" => :list
10
+ map "-d" => :diff
11
+ map "-f" => :find
12
+
13
+ desc "List all running_process", "list -n 'MySql' -gt 100"
14
+ long_desc <<-LONGDESC
15
+ #{LONGDESC_LIST}
16
+ LONGDESC
17
+ option :name, :desc => 'Name of running procces', :banner => 'Name',:aliases => '-n'
18
+ option :less_than, :desc => 'Memory allocate, greater than param', :banner => 'Amount',:aliases => '-lt'
19
+ option :greater_than, :desc => 'Memory allocate, less than param', :banner => 'Amount',:aliases => '-gt'
20
+ def list
21
+ @result = 0
22
+
23
+ if OS.windows?
24
+ _memory_index = 4
25
+ list = `tasklist`
26
+ elsif OS.linux? || OS.mac?
27
+ _memory_index = 3
28
+ list = %x(ps aux)
29
+ end
30
+ _name_index = 0
31
+ outgoing = []
32
+ is_header = true
33
+
34
+ cnt = 0
35
+ list.split("\n").each do |str|
36
+ outgoing << str if is_header
37
+ is_header = false
38
+
39
+ # is options disable or equal to parameters
40
+ arr = str.split(' ')
41
+ is_opt_name_valid = !options[:name] || arr[_name_index].to_s.include?( options[:name].to_s)
42
+ is_opt_lt_valid = !options[:less_than] || options[:less_than].to_i > arr[_memory_index].to_i
43
+ is_opt_gt_valid = !options[:greater_than] || options[:greater_than].to_i < arr[_memory_index].to_i
44
+
45
+ if is_opt_name_valid && is_opt_lt_valid && is_opt_gt_valid
46
+ cnt = cnt + 1
47
+ outgoing << cnt.to_s + ":\t" + str
48
+ end
49
+ end
50
+
51
+ puts outgoing.join("\n")
52
+ # CLI commands should return 0 if command finished succesful, and -1 if failed
53
+ return 0
54
+ rescue Exception => e
55
+ puts "error = #{e}, backtrace = #{e.backtrace.join("\n")}"
56
+ return -1
57
+ end
58
+
59
+ desc "Difference between two files", "diff file1 file2"
60
+ long_desc <<-LONGDESC
61
+ #{LONGDESC_DIFF}
62
+ LONGDESC
63
+ option :show_similar, :type => :boolean, :desc => 'Show similar lines instead differnece', :aliases => '-ss'
64
+ def diff( _file1 , _file2)
65
+ file1 = {}
66
+ file2 = {}
67
+
68
+ file1[:io_file] = File.open(_file1)
69
+ file2[:io_file] = File.open(_file2)
70
+
71
+ file1[:lines] = file1[:io_file].readlines
72
+ file2[:lines] = file2[:io_file].readlines
73
+
74
+ cnt = 0
75
+ puts "File: #{_file1}"
76
+ file1[:lines].each do |line|
77
+ cnt = cnt + 1
78
+ if options[:show_similar]
79
+ puts cnt.to_s + ' ' + line if file2[:lines].include?( line)
80
+ else
81
+ puts cnt.to_s + ' ' + line unless file2[:lines].include?( line)
82
+ end
83
+ end
84
+
85
+ cnt = 0
86
+ puts "**************************************"
87
+ puts "File: #{_file2}"
88
+ file2[:lines].each do |line|
89
+ cnt = cnt + 1
90
+ if options[:show_similar]
91
+ puts cnt.to_s + ' ' + line if file1[:lines].include?( line)
92
+ else
93
+ puts cnt.to_s + ' ' + line unless file1[:lines].include?( line)
94
+ end
95
+ end
96
+
97
+ # CLI commands should return 0 if command finished succesful, and -1 if failed
98
+ return 0
99
+ rescue Exception => e
100
+ puts "error = #{e}, backtrace = #{e.backtrace.join("\n")}"
101
+ return -1
102
+ end
103
+
104
+ desc "Search by names in dir/contents of files", "find --dir --contents '/home' --name 'Diary'"
105
+ long_desc <<-LONGDESC
106
+ #{LONGDESC_FIND}
107
+ LONGDESC
108
+ option :name, :desc => 'Name of files to find', :banner => 'Name',:aliases => '-n', :required => true
109
+ option :contents, :type => :boolean, :desc => 'Search for containig items in files', :aliases => '-cs'
110
+ def find( _path)
111
+ @dir = {}
112
+ @contents = {}
113
+ @dir[:out] = []
114
+ @contents[:out] = []
115
+ @name = options[:name]
116
+ @contents[:enable] = options[:contents]
117
+
118
+ incr_search( _path)
119
+
120
+ puts "Dir:"
121
+ puts @dir[:out].join("\n")
122
+
123
+ puts "*******************************************************" if options[:contents]
124
+ if options[:contents]
125
+ puts "Into files:"
126
+ puts @contents[:out].join("\n")
127
+ end
128
+
129
+ # CLI commands should return 0 if command finished succesful, and -1 if failed
130
+ return 0
131
+ rescue Exception => e
132
+ puts "error = #{e}, backtrace = #{e.backtrace.join("\n")}"
133
+ return -1
134
+ end
135
+
136
+ private
137
+ def incr_search( path)
138
+ if OS.windows?
139
+ path = path[-1,1] == '\\' ? path : path + '\\' # for Win
140
+ elsif OS.linux? || OS.mac?
141
+ path = path[-1,1] == '/' ? path : path + '/'
142
+ end
143
+
144
+ Dir.foreach( path) do |file_name|
145
+ @dir[:out] << path + file_name if file_name.include? @name
146
+ incr_search( path + file_name) if file_name.strip[0,1] != '.' && File.directory?(path + file_name)
147
+
148
+ if file_name.strip[0,1] != '.' && !File.directory?(path + file_name) && @contents[:enable]
149
+ cnt = 0
150
+ file = File.open( path + file_name)
151
+ while ( line = file.gets)
152
+ cnt = cnt + 1
153
+ @contents[:out] << path.to_s + file_name.to_s + "\t" + cnt.to_s + ': '+ line.to_s if line.include? @name
154
+ end
155
+ file.close
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ Rgrep.start(ARGV)
@@ -0,0 +1,13 @@
1
+ module Description
2
+ LONGDESC_HELLO ='MyGrep hello will print out a message to a person of your choosing.
3
+ You can optionally specify a second parameter, which will print
4
+ out a from message as well.
5
+ > $ cli hello "Yehuda Katz" "Carl Lerche"
6
+ > from: Carl Lerche'
7
+
8
+ LONGDESC_LIST = ''
9
+
10
+ LONGDESC_DIFF = ''
11
+
12
+ LONGDESC_FIND = ''
13
+ end
@@ -0,0 +1,3 @@
1
+ module Rgrep
2
+ VERSION = "0.0.1"
3
+ end
Binary file
Binary file
@@ -0,0 +1 @@
1
+ test file
@@ -0,0 +1,2 @@
1
+ test file
2
+ second file
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), "spec_helper.rb")
4
+
5
+ describe Rgrep do
6
+ CLI_VALID_RESPONSE = 0
7
+ CLI_FAIL_RESPONSE = -1
8
+
9
+ PATH = File.join(File.dirname(__FILE__), '/','blueprint', '/')
10
+
11
+ describe "list operation" do
12
+ context 'correct when' do
13
+ it 'run normally' do
14
+ expect(Rgrep.start(["list"]).to_i).to eq( CLI_VALID_RESPONSE)
15
+ end
16
+ it 'run with params - name' do
17
+ expect(Rgrep.start(["list","--name", "chr"]).to_i).to eq( CLI_VALID_RESPONSE)
18
+ end
19
+ it 'run with params - name and greater-than' do
20
+ expect(Rgrep.start(["list", "--name", "chr", "--greater-than", "100"]).to_i).to eq( CLI_VALID_RESPONSE)
21
+ end
22
+ it 'run with params - name and greater-than and less-than' do
23
+ expect(Rgrep.start(["list", "--name", "chr", "--greater-than", "100", "--less-than", "500"]).to_i).to eq( CLI_VALID_RESPONSE)
24
+ end
25
+ end
26
+
27
+ context 'failed when' do
28
+ # TODO: need additioanl time because we should override method from gem Thor
29
+ end
30
+ end
31
+
32
+ describe "diff operation" do
33
+ context 'correct when' do
34
+ it 'run normally' do
35
+ expect(Rgrep.start(["diff", "#{PATH}1.txt", "#{PATH}2.txt"]).to_i).to eq( CLI_VALID_RESPONSE)
36
+ end
37
+ it 'run with argument show_similar' do
38
+ expect(Rgrep.start(["diff", "#{PATH}1.txt", "#{PATH}2.txt", "--show_similar"]).to_i).to eq( CLI_VALID_RESPONSE)
39
+ end
40
+ end
41
+
42
+ context 'failed when' do
43
+ # TODO: need additioanl time because we should override method from gem Thor
44
+ end
45
+ end
46
+
47
+ describe "find operation" do
48
+ context 'correct when' do
49
+ it 'run with arg name' do
50
+ expect(Rgrep.start(["find", PATH.to_s, "--name", "Diary"]).to_i).to eq( CLI_VALID_RESPONSE)
51
+ end
52
+ it 'run with arg name and show contents' do
53
+ expect(Rgrep.start(["find", PATH.to_s, "--name", "Diary", "--contents"]).to_i).to eq( CLI_VALID_RESPONSE)
54
+ end
55
+ end
56
+
57
+ context 'failed when' do
58
+ # TODO: need additioanl time because we should override method from gem Thor
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,93 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ require File.join('..', 'lib', 'Rgrep.rb')
20
+
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+ config.expect_with :rspec do |expectations|
26
+ # This option will default to `true` in RSpec 4. It makes the `description`
27
+ # and `failure_message` of custom matchers include text for helper methods
28
+ # defined using `chain`, e.g.:
29
+ # be_bigger_than(2).and_smaller_than(4).description
30
+ # # => "be bigger than 2 and smaller than 4"
31
+ # ...rather than:
32
+ # # => "be bigger than 2"
33
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
+ end
35
+
36
+ # rspec-mocks config goes here. You can use an alternate test double
37
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
38
+ config.mock_with :rspec do |mocks|
39
+ # Prevents you from mocking or stubbing a method that does not exist on
40
+ # a real object. This is generally recommended, and will default to
41
+ # `true` in RSpec 4.
42
+ mocks.verify_partial_doubles = true
43
+ end
44
+
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ =begin
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Limits the available syntax to the non-monkey patched syntax that is
56
+ # recommended. For more details, see:
57
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
58
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
59
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
60
+ config.disable_monkey_patching!
61
+
62
+ # This setting enables warnings. It's recommended, but in some cases may
63
+ # be too noisy due to issues in dependencies.
64
+ config.warnings = true
65
+
66
+ # Many RSpec users commonly either run the entire suite or an individual
67
+ # file, and it's useful to allow more verbose output when running an
68
+ # individual spec file.
69
+ if config.files_to_run.one?
70
+ # Use the documentation formatter for detailed output,
71
+ # unless a formatter has already been configured
72
+ # (e.g. via a command-line flag).
73
+ config.default_formatter = 'doc'
74
+ end
75
+
76
+ # Print the 10 slowest examples and example groups at the
77
+ # end of the spec run, to help surface which specs are running
78
+ # particularly slow.
79
+ config.profile_examples = 10
80
+
81
+ # Run specs in random order to surface order dependencies. If you find an
82
+ # order dependency and want to debug it, you can fix the order by providing
83
+ # the seed, which is printed after each run.
84
+ # --seed 1234
85
+ config.order = :random
86
+
87
+ # Seed global randomization in this process using the `--seed` CLI option.
88
+ # Setting this allows you to use `--seed` to deterministically reproduce
89
+ # test failures related to randomization by passing the same `--seed` value
90
+ # as the one that triggered the failure.
91
+ Kernel.srand config.seed
92
+ =end
93
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Rgrep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ernest Khasanzhinov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: launchy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: os
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.5.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.5.0
78
+ description: A sample gem. Gem like grep in linux, but ruby.
79
+ email:
80
+ - khasanzhinov@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .DS_Store
86
+ - .rspec
87
+ - Gemfile
88
+ - Rakefile
89
+ - Rgrep.gemspec
90
+ - lib/Rgrep.rb
91
+ - lib/Rgrep/Description.rb
92
+ - lib/Rgrep/version.rb
93
+ - spec/.DS_Store
94
+ - spec/blueprint/.DS_Store
95
+ - spec/blueprint/1.txt
96
+ - spec/blueprint/2.txt
97
+ - spec/rgrep_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: ''
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: A sample gem
124
+ test_files: []