Skip to content

Instantly share code, notes, and snippets.

@arubdesu
Last active November 2, 2015 19:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arubdesu/346a9be9b4fad4c32ff6 to your computer and use it in GitHub Desktop.
Save arubdesu/346a9be9b4fad4c32ff6 to your computer and use it in GitHub Desktop.
Uses a web geoip service and cocoaDialog (via Googles gmacpyutil) to allow end users to change the time zone on their Mac. Deployable via Munki OnDemand task
#!/usr/bin/python
'''Uses a web geoip service and cocoaDialog to allow end users to change
the time zone on their Mac (assuming they can get on the internet)'''
import json
import subprocess
import sys
import urllib2
from gmacpyutil import cocoadialog
def grab_geoip():
'''returns probable timezone info via geoip lookup'''
geoip_checksite = 'http://freegeoip.net/json/'
response = urllib2.urlopen(geoip_checksite).read()
if response:
dict_response = json.loads(response)
tzone = dict_response.get('time_zone', None)
return tzone
else:
print 'foo'
def change_tzone(region):
'''heavy lifting to interact w/ sys setup'''
cmd = ['/usr/sbin/systemsetup', '-settimezone', region]
result = subprocess.check_call(cmd)
return result
def get_tzone():
'''Grab current tz from sys setup'''
cmd = ['/usr/sbin/systemsetup', '-gettimezone']
result = subprocess.check_output(cmd)
return result.strip()[11:]
def cleanup(result):
'''catch errors with feedback, or show success'''
if result == 'foo':
tooold_msg = cocoadialog.MsgBox(title="Sorry!")
tooold_msg.SetInformativeText("There was an error setting your timezone. Please schedule a time for follow-up with the servicedesk.")
button_pressed = tooold_msg.Show()
button = button_pressed.split('\n')[0]
sys.exit(1)
elif result == 'already there':
complete_msg = cocoadialog.MsgBox(title="Pardon")
complete_msg.SetInformativeText("Your computer already seems to be set to the correct timezone. Please schedule a time for follow-up with the servicedesk if you still need assistance.")
button_pressed = complete_msg.Show()
button = button_pressed.split('\n')[0]
if button:
sys.exit(0)
else:
complete_msg = cocoadialog.MsgBox(title="Complete")
complete_msg.SetText("Glad to be of assistance!")
button_pressed = complete_msg.Show()
button = button_pressed.split('\n')[0]
if button:
sys.exit(0)
def main():
'''gimme some main'''
region = grab_geoip()
current_tz = get_tzone()
if region == current_tz:
cleanup('already there')
cd_title = "TimeZone Change Utility"
welcome_prompt = cocoadialog.MsgBox(title=cd_title)
welcome_prompt.SetText("We've detected this timezone: " + region)
welcome_prompt.SetInformativeText("Shall we go ahead and change to that timezone?")
welcome_prompt.SetButton1('Okay')
welcome_prompt.SetButton2('Cancel')
welcome_showed = welcome_prompt.Show()
button, __ = welcome_showed.split('\n')[:2]
if button == 'Okay':
exit_code = change_tzone(region)
if exit_code != 0:
cleanup('foo')
else:
cleanup('success')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment