1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
3 | import textwrap |
---|
4 | import os |
---|
5 | import pkg_resources |
---|
6 | from command import Command, BadCommand |
---|
7 | import fnmatch |
---|
8 | import re |
---|
9 | import traceback |
---|
10 | from cStringIO import StringIO |
---|
11 | import inspect |
---|
12 | import types |
---|
13 | |
---|
14 | class EntryPointCommand(Command): |
---|
15 | |
---|
16 | usage = "ENTRY_POINT" |
---|
17 | summary = "Show information about entry points" |
---|
18 | |
---|
19 | description = """\ |
---|
20 | Shows information about one or many entry points (you can use |
---|
21 | wildcards for entry point names). Entry points are used for Egg |
---|
22 | plugins, and are named resources -- like an application, template |
---|
23 | plugin, or other resource. Entry points have a [group] which |
---|
24 | defines what kind of object they describe, and inside groups each |
---|
25 | entry point is named. |
---|
26 | """ |
---|
27 | |
---|
28 | max_args = 2 |
---|
29 | |
---|
30 | parser = Command.standard_parser(verbose=False) |
---|
31 | parser.add_option('--list', '-l', |
---|
32 | dest='list_entry_points', |
---|
33 | action='store_true', |
---|
34 | help='List all the kinds of entry points on the system') |
---|
35 | parser.add_option('--egg', '-e', |
---|
36 | dest='show_egg', |
---|
37 | help="Show all the entry points for the given Egg") |
---|
38 | parser.add_option('--regex', |
---|
39 | dest='use_regex', |
---|
40 | action='store_true', |
---|
41 | help="Make pattern match as regular expression, not just a wildcard pattern") |
---|
42 | |
---|
43 | def command(self): |
---|
44 | if self.options.list_entry_points: |
---|
45 | return self.list_entry_points() |
---|
46 | if self.options.show_egg: |
---|
47 | return self.show_egg(self.options.show_egg) |
---|
48 | if not self.args: |
---|
49 | raise BadCommand("You must give an entry point (or --list)") |
---|
50 | pattern = self.get_pattern(self.args[0]) |
---|
51 | groups = self.get_groups_by_pattern(pattern) |
---|
52 | if not groups: |
---|
53 | raise BadCommand('No group matched %s' % self.args[0]) |
---|
54 | ep_pat = None |
---|
55 | if len(self.args) > 1: |
---|
56 | ep_pat = self.get_pattern(self.args[1]) |
---|
57 | for group in groups: |
---|
58 | desc = self.get_group_description(group) |
---|
59 | print '[%s]' % group |
---|
60 | if desc: |
---|
61 | print self.wrap(desc) |
---|
62 | print |
---|
63 | by_dist = {} |
---|
64 | self.print_entry_points_by_group(group, ep_pat) |
---|
65 | |
---|
66 | def print_entry_points_by_group(self, group, ep_pat): |
---|
67 | env = pkg_resources.Environment() |
---|
68 | project_names = list(env) |
---|
69 | project_names.sort() |
---|
70 | for project_name in project_names: |
---|
71 | dists = list(env[project_name]) |
---|
72 | assert dists |
---|
73 | dist = dists[0] |
---|
74 | entries = dist.get_entry_map(group).values() |
---|
75 | if ep_pat: |
---|
76 | entries = [e for e in entries |
---|
77 | if ep_pat.search(e.name)] |
---|
78 | if not entries: |
---|
79 | continue |
---|
80 | if len(dists) > 1: |
---|
81 | print '%s (+ %i older versions)' % ( |
---|
82 | dist, len(dists)-1) |
---|
83 | else: |
---|
84 | print '%s' % dist |
---|
85 | entries.sort(lambda a, b: cmp(a.name, b.name)) |
---|
86 | for entry in entries: |
---|
87 | print self._ep_description(entry) |
---|
88 | desc = self.get_entry_point_description(entry, group) |
---|
89 | if desc and desc.description: |
---|
90 | print self.wrap(desc.description, indent=4) |
---|
91 | |
---|
92 | def show_egg(self, egg_name): |
---|
93 | group_pat = None |
---|
94 | if self.args: |
---|
95 | group_pat = self.get_pattern(self.args[0]) |
---|
96 | ep_pat = None |
---|
97 | if len(self.args) > 1: |
---|
98 | ep_pat = self.get_pattern(self.args[1]) |
---|
99 | if egg_name.startswith('egg:'): |
---|
100 | egg_name = egg_name[4:] |
---|
101 | dist = pkg_resources.get_distribution(egg_name) |
---|
102 | entry_map = dist.get_entry_map() |
---|
103 | entry_groups = entry_map.items() |
---|
104 | entry_groups.sort() |
---|
105 | for group, points in entry_groups: |
---|
106 | if group_pat and not group_pat.search(group): |
---|
107 | continue |
---|
108 | print '[%s]' % group |
---|
109 | points = points.items() |
---|
110 | points.sort() |
---|
111 | for name, entry in points: |
---|
112 | if ep_pat: |
---|
113 | if not ep_pat.search(name): |
---|
114 | continue |
---|
115 | print self._ep_description(entry) |
---|
116 | desc = self.get_entry_point_description(entry, group) |
---|
117 | if desc and desc.description: |
---|
118 | print self.wrap(desc.description, indent=2) |
---|
119 | print |
---|
120 | |
---|
121 | def wrap(self, text, indent=0): |
---|
122 | text = dedent(text) |
---|
123 | width = int(os.environ.get('COLUMNS', 70)) - indent |
---|
124 | text = '\n'.join([line.rstrip() for line in text.splitlines()]) |
---|
125 | paras = text.split('\n\n') |
---|
126 | new_paras = [] |
---|
127 | for para in paras: |
---|
128 | if para.lstrip() == para: |
---|
129 | # leading whitespace means don't rewrap |
---|
130 | para = '\n'.join(textwrap.wrap(para, width)) |
---|
131 | new_paras.append(para) |
---|
132 | text = '\n\n'.join(new_paras) |
---|
133 | lines = [' '*indent + line |
---|
134 | for line in text.splitlines()] |
---|
135 | return '\n'.join(lines) |
---|
136 | |
---|
137 | def _ep_description(self, ep, pad_name=None): |
---|
138 | name = ep.name |
---|
139 | if pad_name is not None: |
---|
140 | name = name + ' '*(pad_name-len(name)) |
---|
141 | dest = ep.module_name |
---|
142 | if ep.attrs: |
---|
143 | dest = dest + ':' + '.'.join(ep.attrs) |
---|
144 | return '%s = %s' % (name, dest) |
---|
145 | |
---|
146 | def get_pattern(self, s): |
---|
147 | if not s: |
---|
148 | return None |
---|
149 | if self.options.use_regex: |
---|
150 | return re.compile(s) |
---|
151 | else: |
---|
152 | return re.compile(fnmatch.translate(s), re.I) |
---|
153 | |
---|
154 | def list_entry_points(self): |
---|
155 | pattern = self.get_pattern(self.args and self.args[0]) |
---|
156 | groups = self.get_groups_by_pattern(pattern) |
---|
157 | print '%i entry point groups found:' % len(groups) |
---|
158 | for group in groups: |
---|
159 | desc = self.get_group_description(group) |
---|
160 | print '[%s]' % group |
---|
161 | if desc: |
---|
162 | if hasattr(desc, 'description'): |
---|
163 | desc = desc.description |
---|
164 | print self.wrap(desc, indent=2) |
---|
165 | |
---|
166 | def get_groups_by_pattern(self, pattern): |
---|
167 | env = pkg_resources.Environment() |
---|
168 | eps = {} |
---|
169 | for project_name in env: |
---|
170 | for dist in env[project_name]: |
---|
171 | for name in pkg_resources.get_entry_map(dist): |
---|
172 | if pattern and not pattern.search(name): |
---|
173 | continue |
---|
174 | if (not pattern |
---|
175 | and name.startswith('paste.description.')): |
---|
176 | continue |
---|
177 | eps[name] = None |
---|
178 | eps = eps.keys() |
---|
179 | eps.sort() |
---|
180 | return eps |
---|
181 | |
---|
182 | def get_group_description(self, group): |
---|
183 | for entry in pkg_resources.iter_entry_points('paste.entry_point_description'): |
---|
184 | if entry.name == group: |
---|
185 | ep = entry.load() |
---|
186 | if hasattr(ep, 'description'): |
---|
187 | return ep.description |
---|
188 | else: |
---|
189 | return ep |
---|
190 | return None |
---|
191 | |
---|
192 | def get_entry_point_description(self, ep, group): |
---|
193 | try: |
---|
194 | return self._safe_get_entry_point_description(ep, group) |
---|
195 | except Exception, e: |
---|
196 | out = StringIO() |
---|
197 | traceback.print_exc(file=out) |
---|
198 | return ErrorDescription(e, out.getvalue()) |
---|
199 | |
---|
200 | def _safe_get_entry_point_description(self, ep, group): |
---|
201 | ep.dist.activate() |
---|
202 | meta_group = 'paste.description.'+group |
---|
203 | meta = ep.dist.get_entry_info(meta_group, ep.name) |
---|
204 | if not meta: |
---|
205 | generic = list(pkg_resources.iter_entry_points( |
---|
206 | meta_group, 'generic')) |
---|
207 | if not generic: |
---|
208 | return super_generic(ep.load()) |
---|
209 | # @@: Error if len(generic) > 1? |
---|
210 | obj = generic[0].load() |
---|
211 | desc = obj(ep, group) |
---|
212 | else: |
---|
213 | desc = meta.load() |
---|
214 | return desc |
---|
215 | |
---|
216 | class EntryPointDescription(object): |
---|
217 | |
---|
218 | def __init__(self, group): |
---|
219 | self.group = group |
---|
220 | |
---|
221 | # Should define: |
---|
222 | # * description |
---|
223 | |
---|
224 | class SuperGeneric(object): |
---|
225 | |
---|
226 | def __init__(self, doc_object): |
---|
227 | self.doc_object = doc_object |
---|
228 | self.description = dedent(self.doc_object.__doc__) |
---|
229 | try: |
---|
230 | if isinstance(self.doc_object, (type, types.ClassType)): |
---|
231 | func = self.doc_object.__init__.im_func |
---|
232 | elif (hasattr(self.doc_object, '__call__') |
---|
233 | and not isinstance(self.doc_object, types.FunctionType)): |
---|
234 | func = self.doc_object.__call__ |
---|
235 | else: |
---|
236 | func = self.doc_object |
---|
237 | if hasattr(func, '__paste_sig__'): |
---|
238 | sig = func.__paste_sig__ |
---|
239 | else: |
---|
240 | sig = inspect.getargspec(func) |
---|
241 | sig = inspect.formatargspec(*sig) |
---|
242 | except TypeError: |
---|
243 | sig = None |
---|
244 | if sig: |
---|
245 | if self.description: |
---|
246 | self.description = '%s\n\n%s' % ( |
---|
247 | sig, self.description) |
---|
248 | else: |
---|
249 | self.description = sig |
---|
250 | |
---|
251 | def dedent(s): |
---|
252 | if s is None: |
---|
253 | return s |
---|
254 | s = s.strip('\n').strip('\r') |
---|
255 | return textwrap.dedent(s) |
---|
256 | |
---|
257 | def super_generic(obj): |
---|
258 | desc = SuperGeneric(obj) |
---|
259 | if not desc.description: |
---|
260 | return None |
---|
261 | return desc |
---|
262 | |
---|
263 | class ErrorDescription(object): |
---|
264 | |
---|
265 | def __init__(self, exc, tb): |
---|
266 | self.exc = exc |
---|
267 | self.tb = '\n'.join(tb) |
---|
268 | self.description = 'Error loading: %s' % exc |
---|
269 | |
---|