Files
munki/code/client/launchapp
2010-02-17 18:42:10 +00:00

74 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python
# encoding: utf-8
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
launchapp
Created by Greg Neagle on 2010-02-05.
Launches an app only if we're called in the current
GUI user's session.
Prevents multiple copies of the app from being launched
when Fast User Switching is in use
Intended for use by a launchd LaunchAgent.
"""
import sys
import os
import optparse
import subprocess
import time
def getconsoleuser():
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
cfuser = SCDynamicStoreCopyConsoleUser( None, None, None )
return cfuser[0]
def main():
consoleuser = getconsoleuser()
try:
thisuser = os.environ['USER']
except:
# when run via launchd at loginwindow context, os.environ['USER']
# is undefined, so we'll return root (the effective user)
thisuser = "root"
if (consoleuser == thisuser) or \
(consoleuser == None and thisuser == "root"):
p = optparse.OptionParser()
p.add_option('-a', action='store_true')
p.add_option('-b', action='store_true')
options, arguments = p.parse_args()
cmd = ["/usr/bin/open"]
if options.b:
cmd.append("-b")
else:
cmd.append("-a")
try:
cmd.append(arguments[0])
except:
print >>sys.stderr, "Must specify an app to launch!"
exit(-1)
retcode = subprocess.call(cmd)
exit(retcode)
else:
# sleep 10 seconds so we don't trigger launchd throttling
time.sleep(10)
exit(0)
if __name__ == '__main__':
main()