From ca36bcacbdd0cf550c493255f82b8b92e3a58f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <karel.koci@nic.cz> Date: Wed, 15 Jul 2020 10:59:20 +0200 Subject: [PATCH] nsfarm: replace os.path with pathlib --- nsfarm/lxd/container.py | 27 ++++++++++++++------------- tests/conftest.py | 6 +++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/nsfarm/lxd/container.py b/nsfarm/lxd/container.py index 9483d43..14e67e9 100644 --- a/nsfarm/lxd/container.py +++ b/nsfarm/lxd/container.py @@ -3,6 +3,7 @@ import os import time import itertools +import pathlib import hashlib import logging import pexpect @@ -12,7 +13,7 @@ from . import _lxd IMAGE_INIT_PATH = "/nsfarm-init.sh" # Where we deploy initialization script for image -IMGS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "imgs") +IMGS_DIR = pathlib.Path(__file__).parents[2] / "imgs" logger = logging.getLogger(__package__) @@ -26,13 +27,13 @@ class Container: self._name = img_name self._internet = internet self._devices = tuple(devices) - self._dir_path = os.path.join(IMGS_DIR, img_name) - self._file_path = self._dir_path + ".sh" + self._dir_path = IMGS_DIR / img_name + self._file_path = self._dir_path.with_suffix(self._dir_path.suffix + ".sh") self._logger = logging.getLogger(f"{__package__}[{img_name}]") # Verify existence of image definition - if not os.path.isfile(self._file_path): + if not self._file_path.is_file(): raise Exception(f"There seems to be no file describing image: {self._file_path}") - if not os.path.isdir(self._dir_path): + if not self._dir_path.is_dir(): self._dir_path = None # Make sure that we are connected to LXD _lxd.connect() @@ -66,20 +67,20 @@ class Container: md5sum.update(file.read()) # Additional nodes from directory if self._dir_path: - nodes = os.listdir(self._dir_path) + nodes = [path for path in self._dir_path.iterdir() if path.is_dir()] while nodes: node = nodes.pop() - path = os.path.join(self._dir_path, node) - md5sum.update(node.encode()) - if os.path.isdir(path): - nodes += [os.path.join(node, nd) for nd in os.listdir(path)] - elif os.path.isfile(path): + path = self._dir_path / node + md5sum.update(str(node).encode()) + if path.is_dir(): + nodes += [path for path in node.iterdir() if path.is_dir()] + elif path.is_file(): # For plain file include content with open(path, "rb") as file: md5sum.update(file.read()) - elif os.path.islink(path): + elif path.is_link(): # For link include its target as well - md5sum.update(os.readlink(path).encode()) + md5sum.update(str(path.resolve()).encode()) return md5sum.hexdigest() def prepare_image(self): diff --git a/tests/conftest.py b/tests/conftest.py index 53be587..07c73ea 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ -import os import time import random import string +import pathlib import configparser import pytest import nsfarm.board @@ -33,8 +33,8 @@ def pytest_configure(config): # TODO handle targets in nsfarm library for central management # Parse target configuration targets = configparser.ConfigParser() - targets.read(os.path.expanduser("~/.nsfarm_targets.ini")) - targets.read(os.path.join(config.rootdir, "targets.ini")) + targets.read(pathlib.Path("~/.nsfarm_targets.ini").expanduser()) + targets.read(pathlib.PurePath(config.rootdir).joinpath("targets.ini")) targets.read(config.getoption("-C") or ()) # Set target configuration target = config.getoption("-T") -- GitLab