1 # vim:fileencoding=utf-8
2 # Copyright 2010 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
5 __package__ = 'gentoolkit.eshowkw'
7 __author__ = "Tomáš Chvátal <scarabeus@gentoo.org>"
9 import sys, os, fnmatch
11 from portage import output as porto
12 from portage import settings as ports
13 from portage import config as portc
14 from portage import portdbapi as portdbapi
15 from portage import db as portdb
17 from gentoolkit.eshowkw.keywords_header import keywords_header
18 from gentoolkit.eshowkw.keywords_content import keywords_content
19 from gentoolkit.eshowkw.display_pretty import string_rotator
20 from gentoolkit.eshowkw.display_pretty import display
25 topper = 'versionlist'
27 def process_display(package, keywords, dbapi):
29 portdata = keywords_content(package, keywords.keywords, dbapi, ignore_slots, order, bold, topper)
30 if topper == 'archlist':
31 header = string_rotator().rotateContent(keywords.content, keywords.length, bold)
32 extra = string_rotator().rotateContent(keywords.extra, keywords.length, bold, False)
33 # -1 : space is taken in account and appended by us
34 filler = ''.ljust(portdata.slot_length-1)
35 header = ['%s%s%s' % (x, filler, y) for x, y in zip(header, extra)]
36 content = portdata.content
37 header_length = portdata.version_length
38 content_length = keywords.length
40 header = string_rotator().rotateContent(portdata.content, portdata.content_length, bold)
41 content = keywords.content
42 sep = [''.ljust(keywords.length) for x in range(portdata.slot_length-1)]
44 content.extend(keywords.extra)
45 header_length = keywords.length
46 content_length = portdata.version_length
47 display(content, header, header_length, content_length, portdata.cp, topper)
49 def process_args(argv):
50 """Option parsing via argc"""
51 parser = argparse.ArgumentParser(prog=__package__,
52 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
53 description='Display keywords for specified package or for package that is in pwd.')
55 parser.add_argument('-v', '--version', action='version', version=__version__, help='show package version and exit')
57 parser.add_argument('package', nargs='*', default=None, help='Packages to check.')
59 parser.add_argument('-a', '--arch', nargs=1, default=[], help='Display only specified arch(s)')
61 parser.add_argument('-A', '--align', nargs='?', default='bottom', choices=['top', 'bottom'],
62 help='Specify alignment for descriptions.')
63 parser.add_argument('-T', '--top-position', nargs='?', default='archlist', choices=['archlist', 'versionlist'],
64 help='Specify which fields we want to have in top listing.')
66 parser.add_argument('-B', '--bold', action='store_true', default=False,
67 help='Print out each other column in bold for easier visual separation.')
68 parser.add_argument('-C', '--color', action='store_true', default=False,
69 help='Force colored output')
70 parser.add_argument('-O', '--overlays', action='store_true', default=False,
71 help='Search also overlays')
72 parser.add_argument('-P', '--prefix', action='store_true', default=False,
73 help='Display prefix keywords in output.')
74 parser.add_argument('-S', '--ignore-slot', action='store_true', default=False,
75 help='Treat slots as irelevant during detection of redundant pacakges.')
77 return parser.parse_args(args=argv)
79 def main(argv, indirect = False):
80 global ignore_slots, bold, order, topper
83 opts = process_args(argv)
84 ignore_slots = opts.ignore_slot
85 use_overlays = opts.overlays
86 # user can do both --arch=a,b,c or --arch a b c
87 if len(opts.arch) > 1:
88 opts.arch = ','.join(opts.arch)
89 highlight_arch = ''.join(opts.arch).split(',')
92 topper = opts.top_position
95 package = opts.package
98 if indirect and len(package) <= 0:
99 msg_err = 'No packages specified'
100 raise SystemExit(msg_err)
102 # disable colors when redirected and they are not forced on
103 if not color and not sys.stdout.isatty():
106 keywords = keywords_header(prefix, highlight_arch, order)
108 mysettings = portc(local_config=False)
109 dbapi = portdbapi(mysettings=mysettings)
111 dbapi.porttrees = [dbapi.porttree_root]
113 process_display(pkg, keywords, dbapi)
115 currdir = os.getcwd()
116 # check if there are actualy some ebuilds
117 ebuilds = ['%s' % x for x in os.listdir(currdir)
118 if fnmatch.fnmatch(x, '*.ebuild')]
119 if len(ebuilds) <= 0:
120 msg_err = 'No ebuilds at "%s"' % currdir
121 raise SystemExit(msg_err)
122 package= '%s/%s' % (os.path.basename(os.path.abspath('../')), os.path.basename(currdir))
123 ourtree = os.path.abspath('../../')
124 overlays = '%s %s' % (ports['PORTDIR_OVERLAY'], ourtree)
125 mysettings = portc(local_config=False, env={'PORTDIR_OVERLAY': overlays})
126 dbapi = portdbapi(mysettings=mysettings)
127 # specify that we want just our nice tree we are in cwd
128 dbapi.porttrees = [ourtree]
129 process_display(package, keywords, dbapi)
132 if __name__ == '__main__':
133 sys.exit(main(sys.argv[1:]))