1 4 package org.python.core; 5 6 import java.util.Properties ; 7 import java.util.StringTokenizer ; 8 import java.io.*; 9 10 13 public class SysPackageManager extends PathPackageManager { 14 15 protected void message(String msg) { 16 Py.writeMessage("*sys-package-mgr*",msg); 17 } 18 19 protected void warning(String warn) { 20 Py.writeWarning("*sys-package-mgr*",warn); 21 } 22 23 protected void comment(String msg) { 24 Py.writeComment("*sys-package-mgr*",msg); 25 } 26 27 protected void debug(String msg) { 28 Py.writeDebug("*sys-package-mgr*",msg); 29 } 30 31 public SysPackageManager(File cachedir, Properties registry) { 32 if(useCacheDir(cachedir)) { 33 initCache(); 34 findAllPackages(registry); 35 saveCache(); 36 } 37 } 38 39 public void addJar(String jarfile, boolean cache) { 40 addJarToPackages(new File(jarfile), cache); 41 if (cache) 42 saveCache(); 43 } 44 45 public void addJarDir(String jdir, boolean cache) { 46 addJarDir(jdir, cache, cache); 47 } 48 49 private void addJarDir(String jdir, boolean cache, boolean saveCache) { 50 File file = new File(jdir); 51 if (!file.isDirectory()) return; 52 String [] files = file.list(); 53 for(int i=0; i<files.length; i++) { 54 String entry = files[i]; 55 if (entry.endsWith(".jar") || entry.endsWith(".zip")) { 56 addJarToPackages(new File(jdir,entry), cache); 57 } 58 } 59 if (saveCache) 60 saveCache(); 61 } 62 63 private void addJarPath(String path) { 64 StringTokenizer tok = 65 new StringTokenizer (path, java.io.File.pathSeparator); 66 while (tok.hasMoreTokens()) { 67 String entry = tok.nextToken(); 69 addJarDir(entry, true, false); 70 } 71 } 72 73 private void findAllPackages(Properties registry) { 74 String paths = registry.getProperty( 75 "python.packages.paths", 76 "java.class.path,sun.boot.class.path"); 77 String directories = registry.getProperty( 78 "python.packages.directories", 79 "java.ext.dirs"); 80 String fakepath = registry.getProperty( 81 "python.packages.fakepath", null); 82 StringTokenizer tok = new StringTokenizer (paths, ","); 83 while (tok.hasMoreTokens()) { 84 String entry = tok.nextToken().trim(); 85 String tmp = registry.getProperty(entry); 86 if (tmp == null) continue; 87 addClassPath(tmp); 88 } 89 90 tok = new StringTokenizer (directories, ","); 91 while (tok.hasMoreTokens()) { 92 String entry = tok.nextToken().trim(); 93 String tmp = registry.getProperty(entry); 94 if (tmp == null) continue; 95 addJarPath(tmp); 96 } 97 98 if (fakepath != null) addClassPath(fakepath); 99 } 100 101 public void notifyPackageImport(String pkg, String name) { 102 if (pkg != null && pkg.length()>0) name = pkg + '.' + name; 103 Py.writeComment("import","'"+name+"' as java package"); 104 } 105 106 public Class findClass(String pkg,String name) { 107 Class c = super.findClass(pkg,name); 108 if (c != null) 109 Py.writeComment("import","'"+name+"' as java class"); 110 return c; 111 } 112 113 public Class findClass(String pkg, String name, String reason) { 114 if (pkg != null && pkg.length()>0) name = pkg + '.' + name; 115 return Py.findClassEx(name,reason); 116 } 117 118 public PyList doDir(PyJavaPackage jpkg, boolean instantiate, 119 boolean exclpkgs) 120 { 121 PyList basic = basicDoDir(jpkg, instantiate, exclpkgs); 122 PyList ret = new PyList(); 123 124 doDir(searchPath, ret, jpkg, instantiate, exclpkgs); 125 126 PySystemState system = Py.getSystemState(); 127 128 if (system.getClassLoader() == null) 129 doDir(system.path, ret, jpkg, instantiate, exclpkgs); 130 131 return merge(basic,ret); 132 } 133 134 public boolean packageExists(String pkg, String name) { 135 if (packageExists(searchPath, pkg, name)) return true; 136 137 PySystemState system = Py.getSystemState(); 138 139 if (system.getClassLoader() == null && 140 packageExists(Py.getSystemState().path,pkg,name)) { 141 return true; 142 } 143 144 return false; 145 } 146 147 } 148 | Popular Tags |