-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
brew-file
executable file
·1348 lines (1191 loc) · 50.4 KB
/
brew-file
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Brew-file: Manager for packages of Homebrew
https://github.com/rcmdnk/homebrew-file
requirement: Python 2.7 or later
"""
__prog__ = "brew file"
__author__ = "rcmdnk"
__copyright__ = "Copyright (c) 2013 rcmdnk"
__credits__ = ["rcmdnk"]
__license__ = "MIT"
__version__ = "v2.1.9"
__date__ = "11/Nov/2014"
__maintainer__ = "rcmdnk"
__email__ = "rcmdnk@gmail.com"
__status__ = "Prototype"
import sys, os, urllib2
class Tee:
def __init__(self, out1, out2 = sys.stdout, use2 = True):
from cStringIO import StringIO
if type(out1) == str:
self.out1name = out1
self.out1 = StringIO()
else:
self.out1name = ""
self.out1 = out1
self.use2 = use2
if self.use2:
if type(out2) == str:
self.out2name = out2
self.out2 = StringIO()
else:
self.out2name = ""
self.out2 = out2
def __del__(self):
if self.out1name != "":
self.out1.close()
if self.use2:
if self.out2name != "":
self.out2.close()
def write(self, text):
self.out1.write(text)
if self.use2:
self.out2.write(text)
def writeln(self, text):
self.out1.write(text + "\n")
if self.use2:
self.out2.write(text + "\n")
def flush(self):
self.out1.flush()
if self.use2:
self.out2.flush()
def close(self):
if self.out1name != "":
f = open(self.out1name, "w")
f.write(self.out1.getvalue())
f.close()
if self.use2:
if self.out2name != "":
f = open(self.out2name, "w")
f.write(self.out2.getvalue())
f.close()
self.__del__()
class BrewFile:
"""Main class of Brew-file."""
def __init__(self):
"""initialization."""
import re
cask_opts = {"--caskroom" : ""}
env_cask_opts = os.environ.get("HOMEBREW_CASK_OPTS", "")
try:
if env_cask_opts != "":
cask_opts.update(dict(map(lambda x: x.split("="),
re.split(r'(?<!\\)\s*', env_cask_opts))))
except ValueError:
print "\033[31;1mHOMEBREW_CASK_OPTS: " + env_cask_opts + " is not a proper format.\033[m"
print "\033[31;1mIgnore the value.\n\033[m"
self.opt = {}
self.opt["command"] = ""
self.opt["brew_repo"] = self.proc("brew --prefix", False, False)[1][0]
self.opt["input"] = os.environ.get("HOMEBREW_BREWFILE",
self.opt["brew_repo"] + "/Library/Brewfile")
self.opt["brewfile"] = self.opt["input"]
self.opt["repo"] = ""
self.opt["noupdate"] = False
self.opt["link"] = True
self.opt["dryrun"] = True
self.opt["initialized"] = False
self.opt["tap_dir"] = self.opt["brew_repo"] + "/Library/Taps"
self.opt["cache_dir"] = self.proc("brew --cache", False, False)[1][0]
self.opt["caskroom"] = cask_opts["--caskroom"] \
if cask_opts["--caskroom"] != "" \
else "/opt/homebrew-cask/Caskroom"
self.opt["cask_pack"] = "brew-cask"
self.opt["cask_repo"] = "caskroom/cask"
self.opt["pip_pack"] = "brew-pip"
self.opt["my_editor"] = os.environ.get("EDITOR", "vim")
self.opt["tap_packs"] = []
self.opt["tap_casks"] = []
self.opt["brew_list"] = []
self.opt["pip_list"] = []
self.opt["tap_list"] = []
self.opt["cask_list"] = []
self.opt["cask_list_nocask"] = []
self.opt["is_brew_cmd"] = False
self.opt["is_cask_cmd"] = False
self.opt["is_pip_cmd"] = False
self.opt["cmd_input"] = []
self.opt["cask_input"] = []
self.opt["pip_input"] = []
self.opt["tap_input"] = []
self.opt["brew_input"] = []
self.opt["brew_input_opt"] = []
self.opt["args"] = []
self.opt["yn"] = False
self.opt["verbose"] = os.environ.get("HOMEBREW_FILE_VERBOSE", 1)
self.int_opts=["verbose"]
self.float_opts=[]
def set_args(self, **kw):
"""Set arguments."""
for k, v in kw.items():
self.opt[k] = v
for k in self.int_opts:
self.opt[k] = int(self.opt[k])
for k in self.float_opts:
self.opt[k] = float(self.opt[k])
def proc(self, cmd, print_cmd = True, print_out = True, exit_on_error = True):
""" Get process output."""
import shlex, subprocess
if type(cmd) == str:
cmd = shlex.split(cmd)
if print_cmd and self.opt["verbose"] > 0:
print '$', ' '.join(cmd)
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
lines = []
for l in iter(p.stdout.readline, ''):
lines.append(l.rstrip())
if print_out and self.opt["verbose"] > 0:
print l
ret = p.wait()
if exit_on_error and ret != 0:
if not (print_out and self.opt["verbose"] > 0):
print '\n'.join(lines)
sys.exit(ret)
return (ret, lines)
def ask_yn(self, question):
"""Helper for yes/no."""
if self.opt["yn"] == True:
print question + " [y/n]: y"
return True
yes = ["yes","y", ""]
no = ["no","n"]
# pragma: no cover
yn = raw_input(question + " [y/n]: ").lower()
while True:
if yn in yes:
return True
elif yn in no:
return False
else:
yn = raw_input("Answer with yes (y) or no (n): ").lower()
def print_banner(self, text):
if self.opt["verbose"] < 2:
return
max = 0
for l in text.split("\n"):
if max < len(l):
max = len(l)
print "\n"
print "#" * max
print text
print "#" * max
print "\n"
def remove(self, path):
"""Helper to remove file/directory."""
import shutil
if os.path.islink(path) or os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
else:
if self.opt["verbose"] > 0:
print "WARNING: Tried to remove non usual file/directory:", path
def get_tap_path(self, tap):
"""Get tap path"""
tap_user = os.path.dirname(tap)
tap_repo = os.path.basename(tap)
return self.opt["tap_dir"] + "/" + tap_user + "/homebrew-" + tap_repo
def repo_file(self):
"""Helper to build Brewfile path for the repository."""
repo_name = self.opt["repo"].split("/")[-1].split(".git")[0]
user_name = self.opt["repo"].split("/")[-2].split(":")[-1]
input = self.opt["input"].split("/")[-1]
return self.opt["brew_repo"] + "/Library/" + user_name + "_" + repo_name + "/" + input
def clone_repo(self):
ret = self.proc("git clone " + self.opt["repo"] + " "
+ os.path.dirname(self.opt["brewfile"]),
True, True, False)[0]
if ret != 0:
print "can't clone " + self.opt["repo"] + ".\n"\
"please check the repository, or reset with\n"\
" $ " + __prog__ + " set_repo"
sys.exit(ret)
def check_github_repo(self):
"""helper to check and create GitHub repository."""
import urllib
import urllib2
repo_name = self.opt["repo"].split("/")[-1].split(".git")[0]
user_name = self.opt["repo"].split("/")[-2].split(":")[-1]
url = "https://github.com/" + user_name + "/" + repo_name
# Check if the repository already exists or not.
exist_repo = True
try:
check = urllib2.urlopen(url)
except urllib2.HTTPError, e: # pragma: no cover
exist_repo = False
# Clone if exists
if exist_repo:
self.clone_repo()
return
# Create new repository # pragma: no cover
print "GitHub repository: " + user_name + "/" + repo_name + " doesn't exist."
ans = self.ask_yn("do you want to create the repository?")
if not ans:
exit(0)
# Check password
import getpass
password = getpass.getpass("GitHub password: ")
# Try to create w/o two-factor code
import requests, json
url = "https://api.github.com/user/repos"
description = "package list for Homebrew"
data = {"name": repo_name, "description": "Package list for Homebrew",
"auto_init": "true"}
headers = {}
is_ok = True
while True:
r = requests.post(url=url, data=json.dumps(data),
auth=(user_name, password), headers=headers)
if r.ok:
break
if r.json()["message"] == "Bad credentials":
password = getpass.getpass("\033[31;1mWrong password!\033[0m GitHub password: ")
continue
if r.json()["message"] == "Must specify two-factor authentication OTP code.":
is_ok = False
break
print r.json()["message"]
sys.exit(1)
if not is_ok:
# Try with two-factor code
twofac = raw_input("GitHub two-factor code: ")
headers.update({"X-Github-OTP": twofac})
while True:
r = requests.post(url=url, data=json.dumps(data),
auth=(user_name, password), headers=headers)
if r.ok:
break
if r.json()["message"] == "Must specify two-factor authentication OTP code.":
twofac = getpass.getpass("\033[31;1mWrong code!\033[0m GitHub two-factor code: ")
headers.update({"X-Github-OTP": twofac})
continue
print r.json()["message"]
sys.exit(1)
# Clone and initialize
self.clone_repo()
os.chdir(os.path.dirname(self.opt["brewfile"]))
f = open("README.md", "w")
f.write("# " + repo_name + "\n\n"\
"Package list for [homebrew](http://brew.sh/).\n\n"\
"Managed by [homebrew-file](https://github.com/rcmdnk/homebrew-file).")
f.close()
open(self.opt["brewfile"], "a").close()
ret = self.proc("git add -A")[0]
ret = self.proc(["git", "commit", "-m", "\"Prepared by " + __prog__ + "\""])[0]
ret = self.proc("git push")[0]
ans = self.ask_yn("Do you want to initialize from installed packages?")
if ans:
self.initialize(False)
def check_repo(self):
"""Check input file for GitHub repository."""
import re
self.opt["brewfile"] = self.opt["input"]
# Check input file
if not os.path.exists(self.opt["input"]):
return
# Check input file if it points repository or not
self.opt["repo"] = ""
f = open(self.opt["input"], "r")
lines = f.readlines()
f.close()
for l in lines:
if re.match(" *git ", l) == None:
continue
git_line = l.split()
if len(git_line) > 1:
self.opt["repo"] = git_line[1]
break
if self.opt["repo"] == "":
return
# Check repository name and add git@github.com: if necessary
if (re.match("git://", self.opt["repo"]) == None
and re.match("git@", self.opt["repo"]) == None
and re.match("https?://", self.opt["repo"]) == None):
self.opt["repo"] = "git@github.com:" + self.opt["repo"]
# Set Brewfile in the repository
self.opt["brewfile"] = self.repo_file()
if os.path.exists(self.opt["brewfile"]):
return
# Check and prepare repository
if self.opt["repo"].find("github") >= 0:
self.check_github_repo()
else:
self.clone_repo()
def repomgr(self, cmd=""):
"""Helper of repository management."""
pull = False
push = False
if cmd == "pull":
pull = True
push = False
elif cmd == "push":
pull = False
push = True
# Check the repository
if self.opt["repo"] == "":
print "Please set a repository, or reset with:"
print " $ " + __prog__ + "set_repo\n"
sys.exit(1)
# Clone if it doesn't exist
if not os.path.exists(os.path.dirname(self.opt["brewfile"])):
self.clone_repo()
# pull/push
if self.opt["verbose"] > 0:
print "cd", os.path.dirname(self.opt["brewfile"])
os.chdir(os.path.dirname(self.opt["brewfile"]))
if pull:
ret = self.proc("git pull")[0]
return
elif push:
ret = self.proc("git add " + self.opt["brewfile"].split("/")[-1])[0]
ret = self.proc(
["git", "commit", "-m", "\"Update the package list\""],
exit_on_error=False)[0]
ret = self.proc("git push")[0]
return
def get_tap(self, tap):
"""Helper for tap configuration file"""
tap_path = self.get_tap_path(tap)
if not os.path.isdir(tap_path):
return
self.opt["tap_packs"] = map(lambda x: x.replace(".rb", ""),
filter(lambda y: y.endswith(".rb"), os.listdir(tap_path)))
path = tap_path + "/Formula"
if os.path.isdir(path):
self.opt["tap_packs"] += map(lambda x: x.replace(".rb", ""),
filter(lambda y: y.endswith(".rb"),
os.listdir(path)))
path = tap_path + "/Casks"
if os.path.isdir(path):
self.opt["tap_casks"] = map(lambda x: x.replace(".rb", ""),
filter(lambda y: y.endswith(".rb"),
os.listdir(tap_path + "/Casks")))
def check_brew_cmd(self):
"""Check Homebrew"""
if self.proc("brew help", False, False, False)[0] != 0:
print "Homebrew has not been installed, install now..."
cmd="ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\""
self.proc(cmd)
(ret, lines) = self.proc("brew doctor", True, True, False)
if ret != 0:
self.print_banner("# Check brew environment and fix problems\n"
"# You can check with:\n"
"# $ brew doctor")
def check_cask_cmd(self, force=False):
"""Check cask is installed or not"""
if self.opt["is_cask_cmd"]:
return True
if self.proc("brew cask", False, False, False)[0] == 0:
self.opt["is_cask_cmd"] = True
return True
if force:
print self.opt["cask_pack"]\
+ " has not been installed, install now."
ret = self.proc(["brew", "install",
self.opt["cask_repo"] + "/" + self.opt["cask_pack"]],
True, True, False)[0]
if ret != 0:
print "\n\033[31;1mFailed to install "\
+ self.opt["cask_pack"] + "\033[0m\n"
print list
sys.exit(ret)
if not self.opt["cask_repo"] in self.opt["tap_list"]:
self.opt["tap_list"].append(self.opt["cask_repo"])
if not self.opt["cask_pack"] in self.opt["brew_list"]:
self.opt["brew_list"].append(self.opt["cask_pack"])
self.opt["is_cask_cmd"] = True
return self.opt["is_cask_cmd"]
def check_pip_cmd(self, force=False):
"""Check pip is installed or not"""
if self.opt["is_pip_cmd"]:
return True
if self.proc("brew pip -h", False, False, False)[0] == 0:
self.opt["is_pip_cmd"] = True
return True
if force:
print self.opt["pip_pack"]\
+ " has not been installed, install now."
ret = self.proc(["brew", "install", self.opt["pip_pack"]],
True, True, False)[0]
if ret != 0:
print "\n\033[31;1mFailed to install "\
+ self.opt["pip_pack"] + "\033[0m\n"
print list
sys.exit(ret)
if not self.opt["pip_pack"] in self.opt["brew_list"]:
self.opt["brew_list"].append(self.opt["pip_pack"])
self.opt["is_pip_cmd"] = True
return self.opt["is_pip_cmd"]
def get_list(self):
"""Get List"""
# Brew packages
(ret, lines) = self.proc("brew list", False, False)
self.opt["brew_list"] = []
self.opt["pip_list"] = []
for p in lines:
if p.startswith("pip-"):
self.opt["pip_list"].append(p)
else:
self.opt["brew_list"].append(p)
# Taps
(ret, lines) = self.proc("brew tap", False, False)
self.opt["tap_list"] = lines
# Casks
if self.check_cask_cmd():
(ret, lines) = self.proc("brew cask list", False, False)
self.opt["cask_list"] = []
self.opt["cask_list_nocask"] = []
if ret == 0:
for p in lines:
if len(p.split()) == 1:
self.opt["cask_list"].append(p)
else:
print "\033[31;1mthe cask file of \033[31;7m" + p + "\033[0m\033[31;1m doesn't exist."
print "\033[31;1mplease check later.\n\n\033[0m"
self.opt["cask_list_nocask"].append(p)
def read_input(self):
"""Read input file"""
# Check packages in the input file
self.opt["cmd_input"] = []
self.opt["cask_input"] = []
self.opt["pip_input"] = []
self.opt["tap_input"] = []
self.opt["brew_input"] = []
self.opt["brew_input_opt"] = []
f = open(self.opt["brewfile"], "r")
lines = f.readlines()
f.close()
import re
for l in lines:
if re.match(" *$", l) != None or re.match(" *#", l) != None:
continue
args = l.replace("'","").replace('"',"").split()
cmd = args[0]
if len(args) < 2:
print "\n\033[31;1mWrong line: " + l + "\033[0m"
continue
p = args[1]
if len(args) > 2 and p == "install":
p = args[2]
args.pop(2)
opt = " ".join(args[2:])
if cmd == "brew" or cmd == "install":
self.opt["brew_input"].append(p)
self.opt["brew_input_opt"].append((p + " " + opt).strip())
elif cmd == "tap":
self.opt["tap_input"].append(p)
elif cmd == "tapall":
self.opt["tap_input"].append(p)
self.get_tap(p)
self.opt["brew_input"] += self.opt["tap_packs"]
elif cmd == "cask":
self.opt["cask_input"].append(p)
elif cmd == "pip":
self.opt["pip_input"].append(p)
else:
self.opt["cmd_input"].append(l.strip())
continue
def set_brewfile_repo(self):
"""Set Brewfile repository"""
import re
# Check input file
if os.path.exists(self.opt["input"]):
prev_repo = ""
f = open(self.opt["input"], "r")
lines = f.readlines()
f.close()
for l in lines:
if re.match(" *git ", l) == None:
continue
git_line = l.split()
if len(git_line) > 1:
prev_repo = git_line[1]
break
print "Input file: " + self.opt["input"] + " is already there."
if prev_repo != "":
print "git repository for Brewfile is already set as " + prev_repo
ans = self.ask_yn("Do you want to overwrite it?")
if ans:
os.rename(self.opt["input"], self.opt["input"] + ".bak")
if self.opt["verbose"] > 0:
print "Ok, old input file was moved to " + self.opt["input"] + ".bak."
else:
sys.exit(0)
# Get repository
if self.opt["repo"] == "":
print "Set repository, \"non\" for local Brewfile,"
print "<user>/<repo> for github repository,"
self.opt["repo"] = raw_input("or full path for the repository: ")
while True:
if self.opt["repo"] != "":
break
print "Repository can not be empty"
self.opt["repo"] = raw_input("Set repository: ")
self.print_banner("# Set Brewfile repository as " + self.opt["repo"])
if self.opt["repo"] == "non":
# Reset non repository type Brewfile
self.initialize(False)
return
else:
# Write repository to the input file
f = open(self.opt["input"], "w")
f.write("git " + self.opt["repo"])
f.close()
self.check_repo()
def get_options(self, package):
"""get install options from brew info"""
opt=""
# Get options for build
(ret, lines) = self.proc("brew info " + package, False, False)
versions = {"stable":"", "devel":"", "HEAD":""}
v_tmp = map(lambda x: x.strip().split(),
lines[0].split(":")[1].split(","))
for v in v_tmp:
if v[0] == "stable":
versions["stable"] = v[1]
elif v[0] == "devel":
versions["devel"] = v[1]
elif v[0] == "HEAD":
versions["HEAD"] = "HEAD"
cellar = self.proc("brew --cellar " + package, False, False)[1][0]
for l in lines:
ltmp = l.split("Built from source with: ")
if len(ltmp) > 1:
opt += " " + ltmp[1].replace(",", "")
continue
if l.startswith(cellar):
v = l.split()[0].split("/")[-1]
for k in versions.keys():
if v == versions[k]:
if k != "stable":
opt += " --" + k
break
return opt
def initialize(self, check = True):
"""Initialize Brewfile"""
if self.opt["initialized"]:
return
if check == 1:
if not os.path.exists(self.opt["input"]):
ans = self.ask_yn("Do you want to set a repository (y)? ((n) for local Brewfile).")
if ans:
self.set_brewfile_repo()
return
else:
if self.opt["repo"] != "":
print "You are using Brewfile of " + self.opt["repo"] + "."
else:
print self.opt["input"] + " is already there."
ans = self.ask_yn("Do you want to overwrite it?")
if ans:
if self.opt["repo"] == "":
os.rename(self.opt["input"], self.opt["input"] + ".bak")
if self.opt["verbose"] > 0:
print "Ok, old input file was moved to " + self.opt["input"] + ".bak."
else:
sys.exit(0)
self.print_banner("# Initialize " + self.opt["brewfile"])
# Get installed package list
self.get_list()
# Check list
out = Tee(self.opt["brewfile"], sys.stdout, self.opt["verbose"] > 1)
# Taps
if len(self.opt["tap_list"]) > 0:
out.writeln("# tap repositories and their packages")
for p in self.opt["tap_list"]:
self.get_tap(p)
out.writeln("\n## " + p + "\ntap " + p)
for tp in self.opt["tap_packs"]:
if tp in self.opt["brew_list"]:
opt = self.get_options(tp)
out.writeln("install " + tp + opt)
self.opt["brew_list"].remove(tp)
for tc in self.opt["tap_casks"]:
if tc in self.opt["cask_list"]:
out.writeln("cask install " + tc)
self.opt["cask_list"].remove(tc)
# Brew packages
if len(self.opt["brew_list"]) > 0:
out.writeln("\n# Other Homebrew packages")
for p in self.opt["brew_list"]:
opt = self.get_options(p)
out.writeln("install " + p + opt)
# pip packages
if len(self.opt["pip_list"]) > 0:
out.writeln("\n# pip packages")
for p in self.opt["pip_list"]:
#opt = self.get_options(p)
opt = ""
out.writeln("pip " + p + opt)
# Casks
if len(self.opt["cask_list"]) > 0:
out.writeln("\n# Cask applications")
for p in self.opt["cask_list"]:
#opt = self.get_options(p)
opt = ""
out.writeln("cask install " + p + opt)
# Installed by cask, but cask files were not found...
if len(self.opt["cask_list_nocask"]) > 0:
out.writeln("\n# Below applications were installed by Cask,")
out.writeln("but not corresponding casks were found.\n")
for p in self.opt["cask_list_nocask"]:
#opt = self.get_options(p)
opt = ""
out.writeln("#cask install " + p + opt)
# Close Brewfile
out.close()
self.print_banner("# You can edit " + self.opt["brewfile"] + " with:\n"
"# $ " + __prog__ + " edit")
self.opt["initialized"] = True
def check_input_file(self):
"""Check input file"""
if not os.path.exists(self.opt["brewfile"]):
print "WARNING: Input file $brewfile is not found."
ans = self.ask_yn("WARNING: Do you want to initialize from installed packages?")
if ans:
self.initialize(False)
return
else:
print "WARNING: Ok, please prepare brewfile"
print "WARNING: or you can initialize " + self.opt["brewfile"] + " with:"
print "WARNING: $ " + __prog__ + " init"
sys.exit(1)
def edit_brewfile(self):
"""Edit brewfile"""
import subprocess
subprocess.call([self.opt["my_editor"], self.opt["brewfile"]])
def cleanup(self):
"""Clean up."""
if self.opt["dryrun"]:
self.print_banner("# Dry run")
# Check up packages in the input file
self.read_input()
# Clean up cask packages
if len(self.opt["cask_list"]) > 0:
self.print_banner("# Clean up cask packages")
for p in self.opt["cask_list"]:
if p in self.opt["cask_input"]:
continue
self.check_cask_cmd(True)
cmd = "$ brew cask uninstall " + p
if self.opt["dryrun"]:
print cmd
else:
self.proc(cmd, True, True)
self.opt["cask_list"].remove(p)
# Skip clean up cask at tap/brew if any cask packages exist
if len(self.opt["cask_list"]) > 0:
self.opt["tap_list"].remove(self.opt["cask_repo"])
self.opt["brew_list"].remove(self.opt["cask_pack"])
# Clean up pip packages
if len(self.opt["pip_list"]) > 0:
self.print_banner("# Clean up pip packages")
for p in self.opt["pip_list"]:
if p in self.opt["pip_input"]:
continue
cmd0 = "pip uninstall -y " + p
cmd1 = "brew uninstall " + p
cmd2 = "brew uninstall pip-" + p
if self.opt["dryrun"]:
print cmd0
print cmd1
print cmd2
else:
self.proc(cmd0, False, True)
self.proc(cmd1, False, True)
self.proc(cmd2, False, True)
self.opt["pip_list"].remove(p)
# Skip clean up cask at tap/brew if any cask packages exist
if len(self.opt["pip_list"]) > 0:
self.opt["tap_list"].remove(self.opt["pip_repo"])
self.opt["brew_list"].remove(self.opt["pip_pack"])
# Clean up tap packages
if len(self.opt["tap_list"]) > 0:
self.print_banner("# Clean up tap packages")
for p in self.opt["tap_list"]:
if p in self.opt["tap_input"]:
continue
self.get_tap(p)
untapflag = True
for tp in self.opt["tap_packs"]:
if tp in self.opt["brew_input"]:
# Keep the Tap as related package is remained
untapflag = False
break
if not untapflag:
continue
cmd = "brew untap " + p
if self.opt["dryrun"]:
print cmd
else:
self.proc(cmd, False, True)
# Clean up brew packages
if len(self.opt["brew_list"]) > 0:
self.print_banner("# Clean up brew packages")
for p in self.opt["brew_list"]:
if p in self.opt["brew_input"]:
continue
cmd = "brew uninstall " + p
if self.opt["dryrun"]:
print cmd
else:
(ret, lines) = self.proc("brew info " + p, False, False)
for l in lines:
if l.find("ln -s") >= 0:
if len(l.split()) > 2:
app = l.split()[2]
break
#self.remove("/Applications/" + app)
#self.remove("/Applications/Utilities/" + app)
#self.remove(os.environ["HOME"] + "/Applications/" + app)
#self.remove(os.environ["HOME"] + "/Applications/Utilities/" + app)
self.proc(cmd, False, True)
# Clean up cashe
self.print_banner("# Clean up cache")
cmd0 = "brew cleanup --force"
cmd1 = "rm -rf " + self.opt["cache_dir"]
if self.opt["dryrun"]:
print cmd0
print cmd1
# Dry run message
self.print_banner("# This is dry run.\n"
"# If you want to enforce cleanup, use '-C':\n"
"# $ " + __prog__ + " clean -C")
else:
self.proc(cmd0, False)
self.proc(cmd1, False)
def install(self):
"""Install"""
# First update Homebrew
if not self.opt["noupdate"]:
self.proc("brew update")
# Check packages in the input file
self.read_input()
# Tap
for p in self.opt["tap_input"]:
if not p in self.opt["tap_list"]:
self.proc("brew tap " + p)
# Cask
for p in self.opt["cask_input"]:
if not p in self.opt["cask_list"]:
self.check_cask_cmd(True)
self.proc("brew cask install " + p)
# pip
for p in self.opt["pip_input"]:
if not p in self.opt["pip_list"]:
self.check_pip_cmd(True)
self.proc("brew pip " + p)
# Brew
for p in self.opt["brew_input_opt"]:
if not p.split()[0] in self.opt["brew_list"]:
(ret, lines) = self.proc("brew install " + p)
for l in lines:
if l.find("ln -s") >= 0:
if self.opt["link"]:
self.proc(l)
if l.find("brew linkapps") >= 0:
if self.opt["link"]:
self.proc("brew linkapps")
# Other commands
for c in self.opt["cmd_input"]:
self.proc(c)
if ret != 0:
print c + ": returned error:\n\n" + list
sys.exit(ret)
return 0
def find_app(self, app, taps):
"""Helper function for Cask"""
self.check_cask_cmd(True)
[cask_user, cask_repo_name] = self.opt["cask_repo"].split("/")
cask_namer = self.get_tap_path(self.opt["cask_repo"])\
+ "/developer/bin/cask_namer"
name = ""
lines = self.proc([cask_namer, '"' + app + '"'] , False, False, False)[1]
for l in lines:
if l.find("Proposed Cask name") >= 0:
name = l.split()[3]
tap = ""
installed = False
if name != "":
for t in taps:
d = self.get_tap_path(t) + "/Casks"
if os.path.exists(d + "/" + name + ".rb"):
tap = t
lines = self.proc("brew cask info " + name, False, False)[1]
installed =True
for l in lines:
if l.find("Not installed") >= 0:
installed = False
break
break
if not installed:
name = ""
for t in taps:
d = self.get_tap_path(t) + "/Casks"
for n in map(lambda x: x.replace(".rb", ""),
filter(lambda y: y.endswith(".rb"), os.listdir(d))):
f = open(d + "/" + n + ".rb", "r")
content = f.read()
f.close()
if content.find(app) >= 0:
lines = self.proc("brew cask info " + n, False, False)[1]
installed =True
for l in lines:
if l.find("Not installed") >= 0:
installed = False
break
tap = t
name = n
if self.opt["verbose"] > 1:
if name == "":
print "Non Cask app:", app
elif installed:
print "Installed by Cask:", app, name
else:
print "Installed directly, instead of by Cask:", app, name
return (tap, installed, name)
def check_cask(self):
"""Check applications for Cask"""
self.check_cask_cmd(True)
self.print_banner("# Starting to check applications for Cask...")
# Set cask directories and reset application information list
taps = filter(lambda t: os.path.isdir(self.get_tap_path(t) + "/Casks"),
self.proc("brew tap", False, False)[1])
apps = dict([d,{True:[], False:[]}] for d in taps + [""])
# Set applications directories
app_dirs = [d for d in ["/Applications", "/Applications/Utilities",
os.environ["HOME"] + "/Applications",
os.environ["HOME"] + "/Applications/Utilities"]
if os.path.isdir(d)]
apps_check = {"cask": dict([d, 0] for d in app_dirs),
"has_cask": dict([d, 0] for d in app_dirs),
"no_cask": dict([d, 0] for d in app_dirs)}
# Get applications
napps = 0
for d in app_dirs:
for app in filter(lambda x: x.endswith(".app"), os.listdir(d)):
(tap, installed, name) = self.find_app(app, taps)
apps[tap][installed].append((name, d + "/" + app))
check = "no_cask"
if installed:
check = "cask"
elif name != "":
check = "has_cask"
apps_check[check][d] += 1
napps += 1
# Make list
out = Tee("Caskfile", sys.stdout, self.opt["verbose"] > 1)
out.writeln("# Cask applications")
out.writeln("# Please copy these lines to your Brewfile"
" and use with `" + __prog__ + " install`.\n")
if apps.has_key(self.opt["cask_repo"]):
out.writeln("# Main tap repository for " + self.opt["cask_pack"])
out.writeln("tap " + self.opt["cask_repo"])
out.writeln("install " + self.opt["cask_pack"])
out.writeln("")
out.writeln("# Apps installed by Cask in " + self.opt["cask_pack"])
for (name,app_path) in apps[self.opt["cask_repo"]][True]:
out.writeln("cask install " + name