forked from pqrth/homebrew-cask-replace
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrew_cask_replace.py
212 lines (186 loc) · 7.41 KB
/
brew_cask_replace.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
#!/usr/bin/env python
# coding=utf-8
import sys
import os
import argparse
import requests
import subprocess
from send2trash import send2trash
try:
input = raw_input
except NameError:
pass
_CASKS_HOME = 'http://raw.github.com/caskroom/homebrew-cask/master/Casks/'
def parse_ignores(ignore_file_path):
ignores = set()
if os.path.exists(ignore_file_path):
with open(ignore_file_path, 'r') as f:
for line in f:
line = line.strip()
if not line:
continue
if line[0] == '#':
continue
ignores.add(line)
return ignores
def parse_ignorescask(ignorecask_file_path):
ignorescask = set()
if os.path.exists(ignorecask_file_path):
with open(ignorecask_file_path, 'r') as f:
for line in f:
line = line.strip()
if not line:
continue
if line[0] == '#':
continue
ignorescask.add(line)
return ignorescask
def is_installed_by_appstore(application_path):
cmd = 'codesign -dvvv "{0}"'.format(application_path)
output = subprocess.getoutput(cmd)
return output.find('Authority=Apple Mac OS Application Signing') > 0
def generate_cask_token(application_path, application_name):
cask_token = None
p = subprocess.Popen(
['brew', '--repository'], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
brew_location, err = p.communicate()
brew_location = brew_location.decode('utf-8').strip()
if len(brew_location):
p = subprocess.Popen(
[
brew_location +
'/Library/Taps/homebrew/' +
'homebrew-cask/developer/bin/' +
'generate_cask_token',
application_path,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = p.communicate()
for line in out.decode('utf-8').split('\n'):
key, value = line.split(':')
key = key.strip()
if key == 'Proposed token':
cask_token = value.strip()
break
if not cask_token:
cask_token = '-'.join([x for x in application_name.split()]).lower()
return cask_token
def replace_application_in(
applications_dir, always_yes=False, skip_app_from_appstore=True
):
os.system('brew cask list > ignorecask.txt')
ignores = parse_ignores(os.path.join(os.path.dirname(__file__), 'ignore.txt'))
ignorescask = parse_ignorescask(os.path.join(os.path.dirname(__file__), 'ignorecask.txt'))
not_founded = []
installed_failed = []
send2trash_failed = []
choose_no = []
skipAppstore = []
try:
applications = os.listdir(applications_dir)
for application in applications:
if application in ignores:
continue
application_path = os.path.join(applications_dir, application)
if skip_app_from_appstore and is_installed_by_appstore(application_path):
print('Skip {0} from Appstore'.format(application))
skipAppstore.append(application)
continue
application_name, ext = os.path.splitext(application)
if ext.lower() != '.app':
continue
application_name = generate_cask_token(application_path, application_name)
try:
cask_url = _CASKS_HOME + application_name + '.rb'
application_info_file = requests.get(cask_url, timeout=3)
except Exception:
not_founded.append(application)
continue
if application_info_file.status_code == 200:
application_info = application_info_file.text
else:
not_founded.append(application)
continue
cask = application_info[application_info.find("'")+1:].split()[0]
caskapp = cask.strip('\'"')
if caskapp in ignorescask:
continue
print('{0} -> {1}'.format(application, application_info))
if not always_yes:
replace_it = input('Replace It(Y/n):')
replace_it = replace_it.lower()
if len(replace_it) > 0 and replace_it != 'y' and replace_it != 'yes':
choose_no.append(application)
continue
try:
send2trash(os.path.join(applications_dir, application))
except OSError as e:
send2trash_failed.append(
os.path.join(applications_dir, application)
)
print('\n{0} replace failed \n'.format(application_name))
else:
print('\n{0} successfully sent to trash, now reinstalling via brew \n'.format(application_name))
status = os.system('brew cask install {0}'.format(application_name))
if status != 0:
installed_failed.append(application)
print(
'{0} brew installation failed. Please try to install using the command:\n"brew cask install {0}".\nIf that fails please reinstall manually'.format(application_name)
)
else:
print('{0} successfully reinstalled with cask.\n'.format(application_name))
finally:
for x in not_founded:
print('Not found: {0}'.format(x))
for x in installed_failed:
print('Installed fail: {0}'.format(x))
for x in send2trash_failed:
print('Send to trash fail: {0}'.format(x))
with open('brew_cask_replace_log.txt', 'w') as f:
f.write('Ignored by user-defined ignore.txt]\n')
for x in ignores:
f.write("{}\n".format(x))
f.write('\n[AppStore managed and skipped]\n')
for x in skipAppstore:
f.write("{}\n".format(x))
f.write('\n[Already managed by brew cask]\n')
for x in ignorescask:
f.write("{}\n".format(x))
f.write('\n[User chose not to replace]\n')
for x in choose_no:
f.write("{}\n".format(x))
f.write('\n[Not found on brew cask]\n')
for x in not_founded:
f.write("{}\n".format(x))
f.write('\n[Failed to send to trash (permission issue likely)]\n')
for x in send2trash_failed:
f.write("{}\n".format(x))
f.write('\n[Install failed]\n')
for x in installed_failed:
f.write("{}\n".format(x))
print('Log saved to brew_cask_replace_log.txt')
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'appdir',
nargs='?',
default='/Applications',
help='''the applications directory to search
(/Applications by default)''',
)
parser.add_argument(
'-y', '--always-yes', action='store_true', help='replace all with homebrew-cask'
)
parser.add_argument(
'-f',
'--include-appstore',
action='store_true',
help='include apps in the Mac App Store',
)
args = parser.parse_args(argv)
replace_application_in(args.appdir, args.always_yes, not args.include_appstore)
if __name__ == '__main__':
main(sys.argv[1:])