Skip to content

Commit 66bc8b3

Browse files
committed
WIP
1 parent 98afc88 commit 66bc8b3

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
@@ -11,6 +11,7 @@
1111
import time
1212
from collections import OrderedDict
1313
from functools import cmp_to_key
14+
from typing import Iterable, Optional, Tuple, Union
1415

1516
from . import util
1617
from .coq_version import (
@@ -182,7 +183,20 @@ def os_path_isfile(filename):
182183
return os.path.isfile(filename)
183184

184185

185-
def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_dir=None, **kwargs):
186+
@memoize
187+
def Path_isfile(filename: Path) -> bool:
188+
return filename.is_file()
189+
190+
191+
def get_loadpath(
192+
coqc,
193+
coqc_args,
194+
*,
195+
coqc_is_coqtop: bool = False,
196+
verbose_base: int = 1,
197+
base_dir: Optional[Union[str, Path]] = None,
198+
**kwargs
199+
) -> Iterable[Tuple[Path, str]]:
186200
kwargs = fill_kwargs(kwargs)
187201
output, cmds, retcode, runtime = get_coq_output(
188202
coqc,
@@ -217,7 +231,7 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
217231
kwargs["log"](f"WARNING: Print LoadPath has extra: {extra!r}", level=LOG_ALWAYS)
218232
for logical, physical in reg.findall(output):
219233
logical = logical.strip(" \r\n")
220-
physical = physical.strip(" \r\n")
234+
physical = Path(physical.strip(" \r\n"))
221235
if logical == "<>":
222236
logical = ""
223237
yield (physical, logical)
@@ -228,35 +242,34 @@ def get_loadpath(coqc, coqc_args, *, coqc_is_coqtop=False, verbose_base=1, base_
228242
yield (physical, "Coq." + logical[len("Stdlib.") :])
229243

230244

231-
def filenames_of_lib_helper(lib, non_recursive_libnames, ext):
245+
def filenames_of_lib_helper(lib: str, non_recursive_libnames: Iterable[Tuple[Path, str]], ext: str):
232246
for physical_name, logical_name in list(non_recursive_libnames):
233247
# logical_name = libname_with_dot(logical_name)
234248
*libprefix, lib = lib.split(".")
235249
if ".".join(libprefix) == logical_name:
236-
cur_lib = os.path.join(physical_name, lib)
237-
# TODO HERE USE pathlib
238-
yield fix_path(os.path.relpath(os.path.normpath(cur_lib + ext), "."))
250+
cur_lib = physical_name / lib
251+
yield cur_lib.with_suffix(ext)
239252

240253

241254
@memoize
242-
def filename_of_lib_helper(lib, non_recursive_libnames, ext, base_dir):
255+
def filename_of_lib_helper(
256+
lib: str, non_recursive_libnames: Iterable[Tuple[Path, str]], ext: str, base_dir: Optional[Union[Path, str]]
257+
):
243258
filenames = list(filenames_of_lib_helper(lib, non_recursive_libnames, ext))
244-
existing_filenames = [f for f in filenames if os_path_isfile(f) or os_path_isfile(os.path.splitext(f)[0] + ".v")]
259+
existing_filenames = [f for f in filenames if Path_isfile(f) or Path_isfile(f.with_suffix(".v"))]
245260
if len(existing_filenames) > 0:
246261
retval = existing_filenames[0]
247262
if len(set(existing_filenames)) == 1:
248263
return retval
249264
else:
250265
DEFAULT_LOG(
251-
"WARNING: Multiple physical paths match logical path %s: %s. Selecting %s."
252-
% (lib, ", ".join(sorted(set(existing_filenames))), retval),
266+
f"WARNING: Multiple physical paths match logical path {lib}: {', '.join(sorted(set(existing_filenames)))}. Selecting {retval}.",
253267
level=LOG_ALWAYS,
254268
)
255269
return retval
256270
if len(filenames) != 0:
257271
DEFAULT_LOG(
258-
"WARNING: One or more physical paths match logical path %s, but none of them exist: %s"
259-
% (lib, ", ".join(filenames)),
272+
f"WARNING: One or more physical paths match logical path {lib}, but none of them exist: {', '.join(filenames)}.",
260273
level=LOG_ALWAYS,
261274
)
262275
if len(filenames) > 0:

0 commit comments

Comments
 (0)