-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScriptSession.m
359 lines (282 loc) · 10.3 KB
/
ScriptSession.m
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// ScriptSession.m
// Pipe
//
// Created by RenŽ Puls on 22.03.05.
// Copyright 2005 RenŽ Puls. All rights reserved.
//
#import "ScriptSession.h"
#import "Environment.h"
#import "ScriptVariable.h"
#import "ScriptDocument+Running.h"
@implementation ScriptSession
#pragma mark Init and Cleanup
+ (NSString *)generateUniqueSessionID
{
static unsigned int serialNumber = 0;
@synchronized (self) {
return [NSString stringWithFormat:@"s%u", serialNumber++];
}
return nil; // never reached, but GCC seems to disagree
}
- (id)init
{
if ((self = [super init])) {
sessionID = [[[self class] generateUniqueSessionID] copy];
scriptTask = [[JGBufferedTask alloc] init];
temporaryFilePath = [[NSString alloc] initWithFormat:@"%@/%@.pid%d.%@", NSTemporaryDirectory(), [[NSBundle mainBundle] bundleIdentifier], [[NSProcessInfo processInfo] processIdentifier], sessionID];
[self setupTemporaryFilePath];
}
return self;
}
- (void)addSupportFilesFromPath:(NSString *)supportPath
{
BOOL result;
NSParameterAssert(supportPath != nil);
NSParameterAssert([supportPath isAbsolutePath]);
result = [[NSFileManager defaultManager] createSymbolicLinkAtPath:[self scriptSupportPath] pathContent:supportPath];
NSAssert2(result == YES, @"Failed to link script support path '%@' to temporary directory '%@'", supportPath, [self scriptSupportPath]);
}
- (void)addSupportFilesFromFileWrapper:(NSFileWrapper *)fileWrapper
{
BOOL result;
NSParameterAssert(fileWrapper != nil);
result = [fileWrapper writeToFile:[self scriptSupportPath] atomically:NO updateFilenames:NO];
NSAssert1(result == YES, @"Failed to write script support files to temporary directory '%@'", [self scriptSupportPath]);
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self cleanupTemporaryFilePath];
[sessionID release];
[scriptTask release];
[temporaryFilePath release];
[variables release];
[additionalEnvironment release];
[runCommand release];
[argumentString release];
[super dealloc];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<ScriptSession %p:%@>", self, [self scriptPath]];
}
#pragma mark Accessors
- (NSString *)sessionID
{
return sessionID;
}
- (NSString *)temporaryFilePath
{
return temporaryFilePath;
}
- (NSString *)temporaryWorkPath
{
return [[self temporaryFilePath] stringByAppendingPathComponent:@"tmp"];
}
- (NSString *)scriptSupportPath
{
return [[self temporaryFilePath] stringByAppendingPathComponent:@"support"];
}
- (JGBufferedTask *)scriptTask
{
return scriptTask;
}
- (NSString *)scriptPath
{
return scriptPath;
}
- (void)setScriptPath:(NSString *)newPath
{
if (newPath != scriptPath) {
[scriptPath release];
scriptPath = [newPath copy];
}
}
- (NSArray *)variables
{
return variables;
}
- (void)setVariables:(NSArray *)newVariables
{
if (variables != newVariables) {
[variables release];
variables = [newVariables copy];
}
}
- (NSDictionary *)additionalEnvironment
{
return additionalEnvironment;
}
- (void)setAdditionalEnvironment:(NSDictionary *)newEnvironment
{
if (newEnvironment != additionalEnvironment) {
[additionalEnvironment release];
additionalEnvironment = [newEnvironment copy];
}
}
- (NSString *)runCommand
{
return runCommand;
}
- (void)setRunCommand:(NSString *)newCommand
{
if (newCommand != runCommand) {
[runCommand release];
runCommand = [newCommand copy];
}
}
- (NSString *)argumentString
{
return argumentString;
}
- (void)setArgumentString:(NSString *)newArguments
{
if (newArguments != argumentString) {
[argumentString release];
argumentString = [newArguments copy];
}
}
#pragma mark Variable Support
- (id)variableWithName:(NSString *)variableName
{
NSEnumerator *variableEnumerator;
ScriptVariable *currentVariable;
NSParameterAssert(variableName != nil);
variableEnumerator = [[self variables] objectEnumerator];
while ((currentVariable = [variableEnumerator nextObject])) {
if ([[currentVariable name] isEqualToString:variableName]) {
return currentVariable;
}
}
return nil;
}
#pragma mark Running Synchronously
- (NSData *)dataFromTransformingInputData:(NSData *)theData
{
JGBufferedTask *newTask;
NSData *resultData;
NSParameterAssert(theData != nil);
newTask = [self scriptTask];
[[newTask inputBuffer] appendData:theData];
[self launch];
[newTask waitUntilReadingComplete];
resultData = [[[newTask outputBuffer] copy] autorelease];
return resultData;
}
#pragma mark Temporary File Management
- (void)setupTemporaryFilePath
{
NSDictionary *temporaryFilePathAttributes;
BOOL result;
if (temporaryFilePath == nil)
return;
temporaryFilePathAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0700], NSFilePosixPermissions,
nil];
result = [[NSFileManager defaultManager] createDirectoryAtPath:temporaryFilePath attributes:temporaryFilePathAttributes];
NSAssert1(result == TRUE, @"Failed to create temporary directory '%@'", temporaryFilePath);
result = [[NSFileManager defaultManager] createDirectoryAtPath:[self temporaryWorkPath] attributes:temporaryFilePathAttributes];
NSAssert1(result == TRUE, @"Failed to create temporary directory '%@'", [self temporaryWorkPath]);
}
- (void)cleanupTemporaryFilePath
{
if (temporaryFilePath == nil)
return;
NSAssert2([temporaryFilePath hasPrefix:NSTemporaryDirectory()], @"Temporary directory '%@' is not below system-wide temp path '%@'; refusing to delete", temporaryFilePath, NSTemporaryDirectory());
[[NSFileManager defaultManager] removeFileAtPath:temporaryFilePath handler:nil];
}
- (void)launch
{
NSMutableDictionary *taskEnvironment;
NSMutableArray *taskArguments;
NSEnumerator *variableEnumerator;
ScriptVariable *currentVariable;
NSTask *systemTask;
NSEnumerator *componentEnumerator;
NSString *currentComponent;
systemTask = [[self scriptTask] lowLevelTask];
// Prepare the script environment...
taskEnvironment = [NSMutableDictionary dictionary];
// Start with system environment
[taskEnvironment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
// Merge additional environment variables
[taskEnvironment addEntriesFromDictionary:[self additionalEnvironment]];
// Set session ID and script support path (managed by us)
[taskEnvironment setObject:[self sessionID] forKey:PipeSessionIDEnvironmentKey];
[taskEnvironment setObject:[self scriptSupportPath] forKey:PipeScriptSupportPathEnvironmentKey];
[taskEnvironment setObject:[self temporaryWorkPath] forKey:PipeTemporaryWorkPathEnvironmentKey];
// Add non-sensitive variables
variableEnumerator = [[self variables] objectEnumerator];
while ((currentVariable = [variableEnumerator nextObject])) {
id value = [currentVariable value];
if ((value != nil) && ([currentVariable isSensitive] == NO)) {
switch ([currentVariable type]) {
case ScriptTextVariableType:
case ScriptPasswordVariableType:
[taskEnvironment setObject:value forKey:[currentVariable name]];
break;
case ScriptBooleanVariableType:
if ([value boolValue])
[taskEnvironment setObject:@"1" forKey:[currentVariable name]];
break;
case ScriptEnumVariableType:
if (([value intValue] >= 0) && ([value intValue] < [[currentVariable possibleValues] count])) {
[taskEnvironment setObject:[[currentVariable possibleValues] objectAtIndex:[value intValue]] forKey:[currentVariable name]];
}
break;
}
}
}
// Environment done!
[systemTask setEnvironment:taskEnvironment];
[taskEnvironment setObject:[self scriptPath] forKey:@"script"];
// Prepare arguments
taskArguments = [NSMutableArray array];
if ([self runCommand] != nil) {
NSArray *runComponents;
runComponents = [[self runCommand] commandLineComponents];
NSAssert([runComponents count] > 0, @"Invalid run command (must contain at least a full path to an executable)");
currentComponent = [[runComponents objectAtIndex:0] stringByReplacingPlaceholdersStartingWith:@"$" withStringsFromDictionary:taskEnvironment options:(JGPlaceholderCaseInsensitiveOption|JGPlaceholderKeepNonMatchingOption)];
// The actual task to be launched is the first component of the run command
[systemTask setLaunchPath:currentComponent];
// Build the argument array while expanding all environment variables.
componentEnumerator = [runComponents objectEnumerator];
[componentEnumerator nextObject]; // skip the first component
while ((currentComponent = [componentEnumerator nextObject])) {
currentComponent = [currentComponent stringByReplacingPlaceholdersStartingWith:@"$" withStringsFromDictionary:taskEnvironment options:(JGPlaceholderCaseInsensitiveOption|JGPlaceholderKeepNonMatchingOption)];
[taskArguments addObject:currentComponent];
}
// Now append the actual script arguments, again expanding environment variables.
componentEnumerator = [[[self argumentString] commandLineComponents] objectEnumerator];
while ((currentComponent = [componentEnumerator nextObject])) {
currentComponent = [currentComponent stringByReplacingPlaceholdersStartingWith:@"$" withStringsFromDictionary:taskEnvironment options:(JGPlaceholderCaseInsensitiveOption|JGPlaceholderKeepNonMatchingOption)];
[taskArguments addObject:currentComponent];
}
}
else {
[systemTask setLaunchPath:[self scriptPath]];
componentEnumerator = [[[self argumentString] commandLineComponents] objectEnumerator];
while ((currentComponent = [componentEnumerator nextObject])) {
currentComponent = [currentComponent stringByReplacingPlaceholdersStartingWith:@"$" withStringsFromDictionary:taskEnvironment options:(JGPlaceholderCaseInsensitiveOption|JGPlaceholderKeepNonMatchingOption)];
[taskArguments addObject:currentComponent];
}
}
[taskEnvironment removeObjectForKey:@"script"];
NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:[systemTask launchPath]], @"Script launch path '%@' does not exist", [systemTask launchPath]);
[systemTask setArguments:taskArguments];
[systemTask setCurrentDirectoryPath:[self temporaryFilePath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scriptTaskDidTerminate:) name:JGBufferedTaskDidTerminateNotification object:scriptTask];
[[self scriptTask] launch];
[[self scriptTask] flushInputDataInBackground];
[self retain];
}
- (void)abort
{
[[self scriptTask] abort];
}
- (void)scriptTaskDidTerminate:(NSNotification *)aNotification
{
[self autorelease];
}
@end