Skip to content

Commit

Permalink
Use Paths rather than strings where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 11, 2024
1 parent fbe8d06 commit 725d68e
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 198 deletions.
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def create_tempdir(path: Path = None):
if path.exists():
shutil.rmtree(path)

os.makedirs(str(path))
path.mkdir(parents=True)

try:
yield path
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_activehdl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _test_compile_project_vhdl(self, standard, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
f"-{standard}",
"-work",
"lib",
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_compile_project_vhdl_extra_flags(self, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"custom",
"flags",
"-2008",
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_supports_vhdl_package_generics_false(self, find_prefix):
self.assertFalse(simif.supports_vhdl_package_generics())

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_activehdl_out")
self.output_path = Path(__file__).parent / "test_activehdl_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
34 changes: 17 additions & 17 deletions tests/unit/test_incisive_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,12 @@ def test_simulate_vhdl(self, run_command, find_cds_root_irun, find_cds_root_virt
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -602,12 +602,12 @@ def test_simulate_verilog(self, run_command, find_cds_root_irun, find_cds_root_v
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -681,12 +681,12 @@ def test_simulate_extra_flags(self, run_command, find_cds_root_irun, find_cds_ro
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -719,12 +719,12 @@ def test_simulate_generics_and_parameters(self, run_command, find_cds_root_irun,
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -755,12 +755,12 @@ def test_simulate_hdlvar(self, run_command, find_cds_root_irun, find_cds_root_vi
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -788,7 +788,7 @@ def test_elaborate(self, run_command, find_cds_root_irun, find_cds_root_virtuoso
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
)
]
Expand Down Expand Up @@ -836,7 +836,7 @@ def test_elaborate_fail(self, run_command, find_cds_root_irun, find_cds_root_vir
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
)
]
Expand All @@ -861,12 +861,12 @@ def test_simulate_fail(self, run_command, find_cds_root_irun, find_cds_root_virt
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -899,12 +899,12 @@ def test_simulate_gui(self, run_command, find_cds_root_irun, find_cds_root_virtu
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -974,7 +974,7 @@ def test_configuration_and_entity_selection(self, find_cds_root_irun, find_cds_r
self.assertEqual(simif._select_vhdl_top(config), "lib.tb_entity:arch") # pylint: disable=protected-access

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_incisive_out")
self.output_path = Path(__file__).parent / "test_incisive_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_modelsim_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ def test_overwrites_modelsim_ini_file_from_user(self):
def setUp(self):
self.test_path = str(Path(__file__).parent / "test_modelsim_out")

self.output_path = str(Path(self.test_path) / "modelsim")
self.output_path = Path(self.test_path) / "modelsim"
self.prefix_path = str(Path(self.test_path) / "prefix" / "bin")
renew_path(self.test_path)
renew_path(self.output_path)
renew_path(self.prefix_path)
installed_modelsim_ini = str(Path(self.prefix_path) / ".." / "modelsim.ini")
write_file(installed_modelsim_ini, "[Library]")
self.project = Project()
self.cwd = os.getcwd()
self.cwd = Path(os.getcwd())
os.chdir(self.test_path)

def tearDown(self):
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_rivierapro_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_compile_project_vhdl_2019(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2019",
"-work",
"lib",
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_compile_project_vhdl_2008(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2008",
"-work",
"lib",
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_compile_project_vhdl_2002(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2002",
"-work",
"lib",
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_compile_project_vhdl_93(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-93",
"-work",
"lib",
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_compile_project_vhdl_extra_flags(self, _find_prefix, process, check_out
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"custom",
"flags",
"-2008",
Expand Down Expand Up @@ -385,7 +385,7 @@ def test_compile_project_verilog_define(self, _find_prefix, process, check_outpu
)

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_rivierapro_out")
self.output_path = Path(__file__).parent / "test_rivierapro_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ def test_get_output_path_on_linux(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

output_path = "output_path"
test_name = "123._-+"
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

output_path = "output_path"
Expand All @@ -163,7 +163,7 @@ def test_get_output_path_on_linux(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (safe_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (safe_name + "_" + hash_string(test_name)),
)

def test_get_output_path_on_windows(self):
Expand All @@ -175,15 +175,15 @@ def test_get_output_path_on_windows(self):
with mock.patch("os.environ", new={}):
test_name = "_" * 400
test_output = runner._get_output_path(test_name)
self.assertEqual(len(test_output), 260 - 100 + 1)
self.assertEqual(len(str(test_output)), 260 - 100 + 1)

with mock.patch("os.environ", new={"VUNIT_TEST_OUTPUT_PATH_MARGIN": "-1000"}):
output_path = "output_path"
test_name = "_" * 400
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

with mock.patch("os.environ", new={"VUNIT_SHORT_TEST_OUTPUT_PATHS": ""}):
Expand All @@ -192,7 +192,7 @@ def test_get_output_path_on_windows(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / hash_string(test_name)),
Path(output_path).resolve() / hash_string(test_name),
)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_verilog_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def write_file(self, file_name, contents):
full_name = Path(self.output_path) / file_name
full_path = full_name.parent
if not full_path.exists():
os.makedirs(str(full_path))
full_path.mkdir(parents=True)
with full_name.open("w") as fptr:
fptr.write(contents)

Expand Down
19 changes: 9 additions & 10 deletions vunit/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pathlib import Path
import os
import pickle
import io
import struct
from vunit.ostools import renew_path

Expand Down Expand Up @@ -55,7 +54,7 @@ def _discover_nodes(self):
"""
keys_to_nodes = {}
for file_base_name in os.listdir(self._path):
key = self._read_key(str(Path(self._path) / file_base_name))
key = self._read_key(Path(self._path) / file_base_name)
assert key not in keys_to_nodes # Two nodes contains the same key
keys_to_nodes[key] = int(file_base_name)
return keys_to_nodes
Expand All @@ -70,37 +69,37 @@ def _read_key_from_fptr(fptr):
key = fptr.read(key_size)
return key

def _read_key(self, file_name):
def _read_key(self, file_name: Path):
"""
Read key found in file_name
"""
with io.open(file_name, "rb") as fptr:
with file_name.open("rb") as fptr:
return self._read_key_from_fptr(fptr)

def _read_data(self, file_name):
def _read_data(self, file_name: Path):
"""
Read key found in file_name
"""
with io.open(file_name, "rb") as fptr:
with file_name.open("rb") as fptr:
self._read_key_from_fptr(fptr)
data = fptr.read()
return data

@staticmethod
def _write_node(file_name, key, value):
def _write_node(file_name: Path, key, value):
"""
Write node to file
"""
with io.open(file_name, "wb") as fptr:
with file_name.open("wb") as fptr:
fptr.write(struct.pack("I", len(key)))
fptr.write(key)
fptr.write(value)

def _to_file_name(self, key):
def _to_file_name(self, key) -> Path:
"""
Convert key to file name
"""
return str(Path(self._path) / str(self._keys_to_nodes[key]))
return Path(self._path) / str(self._keys_to_nodes[key])

def _allocate_node_for_key(self, key):
"""
Expand Down
7 changes: 4 additions & 3 deletions vunit/ostools.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,17 @@ def renew_path(path):
http://stackoverflow.com/questions/27625683/can-anyone-explain-this-weird-behaviour-of-shutil-rmtree-and-shutil-copytree
"""
path = Path(path)
if IS_WINDOWS_SYSTEM:
retries = 10
while retries > 0 and Path(path).exists():
while retries > 0 and path.exists():
shutil.rmtree(path, ignore_errors=retries > 1)
time.sleep(0.01)
retries -= 1
else:
if Path(path).exists():
if path.exists():
shutil.rmtree(path)
makedirs(path)
path.mkdir(parents=True)


def simplify_path(path):
Expand Down
Loading

0 comments on commit 725d68e

Please sign in to comment.