overlays/default: Add README and equery-licenses
[catalyst-swc.git] / overlays / default / usr / bin / equery-licenses
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 import glob as _glob
20 import logging as _logging
21
22 import portage as _portage
23 import portage.dep as _portage_dep
24 import portage.versions as _portage_versions
25
26
27 def get_db():
28     """Get a database of installed packages
29     """
30     db = _portage.db
31     for key in ['/', 'vartree']:
32         if key not in db:
33             raise KeyError('{0} not found (choices: {1})'.format(
34                     key, list(db.keys())))
35         db = db[key]
36     return db
37
38 def get_license_lists():
39     """Iterate through installed packages with licenses
40
41     Yields (category-package-version, license-list) tuples
42     """
43     db = get_db()
44     for cpv in db.dbapi.cpv_all():
45         licenses,use = db.dbapi.aux_get(cpv, ['LICENSE', 'USE'])
46         category = _portage_versions.catsplit(cpv)[0]
47         if category == 'virtual':
48             if licenses:
49                 _logging.warn('license for {0}: {1}'.format(cpv, licenses))
50             continue
51         elif not licenses:
52             _logging.warn('no license for {0}'.format(cpv))
53             continue
54         license_list = _portage_dep.use_reduce(
55             licenses, uselist=use, opconvert=True)
56         yield (cpv, license_list)
57
58 def incorperate_or_dependencies(license_set, or_dependencies):
59     while or_dependencies:
60         or_dependencies = set(
61             or_set for or_set in or_dependencies
62             if not license_set.union(or_set))
63         if or_dependencies:
64             raise NotImplementedError('select best subset of or_dependencies')
65     return license_set
66
67 def get_all_licenses():
68     license_set = set()
69     or_dependencies = set()
70     for cpv,license_list in get_license_lists():
71         for token in license_list:
72             if isinstance(token, str):
73                 license_set.add(token)
74             elif token[0] == '||':
75                 or_dependencies.add(tuple(token[1:]))
76             else:
77                 raise NotImplementedError((licenses, token))
78     license_set = incorperate_or_dependencies(license_set, or_dependencies)
79     return license_set
80
81
82 if __name__ == '__main__':
83     licenses = get_all_licenses()
84     print('\n'.join(sorted(licenses)))