8
8
import sys
9
9
from enum import Enum
10
10
from pathlib import Path
11
+ from typing import Union
11
12
12
13
import click
13
14
@@ -35,7 +36,15 @@ def _meson_cli():
35
36
return [meson_cli ]
36
37
37
38
38
- def _editable_install_path (distname ):
39
+ def editable_install_path (distname : str ) -> Union [str , None ]:
40
+ """Return path of the editable install for package `distname`.
41
+
42
+ If the package is not an editable install, return None.
43
+
44
+ See Also
45
+ --------
46
+ is_editable_install
47
+ """
39
48
import importlib_metadata
40
49
41
50
try :
@@ -57,16 +66,34 @@ def _editable_install_path(distname):
57
66
return None
58
67
59
68
60
- def _is_editable_install (distname ):
61
- return _editable_install_path (distname ) is not None
69
+ # backward compat
70
+ _editable_install_path = editable_install_path
71
+
72
+
73
+ def is_editable_install (distname : str , verify_path : bool = False ) -> bool :
74
+ """Whether or not an editable install of `distname` is present.
75
+
76
+ Parameters
77
+ ----------
78
+ `distname` : str
79
+ Name of the package. E.g., ``numpy`` or ``scikit-image``.
80
+ Not always the same as the module name (``numpy`` and
81
+ ``skimage`` for the above).
82
+ """
83
+ return editable_install_path (distname ) is not None
84
+
85
+
86
+ # backward compat
87
+ _is_editable_install = is_editable_install
62
88
63
89
64
- def _is_editable_install_of_same_source (distname ):
65
- editable_path = _editable_install_path (distname )
66
- return editable_path and os .path .samefile (_editable_install_path (distname ), "." )
90
+ def _is_editable_install_of_same_source (distname : str ) -> bool :
91
+ """Check whether the editable install was made from the current directory."""
92
+ editable_path = editable_install_path (distname )
93
+ return (editable_path is not None ) and os .path .samefile (editable_path , "." )
67
94
68
95
69
- def _set_pythonpath (build_dir , quiet = False ):
96
+ def _set_pythonpath (build_dir : str , quiet : bool = False ) -> str :
70
97
"""Set first entry of PYTHONPATH to site packages directory.
71
98
72
99
For editable installs, leave the PYTHONPATH alone.
@@ -78,7 +105,7 @@ def _set_pythonpath(build_dir, quiet=False):
78
105
cfg = get_config ()
79
106
distname = cfg .get ("project.name" , None )
80
107
if distname :
81
- if _is_editable_install (distname ):
108
+ if is_editable_install (distname ):
82
109
if _is_editable_install_of_same_source (distname ):
83
110
if not (quiet ):
84
111
click .secho (
@@ -114,11 +141,11 @@ def _set_pythonpath(build_dir, quiet=False):
114
141
return site_packages
115
142
116
143
117
- def _get_install_dir (build_dir ) :
144
+ def _get_install_dir (build_dir : str ) -> str :
118
145
return f"{ build_dir } -install"
119
146
120
147
121
- def _get_site_packages (build_dir ) :
148
+ def _get_site_packages (build_dir : str ) -> str :
122
149
install_dir = _get_install_dir (build_dir )
123
150
try :
124
151
cfg = get_config ()
@@ -140,13 +167,12 @@ def _get_site_packages(build_dir):
140
167
site_packages = None
141
168
if any (f"python{ X } ." in p for p in candidate_paths ):
142
169
# We have a system that uses `python3.X/site-packages` or `python3.X/dist-packages`
143
- site_packages = [p for p in candidate_paths if f"python{ X } .{ Y } " in p ]
144
- if len (site_packages ) == 0 :
170
+ site_packages_paths = [p for p in candidate_paths if f"python{ X } .{ Y } " in p ]
171
+ if len (site_packages_paths ) == 0 :
145
172
raise FileNotFoundError (
146
173
f"No site-packages found in { install_dir } for Python { X } .{ Y } "
147
174
)
148
- else :
149
- site_packages = site_packages [0 ]
175
+ site_packages = site_packages_paths [0 ]
150
176
else :
151
177
# A naming scheme that does not encode the Python major/minor version is used, so return
152
178
# whatever site-packages path was found
@@ -165,22 +191,22 @@ def _get_site_packages(build_dir):
165
191
return site_packages
166
192
167
193
168
- def _meson_version ():
194
+ def _meson_version () -> Union [ str , None ] :
169
195
try :
170
196
p = _run (_meson_cli () + ["--version" ], output = False , echo = False )
171
197
return p .stdout .decode ("ascii" ).strip ()
172
198
except :
173
- pass
199
+ return None
174
200
175
201
176
- def _meson_version_configured (build_dir ) :
202
+ def _meson_version_configured (build_dir : str ) -> Union [ str , None ] :
177
203
try :
178
204
meson_info_fn = os .path .join (build_dir , "meson-info" , "meson-info.json" )
179
205
with open (meson_info_fn ) as f :
180
206
meson_info = json .load (f )
181
207
return meson_info ["meson_version" ]["full" ]
182
208
except :
183
- pass
209
+ return None
184
210
185
211
186
212
def _meson_coverage_configured () -> bool :
@@ -199,7 +225,7 @@ def _meson_coverage_configured() -> bool:
199
225
return False
200
226
201
227
202
- def _check_coverage_tool_installation (coverage_type : GcovReportFormat , build_dir ):
228
+ def _check_coverage_tool_installation (coverage_type : GcovReportFormat , build_dir : str ):
203
229
requirements = { # https://github.com/mesonbuild/meson/blob/6e381714c7cb15877e2bcaa304b93c212252ada3/docs/markdown/Unit-tests.md?plain=1#L49-L62
204
230
GcovReportFormat .html : ["Gcovr/GenHTML" , "lcov" ],
205
231
GcovReportFormat .xml : ["Gcovr (version 3.3 or higher)" ],
0 commit comments