[196] | 1 | from Authorization import Authorization, kAuthorizationFlagDestroyRights
|
---|
| 2 | from Foundation import NSBundle, NSNotificationCenter, NSObject, NSURL
|
---|
| 3 | from AppKit import NSWorkspace
|
---|
| 4 | from appscript import app, k, its
|
---|
| 5 | import os, osax, subprocess, tempfile, SCNetworkReachability
|
---|
| 6 |
|
---|
| 7 | appBundle = NSBundle.mainBundle()
|
---|
| 8 |
|
---|
| 9 | def ensureKerberosPrincipalsValid(principals):
|
---|
| 10 | kerberosApp = app(id='edu.mit.Kerberos.KerberosApp')
|
---|
| 11 | validPrincipals = kerberosApp.caches.filter(its.time_remaining != 'Expired').principal.get()
|
---|
| 12 | for principal in principals:
|
---|
| 13 | if principal not in validPrincipals:
|
---|
| 14 | # XXX make these async
|
---|
| 15 | kerberosApp.renew_tickets(for_principal=principal)
|
---|
| 16 | # kerberosApp.get_tickets(for_principal=principal)
|
---|
| 17 |
|
---|
| 18 | def setVolumePercent(percent):
|
---|
| 19 | # XXX should use CoreAudio: see Pester/Source/NJRSoundManager.m
|
---|
| 20 | osax.setvolume(int(7 * percent))
|
---|
| 21 |
|
---|
| 22 | def _openURL(url):
|
---|
| 23 | ws = NSWorkspace.sharedWorkspace()
|
---|
| 24 | url = NSURL.URLWithString_(url)
|
---|
| 25 | return ws.openURL_(url)
|
---|
| 26 |
|
---|
| 27 | class _LDURLWatcher(NSObject):
|
---|
| 28 | def URLIsReachable_(self, notification):
|
---|
| 29 | _openURL(notification.object())
|
---|
| 30 |
|
---|
| 31 | _urlWatcher = _LDURLWatcher.alloc().init()
|
---|
| 32 | NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
|
---|
| 33 | _urlWatcher, 'URLIsReachable:', SCNetworkReachability.SCNetworkIsReachable,
|
---|
| 34 | None)
|
---|
| 35 |
|
---|
| 36 | def openURL(url):
|
---|
| 37 | hostname = NSURL.URLWithString_(url).host()
|
---|
| 38 | if not SCNetworkReachability.notify_when_reachable(hostname, url):
|
---|
| 39 | _openURL(url)
|
---|
| 40 |
|
---|
| 41 | def openInBackground(path):
|
---|
| 42 | ws = NSWorkspace.sharedWorkspace()
|
---|
| 43 | path = os.path.expanduser(path)
|
---|
| 44 | return ws.openFile_withApplication_andDeactivate_(path, None, False)
|
---|
| 45 |
|
---|
| 46 | def setDisplayBrightnessPercent(percent):
|
---|
| 47 | # XXX create brightness module
|
---|
| 48 | pathToBrightness = appBundle.pathForResource_ofType_('brightness', None)
|
---|
| 49 | subprocess.call([pathToBrightness, str(percent)], stderr=subprocess.STDOUT,
|
---|
| 50 | stdout=subprocess.PIPE)
|
---|
| 51 |
|
---|
| 52 | def setAdiumStatus(status):
|
---|
| 53 | # XXX doesn't match preset status if available
|
---|
| 54 | adiumApp = app(id='com.adiumX.adiumX')
|
---|
| 55 | if adiumApp.my_status_type() == k.offline:
|
---|
| 56 | adiumApp.my_status.status_message_string.set(status)
|
---|
| 57 | else:
|
---|
| 58 | adiumApp.my_status_message.set(status)
|
---|
| 59 | adiumApp.my_status_type.set(k.available)
|
---|
| 60 |
|
---|
| 61 | def terminalDo(command):
|
---|
[205] | 62 | # XXX if this launches Terminal, it inherits our PYTHONPATH
|
---|
[196] | 63 | terminalApp = app(id='com.apple.Terminal')
|
---|
[205] | 64 | # XXX this does not create a new Terminal window; fix
|
---|
[196] | 65 | terminalApp.do_script(command + '; exit')
|
---|
| 66 |
|
---|
| 67 | _auth = None
|
---|
| 68 |
|
---|
| 69 | def authorizationDo(command, *args):
|
---|
| 70 | global _auth
|
---|
| 71 | if not _auth:
|
---|
| 72 | _auth = Authorization(destroyflags=(kAuthorizationFlagDestroyRights,))
|
---|
| 73 | return _auth.executeWithPrivileges(command, *args)
|
---|
| 74 |
|
---|
| 75 | def stopVPNC():
|
---|
[198] | 76 | # killall uses your uid, not your euid to determine which user's
|
---|
| 77 | # processes to kill; without '-u root', this fails
|
---|
| 78 | authorizationDo('/usr/bin/killall', '-u', 'root', 'vpnc')
|
---|
[196] | 79 |
|
---|
| 80 | def startVPNC(vpncProfile=None):
|
---|
| 81 | stopVPNC()
|
---|
| 82 | args = ['--kernel-ipsec']
|
---|
| 83 | if vpncProfile:
|
---|
| 84 | args.append('/etc/vpnc/%s.conf' % vpncProfile)
|
---|
| 85 | # XXX get password from keychain, then use:
|
---|
| 86 | # authorizationDo('/usr/local/sbin/vpnc', *args)
|
---|
| 87 | terminalDo('sudo /usr/local/sbin/vpnc %s' % ' '.join(args))
|
---|