ファイル情報

Rev. 2d5cda5e9dde331e37d58133b688a8b2c6295345
サイズ 3,548 バイト
日時 2017-09-03 14:40:39
作者 Daigo Moriwaki
ログメッセージ

Merge remote-tracking branch 'origin/master' into wdoor-stable

内容

  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-2012 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 2.0.0 or later including RDoc
  54. #
  55. # $ sudo aptitude install ruby ruby
  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. require 'date'
  66. require 'getoptlong'
  67. #
  68. # Filter the filename. If the file passes the criteria, it goes to STDOUT;
  69. # otherwise, it is ignored.
  70. #
  71. def filter(filename)
  72. # a within filter
  73. if $options["within"]
  74. return unless /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.csa$/ =~ filename
  75. file_date = Date.new($1.to_i, $2.to_i, $3.to_i)
  76. return if $now_date - file_date > $options["within"]
  77. end
  78. puts filename
  79. end
  80. #
  81. # Show an usage of this command
  82. #
  83. def usage(io)
  84. io.puts <<EOF
  85. USAGE:
  86. #{$0} [options] DIR...
  87. DIR where CSA files are looked for recursively
  88. #{$0} [options]
  89. CSA file names are put into STDIN.
  90. OPTOINS:
  91. --within n [days] find records that were played last n days
  92. --help show this message
  93. EOF
  94. end
  95. #
  96. # Main procedure
  97. #
  98. def main
  99. # Parse command options
  100. $options = Hash::new
  101. parser = GetoptLong.new(
  102. ["--within", GetoptLong::REQUIRED_ARGUMENT],
  103. ["--help", "-h", GetoptLong::NO_ARGUMENT]
  104. )
  105. parser.quiet = true
  106. begin
  107. parser.each_option do |name, arg|
  108. name.sub!(/^--/, '')
  109. $options[name] = arg.dup
  110. end
  111. if $options["within"]
  112. $options["within"] = $options["within"].to_i
  113. end
  114. rescue
  115. usage($stderr)
  116. raise parser.error_message
  117. end
  118. if $options["help"]
  119. usage($stdout)
  120. exit 0
  121. end
  122. # main procedure
  123. if ARGV.empty?
  124. while line = $stdin.gets do
  125. filter line
  126. next unless %r!.*\.csa$! =~ line
  127. filter line.strip
  128. end
  129. else
  130. while dir = ARGV.shift do
  131. Dir.glob( File.join(dir, "**", "*.csa") ) {|f| filter(f)}
  132. end
  133. end
  134. end
  135. if __FILE__ == $0
  136. now = Time.now
  137. $now_date = Date.new(now.year, now.month, now.day)
  138. main
  139. end
  140. # vim: ts=2 sw=2 sts=0
旧リポジトリブラウザで表示