~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/src/org/python/pydev/ui/pythonpathconf/InterpreterInfo.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on May 11, 2005
 
3
 *
 
4
 * @author Fabio Zadrozny
 
5
 */
 
6
package org.python.pydev.ui.pythonpathconf;
 
7
 
 
8
import java.io.File;
 
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;
 
15
import java.util.Set;
 
16
import java.util.TreeSet;
 
17
 
 
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;
 
24
 
 
25
 
 
26
public class InterpreterInfo implements Serializable, IInterpreterInfo{
 
27
    
 
28
    /**
 
29
     * check note on http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/version.html#6678
 
30
     * 
 
31
     * changed to 10L with the release 1.0
 
32
     */
 
33
    private static final long serialVersionUID = 10L;
 
34
    
 
35
    /**
 
36
     * For jython, this is the jython.jar
 
37
     * 
 
38
     * For python, this is the path to the python executable 
 
39
     */
 
40
    public String executableOrJar; 
 
41
    
 
42
    /**
 
43
     * folders - they should be passed to the pythonpath
 
44
     */
 
45
    public java.util.List<String> libs = new ArrayList<String>(); 
 
46
    
 
47
    /**
 
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
 
50
     * this information)
 
51
     *  
 
52
     * files: .pyd, .dll, etc.
 
53
     * 
 
54
     * for jython, the jars should appear here.
 
55
     */
 
56
    public java.util.List<String> dllLibs = new ArrayList<String>(); 
 
57
    
 
58
    /**
 
59
     * __builtin__, os, math, etc for python 
 
60
     * 
 
61
     * check sys.builtin_module_names and others that should
 
62
     * be forced to use code completion as builtins, such os, math, etc.
 
63
     * 
 
64
     * for jython, this should 
 
65
     */
 
66
    public Set<String> forcedLibs = new TreeSet<String>(); 
 
67
    
 
68
    /**
 
69
     * module management for the system is always binded to an interpreter (binded in this class)
 
70
     * 
 
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.
 
73
     */
 
74
    public SystemModulesManager modulesManager = new SystemModulesManager(forcedLibs);
 
75
    
 
76
    public InterpreterInfo(String exe, Collection<String> libs0){
 
77
        this.executableOrJar = exe;
 
78
        libs.addAll(libs0);
 
79
    }
 
80
    
 
81
    public InterpreterInfo(String exe, Collection<String> libs0, Collection<String> dlls){
 
82
        this(exe, libs0);
 
83
        dllLibs.addAll(dlls);
 
84
    }
 
85
    
 
86
    public InterpreterInfo(String exe, List<String> libs0, List<String> dlls, List<String> forced) {
 
87
        this(exe, libs0, dlls);
 
88
        forcedLibs.addAll(forced);
 
89
    }
 
90
 
 
91
    /**
 
92
     * @see java.lang.Object#equals(java.lang.Object)
 
93
     */
 
94
    public boolean equals(Object o) {
 
95
        if (!(o instanceof InterpreterInfo)){
 
96
            return false;
 
97
        }
 
98
 
 
99
        InterpreterInfo info = (InterpreterInfo) o;
 
100
        if(info.executableOrJar.equals(this.executableOrJar) == false){
 
101
            return false;
 
102
        }
 
103
        
 
104
        if(info.libs.equals(this.libs) == false){
 
105
            return false;
 
106
        }
 
107
        
 
108
        if(info.dllLibs.equals(this.dllLibs) == false){
 
109
            return false;
 
110
        }
 
111
        
 
112
        if(info.forcedLibs.equals(this.forcedLibs) == false){
 
113
            return false;
 
114
        }
 
115
        
 
116
        return true;
 
117
    }
 
118
 
 
119
    /**
 
120
     * Format we receive should be:
 
121
     * 
 
122
     * Executable:python.exe|lib1|lib2|lib3@dll1|dll2|dll3$forcedBuitin1|forcedBuiltin2
 
123
     * 
 
124
     * Symbols ': @ $'
 
125
     */
 
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)");
 
129
        }
 
130
        received = received.replaceAll("\n", "").replaceAll("\r", "");
 
131
        String[] forcedSplit = received.split("\\$");
 
132
        String[] libsSplit = forcedSplit[0].split("\\@");
 
133
        String exeAndLibs = libsSplit[0];
 
134
        
 
135
        
 
136
        
 
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){
 
143
                l.add(trimmed);
 
144
            }
 
145
        }
 
146
 
 
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){
 
154
                        l1.add(trimmed);
 
155
                    }
 
156
                }
 
157
        }
 
158
                
 
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){
 
166
                        l2.add(trimmed);
 
167
                    }
 
168
                }
 
169
        }                   
 
170
        return new InterpreterInfo(executable, l, l1, l2);
 
171
                
 
172
    }
 
173
    
 
174
    /**
 
175
     * @see java.lang.Object#toString()
 
176
     */
 
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();) {
 
182
            buffer.append("|");
 
183
            buffer.append(iter.next().toString());
 
184
        }
 
185
        buffer.append("@");
 
186
        if(dllLibs.size() > 0){
 
187
                for (Iterator iter = dllLibs.iterator(); iter.hasNext();) {
 
188
                    buffer.append("|");
 
189
                    buffer.append(iter.next().toString());
 
190
                }
 
191
        }
 
192
        
 
193
        buffer.append("$");
 
194
        if(forcedLibs.size() > 0){
 
195
                for (Iterator iter = forcedLibs.iterator(); iter.hasNext();) {
 
196
                    buffer.append("|");
 
197
                    buffer.append(iter.next().toString());
 
198
                }
 
199
        }
 
200
        
 
201
        return buffer.toString();
 
202
    }
 
203
 
 
204
    /**
 
205
     * Adds the compiled libs (dlls)
 
206
     */
 
207
    public void restoreCompiledLibs(IProgressMonitor monitor) {
 
208
        FileFilter filter = new FileFilter() {
 
209
            
 
210
                public boolean accept(File pathname) {
 
211
                    if(pathname.isFile()){
 
212
                        return PythonPathHelper.isValidDll(REF.getFileAbsolutePath(pathname));
 
213
                    }else{
 
214
                        return false;
 
215
                    }
 
216
                }
 
217
        
 
218
            };
 
219
 
 
220
            List<File> dlls = new ArrayList<File>();
 
221
            for (Iterator iter = libs.iterator(); iter.hasNext();) {
 
222
            String folder = iter.next().toString();
 
223
            
 
224
            List<File>[] below = PydevPlugin.getPyFilesBelow(new File(folder), filter, monitor, false);
 
225
            dlls.addAll(below[0]);
 
226
        }
 
227
            
 
228
            dllLibs.clear();
 
229
            for (Iterator iter = dlls.iterator(); iter.hasNext();) {
 
230
            File f = (File) iter.next();
 
231
            
 
232
            this.dllLibs.add(REF.getFileAbsolutePath(f));
 
233
        }
 
234
            
 
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
 
237
        
 
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
 
241
        
 
242
 
 
243
        if(isJythonInfo()){
 
244
            //by default, we don't want to force anything to python.
 
245
            forcedLibs.add("StringIO"); //jython bug: StringIO is not added
 
246
        }else{
 
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");
 
251
        }
 
252
        
 
253
    }
 
254
 
 
255
    /**
 
256
     * Restores the path given non-standard libraries
 
257
     * @param path
 
258
     */
 
259
    public void restorePythonpath(String path, IProgressMonitor monitor) {
 
260
        //no managers involved here...
 
261
        modulesManager.setBuiltins(forcedLibs);
 
262
        modulesManager.changePythonPath(path, null, monitor, null);
 
263
    }
 
264
    
 
265
    /**
 
266
     * Restores the path with the discovered libs
 
267
     * @param path
 
268
     */
 
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);
 
274
            buffer.append("|");
 
275
        }
 
276
        restorePythonpath(buffer.toString(), monitor);
 
277
    }
 
278
    
 
279
    /**
 
280
     * @return whether this info belongs to jython 
 
281
     */
 
282
    public boolean isJythonInfo() {
 
283
        return isJythonExecutable(executableOrJar);
 
284
    }
 
285
 
 
286
    /**
 
287
     * @param executable the executable we want to know about
 
288
     * @return if the executable is the jython jar.
 
289
     */
 
290
    public static boolean isJythonExecutable(String executable) {
 
291
        if (executable.endsWith("\"")) {
 
292
            return executable.endsWith(".jar\"");
 
293
        }
 
294
        return executable.endsWith(".jar");
 
295
    }
 
296
 
 
297
    public String getExeAsFileSystemValidPath() {
 
298
        //   /\:*?"<>|
 
299
        char[] invalidChars = new char[]{
 
300
                '/',
 
301
                '\\',
 
302
                ':',
 
303
                '*',
 
304
                '?',
 
305
                '"',
 
306
                '<',
 
307
                '>',
 
308
                '|'};
 
309
        String systemValid = new String(REF.encodeBase64(executableOrJar.getBytes()));
 
310
        for (char c : invalidChars) {
 
311
            systemValid = systemValid.replace(c, '_');
 
312
        }
 
313
        return systemValid;
 
314
    }
 
315
    
 
316
    
 
317
}
 
 
b'\\ No newline at end of file'