Update download_network.py script, and bench_engine.py script

This commit is contained in:
andrew committed 2023-07-14 22:50:30 -04:00
1 parent 353e9e6f3c
commit 5d6ae6a06b
4 files changed
+104 -185

No files matched your search

+52 -27
View File
@@ -1,46 +1,71 @@
#!/usr/bin/python3
#!/bin/python3
import argparse, hashlib, os, requests
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# OpenBench is a chess engine testing framework by Andrew Grant. #
# <https://github.com/AndyGrant/OpenBench> <andrew@grantnet.us> #
# #
# OpenBench 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. #
# #
# OpenBench 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/>. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
OPENBENCH_SERVER = 'http://chess.grantnet.us'
import argparse
import hashlib
import os
import requests
def download_network(username, password, sha256):
def url_join(*args):
# Join a set of URL paths while maintaining the correct format
return '/'.join([f.lstrip('/').rstrip('/') for f in args]) + '/'
print ('Downloading %s...' % (sha256))
def download_network(username, password, server, engine, name):
# Check if we already have the Network file by looking at SHA256
if os.path.isfile(sha256):
with open(sha256, 'rb') as network:
if sha256 == hashlib.sha256(network.read()).hexdigest()[:8].upper():
return
print ('Downloading %s for %s...' % (name, engine))
target = '%s/clientGetNetwork/%s/' % (OPENBENCH_SERVER, sha256)
target = url_join(server, 'clientGetNetwork', engine, name)
payload = { 'username' : username, 'password' : password }
request = requests.post(data=payload, url=target)
with open(sha256, 'wb') as fout:
with open(name, 'wb') as fout:
for chunk in request.iter_content(chunk_size=1024):
if chunk: fout.write(chunk)
fout.flush()
with open(sha256, 'rb') as network:
if sha256 != hashlib.sha256(network.read()).hexdigest()[:8].upper():
print ('Failed to Download...')
if __name__ == '__main__':
req_user = required=('OPENBENCH_USERNAME' not in os.environ)
req_pass = required=('OPENBENCH_PASSWORD' not in os.environ)
help_user = 'Username. May also be passed as OPENBENCH_USERNAME environment variable'
help_pass = 'Password. May also be passed as OPENBENCH_PASSWORD environment variable'
# We can use ENV variables for Username, Password, and Server
req_user = required=('OPENBENCH_USERNAME' not in os.environ)
req_pass = required=('OPENBENCH_PASSWORD' not in os.environ)
req_server = required=('OPENBENCH_SERVER' not in os.environ)
# For clarity, seperate out this help text
help_user = 'Username. May also be passed as OPENBENCH_USERNAME environment variable'
help_pass = 'Password. May also be passed as OPENBENCH_PASSWORD environment variable'
help_server = ' Server. May also be passed as OPENBENCH_SERVER environment variable'
# Parse all arguments, all of which must exist in some form
p = argparse.ArgumentParser()
p.add_argument('-U', '--username', help=help_user , required=req_user)
p.add_argument('-P', '--password', help=help_pass , required=req_pass)
p.add_argument('-N', '--network', help='Network SHA256', required=True)
arguments = p.parse_args()
p.add_argument('-U', '--username', help=help_user , required=req_user )
p.add_argument('-P', '--password', help=help_pass , required=req_pass )
p.add_argument('-S', '--server' , help=help_server , required=req_server)
p.add_argument('-E', '--engine' , help='Engine' , required=True )
p.add_argument('-N', '--network' , help='Name or Sha', required=True )
args = p.parse_args()
if arguments.username is None: arguments.username = os.environ['OPENBENCH_USERNAME']
if arguments.password is None: arguments.password = os.environ['OPENBENCH_PASSWORD']
# Fallback on ENV variables for Username, Password, and Server
args.username = args.username if args.username else os.environ['OPENBENCH_USERNAME']
args.password = args.password if args.password else os.environ['OPENBENCH_PASSWORD']
args.server = args.server if args.server else os.environ['OPENBENCH_SERVER' ]
download_network(arguments.username, arguments.password, arguments.network)
download_network(args.username, args.password, args.server, args.engine, args.network)