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