2
* Created on May 11, 2005
4
* @author Fabio Zadrozny
6
package org.python.pydev.ui.pythonpathconf;
9
import java.io.FileFilter;
10
import java.io.Serializable;
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Iterator;
14
import java.util.List;
16
import java.util.TreeSet;
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.python.pydev.core.IInterpreterInfo;
20
import org.python.pydev.core.REF;
21
import org.python.pydev.editor.codecompletion.revisited.PythonPathHelper;
22
import org.python.pydev.editor.codecompletion.revisited.SystemModulesManager;
23
import org.python.pydev.plugin.PydevPlugin;
26
public class InterpreterInfo implements Serializable, IInterpreterInfo{
29
* check note on http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/version.html#6678
31
* changed to 10L with the release 1.0
33
private static final long serialVersionUID = 10L;
36
* For jython, this is the jython.jar
38
* For python, this is the path to the python executable
40
public String executableOrJar;
43
* folders - they should be passed to the pythonpath
45
public java.util.List<String> libs = new ArrayList<String>();
48
* Those libraries are not really used in python (they are found in the system pythonpath), and
49
* don't need to be passed in the pythonpath (they are only here so that the user can see
52
* files: .pyd, .dll, etc.
54
* for jython, the jars should appear here.
56
public java.util.List<String> dllLibs = new ArrayList<String>();
59
* __builtin__, os, math, etc for python
61
* check sys.builtin_module_names and others that should
62
* be forced to use code completion as builtins, such os, math, etc.
64
* for jython, this should
66
public Set<String> forcedLibs = new TreeSet<String>();
69
* module management for the system is always binded to an interpreter (binded in this class)
71
* The modules manager is no longer persisted. It is restored from a separate file, because we do
72
* not want to keep it in the 'configuration', as a giant Base64 string.
74
public SystemModulesManager modulesManager = new SystemModulesManager(forcedLibs);
76
public InterpreterInfo(String exe, Collection<String> libs0){
77
this.executableOrJar = exe;
81
public InterpreterInfo(String exe, Collection<String> libs0, Collection<String> dlls){
86
public InterpreterInfo(String exe, List<String> libs0, List<String> dlls, List<String> forced) {
87
this(exe, libs0, dlls);
88
forcedLibs.addAll(forced);
92
* @see java.lang.Object#equals(java.lang.Object)
94
public boolean equals(Object o) {
95
if (!(o instanceof InterpreterInfo)){
99
InterpreterInfo info = (InterpreterInfo) o;
100
if(info.executableOrJar.equals(this.executableOrJar) == false){
104
if(info.libs.equals(this.libs) == false){
108
if(info.dllLibs.equals(this.dllLibs) == false){
112
if(info.forcedLibs.equals(this.forcedLibs) == false){
120
* Format we receive should be:
122
* Executable:python.exe|lib1|lib2|lib3@dll1|dll2|dll3$forcedBuitin1|forcedBuiltin2
126
public static InterpreterInfo fromString(String received) {
127
if(received.toLowerCase().indexOf("executable") == -1){
128
throw new RuntimeException("Unable to recreate the Interpreter info (Its format changed. Please, re-create your Interpreter information)");
130
received = received.replaceAll("\n", "").replaceAll("\r", "");
131
String[] forcedSplit = received.split("\\$");
132
String[] libsSplit = forcedSplit[0].split("\\@");
133
String exeAndLibs = libsSplit[0];
137
String[] exeAndLibs1 = exeAndLibs.split("\\|");
138
String executable = exeAndLibs1[0].substring(exeAndLibs1[0].indexOf(":")+1, exeAndLibs1[0].length());
139
ArrayList<String> l = new ArrayList<String>();
140
for (int i = 1; i < exeAndLibs1.length; i++) { //start at 1 (o is exe)
141
String trimmed = exeAndLibs1[i].trim();
142
if(trimmed.length() > 0){
147
ArrayList<String> l1 = new ArrayList<String>();
148
if(libsSplit.length > 1){
149
String dllLibs = libsSplit[1];
150
String[] dllLibs1 = dllLibs.split("\\|");
151
for (int i = 0; i < dllLibs1.length; i++) {
152
String trimmed = dllLibs1[i].trim();
153
if(trimmed.length() > 0){
159
ArrayList<String> l2 = new ArrayList<String>();
160
if(forcedSplit.length > 1){
161
String forcedLibs = forcedSplit[1];
162
String[] forcedLibs1 = forcedLibs.split("\\|");
163
for (int i = 0; i < forcedLibs1.length; i++) {
164
String trimmed = forcedLibs1[i].trim();
165
if(trimmed.length() > 0){
170
return new InterpreterInfo(executable, l, l1, l2);
175
* @see java.lang.Object#toString()
177
public String toString() {
178
StringBuffer buffer = new StringBuffer();
179
buffer.append("Executable:");
180
buffer.append(executableOrJar);
181
for (Iterator iter = libs.iterator(); iter.hasNext();) {
183
buffer.append(iter.next().toString());
186
if(dllLibs.size() > 0){
187
for (Iterator iter = dllLibs.iterator(); iter.hasNext();) {
189
buffer.append(iter.next().toString());
194
if(forcedLibs.size() > 0){
195
for (Iterator iter = forcedLibs.iterator(); iter.hasNext();) {
197
buffer.append(iter.next().toString());
201
return buffer.toString();
205
* Adds the compiled libs (dlls)
207
public void restoreCompiledLibs(IProgressMonitor monitor) {
208
FileFilter filter = new FileFilter() {
210
public boolean accept(File pathname) {
211
if(pathname.isFile()){
212
return PythonPathHelper.isValidDll(REF.getFileAbsolutePath(pathname));
220
List<File> dlls = new ArrayList<File>();
221
for (Iterator iter = libs.iterator(); iter.hasNext();) {
222
String folder = iter.next().toString();
224
List<File>[] below = PydevPlugin.getPyFilesBelow(new File(folder), filter, monitor, false);
225
dlls.addAll(below[0]);
229
for (Iterator iter = dlls.iterator(); iter.hasNext();) {
230
File f = (File) iter.next();
232
this.dllLibs.add(REF.getFileAbsolutePath(f));
235
//the compiled with the interpreter should be already gotten.
236
forcedLibs.add("os"); //we have it in source, but want to interpret it, source info (ast) does not give us much
238
//as it is a set, there is no problem to add it twice
239
forcedLibs.add("__builtin__"); //jython bug: __builtin__ is not added
240
forcedLibs.add("sys"); //jython bug: sys is not added
244
//by default, we don't want to force anything to python.
245
forcedLibs.add("StringIO"); //jython bug: StringIO is not added
247
//those are sources, but we want to get runtime info on them.
248
forcedLibs.add("OpenGL");
249
forcedLibs.add("wxPython");
250
forcedLibs.add("wx");
256
* Restores the path given non-standard libraries
259
public void restorePythonpath(String path, IProgressMonitor monitor) {
260
//no managers involved here...
261
modulesManager.setBuiltins(forcedLibs);
262
modulesManager.changePythonPath(path, null, monitor, null);
266
* Restores the path with the discovered libs
269
public void restorePythonpath(IProgressMonitor monitor) {
270
StringBuffer buffer = new StringBuffer();
271
for (Iterator iter = libs.iterator(); iter.hasNext();) {
272
String folder = (String) iter.next();
273
buffer.append(folder);
276
restorePythonpath(buffer.toString(), monitor);
280
* @return whether this info belongs to jython
282
public boolean isJythonInfo() {
283
return isJythonExecutable(executableOrJar);
287
* @param executable the executable we want to know about
288
* @return if the executable is the jython jar.
290
public static boolean isJythonExecutable(String executable) {
291
if (executable.endsWith("\"")) {
292
return executable.endsWith(".jar\"");
294
return executable.endsWith(".jar");
297
public String getExeAsFileSystemValidPath() {
299
char[] invalidChars = new char[]{
309
String systemValid = new String(REF.encodeBase64(executableOrJar.getBytes()));
310
for (char c : invalidChars) {
311
systemValid = systemValid.replace(c, '_');
b'\\ No newline at end of file'