Issue #3453 fix. ghidraDev should lookup pydev pysrc directory by bundle path

This commit is contained in:
Zeck 2021-09-26 22:37:42 -05:00 committed by Z
parent 6dd6486627
commit 2a55b5af29

View file

@ -17,6 +17,8 @@ package ghidradev.ghidraprojectcreator.utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -28,12 +30,15 @@ import java.util.stream.Stream;
import javax.naming.OperationNotSupportedException; import javax.naming.OperationNotSupportedException;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IJavaProject;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import ghidradev.Activator; import ghidradev.Activator;
@ -158,25 +163,20 @@ public class PyDevUtils {
* @throws CoreException if there was a problem searching for the PyDev source directory. * @throws CoreException if there was a problem searching for the PyDev source directory.
*/ */
public static File getPyDevSrcDir() throws CoreException { public static File getPyDevSrcDir() throws CoreException {
String eclipsePath = Platform.getInstallLocation().getURL().getFile(); Bundle[] bundles = FrameworkUtil.getBundle(PyDevUtilsInternal.class).getBundleContext().getBundles();
List<File> searchDirs = new ArrayList<>(); Bundle pydevCoreBundle = Stream.of(bundles).filter(bundle -> bundle.getSymbolicName().contains("org.python.pydev.core")).findFirst().orElse(null);
searchDirs.add(new File(eclipsePath, "plugins"));
searchDirs.add(new File(eclipsePath, "dropins"));
for (File searchRoot : searchDirs) { if(pydevCoreBundle != null) {
try (Stream<Path> paths = Files.walk(Paths.get(searchRoot.toURI()))) { try {
Optional<File> pysrcDir = paths.filter( URL pydevDirUrl = FileLocator.toFileURL(pydevCoreBundle.getEntry("/"));
Files::isDirectory) URI pydevDirUri = new URI(pydevDirUrl.getProtocol(), pydevDirUrl.getPath(), null).normalize();
.filter(p -> p.endsWith("pysrc")) Path pysrcDir = Paths.get(pydevDirUri).resolve("pysrc");
.map(p -> p.toFile()) if (Files.exists(pysrcDir)) {
.filter(f -> f.getParentFile().getName().startsWith("org.python.pydev")) return pysrcDir.toFile();
.findFirst();
if (pysrcDir.isPresent()) {
return pysrcDir.get();
} }
} }
catch (IOException e) { catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
IStatus.ERROR, "Problem searching for PyDev source directory", e)); IStatus.ERROR, "Problem searching for PyDev source directory", e));
} }