001: // Copyright (c) Corporation for National Research Initiatives
002: // Copyright 2000 Samuele Pedroni
003:
004: package org.python.core;
005:
006: import java.util.Properties;
007: import java.util.StringTokenizer;
008: import java.io.*;
009:
010: /**
011: * System package manager. Used by org.python.core.PySystemState.
012: */
013: public class SysPackageManager extends PathPackageManager {
014:
015: protected void message(String msg) {
016: Py.writeMessage("*sys-package-mgr*", msg);
017: }
018:
019: protected void warning(String warn) {
020: Py.writeWarning("*sys-package-mgr*", warn);
021: }
022:
023: protected void comment(String msg) {
024: Py.writeComment("*sys-package-mgr*", msg);
025: }
026:
027: protected void debug(String msg) {
028: Py.writeDebug("*sys-package-mgr*", msg);
029: }
030:
031: public SysPackageManager(File cachedir, Properties registry) {
032: if (useCacheDir(cachedir)) {
033: initCache();
034: findAllPackages(registry);
035: saveCache();
036: }
037: }
038:
039: public void addJar(String jarfile, boolean cache) {
040: addJarToPackages(new File(jarfile), cache);
041: if (cache) {
042: saveCache();
043: }
044: }
045:
046: public void addJarDir(String jdir, boolean cache) {
047: addJarDir(jdir, cache, cache);
048: }
049:
050: private void addJarDir(String jdir, boolean cache, boolean saveCache) {
051: File file = new File(jdir);
052: if (!file.isDirectory()) {
053: return;
054: }
055: String[] files = file.list();
056: for (int i = 0; i < files.length; i++) {
057: String entry = files[i];
058: if (entry.endsWith(".jar") || entry.endsWith(".zip")) {
059: addJarToPackages(new File(jdir, entry), cache);
060: }
061: }
062: if (saveCache) {
063: saveCache();
064: }
065: }
066:
067: private void addJarPath(String path) {
068: StringTokenizer tok = new StringTokenizer(path,
069: java.io.File.pathSeparator);
070: while (tok.hasMoreTokens()) {
071: // ??pending: do jvms trim? how is interpreted entry=""?
072: String entry = tok.nextToken();
073: addJarDir(entry, true, false);
074: }
075: }
076:
077: private void findAllPackages(Properties registry) {
078: String paths = registry.getProperty("python.packages.paths",
079: "java.class.path,sun.boot.class.path");
080: String directories = registry.getProperty(
081: "python.packages.directories", "java.ext.dirs");
082: String fakepath = registry.getProperty(
083: "python.packages.fakepath", null);
084: StringTokenizer tok = new StringTokenizer(paths, ",");
085: while (tok.hasMoreTokens()) {
086: String entry = tok.nextToken().trim();
087: String tmp = registry.getProperty(entry);
088: if (tmp == null) {
089: continue;
090: }
091: addClassPath(tmp);
092: }
093:
094: tok = new StringTokenizer(directories, ",");
095: while (tok.hasMoreTokens()) {
096: String entry = tok.nextToken().trim();
097: String tmp = registry.getProperty(entry);
098: if (tmp == null) {
099: continue;
100: }
101: addJarPath(tmp);
102: }
103:
104: if (fakepath != null) {
105: addClassPath(fakepath);
106: }
107: }
108:
109: public void notifyPackageImport(String pkg, String name) {
110: if (pkg != null && pkg.length() > 0) {
111: name = pkg + '.' + name;
112: }
113: Py.writeComment("import", "'" + name + "' as java package");
114: }
115:
116: public Class findClass(String pkg, String name) {
117: Class c = super .findClass(pkg, name);
118: if (c != null) {
119: Py.writeComment("import", "'" + name + "' as java class");
120: }
121: return c;
122: }
123:
124: public Class findClass(String pkg, String name, String reason) {
125: if (pkg != null && pkg.length() > 0) {
126: name = pkg + '.' + name;
127: }
128: return Py.findClassEx(name, reason);
129: }
130:
131: public PyList doDir(PyJavaPackage jpkg, boolean instantiate,
132: boolean exclpkgs) {
133: PyList basic = basicDoDir(jpkg, instantiate, exclpkgs);
134: PyList ret = new PyList();
135:
136: doDir(this .searchPath, ret, jpkg, instantiate, exclpkgs);
137:
138: PySystemState system = Py.getSystemState();
139:
140: if (system.getClassLoader() == null) {
141: doDir(system.path, ret, jpkg, instantiate, exclpkgs);
142: }
143:
144: return merge(basic, ret);
145: }
146:
147: public boolean packageExists(String pkg, String name) {
148: if (packageExists(this .searchPath, pkg, name)) {
149: return true;
150: }
151:
152: PySystemState system = Py.getSystemState();
153:
154: if (system.getClassLoader() == null
155: && packageExists(Py.getSystemState().path, pkg, name)) {
156: return true;
157: }
158:
159: return false;
160: }
161:
162: }
|