2018-08-14 16:36:43 +02:00
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
|
|
|
from ..space import G
|
|
|
|
from . import exceptions
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def submit_benchmark_bgthread(benchmark_data: dict) -> threading.Thread:
|
|
|
|
"""Submit benchmark data in a background thread.
|
|
|
|
|
|
|
|
This will update G.xxx to reflect the state of the submission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
thread = threading.Thread(target=_submit_and_update_g, args=(benchmark_data,))
|
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
|
|
|
|
|
|
|
def _submit_and_update_g(benchmark_data: dict) -> None:
|
|
|
|
with G.progress_lock:
|
|
|
|
G.submission_exception = None
|
|
|
|
G.state = G.State.submitting
|
|
|
|
|
|
|
|
def set_exception(exception):
|
|
|
|
with G.progress_lock:
|
|
|
|
G.state = G.State.complete
|
|
|
|
G.submission_exception = exception
|
|
|
|
|
|
|
|
try:
|
|
|
|
submit_benchmark(benchmark_data)
|
|
|
|
|
|
|
|
except exceptions.CommunicationError as ex:
|
|
|
|
log.error('Error %d submitting benchmark: %s', ex.status_code, ex.message)
|
|
|
|
if ex.json:
|
2018-08-14 17:15:41 +02:00
|
|
|
log.error('Response JSON: %r', ex.json)
|
2018-08-14 16:36:43 +02:00
|
|
|
else:
|
2018-08-14 17:15:41 +02:00
|
|
|
log.error('Response body: %r', ex.body)
|
2018-08-14 16:36:43 +02:00
|
|
|
set_exception(ex)
|
|
|
|
return
|
|
|
|
|
|
|
|
except exceptions.TokenTimeoutError as ex:
|
|
|
|
log.warning('Timeout waiting for a client token. Just try submitting again.')
|
|
|
|
set_exception(ex)
|
|
|
|
return
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
log.error('error submitting benchmark: %s', ex)
|
|
|
|
set_exception(ex)
|
|
|
|
return
|
|
|
|
|
|
|
|
with G.progress_lock:
|
|
|
|
G.state = G.State.complete
|
|
|
|
G.results_submitted = True
|
2018-08-14 15:05:37 +02:00
|
|
|
|
|
|
|
|
2018-08-09 17:09:21 +02:00
|
|
|
def submit_benchmark(benchmark_data: dict):
|
|
|
|
"""Submit benchmark data to MyData.
|
|
|
|
|
|
|
|
Authenticates the user via the web browser and Blender ID if necessary.
|
|
|
|
Authentication tokens are stored on disk and validated before reusing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2018-08-14 15:05:37 +02:00
|
|
|
from .client import BenchmarkClient
|
2018-08-09 17:09:21 +02:00
|
|
|
|
|
|
|
mydata_url = os.environ.get('MYDATA') or 'https://mydata.blender.org/'
|
|
|
|
if 'MYDATA' in os.environ:
|
|
|
|
# Assume we're debugging here.
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(asctime)-15s %(levelname)8s %(name)s %(message)s')
|
|
|
|
|
|
|
|
bc = BenchmarkClient(mydata_url)
|
|
|
|
|
|
|
|
# Make sure we have a token; can start the browser to get one.
|
2018-08-14 16:36:43 +02:00
|
|
|
token = bc.load_auth_token()
|
|
|
|
if not token:
|
|
|
|
raise exceptions.TokenTimeoutError()
|
|
|
|
|
2018-08-09 17:09:21 +02:00
|
|
|
result = bc.submit_benchmark(benchmark_data)
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
# If we get a location from the MyData server, show it in a browser.
|
|
|
|
if result.location:
|
2018-08-14 16:36:43 +02:00
|
|
|
with G.progress_lock:
|
|
|
|
G.results_url = result.location
|
2018-08-09 17:09:21 +02:00
|
|
|
import webbrowser
|
|
|
|
webbrowser.open_new_tab(result.location)
|