From 01704badc2d67cc08bd0e019c2279f601d8df0d9 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sun, 10 Nov 2024 10:26:33 -0300 Subject: [PATCH] Refactor structure to separate helper to compute what folder is part of the interpreter or project and fix test. --- .../DefaultPathsForInterpreterInfo.java | 2 +- .../interpreter_managers/InterpreterInfo.java | 67 ++++++++++++------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/DefaultPathsForInterpreterInfo.java b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/DefaultPathsForInterpreterInfo.java index 567f6743ff..c926370745 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/DefaultPathsForInterpreterInfo.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/DefaultPathsForInterpreterInfo.java @@ -49,7 +49,7 @@ public boolean forceDeselect(String data) { return isRootPath(data, rootPaths); } - public boolean exists(String data) { + public static boolean exists(String data) { return new File(data).exists(); } diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/InterpreterInfo.java b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/InterpreterInfo.java index 6283a270a5..26bd3a4686 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/InterpreterInfo.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/interpreter_managers/InterpreterInfo.java @@ -314,6 +314,45 @@ public int hashCode() { return this.executableOrJar.hashCode(); } + public static class HelperToComputeIfPathIsInInterpreter { + + Set pythonHomePaths = new HashSet<>(); + private DefaultPathsForInterpreterInfo defaultPaths; + + public void setPythonExecutable(String infoExecutable) { + File pythonExecutableFile = new File(infoExecutable); + File pythonHome = pythonExecutableFile.getParentFile(); + if (pythonHome != null) { + if (new File(pythonHome, "pyenv.cfg").exists()) { + pythonHome = pythonHome.getParentFile(); + } + pythonHomePaths.add(Path.fromOSString(pythonHome.toString())); + } + + } + + public HelperToComputeIfPathIsInInterpreter() { + boolean resolvingInterpreter = true; + defaultPaths = new DefaultPathsForInterpreterInfo( + resolvingInterpreter); + + } + + public boolean isInterpreterPath(String data) { + if (!defaultPaths.forceDeselect(data)) { // It's directly a project source folder (don't add it). + // If it's site-package, in python home or not under a source folder add it. + if (data.contains("site-packages") + || (DefaultPathsForInterpreterInfo + .isChildOfRootPath(data, pythonHomePaths)) + || defaultPaths.selectByDefault(data)) { + return true; + } + } + return false; + } + + } + /** * * @param received @@ -372,11 +411,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa List predefinedPaths = new ArrayList(); Properties stringSubstitutionVars = new Properties(); - boolean resolvingInterpreter = true; - DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo( - resolvingInterpreter); - - Set pythonHomePaths = new HashSet<>(); + HelperToComputeIfPathIsInInterpreter helper = new HelperToComputeIfPathIsInInterpreter(); for (int j = 0; j < xmlNodes.getLength(); j++) { Node xmlChild = xmlNodes.item(j); @@ -390,14 +425,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa } else if ("executable".equals(name)) { infoExecutable = data; - - // Handling of python inside of project. - File pythonExecutableFile = new File(infoExecutable); - File pythonHome = pythonExecutableFile.getParentFile(); - if (new File(pythonHome, "pyenv.cfg").exists()) { - pythonHome = pythonHome.getParentFile(); - } - pythonHomePaths.add(Path.fromOSString(pythonHome.toString())); + helper.setPythonExecutable(infoExecutable); } else if ("vmArgs".equals(name)) { infoVmArgs = data; @@ -413,7 +441,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa Node pathIncludeItem = attributes.getNamedItem("path"); if (pathIncludeItem != null) { - if (defaultPaths.exists(data)) { + if (DefaultPathsForInterpreterInfo.exists(data)) { //The python backend is expected to put path='ins' or path='out' //While our own toString() is not expected to do that. //This is probably not a very good heuristic, but it maps the current state of affairs. @@ -422,16 +450,9 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa toAsk.add(data); } - if (!defaultPaths.forceDeselect(data)) { // It's directly a project source folder (don't add it). - // If it's site-package, in python home or not under a source folder add it. - if (data.contains("site-packages") - || (DefaultPathsForInterpreterInfo - .isChildOfRootPath(data, pythonHomePaths)) - || defaultPaths.selectByDefault(data)) { - selection.add(data); - } + if (helper.isInterpreterPath(data)) { + selection.add(data); } - } } else {