/usr/share/gnubg/scripts/batch.py is in gnubg-data 1.04.000-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | #
# batch.py -- batch import and analysis of multiple files using gnubg
#
# by Jon Kinsey <Jon_Kinsey@hotmail.com>, 2004
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# $Id: batch.py,v 1.8 2014/08/08 10:55:50 mdpetch Exp $
"""
batch.py -- batch import and analysis of multiple files using gnubg
by Jon Kinsey <Jon_Kinsey@hotmail.com>, 2004
\n"""
# Achim Mueller <ace@gnubg.org>
#
# Usage: start gnubg -t
# (no game) load python batch.py
# Answer questions regarding source and destination directory
# Enjoy the analysis
import os
if sys.version_info >= (3, 0):
def python3_raw_input(prompt):
return input(prompt)
raw_input = python3_raw_input
# file extensions, names and gnubg import commands
extensions = ['mat', 'pos', 'sgg', 'tmg', 'txt']
extTypes = ['.mat', '.pos', 'Games grid', 'True money game', 'Snowie text']
extCmds = ['mat', 'pos', 'sgg', 'tmg', 'snowietxt']
LAST_VALUES = "batch.dirs"
def CheckFiles(files, dir, destDir):
"Check files aren't already analyzed"
fileList = [[], [], [], [], []]
foundFile = False
for eType in extTypes:
numType = extTypes.index(eType)
for file in files[numType]:
# Check that file hasn't already been analyzed
dot = file.rfind('.')
existingOutputFile = destDir + file[: dot] + '.sgf'
if (os.access(existingOutputFile, os.F_OK)):
# See if newer
if os.stat(existingOutputFile).st_mtime > os.stat(dir + file).st_mtime:
continue # Skip as already processed
foundFile = True
fileList[numType].append(file)
if foundFile:
return fileList
else:
print(" ** All files in directory already processed **")
def GetFiles(dir):
"Look for gnubg import files in dir"
try:
files = os.listdir(dir)
except:
print(" ** Directory not found **")
return 0
fileList = [[], [], [], [], []]
foundAnyFile = False
foundBGFile = False
# Check each file in dir
for file in files:
# Check it's a file (not a directory)
if os.path.isfile(dir + file):
foundAnyFile = True
# Check has supported extension
dot = file.rfind('.')
if dot != -1:
ext = file[dot + 1:].lower()
if ext in extensions:
foundBGFile = True
ftype = extensions.index(ext)
fileList[ftype].append(file)
if foundBGFile:
return fileList
else:
if not foundAnyFile:
print (" ** No files in directory **")
else:
print (" ** No valid files found in directory **")
return 0
def AnalyzeFile(prompt, file, dir, destDir, type):
"Run commands to analyze file in gnubg"
gnubg.command('import ' + extCmds[type] + ' "' + dir + file + '"')
print (prompt + " Analysing " + file)
gnubg.command('analyze match')
file = file[:-len(extensions[type])] + "sgf"
gnubg.command('save match "' + destDir + file + '"')
def GetYN(prompt):
confirm = ''
while len(confirm) == 0 or (confirm[0] != 'y' and confirm[0] != 'n'):
confirm = raw_input(prompt + " (y/n): ").lower()
return confirm
def GetDir(prompt):
dir = raw_input(prompt)
if dir:
# Make sure dir ends in a slash
if (dir[-1] != '\\' and dir[-1] != '/'):
dir = dir + '/'
return dir
def BatchImport():
"Import and analyse all files in a directory"
dirs = [0, 0]
# See if last dirs saved
if (os.access(LAST_VALUES, os.F_OK)):
file = open(LAST_VALUES, "r")
dirs = file.readlines()
file.close()
# Remove new lines
dirs[0] = dirs[0][:-1]
dirs[1] = dirs[1][:-1]
if os.path.isdir(dirs[0]) and os.path.isdir(dirs[1]):
print ("Last used dirs:\n from:", dirs[0][:-1], "\n to:", dirs[1][:-1])
if GetYN("Reuse") == 'n':
dirs = [0, 0]
else:
dirs = [0, 0]
while True:
# Get directory with original files in
if (dirs[0] == 0):
dirs[0] = GetDir(
"Directory containing files to import (enter-exit): ")
if not dirs[0]:
return
# Look for some files
inFiles = GetFiles(dirs[0])
if not inFiles:
dirs = [0, 0]
continue
if (dirs[1] == 0):
# Get directory to put analyzed files in
while True:
dirs[1] = GetDir(
"Directory to put analyzed files in (enter-same dir): ")
if not dirs[1]:
dirs[1] = dirs[0]
if os.path.isdir(dirs[1]):
break
print (" ** Directory not found **")
# Make sure files are new
files = CheckFiles(inFiles, dirs[0], dirs[1])
if files:
break
dirs = [0, 0]
# Display files that will be analyzed
numFound = 0
for eType in extTypes:
numType = extTypes.index(eType)
if len(files[numType]):
print ("\n" + eType + " files:")
for file in files[numType]:
print (" " + file)
numFound = numFound + 1
if (numFound > 1):
print ("\n", numFound, "files found\n")
# Check user wants to continue
if GetYN("Continue") == 'n':
return
# Save dirs for next time
file = open(LAST_VALUES, "w")
file.write(dirs[0] + "\n")
file.write(dirs[1] + "\n")
file.close()
# Analyze each file
num = 0
for eType in extTypes:
if len(files[extTypes.index(eType)]):
print ("\n" + eType + " files:")
numType = extTypes.index(eType)
for file in files[numType]:
num = num + 1
prompt = "\n(%d/%d)" % (num, numFound)
AnalyzeFile(prompt, file, dirs[0], dirs[1], numType)
print ("\n** Finished **")
return
# Run batchimport on load
try:
print (__doc__)
BatchImport()
except Exception:
e = sys.exc_info()[1].args[0]
print ("Error: " + e)
|