-
Notifications
You must be signed in to change notification settings - Fork 11
/
LSAutoTimeZone.py
executable file
·187 lines (153 loc) · 5.79 KB
/
LSAutoTimeZone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
"""
This script has a bunch of stolen work from @arubdesu, @pudquick, and Munki.
It is completely self contained and should do the following:
1. Enable Location Services globally
2. Enable "Set time zone automatically using current location"
3. Force a time zone location lookup at runtime
4. Set correct date, time, and time zone
Optionally set NTP servers.
References/borrowed code:
https://gist.github.com/arubdesu/b72585771a9f606ad800
https://gist.github.com/pudquick/ba235b7e90aafb9986158697a457a0d0
https://github.com/rtrouton/rtrouton_scripts/blob/master/rtrouton_scripts/time_settings/time_settings.sh
https://github.com/munki/munki/blob/master/code/client/munkilib/FoundationPlist.py
"""
import os
import platform
import subprocess
import sys
from Foundation import NSBundle
from Foundation import NSData
from Foundation import NSPropertyListSerialization
from Foundation import NSPropertyListMutableContainers
from Foundation import NSPropertyListXMLFormat_v1_0
NTP_SERVERS = ['time1.google.com', 'time.apple.com']
class FoundationPlistException(Exception):
"""Basic exception for plist errors"""
pass
class NSPropertyListSerializationException(FoundationPlistException):
"""Read/parse error for plists"""
pass
class NSPropertyListWriteException(FoundationPlistException):
"""Write error for plists"""
pass
def readPlist(filepath):
"""
Stolen from Munki FoundationPlist.py
Read a .plist file from filepath. Return the unpacked root object
(which is usually a dictionary).
"""
plistData = NSData.dataWithContentsOfFile_(filepath)
dataObject, dummy_plistFormat, error = (
NSPropertyListSerialization.
propertyListFromData_mutabilityOption_format_errorDescription_(
plistData, NSPropertyListMutableContainers, None, None))
if dataObject is None:
if error:
error = error.encode('ascii', 'ignore')
else:
error = "Unknown error"
errmsg = "%s in file %s" % (error, filepath)
raise NSPropertyListSerializationException(errmsg)
else:
return dataObject
def writePlist(dataObject, filepath):
"""
Stolen from Munki FoundationPlist.py
Write 'rootObject' as a plist to filepath.
"""
plistData, error = (
NSPropertyListSerialization.
dataFromPropertyList_format_errorDescription_(
dataObject, NSPropertyListXMLFormat_v1_0, None))
if plistData is None:
if error:
error = error.encode('ascii', 'ignore')
else:
error = "Unknown error"
raise NSPropertyListSerializationException(error)
else:
if plistData.writeToFile_atomically_(filepath, True):
return
else:
raise NSPropertyListWriteException(
"Failed to write plist data to %s" % filepath)
def ioreg():
"""get UUID to find locationd plist with"""
cmd = ['/usr/sbin/ioreg', '-rd1', '-c', 'IOPlatformExpertDevice']
full_reg = subprocess.check_output(cmd)
reg_list = full_reg.split('\n')
for reg in reg_list:
if reg.startswith(' "IOPlatformUUID"'):
uuid = reg[26:-1]
return uuid
def root_check():
"""Check for sudo access"""
if not os.geteuid() == 0:
exit("This must be run with sudo")
def os_check():
"""Should be good on 10.8 and above"""
maj_os_vers = platform.mac_ver()[0].split('.')[1]
if not 8 <= int(maj_os_vers):
exit("Your OS is not supported at this time: %s" %
platform.mac_ver()[0])
def sysprefs_boxchk():
"""Enables location services in sysprefs globally"""
uuid = ioreg()
path_stub = ("/private/var/db/locationd/Library/Preferences/"
"ByHost/com.apple.locationd.")
das_plist = path_stub + uuid.strip() + ".plist"
on_disk = readPlist(das_plist)
val = on_disk.get('LocationServicesEnabled', None)
if val != 1:
on_disk['LocationServicesEnabled'] = 1
writePlist(on_disk, das_plist)
os.chown(das_plist, 205, 205)
def service_handler(action):
"""Loads or unloads System's location services launchd job"""
launchctl = ['/bin/launchctl', action,
'/System/Library/LaunchDaemons/com.apple.locationd.plist']
subprocess.check_output(launchctl)
def autoset_timezone():
"""Enable Set time zone automatically using current location"""
das_plist = '/Library/Preferences/com.apple.timezone.auto.plist'
enabler = dict()
try:
enabler = readPlist(das_plist)
val = enabler.get('Active')
except:
val = 0
if val != 1:
enabler['Active'] = 1
writePlist(enabler, das_plist)
def timezone_lookup():
"""Force a timezone lookup right now"""
TZPP = NSBundle.bundleWithPath_("/System/Library/PreferencePanes/"
"DateAndTime.prefPane/Contents/"
"Resources/TimeZone.prefPane")
TimeZonePref = TZPP.classNamed_('TimeZonePref')
ATZAdminPrefererences = TZPP.classNamed_('ATZAdminPrefererences')
atzap = ATZAdminPrefererences.defaultPreferences()
pref = TimeZonePref.alloc().init()
atzap.addObserver_forKeyPath_options_context_(pref, "enabled", 0, 0)
result = pref._startAutoTimeZoneDaemon_(0x1)
def enable_ntp():
"""Enable your Mac to use NTP to set clock time and set NTP servers"""
ntpconf = open('/etc/ntp.conf', 'w')
for server in NTP_SERVERS:
ntpconf.write("{0}{1}\n".format('server ', server))
enable_ntp = ['/usr/sbin/systemsetup', '-setusingnetworktime', 'on']
subprocess.check_output(enable_ntp)
def main():
"""gimme some main"""
root_check()
os_check()
service_handler('unload')
sysprefs_boxchk()
service_handler('load')
autoset_timezone()
timezone_lookup()
# enable_ntp()
if __name__ == '__main__':
main()