aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2022-09-24 22:15:13 +0300
committerArthur Zamarin <arthurzam@gentoo.org>2022-09-24 22:15:13 +0300
commit2fda2c8663933ac1c860767a7fa439e851b77017 (patch)
tree61418362024e2b705b4ba4a4eccd24d957406e85
parenttest/mixins: remove `mk_named_tempfile` (diff)
downloadsnakeoil-2fda2c8663933ac1c860767a7fa439e851b77017.tar.gz
snakeoil-2fda2c8663933ac1c860767a7fa439e851b77017.tar.bz2
snakeoil-2fda2c8663933ac1c860767a7fa439e851b77017.zip
fileutils: remove deprecated `write_file`
Last usage was removed, so we can remove the function as well. In most cases, like tests, you can use `Path.write_text` or `Path.write_bytes` instead. If not in tests, just use `open` and `write`. Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r--src/snakeoil/data_source.py4
-rw-r--r--src/snakeoil/fileutils.py15
-rw-r--r--tests/test_fileutils.py37
-rw-r--r--tests/test_osutils.py4
4 files changed, 19 insertions, 41 deletions
diff --git a/src/snakeoil/data_source.py b/src/snakeoil/data_source.py
index ea85ec7..8b9cf72 100644
--- a/src/snakeoil/data_source.py
+++ b/src/snakeoil/data_source.py
@@ -456,7 +456,5 @@ class invokable_data_source(data_source):
def transfer_between_files(read_file, write_file, bufsize=(32 * 1024)):
- data = read_file.read(bufsize)
- while data:
+ while data := read_file.read(bufsize):
write_file.write(data)
- data = read_file.read(bufsize)
diff --git a/src/snakeoil/fileutils.py b/src/snakeoil/fileutils.py
index 7d79a25..2f9da9a 100644
--- a/src/snakeoil/fileutils.py
+++ b/src/snakeoil/fileutils.py
@@ -2,7 +2,7 @@
file related operations, mainly reading
"""
-__all__ = ("AtomicWriteFile", 'write_file', 'UnbufferedWriteHandle', 'touch')
+__all__ = ("AtomicWriteFile", 'UnbufferedWriteHandle', 'touch')
types = [""] + list("_%s" % x for x in ("ascii", "utf8"))
__all__ += tuple("readfile%s" % x for x in types) + tuple("readlines%s" % x for x in types)
del types
@@ -36,19 +36,6 @@ def touch(fname, mode=0o644, **kwargs):
f.fileno() if os.utime in os.supports_fd else fname,
dir_fd=None if os.supports_fd else dir_fd, **kwargs)
-
-def write_file(path, mode, stream, encoding=None):
- f = None
- try:
- f = open(path, mode, encoding=encoding)
- if isinstance(stream, (str, bytes)):
- stream = [stream]
- for data in stream:
- f.write(data)
- finally:
- if f is not None:
- f.close()
-
def mmap_or_open_for_read(path):
size = os.stat(path).st_size
if size == 0:
diff --git a/tests/test_fileutils.py b/tests/test_fileutils.py
index 9f63df5..a4555f8 100644
--- a/tests/test_fileutils.py
+++ b/tests/test_fileutils.py
@@ -8,7 +8,7 @@ pjoin = os.path.join
import pytest
from snakeoil import _fileutils, currying, fileutils
-from snakeoil.fileutils import AtomicWriteFile, write_file
+from snakeoil.fileutils import AtomicWriteFile
from snakeoil.test import random_str
@@ -70,8 +70,7 @@ class TestAtomicWriteFile:
kls = AtomicWriteFile
def test_normal_ops(self, tmp_path):
- fp = tmp_path / "target"
- write_file(fp, "w", "me")
+ (fp := tmp_path / "target").write_text("me")
af = self.kls(fp)
af.write("dar")
assert fileutils.readfile_ascii(fp) == "me"
@@ -91,8 +90,7 @@ class TestAtomicWriteFile:
assert os.stat(fp).st_mode & 0o4777 == 0o644
def test_del(self, tmp_path):
- fp = tmp_path / "target"
- write_file(fp, "w", "me")
+ (fp := tmp_path / "target").write_text("me")
assert fileutils.readfile_ascii(fp) == "me"
af = self.kls(fp)
af.write("dar")
@@ -108,8 +106,7 @@ class TestAtomicWriteFile:
af.close()
def test_discard(self, tmp_path):
- fp = tmp_path / "target"
- write_file(fp, "w", "me")
+ (fp := tmp_path / "target").write_text("me")
assert fileutils.readfile_ascii(fp) == "me"
af = self.kls(fp)
af.write("dar")
@@ -160,7 +157,7 @@ class Test_readfile:
if expected[1] is not None:
encoding = expected[1]
expected = expected[0]
- write_file(fp, 'wb', self.convert_data(expected, encoding))
+ fp.write_bytes(self.convert_data(expected, encoding))
if raised:
with pytest.raises(raised):
self.assertFunc(fp, expected)
@@ -175,7 +172,7 @@ class Test_readfile:
with pytest.raises(FileNotFoundError):
self.func(fp)
assert self.func(fp, True) is None
- write_file(fp, 'wb', self.convert_data('dar', 'ascii'))
+ fp.write_bytes(self.convert_data('dar', 'ascii'))
assert self.func(fp, True) == self.none_on_missing_ret_data
# ensure it handles paths that go through files-
@@ -220,14 +217,14 @@ class readlines_mixin:
with pytest.raises(FileNotFoundError):
self.func(fp)
assert not tuple(self.func(fp, False, True))
- write_file(fp, 'wb', self.convert_data('dar', 'ascii'))
+ fp.write_bytes(self.convert_data('dar', 'ascii'))
assert tuple(self.func(fp, True)) == (self.none_on_missing_ret_data,)
assert not tuple(self.func(fp / 'missing', False, True))
def test_strip_whitespace(self, tmp_path):
fp = tmp_path / 'data'
- write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3\n',
+ fp.write_bytes(self.convert_data(' dar1 \ndar2 \n dar3\n',
'ascii'))
results = tuple(self.func(fp, True))
expected = ('dar1', 'dar2', 'dar3')
@@ -236,28 +233,28 @@ class readlines_mixin:
assert results == expected
# this time without the trailing newline...
- write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3',
+ fp.write_bytes(self.convert_data(' dar1 \ndar2 \n dar3',
'ascii'))
results = tuple(self.func(fp, True))
assert results == expected
# test a couple of edgecases; underly c extension has gotten these
# wrong before.
- write_file(fp, 'wb', self.convert_data('0', 'ascii'))
+ fp.write_bytes(self.convert_data('0', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
- write_file(fp, 'wb', self.convert_data('0\n', 'ascii'))
+ fp.write_bytes(self.convert_data('0\n', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
- write_file(fp, 'wb', self.convert_data('0 ', 'ascii'))
+ fp.write_bytes(self.convert_data('0 ', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
@@ -316,16 +313,14 @@ class Test_mmap_or_open_for_read:
func = staticmethod(fileutils.mmap_or_open_for_read)
def test_zero_length(self, tmp_path):
- path = tmp_path / 'target'
- write_file(path, 'w', '')
+ (path := tmp_path / "target").write_text('')
m, f = self.func(path)
assert m is None
assert f.read() == b''
f.close()
def test_mmap(self, tmp_path, data=b'foonani'):
- path = tmp_path / 'target'
- write_file(path, 'wb', data)
+ (path := tmp_path / "target").write_bytes(data)
m, f = self.func(path)
assert len(m) == len(data)
assert m.read(len(data)) == data
@@ -336,9 +331,7 @@ class Test_mmap_or_open_for_read:
class Test_mmap_and_close:
def test_it(self, tmp_path):
- path = tmp_path / 'target'
- data = b'asdfasdf'
- write_file(path, 'wb', [data])
+ (path := tmp_path / "target").write_bytes(data := b'asdfasdf')
fd, m = None, None
try:
fd = os.open(path, os.O_RDONLY)
diff --git a/tests/test_osutils.py b/tests/test_osutils.py
index e56d3ab..1809282 100644
--- a/tests/test_osutils.py
+++ b/tests/test_osutils.py
@@ -10,7 +10,7 @@ from unittest import mock
import pytest
from snakeoil import osutils
from snakeoil.contexts import Namespace
-from snakeoil.fileutils import touch, write_file
+from snakeoil.fileutils import touch
from snakeoil.osutils import native_readdir, supported_systems, sizeof_fmt
from snakeoil.osutils.mount import MNT_DETACH, MS_BIND, mount, umount
@@ -272,7 +272,7 @@ class Test_unlink_if_exists:
f = self.func
path = tmp_path / 'target'
f(path)
- write_file(path, 'w', '')
+ path.write_text('')
f(path)
assert not path.exists()
# and once more for good measure...