647
|
1 |
"
|
|
2 |
Copyright (c) 2007-2010 Jan Vrany
|
|
3 |
Copyright (c) 2009-2010 eXept Software AG
|
|
4 |
|
|
5 |
Permission is hereby granted, free of charge, to any person
|
|
6 |
obtaining a copy of this software and associated documentation
|
|
7 |
files (the 'Software'), to deal in the Software without
|
|
8 |
restriction, including without limitation the rights to use,
|
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 |
copies of the Software, and to permit persons to whom the
|
|
11 |
Software is furnished to do so, subject to the following
|
|
12 |
conditions:
|
|
13 |
|
|
14 |
The above copyright notice and this permission notice shall be
|
|
15 |
included in all copies or substantial portions of the Software.
|
|
16 |
|
|
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
24 |
OTHER DEALINGS IN THE SOFTWARE.
|
|
25 |
"
|
|
26 |
"{ Package: 'stx:libsvn' }"
|
|
27 |
|
583
|
28 |
"{ NameSpace: SVN }"
|
118
|
29 |
|
|
30 |
Object subclass:#OSProcess
|
647
|
31 |
instanceVariableNames:'executable arguments environment workdir stdin stdout stderr
|
|
32 |
exitValue runningLock'
|
|
33 |
classVariableNames:''
|
|
34 |
poolDictionaries:''
|
|
35 |
category:'OS-Support'
|
118
|
36 |
!
|
|
37 |
|
647
|
38 |
!OSProcess class methodsFor:'documentation'!
|
|
39 |
|
|
40 |
copyright
|
|
41 |
"
|
|
42 |
Copyright (c) 2007-2010 Jan Vrany
|
|
43 |
Copyright (c) 2009-2010 eXept Software AG
|
|
44 |
|
|
45 |
Permission is hereby granted, free of charge, to any person
|
|
46 |
obtaining a copy of this software and associated documentation
|
|
47 |
files (the 'Software'), to deal in the Software without
|
|
48 |
restriction, including without limitation the rights to use,
|
|
49 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
50 |
copies of the Software, and to permit persons to whom the
|
|
51 |
Software is furnished to do so, subject to the following
|
|
52 |
conditions:
|
|
53 |
|
|
54 |
The above copyright notice and this permission notice shall be
|
|
55 |
included in all copies or substantial portions of the Software.
|
|
56 |
|
|
57 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
58 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
59 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
60 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
61 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
62 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
63 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
64 |
OTHER DEALINGS IN THE SOFTWARE.
|
|
65 |
|
|
66 |
"
|
|
67 |
! !
|
118
|
68 |
|
|
69 |
!OSProcess class methodsFor:'private'!
|
|
70 |
|
|
71 |
asShellQuotedArgument: anObject
|
|
72 |
|
|
73 |
| aString unquotedStream quotedStream |
|
|
74 |
aString := anObject asString.
|
|
75 |
|
|
76 |
(aString first = $' and: [aString last = $'])
|
647
|
77 |
ifTrue:[^aString].
|
118
|
78 |
|
|
79 |
(aString first = $" and: [aString last = $"])
|
647
|
80 |
ifTrue:[^aString].
|
118
|
81 |
|
|
82 |
(aString allSatisfy:
|
647
|
83 |
[:char|char isSeparator not and: [(#($" $< $> $& $# $; $\) includes: char) not]])
|
|
84 |
ifTrue:[^aString].
|
118
|
85 |
|
|
86 |
unquotedStream := aString readStream.
|
|
87 |
quotedStream := (String new: aString size + 10) writeStream.
|
|
88 |
quotedStream nextPut: $".
|
|
89 |
[ unquotedStream atEnd ] whileFalse:
|
647
|
90 |
[|char|
|
|
91 |
char := unquotedStream next.
|
|
92 |
(#($" $\) includes: char) ifTrue:[quotedStream nextPut: $\].
|
|
93 |
quotedStream nextPut: char].
|
118
|
94 |
quotedStream nextPut: $".
|
|
95 |
^quotedStream contents.
|
|
96 |
|
|
97 |
"
|
647
|
98 |
OSProcess asShellQuotedArgument: 'Hello' .
|
|
99 |
OSProcess asShellQuotedArgument: 'Hello world'
|
|
100 |
OSProcess asShellQuotedArgument: 'Hello'' world'
|
|
101 |
OSProcess asShellQuotedArgument: 'Hello
|
|
102 |
World'
|
118
|
103 |
"
|
|
104 |
|
|
105 |
"Created: / 10-10-2008 / 12:32:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
106 |
"Modified: / 02-06-2009 / 19:41:05 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
107 |
! !
|
|
108 |
|
|
109 |
!OSProcess methodsFor:'accessing'!
|
|
110 |
|
|
111 |
arguments
|
|
112 |
^ arguments ? #()
|
|
113 |
|
|
114 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
115 |
!
|
|
116 |
|
|
117 |
arguments:something
|
|
118 |
arguments := something.
|
|
119 |
|
|
120 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
121 |
!
|
|
122 |
|
|
123 |
environment
|
|
124 |
^ environment
|
|
125 |
|
|
126 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
127 |
!
|
|
128 |
|
|
129 |
environment:something
|
|
130 |
environment := something.
|
|
131 |
|
|
132 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
133 |
!
|
|
134 |
|
|
135 |
executable
|
|
136 |
^ executable
|
|
137 |
|
|
138 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
139 |
!
|
|
140 |
|
|
141 |
executable:something
|
|
142 |
executable := something.
|
|
143 |
|
|
144 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
145 |
!
|
|
146 |
|
|
147 |
exitValue
|
|
148 |
|
|
149 |
self waitFor.
|
|
150 |
^exitValue
|
|
151 |
|
|
152 |
"Created: / 15-03-2008 / 18:08:00 / janfrog"
|
|
153 |
"Modified: / 19-04-2008 / 12:25:56 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
154 |
!
|
|
155 |
|
|
156 |
stderr
|
|
157 |
^ stderr ? Stderr
|
|
158 |
|
|
159 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
160 |
!
|
|
161 |
|
|
162 |
stderr:something
|
|
163 |
stderr := something.
|
|
164 |
|
|
165 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
166 |
!
|
|
167 |
|
|
168 |
stdin
|
|
169 |
^ stdin ? Stdin
|
|
170 |
|
|
171 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
172 |
!
|
|
173 |
|
|
174 |
stdin:something
|
|
175 |
stdin := something.
|
|
176 |
|
|
177 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
178 |
!
|
|
179 |
|
|
180 |
stdout
|
|
181 |
^ stdout ? Transcript
|
|
182 |
|
|
183 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
184 |
!
|
|
185 |
|
|
186 |
stdout:something
|
|
187 |
stdout := something.
|
|
188 |
|
|
189 |
"Created: / 15-03-2008 / 18:21:05 / janfrog"
|
|
190 |
!
|
|
191 |
|
|
192 |
workdir
|
|
193 |
^ workdir ? Filename defaultDirectory
|
|
194 |
|
|
195 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
196 |
!
|
|
197 |
|
|
198 |
workdir:aStringOrFilename
|
|
199 |
workdir := aStringOrFilename asString.
|
|
200 |
|
|
201 |
"Created: / 15-03-2008 / 18:09:37 / janfrog"
|
|
202 |
"Modified: / 09-04-2009 / 17:32:25 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
203 |
! !
|
|
204 |
|
|
205 |
!OSProcess methodsFor:'conversion'!
|
|
206 |
|
|
207 |
asShellCommandString
|
|
208 |
|
|
209 |
| cmdStream |
|
|
210 |
cmdStream := String new writeStream.
|
|
211 |
cmdStream nextPutAll:self executable.
|
|
212 |
self arguments do:
|
647
|
213 |
[:arg|
|
|
214 |
cmdStream space.
|
|
215 |
cmdStream nextPutAll:(self asShellQuotedArgument: arg)].
|
118
|
216 |
|
|
217 |
^cmdStream contents utf8Encoded
|
|
218 |
|
|
219 |
"Created: / 19-03-2008 / 12:34:59 / janfrog"
|
|
220 |
"Modified: / 31-03-2008 / 14:09:05 / janfrog"
|
|
221 |
"Modified: / 23-03-2009 / 10:09:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
222 |
! !
|
|
223 |
|
|
224 |
!OSProcess methodsFor:'operations'!
|
|
225 |
|
|
226 |
execute
|
|
227 |
"
|
|
228 |
self synchronized:
|
647
|
229 |
[runningLock
|
|
230 |
ifNotNil:[self error:'Process already running']
|
|
231 |
ifNil:[runningLock := Semaphore new:0]].
|
118
|
232 |
[["
|
647
|
233 |
|
|
234 |
OperatingSystem isMSWINDOWSlike"false"
|
|
235 |
ifTrue: [self executeOnStupidPlatform]
|
|
236 |
ifFalse:[self executeOnReasonablePlatform]
|
|
237 |
"
|
|
238 |
|
|
239 |
(OperatingSystem
|
|
240 |
executeCommand: self asShellCommandString
|
|
241 |
inputFrom: self stdin
|
|
242 |
outputTo: self stdout
|
|
243 |
errorTo: self stderr
|
|
244 |
auxFrom: nil
|
|
245 |
environment: nil
|
|
246 |
inDirectory: self workdir asString
|
|
247 |
lineWise: (self stdout = self stderr)
|
|
248 |
onError:[:value|exitValue := value code.false])
|
|
249 |
ifTrue:[exitValue := 0]
|
118
|
250 |
"
|
647
|
251 |
|
118
|
252 |
] ensure:[runningLock signalForAll. runningLock := nil]] fork
|
|
253 |
"
|
|
254 |
|
|
255 |
"Created: / 15-03-2008 / 18:11:20 / janfrog"
|
|
256 |
"Modified: / 19-03-2008 / 12:35:05 / janfrog"
|
|
257 |
"Modified: / 08-06-2008 / 19:15:45 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
647
|
258 |
"Modified: / 11-04-2010 / 13:04:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
118
|
259 |
!
|
|
260 |
|
|
261 |
waitFor
|
|
262 |
|
|
263 |
"| lock |
|
|
264 |
self synchronized:
|
647
|
265 |
[runningLock
|
|
266 |
ifNil:[^self]
|
|
267 |
ifNotNil:[lock := runningLock]].
|
118
|
268 |
lock wait"
|
|
269 |
|
|
270 |
"Created: / 15-03-2008 / 18:32:41 / janfrog"
|
|
271 |
"Modified: / 08-06-2008 / 19:15:22 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
272 |
! !
|
|
273 |
|
|
274 |
!OSProcess methodsFor:'private'!
|
|
275 |
|
|
276 |
asShellQuotedArgument:arg
|
|
277 |
^ self class asShellQuotedArgument:arg
|
|
278 |
|
|
279 |
"Created: / 10-10-2008 / 12:32:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
647
|
280 |
!
|
|
281 |
|
|
282 |
executeOnReasonablePlatform
|
|
283 |
"
|
|
284 |
self synchronized:
|
|
285 |
[runningLock
|
|
286 |
ifNotNil:[self error:'Process already running']
|
|
287 |
ifNil:[runningLock := Semaphore new:0]].
|
|
288 |
[["
|
|
289 |
(OperatingSystem
|
|
290 |
executeCommand: self asShellCommandString
|
|
291 |
inputFrom: self stdin
|
|
292 |
outputTo: self stdout
|
|
293 |
errorTo: self stderr
|
|
294 |
auxFrom: nil
|
|
295 |
environment: nil
|
|
296 |
inDirectory: self workdir asString
|
|
297 |
lineWise: (self stdout = self stderr)
|
|
298 |
onError:[:value|exitValue := value code.false])
|
|
299 |
ifTrue:[exitValue := 0]
|
|
300 |
"
|
|
301 |
] ensure:[runningLock signalForAll. runningLock := nil]] fork
|
|
302 |
"
|
|
303 |
|
|
304 |
"Modified: / 19-03-2008 / 12:35:05 / janfrog"
|
|
305 |
"Modified: / 08-06-2008 / 19:15:45 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
306 |
"Created: / 11-04-2010 / 12:49:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
307 |
!
|
|
308 |
|
|
309 |
executeOnStupidPlatform
|
|
310 |
|
|
311 |
"i.e., on MS Windows. For some reasons sometimes part
|
|
312 |
of stdout bytes are lost. Arghh.
|
|
313 |
To workaround it, redirect output to file and then
|
|
314 |
read that file. Stupid & complicated kludge, sigh."
|
|
315 |
|
|
316 |
| extStdoutF extStdout extStderrF extStderr |
|
|
317 |
[extStdout := (extStdoutF := Filename newTemporary) writeStream.
|
|
318 |
extStderr := (extStderrF := Filename newTemporary) writeStream.
|
|
319 |
(OperatingSystem
|
|
320 |
executeCommand: self asShellCommandString
|
|
321 |
inputFrom: self stdin
|
|
322 |
outputTo: extStdout
|
|
323 |
errorTo: extStderr
|
|
324 |
auxFrom: nil
|
|
325 |
environment: nil
|
|
326 |
inDirectory: self workdir asString
|
|
327 |
lineWise: (self stdout = self stderr)
|
|
328 |
onError:[:value|exitValue := value code.false])
|
|
329 |
ifTrue:[exitValue := 0]
|
|
330 |
] ensure: [
|
|
331 |
extStdout close. extStderr close.
|
|
332 |
stdout nextPutAll: extStdoutF contentsAsString.
|
|
333 |
stderr nextPutAll: extStderrF contentsAsString.
|
|
334 |
"Windows are really stupid!! Sometimes they lock files
|
|
335 |
longer than needed. Even same process is not allowed
|
|
336 |
to work with them. Sigh sigh sigh..."
|
|
337 |
[ extStdoutF remove ] on: Error do:
|
|
338 |
[Delay waitForMilliseconds: 10.
|
|
339 |
[ extStdoutF remove ] on: Error do:
|
|
340 |
[Delay waitForMilliseconds: 20.
|
|
341 |
extStdoutF remove]].
|
|
342 |
"Same for stderr"
|
|
343 |
[ extStderrF remove ] on: Error do:
|
|
344 |
[Delay waitForMilliseconds: 10.
|
|
345 |
[ extStderrF remove ] on: Error do:
|
|
346 |
[Delay waitForMilliseconds: 20.
|
|
347 |
extStderrF remove]]]
|
|
348 |
|
|
349 |
"Created: / 11-04-2010 / 12:50:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
350 |
"Modified: / 04-09-2010 / 23:41:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
118
|
351 |
! !
|
|
352 |
|
|
353 |
!OSProcess class methodsFor:'documentation'!
|
|
354 |
|
|
355 |
version
|
127
|
356 |
^ '$Header$'
|
|
357 |
!
|
|
358 |
|
|
359 |
version_CVS
|
|
360 |
^ '$Header$'
|
126
|
361 |
!
|
|
362 |
|
|
363 |
version_SVN
|
647
|
364 |
^ '§Id: SVN__OSProcess.st 350 2011-07-07 18:42:56Z vranyj1 §'
|
118
|
365 |
! !
|