-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic-dock.py
executable file
·149 lines (127 loc) · 4.02 KB
/
dynamic-dock.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
#!/usr/bin/python
import stat
import os
import json
import sys
from docklib import Dock
try:
from urllib.request import urlopen # Python 3
from urllib.error import HTTPError, URLError
except ImportError:
from urllib2 import urlopen, HTTPError, URLError # Python 2
dock_url = "https://domain.org/dock/"
dock_backup = "/Users/Shared/dock/"
if not os.path.exists(dock_backup):
os.makedirs(dock_backup)
os.chmod(dock_backup, 0o777)
def get_applications(dock_name):
"""
Returns a dictionary of applications from a file called "dock_name.json"
"""
try:
response = urlopen("%s%s.json" % (dock_url, dock_name))
backup_file = "%s%s.json" % (dock_backup, dock_name)
if not os.path.exists(backup_file):
f = open(backup_file, "w")
f.close()
os.chmod(backup_file, 0o777)
f = open(backup_file, "w")
app_json = response.read()
f.write(app_json)
f.close()
dock_dict = json.loads(app_json)
except HTTPError:
"""
404 connection error -
The json for this manifest doesn't exist
"""
dock_dict = {}
pass
except URLError:
"""
Most likely we have lost connection
so we will fall back to the standard dock
"""
f = open("%s%s.json" % (dock_backup, dock_name), "r")
app_json = f.read()
dock_dict = json.loads(app_json)
except Exception as e:
print(e)
return dock_dict
def backup_dock(dock_name):
"""
Create a backup of the dock files in case the machine or server goes offline
"""
response = urlopen("%s%s.json" % (dock_url, dock_name))
return response
def get_munki_manifests():
"""
Returns a list of munki_manifests
"""
manifests = "/Library/Managed Installs/manifests"
munki_manifests = []
for manifest in os.listdir(manifests):
munki_manifests.append(manifest)
return munki_manifests
def get_app_list(target, key):
"""
Returns a list of applications from target
"""
target_dock_dict = get_applications(target)
target_applications = target_dock_dict[key]
return target_applications
def main():
"""
Run the program
"""
dock = Dock()
if dock.mod_count > 3:
sys.exit()
applications_pa = []
applications_po = []
# Get standard applications
try:
applications_pa = get_app_list("global_staff", "persistent-apps")
applications_po = get_app_list("global_staff", "persistent-others")
except:
pass
# Check for names of munki manifests
munki_manifests = get_munki_manifests()
# Check for existence of dock for manifests and clear if one doesn't want global
for munki_manifest in munki_manifests:
try:
if get_app_list(munki_manifest, "use-global") is False:
applications_pa = []
applications_po = []
except:
pass
# Add the applications
for munki_manifest in munki_manifests:
try:
applications_pa = applications_pa + get_app_list(
munki_manifest, "persistent-apps"
)
applications_po = applications_po + get_app_list(
munki_manifest, "persistent-others"
)
except:
pass
# Iterate over applications
dock.items["persistent-apps"] = []
for item in applications_pa:
if os.path.exists(item):
item = dock.makeDockAppEntry(item)
dock.items["persistent-apps"].append(item)
# iterate over others
dock.items["persistent-others"] = []
for item in applications_po:
if "~" in item:
item = dock.makeDockOtherEntry(
os.path.expanduser(item), arrangement=1, displayas=1, showas=3
)
else:
item = dock.makeDockOtherEntry(item, arrangement=1, displayas=1, showas=3)
dock.items["persistent-others"] = [item] + dock.items["persistent-others"]
dock.save()
if __name__ == "__main__":
main()