-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPBPharoPipenvProcess.class.st
273 lines (228 loc) · 7.1 KB
/
PBPharoPipenvProcess.class.st
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
Class {
#name : #PBPharoPipenvProcess,
#superclass : #PBAbstractProcess,
#instVars : [
'process',
'environmentVariables',
'serverDebugger'
],
#classVars : [
'PipenvPath'
],
#category : #'PythonBridge-Pharo-Processes'
}
{ #category : #'class initialization' }
PBPharoPipenvProcess class >> initialize [
SessionManager default registerUserClassNamed: self name.
]
{ #category : #initialization }
PBPharoPipenvProcess class >> pipenvPath [
^ PipenvPath
ifNil: [ PipenvPath := self resolvePipenvPath ]
ifNotNil: [ PipenvPath ]
]
{ #category : #initialization }
PBPharoPipenvProcess class >> pipenvPath: aFileReference [
PipenvPath := aFileReference
]
{ #category : #accessing }
PBPharoPipenvProcess class >> platform [
^ PBPharoPlatform current
]
{ #category : #initialization }
PBPharoPipenvProcess class >> reset [
<script>
PipenvPath := nil.
]
{ #category : #initialization }
PBPharoPipenvProcess class >> resolvePipenvPath [
| path |
(OSSUnixSubprocess new
command: '/usr/bin/which';
arguments: (Array with: 'pipenv');
addAllEnvVariablesFromParentWithoutOverride;
redirectStdout;
terminateOnShutdown;
runAndWaitOnExitDo: [ :command :outString | path := outString trim ]).
path ifEmpty: [ self signalPipenvNotFound ].
^ path asFileReference
]
{ #category : #initialization }
PBPharoPipenvProcess class >> signalPipenvNotFound [
"
PythonBridge use the unix command `which` to find the route of the `pipenv` command. From Pharo
we could not find the route, therefore you have to set it mannualy.
To find the path of Pipenv in your system run the command `which pipenv` in the terminal.
To set the path in PythonBridge send the following message:
PBProcessHandler pipEnvPath: '/PATH/TO/PIPENV/BINARY'
"
Error signal: 'Error: Pipenv command could not be found.'
]
{ #category : #'system startup' }
PBPharoPipenvProcess class >> startUp: resuming [
"On startup any instances saved during the last session will be invalid (since the processes are terminated on shutdown). Mark them as stopped."
resuming ifTrue:
[ self allInstancesDo: [ :each | each stop ] ]
]
{ #category : #'start-stop' }
PBPharoPipenvProcess >> debuggerStateChangedAction: aDAPThreadStateChanged [
"Respond to the debugger state changed notification.
Signal all promises that the debugger has been paused."
aDAPThreadStateChanged isStopped ifTrue:
[ self application notifyDebuggerPaused: serverDebugger ]
]
{ #category : #accessing }
PBPharoPipenvProcess >> environmentVariables [
^ environmentVariables
]
{ #category : #accessing }
PBPharoPipenvProcess >> errorMessage [
^ self stderr
]
{ #category : #testing }
PBPharoPipenvProcess >> hasProcess [
"Answer a boolean indicating whether the receiver has a process object"
^ process isNotNil
]
{ #category : #initialization }
PBPharoPipenvProcess >> initialize [
super initialize.
environmentVariables := Dictionary new.
self setDefaultEnvironmentVariables
]
{ #category : #utils }
PBPharoPipenvProcess >> installModule: aString in: aPBApplication [
"Install the supplied module using `pipenv run pip install aString`"
| pipenvPath proc arguments stdoutStream stderrStream |
pipenvPath := aPBApplication settings pipenvPath ifNil: [ self class pipenvPath ].
arguments := { 'run'. 'pip'. 'install'. aString. }.
stdoutStream := String new writeStream.
stderrStream := String new writeStream.
proc := OSSUnixSubprocess new
command: pipenvPath fullName;
arguments: arguments;
workingDirectory: aPBApplication workingDirectory fullName;
addAllEnvVariablesFromParentWithoutOverride;
terminateOnShutdown;
redirectStdout;
redirectStderr;
yourself.
environmentVariables associationsDo: [ :assoc |
proc environmentAt: assoc key put: assoc value ].
proc run.
proc
waitForExitPollingEvery: (Delay forMilliseconds: 500)
doing: [ :aProcess :outStream :errStream |
stdoutStream nextPutAll: outStream upToEnd.
stderrStream nextPutAll: errStream upToEnd ].
proc isSuccess ifFalse:
[ self error: 'Unable to install module: ', aString asString ].
]
{ #category : #testing }
PBPharoPipenvProcess >> isRunning [
^ process
ifNil: [ false ]
ifNotNil: [ process isRunning ]
]
{ #category : #private }
PBPharoPipenvProcess >> newProcess [
| newProcess |
newProcess := PBUnixSubprocess new
command: (self settings pipenvPath ifNil:
[ self class pipenvPath ]) fullName;
arguments: self processArguments;
workingDirectory: self workingDirectory fullName;
addAllEnvVariablesFromParentWithoutOverride;
terminateOnShutdown;
yourself.
environmentVariables associationsDo: [ :assoc |
newProcess environmentAt: assoc key put: assoc value ].
^ newProcess
]
{ #category : #accessing }
PBPharoPipenvProcess >> pipenvPath [
"Answer the default pipenvPath.
This may be overridden by the application settings."
^ self class pipenvPath
]
{ #category : #accessing }
PBPharoPipenvProcess >> process [
^ process
]
{ #category : #private }
PBPharoPipenvProcess >> processArguments [
| args |
args := OrderedCollection new.
args
add: 'run';
add: 'python'.
self settings serverDebugMode ifTrue:
[ args addAll: {
'-m'.
'debugpy'.
'--listen'.
self settings debugSocketAddress printAddress.
'--wait-for-client'. } ].
args
add: self pythonMainFile fullName;
add: '--port';
add: self settings pythonSocketAddress port asString;
add: '--pharo';
add: self settings pharoSocketAddress port asString;
add: '--method';
add: PBPlatform current messageBrokerStrategy pythonMethodArg.
self debugMode ifTrue: [ args add: '--log' ].
^ args
]
{ #category : #initialization }
PBPharoPipenvProcess >> setDefaultEnvironmentVariables [
environmentVariables
at: 'LC_ALL' put: 'en_US.UTF-8';
at: 'LANG' put: 'en_US.UTF-8';
at: 'PIPENV_VENV_IN_PROJECT' put: '1';
at: 'PIP_DEFAULT_TIMEOUT' put: '100'
]
{ #category : #'start-stop' }
PBPharoPipenvProcess >> start [
process := self newProcess.
process run.
self settings serverDebugMode ifTrue:
[ self startServerDebugger ].
]
{ #category : #'start-stop' }
PBPharoPipenvProcess >> startServerDebugger [
"Start the debugger, tell the server to run and set up inspection on halt"
"Give the server time to start"
1 second wait.
serverDebugger := DAPPythonDebugger new
localRoot: self workingDirectory;
connectTo: #[127 0 0 1] port: self settings debugSocketAddress port.
serverDebugger announcer
when: DAPThreadStateChanged
do: [ :announcement | self debuggerStateChangedAction: announcement ].
]
{ #category : #accessing }
PBPharoPipenvProcess >> stderr [
"Answer the process stderr contents"
^ process stderr
]
{ #category : #accessing }
PBPharoPipenvProcess >> stdout [
"Answer the process stdout contents"
^ process stdout
]
{ #category : #'start-stop' }
PBPharoPipenvProcess >> stop [
process ifNil: [ ^ self ].
[ process queryExitStatus ifNil: [ process terminate ]]
on: Error
do: [ :e | "Do nothing."].
process closeAndCleanStreams.
process := nil.
]
{ #category : #'start-stop' }
PBPharoPipenvProcess >> stopServerDebugger [
"Close the debugger and deregister"
serverDebugger close.
serverDebugger announcer unsubscribe: self debuggerStateChangedAction
]