-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsystemconfigurator.py
executable file
·313 lines (264 loc) · 13.8 KB
/
systemconfigurator.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
import sys
import os
import re
import shutil
import argparse
import subprocess
import plistlib
import logging
from pathlib import Path
from typing import List, Set, Dict, Tuple, Optional, Any, NoReturn
class Tool(object):
def __init__(self, args):
self.args = args
self.cached_icloud_account_info = None
def run(self):
self.check_icloud_account()
self.icloud_service_enabled(identifier='MOBILE_DOCUMENTS', label='Mobile Documents')
self.icloud_service_enabled(identifier='CLOUDDESKTOP', label='iCloud Drive > Desktop')
self.copy_to_directory(source='~/Documents/Fonts/LucasFonts/OpenType/TheSansMonoCd OT/TheSansMonoCd-W5Regular.otf', destination_directory='~/Library/Fonts')
self.copy_to_directory(source='~/Documents/Fonts/LucasFonts/OpenType/TheSansMonoCd OT/TheSansMonoCd-W5RegularItal.otf', destination_directory='~/Library/Fonts')
self.copy_to_directory(source='~/Documents/Fonts/LucasFonts/OpenType/TheSansMonoCd OT/TheSansMonoCd-W7Bold.otf', destination_directory='~/Library/Fonts')
self.copy_to_directory(source='~/Documents/Fonts/LucasFonts/OpenType/TheSansMonoCd OT/TheSansMonoCd-W7BoldItalic.otf', destination_directory='~/Library/Fonts')
self.check_terminal_window_settings(name='Marc’s Settings', source='~/Documents/Theme Settings/Terminal/Marc’s Settings.terminal')
self.symlink(at="~/bin", destination="Documents/bin")
self.symlink(at="~/git", destination="Documents/git")
self.symlink(at="~/.dotfiles", destination="git/dotfiles")
self.symlink(at="~/.oh-my-zsh", destination=".dotfiles/.oh-my-zsh")
self.symlink(at="~/.config", destination=".dotfiles/.config")
self.symlink(at="~/.zshrc", destination=".dotfiles/.zshrc")
self.symlink(at="~/.vimrc", destination=".dotfiles/.vimrc")
self.symlink(at="~/.logrc", destination=".dotfiles/.logrc")
self.symlink(at="~/.jupyter", destination=".dotfiles/.jupyter")
self.symlink(at="~/.gitconfig", destination=".dotfiles/.gitconfig")
self.symlink(at="~/.gitignore.global", destination=".dotfiles/.gitignore.global")
self.symlink(at="~/.cplstoreconfig", destination=".dotfiles/.cplstoreconfig")
self.login_item('Activity Monitor', '/System/Applications/Utilities/Activity Monitor.app')
# Most user defaults tracked down with:
# log stream --debug --predicate 'subsystem = "com.apple.defaults" and eventMessage contains "setting {"'
self.user_defaults('com.apple.Dock', {'autohide': True, 'orientation': 'left'}, 'Dock')
self.user_defaults('com.apple.ActivityMonitor', {'IconType': 6}, 'Activity Monitor')
self.user_defaults('-g', {
'InitialKeyRepeat': 15,
'KeyRepeat': 2,
'NSAutomaticPeriodSubstitutionEnabled': 0,
'NSAutomaticPeriodSubstitutionEnabled': 0,
'NSAutomaticCapitalizationEnabled': 0,
'NSAutomaticSpellingCorrectionEnabled': 0,
'WebAutomaticSpellingCorrectionEnabled': 0,
'NSAutomaticQuoteSubstitutionEnabled': 0,
'NSAutomaticDashSubstitutionEnabled': 0,
})
# https://www.macworld.com/article/178496/crashreport.html
# https://www.defaults-write.com/os-x-make-crash-reporter-appear-as-a-notification/
self.user_defaults('com.apple.CrashReporter', {'DialogType': 'developer', 'UseRegularActivationPolicy': True, 'UseUNC': True})
# This one doesn't work because this domain is not writable via command line tools.
self.user_defaults('com.apple.universalaccess', {
'com.apple.custommenu.apps': ['com.apple.mail'],
'showWindowTitlebarIcons': True,
'reduceTransparency': True,
})
self.user_defaults('com.apple.iCal', {
'first day of week': 1,
})
self.user_defaults('com.apple.mail', {
'ConversationViewSortDescending': True,
'SuppressDeliveryFailure': True,
'AutoReplyFormat': True,
'NSFixedPitchFont': 'TheSansMonoCd-W5Regular',
'AlertForNonmatchingDomains': True,
'DomainForMatching': ['@apple.com', '@group.apple.com'],
'NSFixedPitchFontSize': 12,
'NumberOfSnippetLines': 1,
'MailUserNotificationScope': 2,
'CustomHeaders': ['List-Id', 'X-Member-Count'],
'NSUserKeyEquivalents': {'Mark All Messages as Read': '^r'}
})
- [ ] defaults write com.apple.finder QuitMenuItem -bool YES && killall Finder
- [ ] defaults write com.apple.finder _FXShowPosixPathInTitle -bool true
self.user_defaults('com.apple.Terminal', {'NewWindowWorkingDirectoryBehavior': 2})
self.app_store_app('BetterSnapTool', '417375580')
self.user_defaults('com.hegenberg.BetterSnapTool', {
'shiftMove': True,
'cmdMove': True,
'shiftResize': True,
'optResize': True,
'snapBottomLeft': False,
'snapBottomRight': False,
'snapLeft': False,
'snapRight': False,
'snapTop': False,
'snapTopLeft': False,
'snapTopRight': False,
})
self.app_store_app('Paste', '967805235')
self.app_store_app('Step Two', '1448916662')
self.app_store_app('Slack', '803453959')
self.app_store_app('1Blocker', '1365531024')
return 0
def app_store_app(self, name: str, identifier: str):
cmd = '/Users/liyanage/bin/mas', 'account'
result = subprocess.run(cmd, capture_output=True, text=True)
if '@' not in result.stdout:
self.fatal_exit(f'Please sign in to the Mac App Store and ensure the "mas" command line tool is installed: https://github.com/mas-cli/mas')
cmd = '/Users/liyanage/bin/mas', 'list'
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
self.fatal_exit(f'"mas list" returned non-zero exit code: {result.stderr}')
installed_apps = re.findall(r'^(\d+)\s+(.+?)\s+\((.+)\)$', result.stdout, flags=re.MULTILINE)
installed_apps = {x[0]: x[1] for x in installed_apps}
if identifier in installed_apps and installed_apps[identifier] == name:
return
print(f'Installing "{name}" from Mac App Store')
cmd = '/Users/liyanage/bin/mas', 'install', str(identifier)
result = subprocess.run(cmd, text=True, capture_output=True)
def login_item(self, name, path):
cmd = 'osascript', '-e', 'tell application "System Events" to login items'
result = subprocess.run(cmd, text=True, capture_output=True)
current_items = re.findall(r'login item (.+?)[,\n]', result.stdout)
if name in current_items:
return
path = Path(path)
if not path.exists():
self.fatal_exit(f'Path for login item does not exist: {path.as_posix()}')
cmd = 'osascript', '-e', f'tell application "System Events" to make login item at end with properties {{path:"{path.as_posix()}", hidden:false}}'
subprocess.run(cmd)
def user_defaults(self, domain: str, pairs: Dict[str, Any], process_to_kill: str=None):
def defaults_value_arguments_for_value(key, value):
if isinstance(value, bool):
return ['-bool', 'YES' if value else 'NO']
elif isinstance(value, int):
return ['-int', str(value)]
elif isinstance(value, str):
return [value]
else:
self.fatal_exit(f'Unknown data type "{type(value)}" for defaults domain/key "{domain}/{key}"')
def run_user_default_command(cmd):
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f'Unable to execute command "{" ".join(cmd)}", please perform the equivalent UI action manually. Error: "{result.stderr.strip()}"')
current_defaults = self.user_defaults_for_domain(domain)
did_write = False
for key, value in pairs.items():
current_value = current_defaults.get(key)
cmd = ['defaults', 'write', domain, key]
if isinstance(value, dict):
for key2, value2 in value.items():
if isinstance(current_value, dict) and key2 in current_value and current_value[key2] == value2:
continue
did_write = True
cmd2 = cmd + ['-dict-add', key2] + defaults_value_arguments_for_value(f'{key}.{key2}', value2)
run_user_default_command(cmd2)
elif isinstance(value, list):
for value2 in value:
if isinstance(current_value, list) and value2 in current_value:
continue
did_write = True
cmd2 = cmd + ['-array-add'] + defaults_value_arguments_for_value(key, value2)
run_user_default_command(cmd2)
else:
if current_value is not None and current_value == value:
continue
did_write = True
cmd += defaults_value_arguments_for_value(key, value)
run_user_default_command(cmd)
if did_write and process_to_kill:
cmd = 'pkill', '-x', process_to_kill
subprocess.run(cmd)
def check_terminal_window_settings(self, name, source):
source = self.path_for_source(source)
defaults = self.user_defaults_for_domain('com.apple.Terminal')
window_settings = defaults['Window Settings']
logging.debug(window_settings.keys())
if name not in window_settings:
self.fatal_exit(f'Terminal config "{name}" not found in Terminal preferences, please import config "{source}"')
keys = 'Startup Window Settings', 'Default Window Settings'
for key in keys:
default_window_settings = defaults.get(key)
if default_window_settings != name:
cmd = 'defaults', 'write', 'com.apple.Terminal', key, name
subprocess.run(cmd)
def user_defaults_for_domain(self, domain):
# cmd = 'defaults', 'export', domain, '-'
cmd = f'defaults export {domain} - | plutil -convert binary1 -o - -'
return self.plist_for_command(cmd, shell=True)
def path_for_source(self, path_string):
path = Path(path_string).expanduser()
if not path.exists():
self.fatal_exit(f'"{path}" does not exist')
return path
def symlink(self, at, destination):
at = Path(at).expanduser()
destination = Path(destination).expanduser()
if at.exists():
if at.is_symlink():
current_destination = os.readlink(at)
if current_destination == destination.as_posix():
return
self.fatal_exit(f'"{at}" is a symlink but points to "{current_destination}" instead of "{destination}"')
self.fatal_exit(f'"{at}" exists but is not a symlink')
at.symlink_to(destination)
def copy_to_directory(self, source, destination_directory, create_destination=False):
source = self.path_for_source(source)
destination_dir = Path(destination_directory).expanduser()
if destination_dir.exists():
if not destination_dir.is_dir():
self.fatal_exit(f'"{destination_dir}" exists but is not a directory')
else:
if create_destination:
destination_dir.mkdir(parents=True)
else:
self.fatal_exit(f'"{destination_dir}" directory does not exist and create_destination is not set')
destination = destination_dir / source.name
if destination.exists():
if not destination.is_file():
self.fatal_exit(f'"{destination}" exists but is not a file')
if destination.stat().st_size == source.stat().st_size:
return
else:
self.fatal_exit(f'"{destination}" exists but has different length from "{source}"')
shutil.copy(source, destination)
def icloud_service_enabled(self, *, identifier, label):
is_enabled = True
for service in self.icloud_account_info()['Services']:
name = service['Name']
if name != identifier:
continue
logging.debug(service)
enabled = service.get('Enabled')
if enabled:
is_enabled = bool(enabled)
status = service.get('status')
if status:
is_enabled = status == 'active'
if not is_enabled:
self.fatal_exit(f'Please enable iCloud service {label}')
def check_icloud_account(self):
ai = self.icloud_account_info()
logging.debug(ai)
def plist_for_command(self, cmd, shell=False):
logging.debug(cmd)
result = subprocess.run(cmd, capture_output=True, shell=shell)
return plistlib.loads(result.stdout)
def icloud_account_info(self):
if not self.cached_icloud_account_info:
ai = self.user_defaults_for_domain('MobileMeAccounts')
if 'Accounts' not in ai:
self.fatal_exit('Please sign into iCloud')
self.cached_icloud_account_info = ai['Accounts'][0]
return self.cached_icloud_account_info
def fatal_exit(self, msg):
print(msg, file=sys.stderr)
sys.exit(1)
@classmethod
def main(cls):
parser = argparse.ArgumentParser(description='Description')
# parser.add_argument('path', type=Path, help='Path to something')
parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose debug logging')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
return cls(args).run()
if __name__ == "__main__":
sys.exit(Tool.main())