Skip to content

Commit

Permalink
Christ, I'd forgotten how horrific the with syntax was <3.9
Browse files Browse the repository at this point in the history
Can't use bracketed expressions to split multiple context managers
across lines prior to Python 3.9. Have to use the horrid line-ending
markers instead...
  • Loading branch information
waveform80 committed Feb 9, 2024
1 parent c04c236 commit a3f9a4e
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 133 deletions.
16 changes: 8 additions & 8 deletions nobodd/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def prepare_image(conf):
conf.logger.info(
'Skipping resize; %s is already %d bytes or larger',
conf.image, conf.size)
with (
DiskImage(conf.image, access=mmap.ACCESS_WRITE) as img,
FatFileSystem(img.partitions[conf.boot_partition].data) as fs
):
with \
DiskImage(conf.image, access=mmap.ACCESS_WRITE) as img, \
FatFileSystem(img.partitions[conf.boot_partition].data) as fs:

cmdline = fs.root / conf.cmdline
conf.logger.info(
'Re-writing %s in partition %d', conf.cmdline, conf.boot_partition)
Expand Down Expand Up @@ -128,10 +128,10 @@ def detect_partitions(conf):
partition.
"""
conf.logger.info('Detecting partitions')
with (
conf.image.open('rb') as img_file,
DiskImage(img_file) as img
):
with \
conf.image.open('rb') as img_file, \
DiskImage(img_file) as img:

fat_types = (
{UUID('ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'),
UUID('c12a7328-f81f-11d2-ba4b-00a0c93ec93b')}
Expand Down
8 changes: 4 additions & 4 deletions nobodd/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ def main(args=None):
board.serial: board
for board in conf.boards
}
with (
BootServer((conf.listen, conf.port), boards) as server,
DefaultSelector() as selector
):
with \
BootServer((conf.listen, conf.port), boards) as server, \
DefaultSelector() as selector:

server.logger.addHandler(logging.StreamHandler(sys.stderr))
server.logger.setLevel(logging.DEBUG if debug else logging.INFO)
server.logger.info('Ready')
Expand Down
58 changes: 29 additions & 29 deletions tests/test_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_help(capsys):


def test_error_exit_no_debug(capsys, monkeypatch):
with (
mock.patch('nobodd.prep.get_parser') as get_parser,
monkeypatch.context() as m,
):
with \
mock.patch('nobodd.prep.get_parser') as get_parser, \
monkeypatch.context() as m:

m.delenv('DEBUG', raising=False)
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -38,10 +38,10 @@ def test_error_exit_no_debug(capsys, monkeypatch):


def test_error_exit_with_debug(monkeypatch):
with (
mock.patch('nobodd.prep.get_parser') as get_parser,
monkeypatch.context() as m,
):
with \
mock.patch('nobodd.prep.get_parser') as get_parser, \
monkeypatch.context() as m:

m.setenv('DEBUG', '1')
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -50,11 +50,11 @@ def test_error_exit_with_debug(monkeypatch):


def test_error_exit_with_pdb(monkeypatch):
with (
mock.patch('nobodd.prep.get_parser') as get_parser,
mock.patch('pdb.post_mortem') as post_mortem,
monkeypatch.context() as m
):
with \
mock.patch('nobodd.prep.get_parser') as get_parser, \
mock.patch('pdb.post_mortem') as post_mortem, \
monkeypatch.context() as m:

m.setenv('DEBUG', '2')
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -72,21 +72,21 @@ def test_regular_operation(fat_disks_w):
str(fat_disk)
]) == 0
assert fat_disk.stat().st_size == 50 * 1048576
with (
DiskImage(fat_disk) as img,
FatFileSystem(img.partitions[1].data) as fs,
):
with \
DiskImage(fat_disk) as img, \
FatFileSystem(img.partitions[1].data) as fs:

assert (fs.root / 'cmdline.txt').read_text() == (
'ip=dhcp nbdroot=myserver/myshare root=/dev/nbd0p5 '
'console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 '
'rootfstype=ext4 rootwait fixrtc quiet splash')


def test_cmdline_no_newline(fat16_disk_w):
with (
DiskImage(fat16_disk_w, access=mmap.ACCESS_WRITE) as img,
FatFileSystem(img.partitions[1].data) as fs
):
with \
DiskImage(fat16_disk_w, access=mmap.ACCESS_WRITE) as img, \
FatFileSystem(img.partitions[1].data) as fs:

# Ensure the transformation works even when cmdline.txt has no newlines
path = fs.root / 'cmdline.txt'
path.write_text(path.read_text().rstrip('\n'))
Expand All @@ -100,10 +100,10 @@ def test_cmdline_no_newline(fat16_disk_w):
str(fat16_disk_w)
]) == 0

with (
DiskImage(fat16_disk_w) as img,
FatFileSystem(img.partitions[1].data) as fs,
):
with \
DiskImage(fat16_disk_w) as img, \
FatFileSystem(img.partitions[1].data) as fs:

assert (fs.root / 'cmdline.txt').read_text() == (
'ip=dhcp nbdroot=myserver/myshare root=/dev/nbd0p5 '
'console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 '
Expand Down Expand Up @@ -184,10 +184,10 @@ def test_default_host_share(fat16_disk_w):
str(fat16_disk_w)
]) == 0

with (
DiskImage(fat16_disk_w) as img,
FatFileSystem(img.partitions[1].data) as fs,
):
with \
DiskImage(fat16_disk_w) as img, \
FatFileSystem(img.partitions[1].data) as fs:

assert (fs.root / 'cmdline.txt').read_text() == (
'ip=dhcp nbdroot=louis.prima.org/fat16-mutable root=/dev/nbd0p5 '
'console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 '
Expand Down
34 changes: 17 additions & 17 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def test_sigterm(main_thread, capsys):


def test_error_exit_no_debug(main_thread, capsys, monkeypatch):
with (
mock.patch('nobodd.server.get_parser') as get_parser,
monkeypatch.context() as m,
):
with \
mock.patch('nobodd.server.get_parser') as get_parser, \
monkeypatch.context() as m:

m.delenv('DEBUG', raising=False)
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -102,10 +102,10 @@ def test_error_exit_no_debug(main_thread, capsys, monkeypatch):


def test_error_exit_with_debug(main_thread, monkeypatch):
with (
mock.patch('nobodd.server.get_parser') as get_parser,
monkeypatch.context() as m,
):
with \
mock.patch('nobodd.server.get_parser') as get_parser, \
monkeypatch.context() as m:

m.setenv('DEBUG', '1')
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -116,11 +116,11 @@ def test_error_exit_with_debug(main_thread, monkeypatch):


def test_error_exit_with_pdb(main_thread, capsys, monkeypatch):
with (
mock.patch('nobodd.server.get_parser') as get_parser,
mock.patch('pdb.post_mortem') as post_mortem,
monkeypatch.context() as m,
):
with \
mock.patch('nobodd.server.get_parser') as get_parser, \
mock.patch('pdb.post_mortem') as post_mortem, \
monkeypatch.context() as m:

m.setenv('DEBUG', '2')
get_parser.side_effect = RuntimeError('trouble is bad')

Expand All @@ -131,10 +131,10 @@ def test_error_exit_with_pdb(main_thread, capsys, monkeypatch):


def test_regular_operation(fat16_disk, main_thread, capsys):
with (
disk.DiskImage(fat16_disk) as img,
fs.FatFileSystem(img.partitions[1].data) as boot,
):
with \
disk.DiskImage(fat16_disk) as img, \
fs.FatFileSystem(img.partitions[1].data) as boot:

expected = (boot.root / 'random').read_bytes()

main_thread.argv = [
Expand Down
Loading

0 comments on commit a3f9a4e

Please sign in to comment.