Auto consolidate .pgn.bz2 files into a .tar archive
This commit is contained in:
1 parent
a280e63dba
commit
468281d52e
5 files changed
+78
No files matched your search
@@ -14,3 +14,7 @@ Media/*
|
|||||||
!Client/*.py
|
!Client/*.py
|
||||||
!Client/cutechess-ob
|
!Client/cutechess-ob
|
||||||
!Client/cutechess-ob.exe
|
!Client/cutechess-ob.exe
|
||||||
|
|
||||||
|
Media/PGNs/*
|
||||||
|
Media/Networks/*
|
||||||
|
Media/Events/*
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
from OpenBench.watcher import ArtifactWatcher
|
from OpenBench.watcher import ArtifactWatcher
|
||||||
|
from OpenBench.pgn_watcher import PGNWatcher
|
||||||
|
|
||||||
from django.core.management.commands.runserver import Command as BaseRunserverCommand
|
from django.core.management.commands.runserver import Command as BaseRunserverCommand
|
||||||
|
|
||||||
class Command(BaseRunserverCommand):
|
class Command(BaseRunserverCommand):
|
||||||
@@ -30,6 +32,8 @@ class Command(BaseRunserverCommand):
|
|||||||
|
|
||||||
def pre_start(self):
|
def pre_start(self):
|
||||||
self.watcher = ArtifactWatcher().start()
|
self.watcher = ArtifactWatcher().start()
|
||||||
|
self.pgn_watcher = PGNWatcher().start()
|
||||||
|
|
||||||
def pre_quit(self):
|
def pre_quit(self):
|
||||||
self.watcher.kill()
|
self.watcher.kill()
|
||||||
|
self.pgn_watcher.kill()
|
||||||
@@ -201,6 +201,7 @@ class PGN(Model):
|
|||||||
test_id = IntegerField(default=0)
|
test_id = IntegerField(default=0)
|
||||||
result_id = IntegerField(default=0)
|
result_id = IntegerField(default=0)
|
||||||
book_index = IntegerField(default=0)
|
book_index = IntegerField(default=0)
|
||||||
|
processed = BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.filename()
|
return self.filename()
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
# #
|
||||||
|
# OpenBench is a chess engine testing framework authored 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/>. #
|
||||||
|
# #
|
||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from OpenBench.models import PGN
|
||||||
|
|
||||||
|
from django.db import transaction
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
|
from django.core.files.storage import FileSystemStorage
|
||||||
|
|
||||||
|
class PGNWatcher(threading.Thread):
|
||||||
|
|
||||||
|
def process_pgn(self, pgn):
|
||||||
|
|
||||||
|
tar_path = FileSystemStorage('Media/PGNs').path('%d.pgn.tar' % (pgn.test_id))
|
||||||
|
pgn_path = FileSystemStorage().path(pgn.filename())
|
||||||
|
|
||||||
|
with transaction.atomic():
|
||||||
|
|
||||||
|
# Ensure Media/PGNs exists
|
||||||
|
dir_name = os.path.dirname(tar_path)
|
||||||
|
if not os.path.exists(dir_name):
|
||||||
|
os.makedirs(dir_name)
|
||||||
|
|
||||||
|
# First PGN will create the initial .tar file
|
||||||
|
mode = 'a' if os.path.exists(tar_path) else 'w'
|
||||||
|
with tarfile.open(tar_path, mode) as tar:
|
||||||
|
tar.add(pgn_path, arcname=pgn.filename())
|
||||||
|
|
||||||
|
# Delete the raw .pgn.bz2 file, and don't process it again
|
||||||
|
FileSystemStorage().delete(pgn.filename())
|
||||||
|
pgn.processed = True
|
||||||
|
pgn.save()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
for pgn in PGN.objects.filter(processed=False):
|
||||||
|
try:
|
||||||
|
self.process_pgn(pgn)
|
||||||
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.stdout.flush()
|
||||||
|
time.sleep(15)
|
||||||
@@ -39,6 +39,7 @@ from OpenBench.models import *
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from OpenSite.settings import MEDIA_ROOT
|
from OpenSite.settings import MEDIA_ROOT
|
||||||
|
|
||||||
|
from django.db import transaction
|
||||||
from django.db.models import F, Q
|
from django.db.models import F, Q
|
||||||
from django.http import HttpResponse, JsonResponse
|
from django.http import HttpResponse, JsonResponse
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
@@ -762,6 +763,8 @@ def client_heartbeat(request, machine):
|
|||||||
@verify_worker
|
@verify_worker
|
||||||
def client_submit_pgn(request, machine):
|
def client_submit_pgn(request, machine):
|
||||||
|
|
||||||
|
with transaction.atomic():
|
||||||
|
|
||||||
# Format: test.result.book-index.pgn.bz2
|
# Format: test.result.book-index.pgn.bz2
|
||||||
pgn = PGN()
|
pgn = PGN()
|
||||||
pgn.test_id = int(request.POST['test_id'] )
|
pgn.test_id = int(request.POST['test_id'] )
|
||||||
|
|||||||
Reference in new issue
Block a user