-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path_posixsubprocess.java
More file actions
128 lines (124 loc) · 5.27 KB
/
Copy path_posixsubprocess.java
File metadata and controls
128 lines (124 loc) · 5.27 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
package org.python.modules;
import jnr.constants.platform.Fcntl;
import jnr.posix.POSIX;
import jnr.posix.SpawnAttribute;
import jnr.posix.SpawnFileAction;
import jnr.posix.util.Platform;
import org.python.core.ArgParser;
import org.python.core.Py;
import org.python.core.PyBytes;
import org.python.core.PyDictionary;
import org.python.core.PyList;
import org.python.core.PyLong;
import org.python.core.PyObject;
import org.python.core.PySequence;
import org.python.expose.ExposedFunction;
import org.python.expose.ExposedModule;
import org.python.modules._io.PyFileIO;
import org.python.modules.posix.PosixModule;
import org.python.util.FilenoUtil;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ExposedModule(doc = "A POSIX helper for the subprocess module.")
public class _posixsubprocess {
private static final POSIX posix = PosixModule.getPOSIX();
// subprocess_fork_exec
// public static final PyObject fork_exec(PyObject process_args, PyObject executable_list,
// PyObject close_fds, PyObject fds_to_keep, PyObject cwd, PyObject env,
// PyObject p2cread, PyObject p2cwrite, PyObject c2pread, PyObject c2pwrite,
// PyObject errread, PyObject errwrite, PyObject errpipe_read, PyObject errpipe_write,
// PyObject restore_signals, PyObject call_setsid, PyObject preexec_fn) {
@ExposedFunction
public static final PyObject fork_exec(PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("fork_exec", args, keywords, "args", "executable", "close_fds",
"fds_to_keep", "cwd", "env", "p2cread", "p2cwrite", "c2pread", "c2pwrite",
"errread", "errwrite", "errpipe_read", "errpipe_write", "restore_signals", "call_setsid", "preexec_fn");
PyObject mOne = new PyLong(-1);
PyObject processArgs = ap.getPyObject(0);
String[] pbArgs = new String[processArgs.__len__()];
String cwd = ap.getString(4);
PyObject env = ap.getPyObject(5);
PyObject p2cread = ap.getPyObject(7);
PyObject c2pwrite = ap.getPyObject(9);
PyObject errwrite = ap.getPyObject(11);
PyObject errpipeWrite = ap.getPyObject(13);
int i = 0;
for (PyObject arg : processArgs.asIterable()) {
pbArgs[i++] = arg.toString();
}
ProcessBuilder pb = new ProcessBuilder(pbArgs);
Map<String, String> environ = pb.environment();
Map<?, ?> map;
if (env instanceof PyDictionary) {
map = ((PyDictionary) env).getMap();
} else {
map = new HashMap<>();
}
for (Object key : map.keySet()) {
environ.put((String) key, (String) map.get(key));
}
if (cwd != null) {
pb.directory(new File(cwd));
}
if (p2cread.equals(mOne)) {
pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
} else {
pb.redirectInput(new File(p2cread.toString()));
}
if (c2pwrite.equals(mOne)) {
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
} else {
pb.redirectOutput(new File(c2pwrite.toString()));
}
if (errwrite.equals(mOne)) {
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
} else {
pb.redirectError(new File(errwrite.toString()));
}
// pb.inheritIO();
try {
Process process = pb.start();
return Py.java2py(process);
} catch (IOException e) {
((PyFileIO) errpipeWrite).getRawIO().write(ByteBuffer.wrap(e.getMessage().getBytes()));
return Py.None;
}
// PyList argv = new PyList(process_args);
// PyList envp;
//// String[] command = new String[argv.size() + 1];
//// System.arraycopy(argv.toArray(), 0, command, 1, argv.size());
//// command[0] = exec_array.__getitem__(0).asString();
// ExecArg eargp = new ExecArg();
// PyObject m1 = Py.newLong(-1);
//
// if (p2cread != m1) {
// eargp.fileActions.add(SpawnFileAction.dup(FilenoUtil.filenoFrom(p2cread), 0));
// eargp.fileActions.add(SpawnFileAction.close(FilenoUtil.filenoFrom(p2cwrite)));
// }
// if (c2pread != m1) {
// eargp.fileActions.add(SpawnFileAction.dup(FilenoUtil.filenoFrom(c2pwrite), 1));
// eargp.fileActions.add(SpawnFileAction.close(FilenoUtil.filenoFrom(c2pread)));
// }
// if (errread != m1) {
// eargp.fileActions.add(SpawnFileAction.dup(FilenoUtil.filenoFrom(errwrite), 2));
// eargp.fileActions.add(SpawnFileAction.close(FilenoUtil.filenoFrom(errread)));
// }
//
// POSIX posix = PosixModule.getPOSIX();
// eargp.command_name = (PyBytes) exec_array.__getitem__(0);
// long status = posix.posix_spawnp(
// eargp.command_name.asString(),
// eargp.fileActions,
// Arrays.asList((String[]) argv.toArray(new String[0])),
// Collections.EMPTY_LIST);
//
// return Py.newLong(status);
}
}