Replace pysendfile with os.sendfile

Change-Id: I1ef33d41fd3784f55929fa6a086fca0c335212e5
This commit is contained in:
Dmitry Tantsur 2021-01-22 17:52:21 +01:00
parent 3858b95204
commit 6e09a008ab
5 changed files with 9 additions and 16 deletions

View File

@ -25,7 +25,6 @@ from glanceclient import client
from glanceclient import exc as glance_exc from glanceclient import exc as glance_exc
from oslo_log import log from oslo_log import log
from oslo_utils import uuidutils from oslo_utils import uuidutils
import sendfile
from swiftclient import utils as swift_utils from swiftclient import utils as swift_utils
import tenacity import tenacity
@ -196,7 +195,7 @@ class GlanceImageService(object):
if url.scheme == "file": if url.scheme == "file":
with open(url.path, "r") as f: with open(url.path, "r") as f:
filesize = os.path.getsize(f.name) filesize = os.path.getsize(f.name)
sendfile.sendfile(data.fileno(), f.fileno(), 0, filesize) os.sendfile(data.fileno(), f.fileno(), 0, filesize)
return return
image_chunks = self.call('data', image_id) image_chunks = self.call('data', image_id)

View File

@ -26,7 +26,6 @@ from oslo_log import log
from oslo_utils import strutils from oslo_utils import strutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
import requests import requests
import sendfile
from ironic.common import exception from ironic.common import exception
from ironic.common.glance_service.image_service import GlanceImageService from ironic.common.glance_service.image_service import GlanceImageService
@ -235,13 +234,11 @@ class FileImageService(BaseImageService):
offset = 0 offset = 0
with open(source_image_path, 'rb') as input_img: with open(source_image_path, 'rb') as input_img:
while offset < filesize: while offset < filesize:
# TODO(kaifeng) Use os.sendfile() and remove sendfile
# dependency when python2 support is dropped.
count = min(SENDFILE_CHUNK_SIZE, filesize - offset) count = min(SENDFILE_CHUNK_SIZE, filesize - offset)
nbytes_out = sendfile.sendfile(image_file.fileno(), nbytes_out = os.sendfile(image_file.fileno(),
input_img.fileno(), input_img.fileno(),
offset, offset,
count) count)
offset += nbytes_out offset += nbytes_out
except Exception as e: except Exception as e:
raise exception.ImageDownloadFailed(image_href=image_href, raise exception.ImageDownloadFailed(image_href=image_href,

View File

@ -228,7 +228,7 @@ class TestGlanceImageService(base.TestCase):
'image contains no data', 'image contains no data',
self.service.download, image_id) self.service.download, image_id)
@mock.patch('sendfile.sendfile', autospec=True) @mock.patch('os.sendfile', autospec=True)
@mock.patch('os.path.getsize', autospec=True) @mock.patch('os.path.getsize', autospec=True)
@mock.patch('%s.open' % __name__, new=mock.mock_open(), create=True) @mock.patch('%s.open' % __name__, new=mock.mock_open(), create=True)
def test_download_file_url(self, mock_getsize, mock_sendfile): def test_download_file_url(self, mock_getsize, mock_sendfile):

View File

@ -21,7 +21,6 @@ from unittest import mock
from oslo_config import cfg from oslo_config import cfg
from oslo_utils import uuidutils from oslo_utils import uuidutils
import requests import requests
import sendfile
from ironic.common import exception from ironic.common import exception
from ironic.common.glance_service import image_service as glance_v2_service from ironic.common.glance_service import image_service as glance_v2_service
@ -447,7 +446,7 @@ class FileImageServiceTestCase(base.TestCase):
remove_mock.assert_called_once_with('file') remove_mock.assert_called_once_with('file')
link_mock.assert_called_once_with(self.href_path, 'file') link_mock.assert_called_once_with(self.href_path, 'file')
@mock.patch.object(sendfile, 'sendfile', return_value=42, autospec=True) @mock.patch.object(os, 'sendfile', return_value=42, autospec=True)
@mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
@mock.patch.object(builtins, 'open', autospec=True) @mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(os, 'access', return_value=False, autospec=True) @mock.patch.object(os, 'access', return_value=False, autospec=True)
@ -470,7 +469,7 @@ class FileImageServiceTestCase(base.TestCase):
input_mock.__enter__().fileno(), input_mock.__enter__().fileno(),
0, 42) 0, 42)
@mock.patch.object(sendfile, 'sendfile', autospec=True) @mock.patch.object(os, 'sendfile', autospec=True)
@mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
@mock.patch.object(builtins, 'open', autospec=True) @mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(os, 'access', return_value=False, autospec=True) @mock.patch.object(os, 'access', return_value=False, autospec=True)
@ -520,8 +519,7 @@ class FileImageServiceTestCase(base.TestCase):
self.assertEqual(2, stat_mock.call_count) self.assertEqual(2, stat_mock.call_count)
access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK) access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK)
@mock.patch.object(sendfile, 'sendfile', side_effect=OSError, @mock.patch.object(os, 'sendfile', side_effect=OSError, autospec=True)
autospec=True)
@mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True)
@mock.patch.object(builtins, 'open', autospec=True) @mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(os, 'access', return_value=False, autospec=True) @mock.patch.object(os, 'access', return_value=False, autospec=True)

View File

@ -14,7 +14,6 @@ ironic-lib>=4.3.0 # Apache-2.0
python-swiftclient>=3.2.0 # Apache-2.0 python-swiftclient>=3.2.0 # Apache-2.0
pytz>=2013.6 # MIT pytz>=2013.6 # MIT
stevedore>=1.20.0 # Apache-2.0 stevedore>=1.20.0 # Apache-2.0
pysendfile>=2.0.0;sys_platform!='win32' # MIT
oslo.concurrency>=4.2.0 # Apache-2.0 oslo.concurrency>=4.2.0 # Apache-2.0
oslo.config>=6.8.0 # Apache-2.0 oslo.config>=6.8.0 # Apache-2.0
oslo.context>=2.19.2 # Apache-2.0 oslo.context>=2.19.2 # Apache-2.0