gren 0.0.3 → 0.0.4

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.
@@ -6,6 +6,8 @@ Rakefile
6
6
  bin/gren
7
7
  lib/gren.rb
8
8
  lib/gren/cli.rb
9
+ lib/gren/findgrep.rb
10
+ lib/gren/result.rb
9
11
  script/console
10
12
  script/destroy
11
13
  script/generate
data/bin/gren CHANGED
@@ -7,5 +7,5 @@ require 'rubygems'
7
7
  require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
8
  require "gren/cli"
9
9
 
10
- Version = "0.0.3"
10
+ Version = "0.0.4"
11
11
  Gren::CLI.execute(STDOUT, ARGV)
@@ -2,5 +2,5 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Gren
5
- VERSION = '0.0.3'
5
+ VERSION = '0.0.4'
6
6
  end
@@ -1,74 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
- require "find"
3
2
  require 'optparse'
3
+ require File.join(File.dirname(__FILE__), 'findgrep')
4
4
 
5
5
  module Gren
6
- class FindGrep
7
- DEFAULT_DIR = '.'
8
- DEFAULT_FILE_PATTERN = '(\.cpp$)|(\.c$)|(\.h$)|(\.hpp$)|(\.csv$)|(makefile$)|(makefile\.[0-9A-Za-z]+$)|(\.mk$)|(\.rb$)|(\.ags$)'
9
- DEFAULT_FPATH_PATTERN = '(\.svn)|(\.git)|(CVS)'
10
-
11
- attr_writer :ignoreCase
12
- attr_writer :fpathDisp
13
-
14
- def initialize(pattern, dir = DEFAULT_DIR, filePattern = DEFAULT_FILE_PATTERN, fpathPattern = DEFAULT_FPATH_PATTERN)
15
- @pattern = pattern
16
- @dir = dir
17
- @filePattern = filePattern
18
- @fpathPattern = fpathPattern
19
- @ignoreCase = false
20
- @fpathDisp = false
21
- end
22
-
23
- def searchAndPrint(stdout)
24
- fileRegexp = Regexp.new(@filePattern)
25
- patternRegexp = makePattenRegexp
26
-
27
- Find::find(@dir) { |fpath|
28
- if (File.file?(fpath) &&
29
- fileRegexp.match(fpath) &&
30
- fpath !~ /#{@fpathPattern}/)
31
- # 行頭の./は削除
32
- fpath.gsub!(/^.\//, "");
33
-
34
- # ファイルパスを表示
35
- if (@fpathDisp)
36
- stdout.print "#{fpath}"
37
- end
38
-
39
- # 検索
40
- open(fpath, "r") { |file|
41
- file.each() { |line|
42
- line.chomp!
43
- if (patternRegexp.match(line))
44
- if (!@fpathDisp)
45
- stdout.print "#{fpath}:#{file.lineno}:#{line}\n"
46
- else
47
- # 隠しコマンド
48
- # patternに"."を渡した時はFound patternを表示しない
49
- # ファイル名一覧を取得する時等に便利
50
- stdout.print " ........ Found pattern." unless (@pattern == ".")
51
- break
52
- end
53
- end
54
- }
55
- }
56
-
57
- # 改行
58
- stdout.puts if (@fpathDisp)
59
-
60
- end
61
- }
62
- end
63
-
64
- def makePattenRegexp
65
- option = 0
66
- option |= Regexp::IGNORECASE if (@ignoreCase)
67
- Regexp.new(@pattern, option)
68
- end
69
- private :makePattenRegexp
70
- end
71
-
72
6
  class CLI
73
7
  def self.execute(stdout, arguments=[])
74
8
  # オプション
@@ -76,7 +10,7 @@ module Gren
76
10
  fpathDisp = false
77
11
 
78
12
  # オプション解析
79
- opt = OptionParser.new("#{File.basename($0)} [option] pattern [dir] [file_pattern] [fpath]")
13
+ opt = OptionParser.new("#{File.basename($0)} [option] pattern [dir] [filename_regexp]")
80
14
  opt.on('-i', 'Ignore case.') {|v| ignoreCase = true}
81
15
  opt.on('-f', 'The searched file name is displayed.') {|v| fpathDisp = true}
82
16
  opt.parse!(arguments)
@@ -0,0 +1,121 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'find'
3
+ require File.join(File.dirname(__FILE__), 'result')
4
+
5
+ module Gren
6
+ class FindGrep
7
+ # DEFAULT_FPATH_PATTERN = '(\.svn)|(\.git)|(CVS)|(\.o$)|(\.lo$)|(\.la$)|(^#.*#$)|(~$)|(^.#)|(^\.DS_Store$)|(\.bak$)|(\.BAK$)'
8
+ IGNORE_FILE = /(\A#.*#\Z)|(~\Z)|(\A\.#)/
9
+ IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
10
+
11
+ attr_writer :ignoreCase
12
+ attr_writer :fpathDisp
13
+
14
+ def initialize(pattern, dir = '.', filePattern = '.')
15
+ @pattern = pattern
16
+ @dir = dir
17
+ @fileRegexp = Regexp.new(filePattern)
18
+ @ignoreCase = false
19
+ @fpathDisp = false
20
+ @patternRegexp = makePattenRegexp
21
+ @result = Result.new(@dir)
22
+ end
23
+
24
+ def searchAndPrint(stdout)
25
+ Find::find(@dir) { |fpath|
26
+ # 除外ディレクトリ
27
+ Find.prune if ignoreDir?(fpath)
28
+
29
+ # ファイルでなければ探索しない
30
+ next unless FileTest.file?(fpath)
31
+
32
+ @result.count += 1
33
+
34
+ # 読み込み不可ならば探索しない
35
+ next unless FileTest.readable?(fpath)
36
+
37
+ @result.size += FileTest.size(fpath)
38
+
39
+ # 除外ファイル
40
+ next if ignoreFile?(fpath)
41
+
42
+ @result.search_count += 1
43
+ @result.search_size += FileTest.size(fpath)
44
+
45
+ # 行頭の./は削除
46
+ fpath.gsub!(/^.\//, "");
47
+
48
+ # 検索
49
+ searchMain(stdout, fpath)
50
+ }
51
+
52
+ @result.time_stop
53
+ stdout.puts
54
+ @result.print(stdout)
55
+ end
56
+
57
+ def makePattenRegexp
58
+ option = 0
59
+ option |= Regexp::IGNORECASE if (@ignoreCase)
60
+ Regexp.new(@pattern, option)
61
+ end
62
+ private :makePattenRegexp
63
+
64
+ def ignoreDir?(fpath)
65
+ FileTest.directory?(fpath) &&
66
+ IGNORE_DIR.match(File.basename(fpath))
67
+ end
68
+ private :ignoreDir?
69
+
70
+ def readFile?(fpath)
71
+ @fileRegexp.match(fpath) &&
72
+ !IGNORE_FILE.match(File.basename(fpath)) &&
73
+ !binary?(fpath)
74
+ end
75
+ private :readFile?
76
+
77
+ def ignoreFile?(fpath)
78
+ !readFile?(fpath)
79
+ end
80
+ private :ignoreFile?
81
+
82
+ def binary?(file)
83
+ s = File.read(file, 1024) or return false
84
+ return s.index("\x00")
85
+ end
86
+
87
+ def searchMain(stdout, fpath)
88
+ # ファイルパスを表示
89
+ stdout.print "#{fpath}" if (@fpathDisp)
90
+
91
+ open(fpath, "r") { |file|
92
+ match_file = false
93
+ file.each() { |line|
94
+ line.chomp!
95
+ if (@patternRegexp.match(line))
96
+ if (!@fpathDisp)
97
+ stdout.print "#{fpath}:#{file.lineno}:#{line}\n"
98
+ else
99
+ # 隠しコマンド
100
+ # patternに"."を渡した時はFound patternを表示しない
101
+ # ファイル名一覧を取得する時等に便利
102
+ stdout.print " ........ Found pattern." unless (@pattern == ".")
103
+ end
104
+
105
+ unless match_file
106
+ @result.match_file += 1
107
+ match_file = true
108
+ end
109
+
110
+ @result.match_count += 1
111
+ end
112
+ }
113
+ }
114
+
115
+ # 改行
116
+ stdout.puts if (@fpathDisp)
117
+ end
118
+ private :searchMain
119
+
120
+ end
121
+ end
@@ -0,0 +1,74 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'find'
3
+
4
+ module Gren
5
+ class Result
6
+ attr_accessor :count
7
+ attr_accessor :search_count
8
+ attr_accessor :match_file
9
+ attr_accessor :match_count
10
+ attr_accessor :size
11
+ attr_accessor :search_size
12
+
13
+ def initialize(start_dir)
14
+ @start_dir = File.expand_path(start_dir)
15
+ @count, @search_count, @match_file, @match_count, @size, @search_size = 0, 0, 0, 0, 0, 0
16
+ @start_time = Time.now
17
+ end
18
+
19
+ def time_stop
20
+ @end_time = Time.now
21
+ end
22
+
23
+ def time
24
+ @end_time - @start_time
25
+ end
26
+
27
+ def time_s
28
+ t = time.truncate
29
+ h = t / 3600
30
+ t = t % 3600
31
+ m = t / 60
32
+ t = t % 60
33
+ t += round(time - time.prec_i, 2)
34
+
35
+ if (h > 0 && m > 0)
36
+ "#{h}h #{m}m #{t}s"
37
+ elsif (m > 0)
38
+ "#{m}m #{t}s"
39
+ else
40
+ "#{t}sec"
41
+ end
42
+ end
43
+
44
+ def round(n, d)
45
+ (n * 10 ** d).round / 10.0 ** d
46
+ end
47
+
48
+ def size_s(size)
49
+ tb = 1024 ** 4
50
+ gb = 1024 ** 3
51
+ mb = 1024 ** 2
52
+ kb = 1024
53
+
54
+ if (size >= tb)
55
+ round(size / tb.prec_f, 2).to_s + "TB"
56
+ elsif (size >= gb)
57
+ round(size / gb.prec_f, 2).to_s + "GB"
58
+ elsif (size >= mb)
59
+ round(size / mb.prec_f, 2).to_s + "MB"
60
+ elsif (size >= kb)
61
+ round(size / kb.prec_f, 2).to_s + "KB"
62
+ else
63
+ size.to_s + "Byte"
64
+ end
65
+ end
66
+
67
+ def print(stdout)
68
+ stdout.puts "dir : #{@start_dir} (#{time_s})"
69
+ stdout.puts "files : #{@search_count} in #{@count} (#{size_s(@search_size)} in #{size_s(@size)})"
70
+ stdout.puts "match : #{@match_file} files, #{match_count} match"
71
+ end
72
+
73
+ end
74
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - ongaeshi
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-22 00:00:00 +09:00
17
+ date: 2010-07-25 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,8 @@ files:
65
65
  - bin/gren
66
66
  - lib/gren.rb
67
67
  - lib/gren/cli.rb
68
+ - lib/gren/findgrep.rb
69
+ - lib/gren/result.rb
68
70
  - script/console
69
71
  - script/destroy
70
72
  - script/generate