From 353e9e6f3cc27b519637c11126411c2791f65014 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 14 Jul 2023 21:42:10 -0400 Subject: [PATCH] Update upload_network.py script --- OpenBench/utils.py | 4 +- OpenBench/views.py | 9 ++++- Scripts/upload_net.py | 90 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 514bb6b..9f6dd5b 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -577,7 +577,7 @@ def network_upload(request, engine, name): # Save the file locally into /Media/ if we don't already have this file if not Network.objects.filter(sha256=sha256): - FileSystemStorage().save('%s' % (engine, sha256), netfile) + FileSystemStorage().save('%s' % (sha256), netfile) # Create the Network object mapping to the saved local file Network.objects.create( @@ -585,7 +585,7 @@ def network_upload(request, engine, name): engine=engine, author=request.user.username) # Redirect to Engine specific view, to add clarity - return redirect(request, '/networks/%s/' % (engine), status='Uploaded %s for %s' % (engine, name)) + return redirect(request, '/networks/%s/' % (engine), status='Uploaded %s for %s' % (name, engine)) def network_default(request, engine, network): diff --git a/OpenBench/views.py b/OpenBench/views.py index c0be8b6..2c0f7d4 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -495,8 +495,13 @@ def scripts(request): login(request) # All requests are attached to a User - if request.POST['action'] == 'UPLOAD': - return networks(request, target='UPLOAD') + if request.POST['action'] == 'UPLOAD_NETWORK': + engine = request.POST['engine'] + name = request.POST['name'] + import sys + sys.stdout.write(engine + ' ' + name + '\n') + sys.stdout.flush() + return networks(request, engine, 'upload', name) if request.POST['action'] == 'CREATE_TEST': return newTest(request) diff --git a/Scripts/upload_net.py b/Scripts/upload_net.py index 2a9ebce..235d181 100755 --- a/Scripts/upload_net.py +++ b/Scripts/upload_net.py @@ -1,17 +1,83 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# OpenBench is a chess engine testing framework by Andrew Grant. # +# # +# # +# 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 . # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import argparse +import os +import re import requests -URL = 'http://chess.grantnet.us/scripts/' -FILENAME = '' +from urllib.parse import urljoin -data = { - 'engine' : '', - 'name' : '', - 'username' : '', - 'password' : '', - 'action' : 'UPLOAD', -} +def upload_file(): -with open(FILENAME, 'rb') as file: - r = requests.post(URL, data=data, files={'netfile' : file}) + # 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) -print(r.text) \ No newline at end of file + # 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('-S', '--server' , help=help_server , required=req_server) + p.add_argument('-E', '--engine' , help='Engine' , required=True ) + p.add_argument('-N', '--name' , help='Network Name', required=True ) + p.add_argument('-F', '--file' , help='File Name' , required=True ) + args = p.parse_args() + + # 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' ] + + # All scripts connect through this API point, with an action in the POST data + url = urljoin(args.server, 'scripts/') + + # POST payload must contain an action value + data = { + 'username' : args.username, + 'password' : args.password, + 'engine' : args.engine, + 'name' : args.name, + 'action' : 'UPLOAD_NETWORK', + } + + # Upload the file and report the status code + with open(args.file, 'rb') as network: + r = requests.post(url, data=data, files={ 'netfile' : network }) + print ('Code : %s' % (r.status_code)) + + # Report any error messages + pattern = r'
\s*
(.*?)
\s*
' + if matches := re.findall(pattern, r.text, re.DOTALL): + print ('Error : %s' % (matches[0].strip())) + + # Report any status messages + pattern = r'
\s*
(.*?)
\s*
' + if matches := re.findall(pattern, r.text, re.DOTALL): + print ('Status: %s' % (matches[0].strip())) + +if __name__ == '__main__': + upload_file() \ No newline at end of file