Skip to content

Commit 0c3e2ee

Browse files
committed
WIP
1 parent ef40067 commit 0c3e2ee

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

coq_tools/import_util.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import time
1313
from collections import OrderedDict
1414
from functools import cmp_to_key
15+
from typing import Iterable, Optional, Tuple, Union
1516

1617
from . import util
1718
from .coq_version import (
@@ -184,7 +185,20 @@ def os_path_isfile(filename):
184185
return os.path.isfile(filename)
185186

186187

187-
def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_dir=None, **kwargs):
188+
@memoize
189+
def Path_isfile(filename: Path) -> bool:
190+
return filename.is_file()
191+
192+
193+
def get_loadpath(
194+
coqc,
195+
coqc_args,
196+
*,
197+
coqc_is_coqtop: bool = False,
198+
verbose_base: int = 1,
199+
base_dir: Optional[Union[str, Path]] = None,
200+
**kwargs
201+
) -> Iterable[Tuple[Path, str]]:
188202
kwargs = fill_kwargs(kwargs)
189203
output, cmds, retcode, runtime = get_coq_output(
190204
coqc,
@@ -219,7 +233,7 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
219233
kwargs["log"](f"WARNING: Print LoadPath has extra: {extra!r}", level=LOG_ALWAYS)
220234
for logical, physical in reg.findall(output):
221235
logical = logical.strip(" \r\n")
222-
physical = physical.strip(" \r\n")
236+
physical = Path(physical.strip(" \r\n"))
223237
if logical == "<>":
224238
logical = ""
225239
yield (physical, logical)
@@ -230,35 +244,34 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
230244
yield (physical, "Coq." + logical[len("Stdlib.") :])
231245

232246

233-
def filenames_of_lib_helper(lib, non_recursive_libnames, ext):
247+
def filenames_of_lib_helper(lib: str, non_recursive_libnames: Iterable[Tuple[Path, str]], ext: str):
234248
for physical_name, logical_name in list(non_recursive_libnames):
235249
# logical_name = libname_with_dot(logical_name)
236250
*libprefix, lib = lib.split(".")
237251
if ".".join(libprefix) == logical_name:
238-
cur_lib = os.path.join(physical_name, lib)
239-
# TODO HERE USE pathlib
240-
yield fix_path(os.path.relpath(os.path.normpath(cur_lib + ext), "."))
252+
cur_lib = physical_name / lib
253+
yield cur_lib.with_suffix(ext)
241254

242255

243256
@memoize
244-
def filename_of_lib_helper(lib, non_recursive_libnames, ext, base_dir):
257+
def filename_of_lib_helper(
258+
lib: str, non_recursive_libnames: Iterable[Tuple[Path, str]], ext: str, base_dir: Optional[Union[Path, str]]
259+
):
245260
filenames = list(filenames_of_lib_helper(lib, non_recursive_libnames, ext))
246-
existing_filenames = [f for f in filenames if os_path_isfile(f) or os_path_isfile(os.path.splitext(f)[0] + ".v")]
261+
existing_filenames = [f for f in filenames if Path_isfile(f) or Path_isfile(f.with_suffix(".v"))]
247262
if len(existing_filenames) > 0:
248263
retval = existing_filenames[0]
249264
if len(set(existing_filenames)) == 1:
250265
return retval
251266
else:
252267
DEFAULT_LOG(
253-
"WARNING: Multiple physical paths match logical path %s: %s. Selecting %s."
254-
% (lib, ", ".join(sorted(set(existing_filenames))), retval),
268+
f"WARNING: Multiple physical paths match logical path {lib}: {', '.join(sorted(set(existing_filenames)))}. Selecting {retval}.",
255269
level=LOG_ALWAYS,
256270
)
257271
return retval
258272
if len(filenames) != 0:
259273
DEFAULT_LOG(
260-
"WARNING: One or more physical paths match logical path %s, but none of them exist: %s"
261-
% (lib, ", ".join(filenames)),
274+
f"WARNING: One or more physical paths match logical path {lib}, but none of them exist: {', '.join(filenames)}.",
262275
level=LOG_ALWAYS,
263276
)
264277
if len(filenames) > 0:

0 commit comments

Comments
 (0)