This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dock-maintainer-updater.py
107 lines (96 loc) · 3.67 KB
/
dock-maintainer-updater.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
#!/usr/bin/python
'''
Downloads file as needed for dock-maintainer. Sets extended attribute to be
the last mod date for the plist according to the server. Suggested work around
by elios on macadmins.org
'''
import urllib2
import datetime
from time import mktime
import os
import logging
import xattr
from Foundation import CFPreferencesCopyAppValue
logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p',
level=logging.INFO,
filename=os.path.normpath("/Library/Logs/dock-maintainer.log"))
stdout_logging = logging.StreamHandler()
stdout_logging.setFormatter(logging.Formatter())
logging.getLogger().addHandler(stdout_logging)
def downloadFile(url, filepath, attributedate):
'''
Downloads url to filepath
'''
with open(filepath, "wb") as code:
code.write(url.read())
xattr.setxattr(filepath, 'dock-maintainer.Last-Modified-date', str(attributedate))
logging.info("Downloaded File to %s", filepath)
return filepath
def wait_for_internet_connection():
'''
Shameless swipe from stackoverflow.
https://stackoverflow.com/a/10609378
'''
while True:
try:
response = urllib2.urlopen('https://www.google.com/', timeout=1)
return
except urllib2.URLError:
pass
def main():
'''
Main Controlling Module:
- Checks preferences set
- Checks for Path, if not creates
- Downloads plist if needed
'''
keys = {}
keys["ManagedUser"] = CFPreferencesCopyAppValue("ManagedUser",
"com.github.wardsparadox.dock-maintainer")
keys["ServerURL"] = CFPreferencesCopyAppValue("ServerURL",
"com.github.wardsparadox.dock-maintainer")
keys["FileName"] = CFPreferencesCopyAppValue("FileName",
"com.github.wardsparadox.dock-maintainer")
path = os.path.realpath("/Library/Application Support/com.github.wardsparadox.dock-maintainer")
if os.path.exists(path):
logging.info("Path exists at %s", path)
else:
logging.info("Path not found, creating at %s", path)
os.mkdir(path, 0755)
if keys["ManagedUser"] is None:
logging.error("No ManagedUser Preference set")
exit(2)
else:
plistfilepath = os.path.join(path, keys["ManagedUser"])
completeurl = os.path.join(keys["ServerURL"], keys["FileName"])
try:
fileurl = urllib2.urlopen(completeurl)
meta = fileurl.info().getheaders("Last-Modified")[0]
servermod = datetime.datetime.fromtimestamp(mktime(
datetime.datetime.strptime(
meta, "%a, %d %b %Y %X GMT").timetuple()))
except urllib2.HTTPError:
logging.error("Can not connect to url")
exit(1)
if not os.path.isfile(plistfilepath):
logging.info("File not found! Downloading")
downloadFile(fileurl, plistfilepath, servermod)
exit(0)
if xattr.listxattr(plistfilepath):
logging.info("Got File Attributes")
else:
logging.info("No Attributes found! Downloading and setting them")
downloadFile(fileurl, plistfilepath, servermod)
exit(0)
filexattrdate = xattr.getxattr(plistfilepath,
'dock-maintainer.Last-Modified-date')
if str(servermod) != filexattrdate:
logging.info("File is out of date")
downloadFile(fileurl, plistfilepath, servermod)
else:
logging.info("File is synced.")
exit(0)
if __name__ == '__main__':
wait_for_internet_connection()
main()