Esempio n. 1
0
    def launchScript_(self, args):
        LogDebug("launchScript:")

        if (self.authPassword() is None) and (os.getuid() != 0):
            # Use GUI dialog to elevate privileges.
            task = STPrivilegedTask.alloc().init()
            task.setLaunchPath_(args[0])
            task.setArguments_(args[1:])
            status = task.launch()
            LogNotice("Install task launched with return code: %d", status)
            if status:
                self.performSelectorOnMainThread_withObject_waitUntilDone_(
                    self.handleLaunchScriptError_, status, False)
        else:
            task = NSTask.alloc().init()
            if os.getuid() == 0:
                # No privilege elevation necessary.
                task.setLaunchPath_(args[0])
                task.setArguments_(args[1:])
            else:
                # Use sudo to elevate privileges.
                task.setLaunchPath_("/usr/bin/sudo")
                task.setArguments_(["-kSE"] + args)
                # Send password to sudo on stdin.
                passwordpipe = NSPipe.alloc().init()
                task.setStandardInput_(passwordpipe.fileHandleForReading())
                task.setStandardOutput_(
                    NSFileHandle.fileHandleWithNullDevice())
                task.setStandardError_(NSFileHandle.fileHandleWithNullDevice())
                writer = passwordpipe.fileHandleForWriting()
                pwd = NSString.stringWithString_(self.authPassword() + "\n")
                writer.writeData_(pwd.dataUsingEncoding_(NSUTF8StringEncoding))
                writer.closeFile()
            try:
                task.launch()
                LogNotice("Install task launched%@",
                          " with sudo" if os.getuid() != 0 else "")
            except BaseException as e:
                LogWarning("Install task launch failed with exception")
                self.performSelectorOnMainThread_withObject_waitUntilDone_(
                    self.handleLaunchScriptError_, str(e), False)
                return
            task.waitUntilExit()
            LogNotice("Install task finished with exit status %d",
                      task.terminationStatus())
            if task.terminationStatus() != 0:
                self.performSelectorOnMainThread_withObject_waitUntilDone_(
                    self.handleLaunchScriptError_,
                    "Install task failed with status %d" %
                    task.terminationStatus(), False)
Esempio n. 2
0
 def launchScript_(self, args):
     LogDebug("launchScript:")
     
     if (self.authPassword() is None) and (os.getuid() != 0):
         # Use GUI dialog to elevate privileges.
         task = STPrivilegedTask.alloc().init()
         task.setLaunchPath_(args[0])
         task.setArguments_(args[1:])
         status = task.launch()
         LogNotice("Install task launched with return code: %d", status)
         if status:
             self.performSelectorOnMainThread_withObject_waitUntilDone_(self.handleLaunchScriptError_, status, False)
     else:
         task = NSTask.alloc().init()
         if os.getuid() == 0:
             # No privilege elevation necessary.
             task.setLaunchPath_(args[0])
             task.setArguments_(args[1:])
         else:
             # Use sudo to elevate privileges.
             task.setLaunchPath_("/usr/bin/sudo")
             task.setArguments_(["-kSE"] + args)
             # Send password to sudo on stdin.
             passwordpipe = NSPipe.alloc().init()
             task.setStandardInput_(passwordpipe.fileHandleForReading())
             task.setStandardOutput_(NSFileHandle.fileHandleWithNullDevice())
             task.setStandardError_(NSFileHandle.fileHandleWithNullDevice())
             writer = passwordpipe.fileHandleForWriting()
             pwd = NSString.stringWithString_(self.authPassword() + "\n")
             writer.writeData_(pwd.dataUsingEncoding_(NSUTF8StringEncoding))
             writer.closeFile()
         try:
             task.launch()
             LogNotice("Install task launched%@", " with sudo" if os.getuid() != 0 else "")
         except BaseException as e:
             LogWarning("Install task launch failed with exception")
             self.performSelectorOnMainThread_withObject_waitUntilDone_(self.handleLaunchScriptError_, str(e), False)
             return
         task.waitUntilExit()
         LogNotice("Install task finished with exit status %d", task.terminationStatus())
         if task.terminationStatus() != 0:
             self.performSelectorOnMainThread_withObject_waitUntilDone_(self.handleLaunchScriptError_,
                                                                        "Install task failed with status %d" % task.terminationStatus(),
                                                                        False)