-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrew.py
executable file
·269 lines (210 loc) · 8.15 KB
/
brew.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import yaml
import os.path
import collections
import argparse
class Brew(object):
def __init__(self, data):
super(Brew, self).__init__()
self.groups = []
self.configurations = []
self.parse_data(data)
self.generate()
# print(self.groups)
# print(self.configurations)
def parse_data(self, data):
groups = data.get("groups", {}) or {}
configurations = data.get("configurations", {}) or {}
self.groups = [Group(g) for g in groups.iteritems()]
self.configurations = [Configuration(c) for c in configurations.iteritems()]
def generate(self):
for c in self.configurations:
c.groups = [self.group_with_name(g) for g in c.groups_names]
c.generate()
def group_with_name(self, name):
return next((g for g in self.groups if g.name == name), None)
def __str__(self):
return "Groups: {}, configurations: {}".format(len(self.groups), len(self.configurations))
def __repr__(self):
return "<Groups:{}, configurations:{}>".format(len(self.groups), len(self.configurations))
class Group(object):
def __init__(self, data):
super(Group, self).__init__()
self.name = data[0]
self.taps = []
self.privileged = None
self.brews = []
self.casks = []
self.mas_apps = []
self._parse(data[1])
def _parse(self, data):
if data is None:
return
self.taps = unique_sort(data.get("taps", []))
privileged = data.get("privileged")
self.privileged = Privileged(privileged) if privileged else None
self.brews = unique_sort(data.get("brews", []))
self.casks = unique_sort(data.get("casks", []))
self.mas_apps = unique_sort(data.get("mas_apps", []))
def __str__(self):
return "Group: {}, taps: {}, brews: {}, casks: {}, mas: {}".format(self.name, len(self.taps), len(self.brews), len(self.casks), len(self.mas_apps))
def __repr__(self):
return "<Group:'{}', taps:{}, brews:{}, casks:{}, mas:{}>".format(self.name, len(self.taps), len(self.brews), len(self.casks), len(self.mas_apps))
class Privileged(object):
def __init__(self, data):
super(Privileged, self).__init__()
self.brews = []
self.casks = []
self.mas_apps = []
self._parse(data)
def _parse(self, data):
if data is None:
return
self.brews = unique_sort(data.get("brews", []))
self.casks = unique_sort(data.get("casks", []))
self.mas_apps = unique_sort(data.get("mas_apps", []))
def __str__(self):
return "Privileged: brews: {}, casks: {}, mas: {}".format(len(self.brews), len(self.casks), len(self.mas_apps))
def __repr__(self):
return "<Privileged:'brews:{}, casks:{}, mas:{}>".format(len(self.brews), len(self.casks), len(self.mas_apps))
class Configuration(object):
def __init__(self, data):
super(Configuration, self).__init__()
self.name = data[0]
self.groups_names = data[1]
self.groups = []
@property
def brewfile_name(self):
if self.name == "main":
return "Brewfile"
return "{}.Brewfile".format(self.name)
@property
def taps(self):
return unique_sort([tap for g in self.groups for tap in g.taps])
@property
def brews(self):
return unique_sort([brew for g in self.groups for brew in g.brews])
@property
def privileged_brews(self):
return unique_sort([brew for g in self.groups if g.privileged for brew in g.privileged.brews])
@property
def casks(self):
return unique_sort([cask for g in self.groups for cask in g.casks])
@property
def privileged_casks(self):
return unique_sort([cask for g in self.groups if g.privileged for cask in g.privileged.casks])
@property
def mas_apps(self):
return unique_sort([mas_app for g in self.groups for mas_app in g.mas_apps])
@property
def privileged_mas_apps(self):
return unique_sort([mas_app for g in self.groups if g.privileged for mas_app in g.privileged.mas_apps])
def generate(self):
print("Generating: {}".format(self.name))
with open(self.brewfile_name, "w") as f:
f.write("""# to run:
# $ brew tap Homebrew/bundle
# $ brew bundle
# // or
# $ brew bundle --file=<name>.Brewfile
#
# Tip:
# $ sudo chmod -R +ai "group:admin allow list,add_file,search,delete,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,writesecurity,chown,file_inherit,directory_inherit" /usr/local/*
""")
# Taps
self.print_packages(f, "Taps:", self.taps, self.tap)
# Privileged Brews
self.print_packages(f, "Privileged Brews:", self.privileged_brews, self.brew)
# Privileged Casks
self.print_packages(f, "Privileged Casks:", self.privileged_casks, self.cask)
# Privileged Mas apps
self.print_packages(f, "Privileged Mac App Store:", self.privileged_mas_apps, self.mas)
# Brews
self.print_packages(f, "Brews:", self.brews, self.brew)
# Casks
self.print_packages(f, "Casks:", self.casks, self.cask)
# Mas apps
self.print_packages(f, "Mac App Store:", self.mas_apps, self.mas)
@classmethod
def print_packages(cls, f, title, packages, print_method):
if len(packages):
f.write("# {}\n".format(title))
for element in packages:
f.write(print_method(element))
f.write("\n")
@classmethod
def tap(cls, t):
return "tap {}\n".format(cls.brewfile_value(t))
@classmethod
def brew(cls, b):
return "brew {}\n".format(cls.brewfile_value(b))
@classmethod
def cask(cls, c):
return "cask {}\n".format(cls.brewfile_value(c))
@classmethod
def mas(cls, m):
return "mas {}\n".format(cls.brewfile_value(m))
@classmethod
def brewfile_value(cls, v):
if isinstance(v, str):
return "'{}'".format(v)
# Dictionary:
name = v.keys()[0]
values = []
for k, v in v[name].iteritems():
value = None
if isinstance(v, str):
value = "{}: '{}'".format(k, v)
elif isinstance(v, int):
value = "{}: {}".format(k, v)
elif isinstance(v, list):
value = "{}: {}".format(k, cls.list(v))
if value:
values.append(value)
return "'{}', {}".format(name, ", ".join(values))
@classmethod
def list(cls, l):
values = ["'{}'".format(v) for v in l]
return "[{}]".format(", ".join(values))
def __str__(self):
return "Configuration: {}, groups: {}".format(self.name, len(self.groups_names))
def __repr__(self):
return "<Configuration:'{}', groups:{}>".format(self.name, len(self.groups_names))
def unique_sort(seq):
def get_key(element):
if isinstance(element, collections.Hashable):
return element
elif isinstance(element, dict):
return element.keys()[0]
return None
def unique(seq):
values = []
seen = set()
for x in seq:
key = get_key(x)
if not (key in seen):
seen.add(key)
values.append(x)
return values
def sort(seq):
return sorted(seq, key=get_key)
return sort(unique(seq))
if __name__ == "__main__":
def parse_file(file_name):
with open(file_name, "r") as f:
brew = Brew(yaml.load(f))
parser = argparse.ArgumentParser(description="Generates Brewfiles from YAML template")
parser.add_argument("-f", "--file", help="YAML file to parse, by default `brew.yml` or `brew.yaml`")
args = parser.parse_args()
if args.file:
parse_file(args.file)
else:
brew_file = None
if os.path.exists("brew.yml"):
brew_file = "brew.yml"
elif os.path.exists("brew.yaml"):
brew_file = "brew.yaml"
else:
exit(1)
parse_file(brew_file)