OSDN Git Service

Added a sample command line to run the server.
[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
80     return if $now_date - file_date > $options["within"]
81   end
82     
83   puts filename
84 end
85
86 #
87 # Show an usage of this command
88 #
89 def usage(io)
90     io.puts <<EOF
91 USAGE: 
92 #{$0} [options] DIR...
93   DIR                where CSA files are looked for recursively
94 #{$0} [options]
95   CSA file names are put into STDIN.
96
97 OPTOINS:
98   --within n [days]  find records that were played last n days
99   --help             show this message
100 EOF
101 end
102
103 #
104 # Main procedure
105 #
106 def main
107   # Parse command options
108   $options = Hash::new
109   parser = GetoptLong.new(
110     ["--within",            GetoptLong::REQUIRED_ARGUMENT],
111     ["--help", "-h",        GetoptLong::NO_ARGUMENT]
112   )
113   parser.quiet = true
114   begin
115     parser.each_option do |name, arg|
116       name.sub!(/^--/, '')
117       $options[name] = arg.dup
118     end
119     if $options["within"]
120       $options["within"] = $options["within"].to_i
121     end
122   rescue
123     usage($stderr)
124     raise parser.error_message
125   end
126   if $options["help"]
127     usage($stdout)
128     exit 0
129   end
130
131   # main procedure
132   if ARGV.empty?
133     while line = $stdin.gets do
134       filter line
135       next unless %r!.*\.csa$! =~ line
136       filter line.strip
137     end
138   else
139     while dir = ARGV.shift do
140       Dir.glob( File.join(dir, "**", "*.csa") ) {|f| filter(f)}
141     end
142   end
143 end
144
145 if __FILE__ == $0
146   now       = Time.now
147   $now_date  = Date.new(now.year, now.month, now.day)
148   main
149 end
150
151 # vim: ts=2 sw=2 sts=0