OSDN Git Service

Forgot to add this file.
[shogi-server/shogi-server.git] / csa-file-filter
1 #!/usr/bin/ruby
2 # $Id$
3 #
4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
6 #
7 #--
8 # Copyright (C) 2008 Daigo Moriwaki <daigo at debian dot org>
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 #++
24 #
25 # == Synopsis
26
27 # mk_rate reads CSA files, calculates rating scores of each player, and then
28 # outputs a yaml file (players.yaml) that Shogi-server can recognize.
29
30 # == Usage
31
32 # (1) csa-file-filter.rb [options] DIR...
33
34 # [<tt>DIR</tt>]
35 #   where CSA files are looked for recursively
36
37 # (2) csa-file-filter.rb [options]
38
39 # CSA file names are put into STDIN.
40
41 # OPTOINS:
42
43 # [<tt>--within</tt> <i>n</i> [days]]
44 #   find records that were played last n days
45
46 # [<tt>--help</tt>]
47 #   show this message
48
49 # == Prerequire
50
51 # Sample Command lines that isntall prerequires will work on Debian.
52
53 # * Ruby 1.8.7 including RDoc
54
55 #   $ sudo aptitude install ruby ruby1.8
56
57 # == Example
58 #
59 # If you want kifu files that were played last 14 days to be rated,
60 #
61 #   $ find /path/to/dir -name "wdoor+floodgate*.csa" | \
62 #     ./csa-file-filter --within 14 | \
63 #     ./mk_rate --half-life-ignore 14
64 #
65
66 require 'date'
67 require 'getoptlong'
68
69 #
70 # Filter the filename. If the file passes the criteria, it goes to STDOUT; 
71 # otherwise, it is ignored.
72 #
73 def filter(filename)
74   # a within filter
75   if $options["within"]
76     return unless /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.csa$/ =~ filename
77
78     file_date = Date.new($1.to_i, $2.to_i, $3.to_i)
79     now       = Time.now
80     now_date  = Date.new(now.year, now.month, now.day)
81
82     return if now_date - file_date > $options["within"]
83   end
84     
85   puts filename
86 end
87
88 #
89 # Show an usage of this command
90 #
91 def usage(io)
92     io.puts <<EOF
93 USAGE: 
94 #{$0} [options] DIR...
95   DIR                where CSA files are looked for recursively
96 #{$0} [options]
97   CSA file names are put into STDIN.
98
99 OPTOINS:
100   --within n [days]  find records that were played last n days
101   --help             show this message
102 EOF
103 end
104
105 #
106 # Main procedure
107 #
108 def main
109   # Parse command options
110   $options = Hash::new
111   parser = GetoptLong.new(
112     ["--within",            GetoptLong::REQUIRED_ARGUMENT],
113     ["--help", "-h",        GetoptLong::NO_ARGUMENT]
114   )
115   parser.quiet = true
116   begin
117     parser.each_option do |name, arg|
118       name.sub!(/^--/, '')
119       $options[name] = arg.dup
120     end
121     if $options["within"]
122       $options["within"] = $options["within"].to_i
123     end
124   rescue
125     usage($stderr)
126     raise parser.error_message
127   end
128   if $options["help"]
129     usage($stdout)
130     exit 0
131   end
132
133   # main procedure
134   if ARGV.empty?
135     while line = $stdin.gets do
136       filter line
137       next unless %r!.*\.csa$! =~ line
138       filter line.strip
139     end
140   else
141     while dir = ARGV.shift do
142       Dir.glob( File.join(dir, "**", "*.csa") ) {|f| filter(f)}
143     end
144   end
145 end
146
147 if __FILE__ == $0
148   main
149 end
150
151 # vim: ts=2 sw=2 sts=0