Skip to content

Commit

Permalink
Forbid use of os.path, point user to tmt._compat.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
happz committed Sep 12, 2024
1 parent fd6a341 commit a85dde5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ builtins-ignorelist = ["help", "format", "input", "filter", "copyright", "max"]
"pathlib.Path".msg = "Use tmt._compat.pathlib.Path instead."
"pathlib.PosixPath".msg = "Use tmt._compat.pathlib.Path instead."
"warnings.deprecated".msg = "Use tmt._compat.warnings.deprecated instead."
"os.path.join".msg = "Use tmt._compat.pathlib.Path and pathlib instead."

[tool.ruff.lint.isort]
known-first-party = ["tmt"]
Expand Down
11 changes: 5 additions & 6 deletions tests/discover/data/distgit_test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from tmt.utils import DistGitHandler
from tmt.utils import DistGitHandler, Path

MOCK_SOURCES_FILENAME = 'mock_sources'
SERVER_PORT = 9000
Expand All @@ -17,14 +16,14 @@ class TestDistGit(DistGitHandler):
usage_name = "TESTING"
server = f"http://localhost:{SERVER_PORT}"

def url_and_name(self, cwd='.'):
with open(os.path.join(cwd, MOCK_SOURCES_FILENAME)) as f:
data = f.read()
def url_and_name(self, cwd=None):
cwd = cwd or Path.cwd()
data = (cwd / MOCK_SOURCES_FILENAME).read_text()
for line in data.splitlines():
split = line.split(' ', maxsplit=2)
url = split[0]
try:
src_name = split[1]
except IndexError:
src_name = url
yield (os.path.join(self.server, url), src_name)
yield (Path(self.server) / url, src_name)
5 changes: 3 additions & 2 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import tmt.cli
import tmt.log
from tests import CliRunner
from tmt.utils import Path

# Prepare path to examples
PATH = os.path.dirname(os.path.realpath(__file__))
PATH = Path(__file__).resolve().parent


def example(name):
""" Return path to given example """
return os.path.join(PATH, "../../examples/", name)
return PATH / "../../examples/" / name


runner = CliRunner()
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import tmt
import tmt.log
import tmt.utils
from tmt.utils import Path

PATH = os.path.dirname(os.path.realpath(__file__))
ROOTDIR = os.path.join(PATH, "../..")
PATH = Path(__file__).resolve().parent
ROOTDIR = PATH.parent.parent

LOGGER = tmt.log.Logger.create(verbose=0, debug=0, quiet=False)

Expand Down Expand Up @@ -44,7 +45,7 @@ def _iter_trees():
# But the code below works, like a charm, and we will need to cover more trees than
# just the root one, so leaving the code here but disabled.
if False:
for dirpath, dirnames, _ in os.walk(os.path.join(ROOTDIR, 'tests')):
for dirpath, dirnames, _ in os.walk(ROOTDIR / 'tests'):
if '.fmf' in dirnames:
yield tmt.Tree(path=dirpath)

Expand Down Expand Up @@ -193,7 +194,7 @@ def test_hw_schema_examples(hw: str, request) -> None:
validate_node(
tree,
node,
os.path.join('provision', 'artemis.yaml'),
Path('provision') / 'artemis.yaml',
'HW requirements',
request.node.callspec.id
)
Expand Down Expand Up @@ -248,7 +249,7 @@ def test_ks_schema_examples(ks: str, request) -> None:
validate_node(
tree,
node,
os.path.join('provision', 'artemis.yaml'),
Path('provision') / 'artemis.yaml',
'Kickstart requirements',
request.node.callspec.id
)
Expand All @@ -272,7 +273,7 @@ def test_watchdog_specification() -> None:
validate_node(
tree,
node,
os.path.join('provision', 'artemis.yaml'),
Path('provision') / 'artemis.yaml',
'Watchdog specification',
'both-wd-options'
)
7 changes: 3 additions & 4 deletions tmt/steps/discover/fmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ def post_dist_git(self, created_content: list[Path]) -> None:
# glob otherwise
if dist_git_extract and dist_git_extract != '/':
try:
dist_git_extract = Path(glob.glob(os.path.join(
sourcedir, dist_git_extract.lstrip('/')))[0])

dist_git_extract = Path(glob.glob(sourcedir / dist_git_extract.lstrip('/'))[0])
except IndexError:
raise tmt.utils.DiscoverError(
f"Couldn't glob '{dist_git_extract}' "
Expand All @@ -673,8 +673,7 @@ def post_dist_git(self, created_content: list[Path]) -> None:
except tmt.utils.MetadataError:
self.warn("No fmf root to remove, there isn't one already.")
if not self.is_dry_run:
shutil.rmtree(os.path.join(
dist_git_extract or extracted_fmf_root, '.fmf'))
shutil.rmtree((dist_git_extract or extracted_fmf_root) / '.fmf')
if not dist_git_extract:
try:
top_fmf_root = tmt.utils.find_fmf_root(sourcedir, ignore_paths=[sourcedir])[0]
Expand Down
27 changes: 12 additions & 15 deletions tmt/steps/provision/mrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import tmt.steps
import tmt.steps.provision
import tmt.utils
from tmt.utils import Command, ProvisionError, ShellScript, UpdatableMessage, field
from tmt.utils import Command, Path, ProvisionError, ShellScript, UpdatableMessage, field

mrack: Any
providers: Any
Expand Down Expand Up @@ -824,28 +824,25 @@ async def __init__(self, guest: 'GuestBeaker') -> None: # type: ignore[misc]

# use global context class
global_context = mrack.context.global_context
mrack_config = ""

if os.path.exists(os.path.join(os.path.dirname(__file__), "mrack/mrack.conf")):
mrack_config = os.path.join(
os.path.dirname(__file__),
"mrack/mrack.conf",
)

if os.path.exists("/etc/tmt/mrack.conf"):
mrack_config = "/etc/tmt/mrack.conf"
mrack_config_locations = [
Path(__file__).parent / "mrack/mrack.conf",
Path("/etc/tmt/mrack.conf"),
Path("~/.mrack/mrack.conf").expanduser(),
Path.cwd() / "mrack.conf"
]

if os.path.exists(os.path.join(os.path.expanduser("~"), ".mrack/mrack.conf")):
mrack_config = os.path.join(os.path.expanduser("~"), ".mrack/mrack.conf")
mrack_config: Optional[Path] = None

if os.path.exists(os.path.join(os.getcwd(), "mrack.conf")):
mrack_config = os.path.join(os.getcwd(), "mrack.conf")
for potential_location in mrack_config_locations:
if potential_location.exists():
mrack_config = potential_location

if not mrack_config:
raise ProvisionError("Configuration file 'mrack.conf' not found.")

try:
global_context.init(mrack_config)
global_context.init(str(mrack_config))
except mrack.errors.ConfigError as mrack_conf_err:
raise ProvisionError(mrack_conf_err)

Expand Down

0 comments on commit a85dde5

Please sign in to comment.