-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.py
executable file
·216 lines (186 loc) · 7.6 KB
/
go.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
#!/usr/bin/env python3
from collections.abc import MutableSequence
from itertools import product
from pathlib import Path
from portage.dep import isvalidatom
import argparse
import portage
#______________________________________________________________________________
class PortageSet(MutableSequence):
def __init__(self, path):
self.path = path
self.entries = []
def __repr__(self):
return "PortageSet('%s (%d entries)')" % (self.path, len(self.entries))
@property
def name(self):
return self.path.name
def check(self):
[e.check() for e in self.entries]
def import_set(self):
lines = enumerate(open(self.path, 'r').read().splitlines(), 1)
for line_no, line in lines:
if not line or line.lstrip()[0] == '#':
self.entries.append(Comment(self.path, line, line_no))
else:
self.entries.append(EBuild(self.path, line, line_no))
# abc methods
def __delitem__(self, index):
del self.entries[index]
def __getitem__(self, index):
return self.entries[index]
def __setitem__(self, index, value):
self.entries[index] = value
def __len__(self):
return len(self.entries)
def insert(self, index, value):
self.entries.insert(index, value)
class Entry():
def __init__(self, path, line, line_no):
self.path = path
self.line = line
self.line_no = line_no
def __repr__(self):
return "Entry('%s:%d')" % (self.path, self.line_no)
def check(self):
pass
class Comment(Entry):
def __init__(self, path, line, line_no):
super().__init__(path, line, line_no)
def __repr__(self):
return "Comment('%s:%d')" % (self.path, self.line_no)
def check(self):
return True
def pretty_print(self, prefix, color=True):
if color:
return '%s\033[2m%s\033[0m' % (prefix, self.line)
else:
return '%s' % self.line
def formatted(self, destination):
return '%s' % self.line
class EBuild(Entry):
def __init__(self, path, line, line_no):
super().__init__(path, line, line_no)
tokens = line.split()
self.keyword = False
self.skip = False
# Entries which start with a ! are keyworded
if tokens[0] == '!':
self.keyword = True
tokens = tokens[1:]
# Entries which start with a - set use flags, but not explicitly select the ebuild
elif tokens[0] == '-':
self.skip = True
tokens = tokens[1:]
else:
self.keyword = False
self.cpv = tokens[0]
# Sort USE flags in [use, +use, -use] order
self.uses = tokens[1:]
self.uses.sort()
self.uses.sort(key=lambda x:x[0]=='+')
self.uses.sort(key=lambda x:x[0]=='-')
def __repr__(self):
status = ''
if self.keyword:
status = '!'
if self.skip:
status = '-'
return "EBuild('%s:%d:%s%s')" % (self.path, self.line_no, status, self.cpv)
def check(self):
if not isvalidatom(self.cpv):
print('%s:%d: error: not a valid portage atom: %s' % (self.path.name, self.line_no, self.cpv))
return False
# https://www.funtoo.org/Portage_API
p = portage.db[portage.root]["porttree"].dbapi
resolved_ebuild = p.xmatch(origdep=self.cpv, level='bestmatch-visible')
if not resolved_ebuild:
print('%s:%d: error: portage atom not in any repo: %s' % (self.path.name, self.line_no, self.cpv))
return False
def filtered_use(uses):
for use in uses:
if use[0] in ('+', '-'):
yield use[1:]
else:
yield use
ebuild_uses = set(filtered_use(p.aux_get(resolved_ebuild, ['IUSE'])[0].split()))
given_uses = set(filtered_use(self.uses))
unknown_uses = given_uses - ebuild_uses
if unknown_uses:
print('%s:%d: error: "%s" not a valid use flag: %s' % (
self.path.name, self.line_no, ' '.join(unknown_uses), self.line
))
print('└─ available USE flags: %s' % ' '.join(ebuild_uses))
return False
return True
def pretty_print(self, color=True):
if color:
pretty_cpv = '\033[32m%s\033[0m' % self.cpv
pretty_uses = []
for use in self.uses:
if use[0] == '+':
use = '\033[34m%s\033[0m' % use
elif use[0] == '-':
use = '\033[31m%s\033[0m' % use
else:
use = '\033[97m%s\033[0m' % use
pretty_uses.append(use)
return '%s %s' % (pretty_cpv, ' '.join(pretty_uses))
else:
return '%s %s' % (self.cpv, ' '.join(self.uses))
def formatted(self, destination):
if destination == 'package.accept_keywords':
if self.keyword:
return self.cpv
else:
return '#%s' % self.cpv
if destination == 'package.use':
if self.uses:
return '%s %s' % (self.cpv, ' '.join(self.uses))
else:
return '#%s' % self.cpv
if destination == 'sets':
if self.skip:
return '#%s (skipped)' % self.cpv
else:
return self.cpv
#______________________________________________________________________________
def lookahead(iterable):
it = iter(iterable)
last_value = next(it)
for value in it:
yield last_value, True
last_value = value
yield last_value, False
#______________________________________________________________________________
def main():
make_conf_path = Path(str(portage.root)) / Path(portage.MAKE_CONF_FILE)
parser = argparse.ArgumentParser(description='', epilog='')
parser.add_argument('-f', '--force', help='Force overwriting of portage set files', action='store_true')
parser.add_argument('-o', '--output', help='Portage configuration path. Formatted sets files will be created by default within /etc/portage/{package.accept_keywords,package.use,sets}/ directories. (autodetected: %(default)s)', default=make_conf_path.parent, action='store', type=Path)
parser.add_argument('-q', '--quiet', help='Suppress output', action='store_true')
parser.add_argument('-n', '--dry-run', help='Only print what would be done', action='store_true')
parser.add_argument('--strict', help='Fail on warnings (unknown ebuilds, unknown USE flags)', action='store_true')
parser.add_argument('--no-color', help='Disable color output', dest='color', default=True, action='store_false')
parser.add_argument('sets', help='Portage set helper definitions', nargs='+', type=Path)
args = parser.parse_args()
#args.output = Path('test')
portage_sets = [
PortageSet(path)
for path in args.sets
]
for portage_set in portage_sets:
portage_set.import_set()
portage_set.check()
import pdb; pdb.set_trace()
destinations = 'package.accept_keywords', 'package.use', 'sets'
for destination, portage_set in product(destinations, portage_sets):
path = Path.joinpath(args.output, destination, portage_set.name)
path.parent.mkdir(parents=True, exist_ok=True)
print(path)
with path.open('w') as outfile:
for entry in portage_set:
print(entry.formatted(destination), file=outfile)
print(entry.pretty_print(''))
if __name__=="__main__":
main()