[28253] users/rhwood/Pallet/PortAuthority.m

source_changes at macosforge.org source_changes at macosforge.org
Sun Aug 26 02:56:41 PDT 2007


Revision: 28253
          http://trac.macosforge.org/projects/macports/changeset/28253
Author:   rhwood at macports.org
Date:     2007-08-26 02:56:40 -0700 (Sun, 26 Aug 2007)

Log Message:
-----------
Add the MPAgent (PalletHelper) calls and delegate methods.

Modified Paths:
--------------
    users/rhwood/Pallet/PortAuthority.m

Modified: users/rhwood/Pallet/PortAuthority.m
===================================================================
--- users/rhwood/Pallet/PortAuthority.m	2007-08-26 09:55:14 UTC (rev 28252)
+++ users/rhwood/Pallet/PortAuthority.m	2007-08-26 09:56:40 UTC (rev 28253)
@@ -44,6 +44,19 @@
 
 - (void)awakeFromNib
 {
+	// Get a MacPorts Interpeter
+	interpeter = [[MPInterp alloc] init];
+	if (![interpeter loadPackage:MPPackageName version:MPPackageVersion usingCommand:MPPackageInit]) {
+		NSLog(@"Failed to load interpeter");
+		exit(1);
+	}
+	if (![interpeter redirectCommand:MPUIPuts toObject:self]) {
+		NSLog(@"Failed to redirect interpeter input");
+		exit(1);
+	}
+	NSLog(@"Printing system_options");
+	NSLog([[interpeter getVariable:[MPObject objectWithString:@"system_options"]] stringValue]);
+	NSLog(@"system_options printed");
 	// Load/set preferences/defaults
 	if (![[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"portInstallationPath"]) {
 		[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:@"/opt/local" forKey:@"portInstallationPath"];
@@ -68,8 +81,10 @@
 	// Setup the port task
 	portIsRunning = NO;
 	launcher = [[NSBundle mainBundle] pathForResource:@"Launcher" ofType:nil];
+	agentTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
 	authPortTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
 	killTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
+	[agentTask setDelegate:self];
 	[authPortTask setDelegate:self];
 	[killTask setDelegate:self];
 	// Setup the value transformers used in bindings
@@ -78,7 +93,6 @@
 	// UI Tweaks
 	[[portsList headerView] setMenu:portsListHeaderMenu];
 	[[portsList cornerView] setMenu:portsListHeaderMenu];
-	
 	// TESTING TESTING TESTING
 	NSLog(@"Ports Database path: %@", [self configurationSetting:@"portdbpath"]);
 }
@@ -88,19 +102,44 @@
  */
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
+	// configure and register the connection to the PalletHelper
+	connection = [NSConnection defaultConnection];
+    [connection setRootObject: self];
+    [connection enableMultipleThreads];
+    if ([connection registerName:MPAppMessagePort] == NO) {
+        NSRunAlertPanel(@"Pallet", @"Could not register Pallet connection on this host.", nil, @"Quit", nil);
+        [NSApp terminate: self];
+    }
+	
 	// sync or selfupdate as required
 	if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"updateMacPortsOnStartup"] boolValue] == YES) {
 		[self updateMacPorts:nil];
 	} else if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"synchronizePortsListOnStartup"] boolValue] == YES) {
 		[self syncPortsList:nil];
 	}
-	// List ports
+	// List ports	
 	[self getPortIndexes];
 	[self populatePortIndex];
 }
 
 #pragma mark PORT INDEX
 
+- (NSDictionary *)tPorts
+{
+	if (!_ports) {
+		NSPropertyListFormat format;
+		NSString *error;
+		NSData *portsData = [[self agent] portsData];
+		_ports= [[NSPropertyListSerialization propertyListFromData:portsData
+												  mutabilityOption:NSPropertyListMutableContainersAndLeaves
+															format:&format
+												  errorDescription:&error]
+			retain];
+	}
+	NSLog(_ports);
+	return _ports;
+}
+
 - (void)getPortIndexes
 {
 	NSArray *conf;
@@ -113,7 +152,7 @@
 
 	source = [[NSMutableString alloc] init];
 	sources = [[NSMutableDictionary alloc] init];
-	path = [macPortsPath stringByAppendingPathComponent:@"etc/ports/sources.conf"];
+	path = [macPortsPath stringByAppendingPathComponent:@"etc/macports/sources.conf"];
 	file = [[NSString alloc] initWithContentsOfFile:path];
 	conf = [[NSArray alloc] initWithArray:[file componentsSeparatedByString:@"\n"]];
 	if ([conf count] == 1) {
@@ -141,14 +180,13 @@
 - (NSString *)pathToPortIndexWithSource:(NSString *)source
 {
 	NSMutableString *path;
+	NSRange range;
 	path = [[NSMutableString alloc] init];
-	if ([[source substringToIndex:4] isEqualToString:@"file"]) {
-		[path setString:[source substringFromIndex:7]];
-	} else if ([[source substringToIndex:5] isEqualToString:@"rsync"]) {
-		[path setString:source];
-		[path replaceOccurrencesOfString:@"/" withString:@"_" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [path length])];
-		[path replaceOccurrencesOfString:@":__" withString:@"." options:NSCaseInsensitiveSearch range:NSMakeRange(0, [path length])];
-		[path setString:[[macPortsPath stringByAppendingPathComponent:@"var/db/dports/sources"] stringByAppendingPathComponent:path]];
+	range = [source rangeOfString:@"://"];
+	[path setString:[source substringFromIndex:(range.location + range.length)]];
+	// if the source URL is not a file:// type URL, prepend it with ${prefix}/var/macports/sources
+	if (![[source substringToIndex:4] isEqualToString:@"file"]) {
+		[path setString:[[macPortsPath stringByAppendingPathComponent:@"var/macports/sources"] stringByAppendingPathComponent:path]];
 	}
 	return [path stringByAppendingPathComponent:@"PortIndex"];
 }
@@ -303,7 +341,7 @@
 	if ([portSettings valueForKey:setting] == nil) {
 		int tclResult;
 		Tcl_Interp *tcl;
-		NSString *script = [[NSString alloc] initWithFormat:@"source %@/etc/ports/ports.conf\nproc %@ x {puts [list %2$@ $x]}", macPortsPath, setting, nil];
+		NSString *script = [[NSString alloc] initWithFormat:@"source %@/etc/macports/macports.conf\nproc %@ x {puts [list %2$@ $x]}", macPortsPath, setting, nil];
 		NSLog(script);
 		tcl = Tcl_CreateInterp();
 		tclResult = Tcl_EvalEx(tcl, [script cString], -1, TCL_EVAL_GLOBAL);
@@ -571,6 +609,150 @@
 	return YES;
 }
 
+#pragma mark MACPORTS INTERPETER
+
+- (NSDictionary *)listPorts:(NSString *)query
+{    
+    Tcl_Obj **objv;
+    int count = 0, i=0;
+    MPObject *result;
+    NSString *error;
+    NSMutableDictionary *portsData;
+
+	//    [_ports removeAllObjects];
+    result = [interpeter evaluateCommand:[MPObject objectWithString:MPSearchCommand] 
+							  withObject:[MPObject objectWithString:query]];
+    Tcl_ListObjGetElements(NULL, [result tclObj], &count, &objv);	
+    while (++i < count) {	
+        NSMutableDictionary *portDict = [NSMutableDictionary dictionary];
+        Tcl_Obj **innerobjs;
+        int innercount = 0, j = 0;    
+        Tcl_ListObjGetElements(NULL, objv[i++], &innercount, &innerobjs);
+        while (j < innercount)
+        {
+            MPObject *keyObject = [MPObject objectWithTclObj: innerobjs[j++]];
+            MPObject *valueObject = [MPObject objectWithTclObj: innerobjs[j++]];
+            NSString *key = [keyObject stringValue];
+            id value;
+            if ([key isEqualToString: MPCategoriesKey] || [key isEqualToString: MPMaintainersKey]) {
+                NSEnumerator *enm = [[[valueObject stringValue] componentsSeparatedByString: @" "] objectEnumerator];
+                NSString *component;
+                value = [NSMutableArray array];
+                while (component = [enm nextObject]) {
+                    if (![value containsObject: component]) {
+                        [value addObject: component];
+                    }
+                }
+            } else if ([key rangeOfString: MPDependsKey].location != NSNotFound) {
+                NSEnumerator *dependencyEnm = [[[valueObject stringValue] componentsSeparatedByString: @" "] objectEnumerator];
+                NSString *component;
+                value = [NSMutableArray array];
+                while (component = [dependencyEnm nextObject]) {
+                    NSString *dependencyName = [[component componentsSeparatedByString: @":"] objectAtIndex: 2];
+                    if (![value containsObject: dependencyName]) {
+                        [value addObject: dependencyName];
+                    }
+                }
+                key = MPDependsKey;
+            } else {
+                value = [valueObject stringValue];
+            }
+            [portDict setObject:value forKey:key];
+        }
+        [portsData setObject:portDict forKey:[portDict objectForKey:MPNameKey]];
+    }
+    return [NSDictionary dictionaryWithDictionary:portsData];
+}
+
+- (MPObject *) ui_puts: (NSArray *)array 
+{
+    NSDictionary *message = [[array objectAtIndex: 1] dictionaryValue];
+    if (message == nil)
+    	return [MPObject objectWithString: MPNoResult];
+	
+    NSString *data = [message objectForKey: @"data"];
+    NSString *priority = [message objectForKey: @"priority"];
+    if (data == nil || priority == nil)
+    	return [MPObject objectWithString: MPNoResult];
+	
+    id delegate = [[[NSThread currentThread] threadDictionary] objectForKey: @"delegate"];
+    //[delegate displayMessage: message forPortName: [_currentOp objectForKey: @"portName"]];
+    return [MPObject objectWithString: MPYesResult];
+}
+
+#pragma mark MACPORTS AGENT
+
+- (id <MPAgentProtocol>)agent {
+	if (agentIsBusy) {
+		return nil;
+	}
+	agentIsBusy = YES;
+	if (!agent) {
+		NSString *agentPath = [[NSBundle mainBundle] pathForResource:@"PalletHelper" ofType:@""];
+		int i;
+//		[self launchAuthorizedExecutableWithArguments:];
+		[agentTask setArguments:[NSArray arrayWithObjects:agentPath, nil]];
+		[agentTask authorizeWithQuery];
+		[agentTask start];
+		for (i = 0; i < 10; i++) {
+			connection = [[NSConnection connectionWithRegisteredName:MPAgentMessagePort host:nil] retain];
+			if (connection) {
+				break;
+			}
+			sleep(1);
+		}
+		if (!connection) {
+			NSRunAlertPanel(@"Pallet", @"Could not connect to PalletHelper.", nil, nil, nil);
+			// exit(0); don't exit as I want to see the bloody console window
+		}
+		agent = [[connection rootProxy] retain];
+		[(NSDistantObject *)agent setProtocolForProxy:@protocol(MPAgentProtocol)];
+		[connection setRootObject:self];
+		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionDidDie:) name:NSConnectionDidDieNotification object:connectionInvalid];
+	}
+	agentIsBusy = NO;
+	return agent;
+}
+
+- (void)connectionDidDie:(id)server
+{
+    NSRunAlertPanel(@"Pallet", @"Connection to PalletHelper died!", nil, nil, nil);
+    agent = nil;
+    // resetting agent to nil will cause a new instance of the agent to
+    // be spawned by [PortAuthority agent] next time someone tries to access it
+}
+
+- (oneway void)displayMessage:(in bycopy NSDictionary *)message forPortName:(in bycopy NSString *)portName {
+	[self appendOutput:[message objectForKey:@"data"]];
+}
+
+- (void)executeTarget:(NSString *)target forPortName:(NSString *)portName {
+	NSDictionary *operation = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
+		portName, @"portName",
+		target, "@target",
+		nil];
+	if (!operations) {
+		operations = [[NSMutableArray alloc] init];
+	}
+	[operations addObject:operation];
+	[[self agent] executeTarget:target forPortName:portName];
+}
+
+- (BOOL)shouldPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName {
+	return YES;
+}
+
+- (oneway void)willPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName {
+	[progressIndicator startAnimation:nil];
+	[NSApp setApplicationIconImage:[NSImage imageNamed:@"ApplicationIconBusy"]];
+}
+
+- (oneway void)didPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName withResult:(in bycopy NSString *)result {
+	[progressIndicator stopAnimation:nil];
+	[NSApp setApplicationIconImage:[NSImage imageNamed:@"ApplicationIcon"]];
+	[status setStringValue:@""];
+}
+
 #pragma mark AUTHORIZED EXECUTABLE DELEGATES / TASK WRAPPER CONTROLLER
 // This callback is implemented as part of conforming to the ProcessController protocol.
 // It will be called whenever there is output from the TaskWrapper.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20070826/5447dbed/attachment.html


More information about the macports-changes mailing list