From 05e8126b54cafd64886e49c409db6cee0f808488 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 19 Sep 2023 16:34:52 +0100 Subject: [PATCH 1/3] move database call out of spalloc --- spinnman/spalloc/spalloc_client.py | 9 +++------ spinnman/spalloc/spalloc_job.py | 8 ++------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index cd2758d4a..1563406b5 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -457,8 +457,8 @@ def __init__(self, session, job_handle): self.__proxy_thread = None self.__proxy_ping = None - @overrides(SpallocJob._write_session_credentials_to_db) - def _write_session_credentials_to_db(self, cur): + @overrides(SpallocJob.get_session_credentials_for_db) + def get_session_credentials_for_db(self): config = {} config["SPALLOC", "service uri"] = self._service_url config["SPALLOC", "job uri"] = self._url @@ -471,10 +471,7 @@ def _write_session_credentials_to_db(self, cur): # We never write the auth headers themselves; we just extend the # session del headers["Authorization"] - cur.executemany(""" - INSERT INTO proxy_configuration(kind, name, value) - VALUES(?, ?, ?) - """, [(k1, k2, v) for (k1, k2), v in config.items()]) + return config @overrides(SpallocJob.get_state) def get_state(self): diff --git a/spinnman/spalloc/spalloc_job.py b/spinnman/spalloc/spalloc_job.py index 3ed3edd16..e9afaef95 100644 --- a/spinnman/spalloc/spalloc_job.py +++ b/spinnman/spalloc/spalloc_job.py @@ -194,17 +194,13 @@ def where_is_machine(self, x: int, y: int) -> Tuple[int, int, int]: """ @abstractmethod - def _write_session_credentials_to_db(self, cur: Cursor): + def get_session_credentials_for_db(self): """ - Write the session credentials for the job to the database accessed by - the given cursor. + Get the session credentials for the job to be written into a database .. note:: May assume that there is a ``proxy_configuration`` table with ``kind``, ``name`` and ``value`` columns. - - :param ~sqlite3.Cursor cur: - The open cursor to the database. """ def __enter__(self): From 4d5c2f7da983b15b3a58cf2367675ae3e340f164 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Wed, 20 Sep 2023 09:39:41 +0100 Subject: [PATCH 2/3] move database call out --- spinnman/spalloc/spalloc_client.py | 31 ++++++------------------------ 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index 1563406b5..d8981a412 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -98,7 +98,7 @@ def __init__( logger.info("established session to {} for {}", service_url, username) @staticmethod - def open_job_from_database(conn: sqlite3.Cursor) -> SpallocJob: + def open_job_from_database(service_url, job_url, cookies, headers) -> SpallocJob: """ Create a job from the description in the attached database. This is intended to allow for access to the job's allocated resources from @@ -110,35 +110,16 @@ def open_job_from_database(conn: sqlite3.Cursor) -> SpallocJob: credentials may have expired; if so, the job will be unable to regenerate them. - :param ~sqlite3.Cursor conn: - The database cursor to retrieve the job details from. Assumes - the presence of a ``proxy_configuration`` table with ``kind``, - ``name`` and ``value`` columns. + :param str service_url: + :param str job_url: + :param dict(str, str) cookies: + :param dict(str, str) headers: + :return: The job handle, or ``None`` if the records in the database are absent or incomplete. :rtype: SpallocJob """ - service_url = None - job_url = None - cookies = {} - headers = {} - for row in conn.execute(""" - SELECT kind, name, value FROM proxy_configuration - """): - kind, name, value = row - if kind == "SPALLOC": - if name == "service uri": - service_url = value - elif name == "job uri": - job_url = value - elif kind == "COOKIE": - cookies[name] = value - elif kind == "HEADER": - headers[name] = value - if not service_url or not job_url or not cookies or not headers: - # Cannot possibly work without a session or job - return None session = Session(service_url, session_credentials=(cookies, headers)) return _SpallocJob(session, job_url) From 7674f60e886a0b2c5727a19b71217fdfa0a0b135 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Wed, 20 Sep 2023 11:21:21 +0100 Subject: [PATCH 3/3] flake8 --- spinnman/spalloc/spalloc_client.py | 4 ++-- spinnman/spalloc/spalloc_job.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index d8981a412..90bd74179 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -21,7 +21,6 @@ from packaging.version import Version import queue import requests -import sqlite3 import struct import threading from typing import Dict, List, Tuple @@ -98,7 +97,8 @@ def __init__( logger.info("established session to {} for {}", service_url, username) @staticmethod - def open_job_from_database(service_url, job_url, cookies, headers) -> SpallocJob: + def open_job_from_database( + service_url, job_url, cookies, headers) -> SpallocJob: """ Create a job from the description in the attached database. This is intended to allow for access to the job's allocated resources from diff --git a/spinnman/spalloc/spalloc_job.py b/spinnman/spalloc/spalloc_job.py index e9afaef95..3666a5746 100644 --- a/spinnman/spalloc/spalloc_job.py +++ b/spinnman/spalloc/spalloc_job.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from sqlite3 import Cursor from typing import Dict, Tuple from spinn_utilities.abstract_base import AbstractBase, abstractmethod from spinn_utilities.abstract_context_manager import AbstractContextManager