diff --git a/src/main/kotlin/PythonDependencies.kt b/src/main/kotlin/PythonDependencies.kt index 83dc66d..faecd57 100644 --- a/src/main/kotlin/PythonDependencies.kt +++ b/src/main/kotlin/PythonDependencies.kt @@ -585,7 +585,9 @@ internal object PythonDependencies { def top_level_name(path, vendored=False): # the top-level module of a path recorded in RECORD, # like demo of demo/__init__.py, demo.py or demo.cpython-310-x86_64-linux-gnu.so, - # also of demo.libs/ of auditwheel, if vendored, as the demo module loads the shared libraries in it, + # also of demo.libs/ of auditwheel, if vendored, holding the shared libraries the modules load: + # auditwheel names it after the distribution, not the module, like pillow.libs/ of the PIL module of pillow, + # so it is told as a file of an imported module only when the module is named like the distribution, # or None of e.g. ../../bin/ of pip install --target, the dist-info, the data, __pycache__ or a .pth file first, separator, _ = path.partition("/") if separator: @@ -613,13 +615,22 @@ internal object PythonDependencies { except (OSError, ValueError, csv.Error): return set() + # the top-level modules of each dist-info in the dependency directory, by its path, told once, + # as is_imported asks for all the dist-info of a name for each of them + dist_info_top_levels = {} + + def dist_info_top_level(path): + if path not in dist_info_top_levels: + dist_info_top_levels[path] = top_level(metadata.PathDistribution(Path(path))) + return dist_info_top_levels[path] + def is_imported(name, distribution): # whether the distribution in the dependency directory, or another dist-info of its name there, # tells an imported top-level module - candidates = [distribution] + [ - metadata.PathDistribution(Path(path)) for path in dependency_dist_infos.get(normalized_name(name), []) - ] - return any(not imported.isdisjoint(top_level(candidate)) for candidate in candidates) + return not imported.isdisjoint(top_level(distribution)) or any( + not imported.isdisjoint(dist_info_top_level(path)) + for path in dependency_dist_infos.get(normalized_name(name), []) + ) def imported_files(distribution): # the number of the recorded files of the imported top-level modules of the distribution, diff --git a/src/test/kotlin/PythonDependenciesTest.kt b/src/test/kotlin/PythonDependenciesTest.kt index 9d0c6d5..0a03e42 100644 --- a/src/test/kotlin/PythonDependenciesTest.kt +++ b/src/test/kotlin/PythonDependenciesTest.kt @@ -1513,6 +1513,50 @@ class PythonDependenciesTest { } } + @Test + fun `should constrain imported distribution regardless of top-level non-module files named like its module`() { + // given + // the .pth and .pyi files of 2.0 are not files of the replaced_probe module, + // so that they do not tell which version the installed module is of, here e.g. after 1.0 removed them + withDistInfos { directory -> + val modules = mapOf("replaced_probe/__init__.py" to "X = 1\n") + modules.forEach { (path, content) -> directory.resolve(path).createParentDirectories().writeText(content) } + writeDistInfo(directory, "1.0", modules) + writeDistInfo( + directory, "2.0", + modules + mapOf("replaced_probe.pth" to "import os\n", "replaced_probe.pyi" to "X: int\n") + ) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==2.0")) + } + } + + @Test + fun `should constrain imported distribution to version of its installed vendored libraries`() { + // given + // replaced_probe.libs/ of auditwheel holds the shared libraries the replaced_probe module loads, + // named by their hash, so that those of 2.0 are not installed + withDistInfos { directory -> + val modules = mapOf("replaced_probe/__init__.py" to "X = 1\n") + val libraries = mapOf("replaced_probe.libs/libprobe-1a2b3c.so" to "1.0\n") + (modules + libraries).forEach { (path, content) -> + directory.resolve(path).createParentDirectories().writeText(content) + } + writeDistInfo(directory, "1.0", modules + libraries) + writeDistInfo(directory, "2.0", modules + mapOf("replaced_probe.libs/libprobe-4d5e6f.so" to "2.0\n")) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + @Test fun `should constrain imported package of distribution without top_level_txt`() { // given @@ -1972,7 +2016,7 @@ class PythonDependenciesTest { version: String, files: Map = emptyMap(), name: String = "replaced-probe", - // as importlib.metadata.packages_distributions of Python 3.10 tells the modules only by it + // as a distribution built by setuptools tells its top-level modules by it, unlike e.g. one built by hatchling topLevel: String? = "${name.replace('-', '_')}\n", requires: List = emptyList(), directUrl: String? = null, @@ -1990,13 +2034,15 @@ class PythonDependenciesTest { /** The suffix of the extension modules of the Python of the tests, like `.cpython-310-x86_64-linux-gnu.so`. */ private fun extensionSuffix(): String { - val process = ProcessBuilder( - PythonEnvironment.executable, "-c", "import importlib.machinery; print(importlib.machinery.EXTENSION_SUFFIXES[0])" - ).redirectError(ProcessBuilder.Redirect.INHERIT).start() - process.outputStream.close() - val suffix = process.inputStream.bufferedReader().readLine()?.trim()?.ifEmpty { null } - check(process.waitFor(5, TimeUnit.MINUTES) && process.exitValue() == 0) { "The Python did not tell the extension suffix" } - return checkNotNull(suffix) { "No extension suffix of the Python" } + val (exitCode, output, error) = runProcess( + pythonCommand( + PythonEnvironment.executable, + "import importlib.machinery; print(importlib.machinery.EXTENSION_SUFFIXES[0])" + ), + mergeErrorStream = false + ) + check(exitCode == 0 && output.isNotEmpty()) { "The Python did not tell the extension suffix: $error" } + return output } private fun Path.distInfoNames(): List =