Skip to content

Commit

Permalink
Merge pull request #53 from SpiNNakerManchester/end-27
Browse files Browse the repository at this point in the history
Remove Python 2.7 support
  • Loading branch information
Christian-B committed Feb 12, 2021
2 parents 4f763cd + 70c0af6 commit ad6e105
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8]

steps:
- name: Set up Python ${{ matrix.python-version }}
Expand Down
5 changes: 1 addition & 4 deletions requirements-docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

sphinx
sphinx >= 2
numpydoc
six
enum-compat
sentinel
mock
future
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

six>=1.8.0
appdirs
enum-compat
SpiNNUtilities >= 1!5.1.1, < 1!6.0.0
future
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@
"Operating System :: MacOS",

"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
keywords="spinnaker allocation packing management supercomputer",

# Requirements
install_requires=["six>=1.8.0",
"appdirs",
"enum-compat",
'SpiNNUtilities >= 1!5.1.1, < 1!6.0.0',
"future"],
install_requires=["appdirs",
'SpiNNUtilities >= 1!5.1.1, < 1!6.0.0'],
# Scripts
entry_points={
"console_scripts": [
Expand Down
1 change: 0 additions & 1 deletion spalloc/_keepalive_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
""" A script for keeping Spalloc jobs alive, intended to only ever be run\
from the Spalloc client itself.
"""
from __future__ import print_function
import sys
import threading
from spalloc.protocol_client import ProtocolClient, ProtocolTimeoutError
Expand Down
13 changes: 3 additions & 10 deletions spalloc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@
"""
import os.path
import appdirs
from six import iteritems, PY3
from six.moves import configparser
import configparser

# The application name to use in config file names
_name = "spalloc"
Expand Down Expand Up @@ -159,20 +158,14 @@ def read_config(filenames=None):

# Set default config values (NB: No read_dict in Python 2.7)
parser.add_section(SECTION)
for key, value in iteritems(DEFAULT_CONFIG):
for key, value in DEFAULT_CONFIG.items():
parser.set(SECTION, key, value)

# Attempt to read from each possible file location in turn
for filename in filenames:
try:
with open(filename, "r") as f:
# TODO: Remove use of readfp once we stop supporting 2.7
if PY3:
parser.read_file( # pylint: disable=no-member
f, filename)
else:
parser.readfp( # pylint: disable=deprecated-method
f, filename)
parser.read_file(f, filename)
except (IOError, OSError):
# File did not exist, keep trying
pass
Expand Down
24 changes: 8 additions & 16 deletions spalloc/protocol_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import errno
import json
import socket
import sys
from threading import current_thread, RLock, local
from six import raise_from
from spinn_utilities.abstract_context_manager import AbstractContextManager
from spinn_utilities.overrides import overrides
from spalloc._utils import time_left, timed_out, make_timeout
Expand Down Expand Up @@ -49,7 +47,7 @@ class _ProtocolThreadLocal(local):
"""
# See https://github.com/SpiNNakerManchester/spalloc/issues/12
def __init__(self):
local.__init__(self)
super().__init__()
self.buffer = b""
self.sock = None

Expand Down Expand Up @@ -114,10 +112,7 @@ def _context_entered(self): # pragma: no cover

def _get_connection(self, timeout):
if self._dead:
if sys.version_info[0] > 2:
raise OSError(errno.ENOTCONN, "not connected")
else:
raise socket.error(errno.ENOTCONN, "not connected")
raise OSError(errno.ENOTCONN, "not connected")
connect_needed = False
key = current_thread()
with self._socks_lock:
Expand Down Expand Up @@ -148,9 +143,6 @@ def _do_connect(self, sock):
except OSError as e:
if e.errno != errno.EISCONN:
raise
except socket.error as e: # pylint: disable=duplicate-except
if e[0] != errno.EISCONN: # pylint: disable=unsubscriptable-object
raise
return success

def _has_open_socket(self):
Expand Down Expand Up @@ -231,8 +223,8 @@ def _recv_json(self, timeout=None):
while b"\n" not in self._local.buffer:
try:
data = sock.recv(1024)
except socket.timeout:
raise ProtocolTimeoutError("recv timed out.")
except socket.timeout as e:
raise ProtocolTimeoutError("recv timed out.") from e

# Has socket closed?
if not data:
Expand Down Expand Up @@ -270,8 +262,8 @@ def _send_json(self, obj, timeout=None):
if sock.send(data) != len(data):
# XXX: If can't send whole command at once, just fail
raise OSError("Could not send whole command.")
except socket.timeout:
raise ProtocolTimeoutError("send timed out.")
except socket.timeout as e:
raise ProtocolTimeoutError("send timed out.") from e

def call(self, name, *args, **kwargs):
""" Send a command to the server and return the reply.
Expand Down Expand Up @@ -316,7 +308,7 @@ def call(self, name, *args, **kwargs):
with self._notifications_lock:
self._notifications.append(obj)
except (IOError, OSError) as e:
raise_from(ProtocolError(str(e)), e)
raise ProtocolError(str(e)) from e

def wait_for_notification(self, timeout=None):
""" Return the next notification to arrive.
Expand Down Expand Up @@ -358,7 +350,7 @@ def wait_for_notification(self, timeout=None):
try:
return self._recv_json(timeout)
except (IOError, OSError) as e: # pragma: no cover
raise_from(ProtocolError(str(e)), e)
raise ProtocolError(str(e)) from e

# The bindings of the Spalloc protocol methods themselves; simplifies use
# from IDEs.
Expand Down
10 changes: 5 additions & 5 deletions spalloc/scripts/alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
import subprocess
import sys
import tempfile
from six import iteritems
from six.moves import input, shlex_quote # @UnresolvedImport
from shlex import quote
from spalloc import (
config, Job, JobState, __version__, ProtocolError, ProtocolTimeoutError,
SpallocServerException)
Expand All @@ -131,6 +130,7 @@

arguments = None
t = None
_input = input # This is so we can monkeypatch input during testing


def write_ips_to_csv(connections, ip_file_filename):
Expand All @@ -149,7 +149,7 @@ def write_ips_to_csv(connections, ip_file_filename):
f.write("x,y,hostname\n")
f.write("".join("{},{},{}\n".format(x, y, hostname)
for (x, y), hostname
in sorted(iteritems(connections))))
in sorted(connections.items())))


def print_info(machine_name, connections, width, height, ip_file_filename):
Expand Down Expand Up @@ -183,7 +183,7 @@ def print_info(machine_name, connections, width, height, ip_file_filename):
print(render_definitions(to_print))

try:
input(t_stdout.dim("<Press enter when done>"))
_input(t_stdout.dim("<Press enter when done>"))
except (KeyboardInterrupt, EOFError):
print("")

Expand Down Expand Up @@ -246,7 +246,7 @@ def run_command(command, job_id, machine_name, connections, width, height,

# NB: When using shell=True, commands should be given as a string rather
# than the usual list of arguments.
command = " ".join(map(shlex_quote, command))
command = " ".join(map(quote, command))
p = subprocess.Popen(command, shell=True)

# Pass through keyboard interrupts
Expand Down
3 changes: 1 addition & 2 deletions spalloc/scripts/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import argparse
from collections import OrderedDict
import sys
from six import iteritems
from spalloc import __version__, JobState
from spalloc.term import (
Terminal, render_definitions, render_boards, DEFAULT_BOARD_EDGES)
Expand Down Expand Up @@ -140,7 +139,7 @@ def show_job_info(t, client, timeout, job_id):
", ".join(map(str, args)),
",\n " if args and kwargs else "",
",\n ".join("{}={!r}".format(k, v) for
k, v in sorted(iteritems(kwargs)))
k, v in sorted(kwargs.items()))
)

if job["boards"] is not None:
Expand Down
1 change: 0 additions & 1 deletion spalloc/scripts/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import argparse
from collections import defaultdict, OrderedDict
import sys
from six import next # pylint: disable=redefined-builtin
from spalloc import __version__
from spalloc.term import (
Terminal, render_table, render_definitions, render_boards, render_cells,
Expand Down
2 changes: 1 addition & 1 deletion spalloc/scripts/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class Terminate(Exception):
def __init__(self, code, *args):
super(Terminate, self).__init__()
super().__init__()
self._code = code
args = list(args)
message = args.pop(0) if args else None
Expand Down
9 changes: 4 additions & 5 deletions spalloc/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from collections import defaultdict
from enum import IntEnum
from functools import partial
from six import iteritems, string_types


class ANSIDisplayAttributes(IntEnum):
Expand Down Expand Up @@ -212,7 +211,7 @@ def render_table(table, column_sep=" "):
column_widths = defaultdict(lambda: 0)
for row in table:
for i, column in enumerate(row):
if isinstance(column, string_types):
if isinstance(column, str):
string = column
elif isinstance(column, int):
string = str(column)
Expand All @@ -227,15 +226,15 @@ def render_table(table, column_sep=" "):
out.append(rendered_row)
for i, column in enumerate(row):
# Get string length and formatted string
if isinstance(column, string_types):
if isinstance(column, str):
string = column
length = len(string)
right_align = False
elif isinstance(column, int):
string = str(column)
length = len(string)
right_align = True
elif isinstance(column[1], string_types):
elif isinstance(column[1], str):
f, string = column
length = len(string)
right_align = False
Expand Down Expand Up @@ -283,7 +282,7 @@ def render_definitions(definitions, seperator=": "):
return "\n".join("{:>{}s}{}{}".format(
key, col_width, seperator, str(value).replace(
"\n", "\n{}".format(" "*(col_width + len(seperator)))))
for key, value in iteritems(definitions))
for key, value in definitions.items())


def _board_to_cartesian(x, y, z):
Expand Down
3 changes: 2 additions & 1 deletion tests/scripts/test_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from spalloc import JobState, JobDestroyedError
from spalloc.scripts.alloc import (
write_ips_to_csv, print_info, run_command, main)
# pylint: disable=redefined-outer-name, unused-argument


@pytest.yield_fixture
Expand All @@ -33,7 +34,7 @@ def filename():
def mock_input(monkeypatch):
m = Mock()
import spalloc.scripts.alloc
monkeypatch.setattr(spalloc.scripts.alloc, "input", m)
monkeypatch.setattr(spalloc.scripts.alloc, "_input", m)
return m


Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

[tox]
envlist = py27, py34, py35, pep8
envlist = py36, pep8

[testenv]
passenv=
Expand Down

0 comments on commit ad6e105

Please sign in to comment.