Use py3 pathlib in DibImageFile

The pathlib library was introduced in python 3.4 and is a more
concise way to deal with paths versus the overburdened os lib.

Change-Id: I468c4aa0a41f07c0b0009c2c387712b001a03e5d
This commit is contained in:
David Shrewsbury 2019-05-20 14:31:12 -04:00
parent 6182f7bc3a
commit d0bb3d004d
2 changed files with 34 additions and 19 deletions

View File

@ -25,6 +25,8 @@ import time
import shlex
import uuid
from pathlib import Path
from nodepool import config as nodepool_config
from nodepool import exceptions
from nodepool import provider_manager
@ -63,55 +65,60 @@ class DibImageFile(object):
self.sha256 = None
self.sha256_file = None
# File extension is compared to image type (sans '.') so we
# store it the same way.
if self.extension:
self.extension = self.extension.lstrip('.')
@staticmethod
def from_path(path):
image_file = os.path.basename(path)
image_id, extension = image_file.rsplit('.', 1)
return DibImageFile(image_id, extension)
if isinstance(path, str):
path = Path(path)
return DibImageFile(path.stem, path.suffix)
@staticmethod
def from_image_id(images_dir, image_id):
images = []
for image_filename in os.listdir(images_dir):
if os.path.isfile(os.path.join(images_dir, image_filename)):
image = DibImageFile.from_path(image_filename)
for image_file in Path(images_dir).iterdir():
if image_file.is_file():
image = DibImageFile.from_path(image_file)
if image.image_id == image_id:
images.append(image)
return images
@staticmethod
def from_images_dir(images_dir):
return [DibImageFile.from_path(x) for x in os.listdir(images_dir)]
return [DibImageFile.from_path(x) for x in Path(images_dir).iterdir()]
def to_path(self, images_dir, with_extension=True):
my_path = os.path.join(images_dir, self.image_id)
my_path = Path(images_dir) / self.image_id
if with_extension:
if self.extension is None:
raise exceptions.BuilderError(
'Cannot specify image extension of None'
)
my_path += '.' + self.extension
my_path = my_path.with_suffix('.' + self.extension)
md5_path = '%s.%s' % (my_path, 'md5')
# Path.with_suffix() will replace an existing suffix, so we create
# new Path objects from strings for the checksum files.
md5_path = Path(str(my_path) + '.md5')
md5 = self._checksum(md5_path)
if md5:
self.md5_file = md5_path
self.md5_file = str(md5_path)
self.md5 = md5[0:32]
sha256_path = '%s.%s' % (my_path, 'sha256')
sha256_path = Path(str(my_path) + '.sha256')
sha256 = self._checksum(sha256_path)
if sha256:
self.sha256_file = sha256_path
self.sha256_file = str(sha256_path)
self.sha256 = sha256[0:64]
return my_path
return str(my_path)
def _checksum(self, filename):
if not os.path.isfile(filename):
return None
with open(filename, 'r') as f:
data = f.read()
return data
if filename.is_file():
return filename.read_text()
return None
class BaseWorker(threading.Thread):

View File

@ -19,6 +19,8 @@ import fixtures
import mock
import time
from pathlib import Path
from nodepool import builder, exceptions, tests
from nodepool.driver.fake import provider as fakeprovider
from nodepool import zk
@ -31,6 +33,12 @@ class TestNodepoolBuilderDibImage(tests.BaseTestCase):
self.assertEqual(image.image_id, 'myid1234')
self.assertEqual(image.extension, 'qcow2')
def test_from_path_with_Path(self):
image = builder.DibImageFile.from_path(
Path('/foo/bar/myid1234.qcow2'))
self.assertEqual(image.image_id, 'myid1234')
self.assertEqual(image.extension, 'qcow2')
def test_from_image_id(self):
tempdir = fixtures.TempDir()
self.useFixture(tempdir)