3 # Copyright 2006 Matt Mackall <mpm@selenic.com> |
3 # Copyright 2006 Matt Mackall <mpm@selenic.com> |
4 # |
4 # |
5 # This software may be used and distributed according to the terms of the |
5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2, incorporated herein by reference. |
6 # GNU General Public License version 2, incorporated herein by reference. |
7 |
7 |
8 import os, sys |
8 from i18n import _ |
9 from i18n import _, gettext |
|
10 import extensions |
9 import extensions |
11 |
10 |
12 |
|
13 # borrowed from pydoc |
|
14 def pathdirs(): |
|
15 '''Convert sys.path into a list of absolute, existing, unique paths.''' |
|
16 dirs = [] |
|
17 normdirs = [] |
|
18 for dir in sys.path: |
|
19 dir = os.path.abspath(dir or '.') |
|
20 normdir = os.path.normcase(dir) |
|
21 if normdir not in normdirs and os.path.isdir(dir): |
|
22 dirs.append(dir) |
|
23 normdirs.append(normdir) |
|
24 return dirs |
|
25 |
11 |
26 # loosely inspired by pydoc.source_synopsis() |
12 # loosely inspired by pydoc.source_synopsis() |
27 # rewritten to handle ''' as well as """ |
13 # rewritten to handle ''' as well as """ |
28 # and to return the whole text instead of just the synopsis |
14 # and to return the whole text instead of just the synopsis |
29 def moduledoc(file): |
15 def moduledoc(file): |
51 else: |
37 else: |
52 return None |
38 return None |
53 |
39 |
54 return ''.join(result) |
40 return ''.join(result) |
55 |
41 |
56 def additionalextensions(): |
|
57 '''Find the extensions shipped with Mercurial but not enabled |
|
58 |
|
59 Returns extensions names and descriptions, and the max name length |
|
60 ''' |
|
61 exts = {} |
|
62 maxlength = 0 |
|
63 |
|
64 for dir in filter(os.path.isdir, |
|
65 (os.path.join(pd, 'hgext') for pd in pathdirs())): |
|
66 for e in os.listdir(dir): |
|
67 if e.endswith('.py'): |
|
68 name = e.rsplit('.', 1)[0] |
|
69 path = os.path.join(dir, e) |
|
70 else: |
|
71 name = e |
|
72 path = os.path.join(dir, e, '__init__.py') |
|
73 |
|
74 if name in exts or name == '__init__' or not os.path.exists(path): |
|
75 continue |
|
76 |
|
77 try: |
|
78 extensions.find(name) |
|
79 except KeyError: |
|
80 pass |
|
81 else: |
|
82 continue # enabled extension |
|
83 |
|
84 try: |
|
85 file = open(path) |
|
86 except IOError: |
|
87 continue |
|
88 else: |
|
89 doc = moduledoc(file) |
|
90 file.close() |
|
91 |
|
92 if doc: # extracting localized synopsis |
|
93 exts[name] = gettext(doc).splitlines()[0] |
|
94 else: |
|
95 exts[name] = _('(no help text available)') |
|
96 if len(name) > maxlength: |
|
97 maxlength = len(name) |
|
98 |
|
99 return exts, maxlength |
|
100 |
|
101 def enabledextensions(): |
|
102 '''Return the list of enabled extensions, and max name length''' |
|
103 enabled = list(extensions.extensions()) |
|
104 exts = {} |
|
105 maxlength = 0 |
|
106 |
|
107 if enabled: |
|
108 exthelps = [] |
|
109 for ename, ext in enabled: |
|
110 doc = (gettext(ext.__doc__) or _('(no help text available)')) |
|
111 ename = ename.split('.')[-1] |
|
112 maxlength = max(len(ename), maxlength) |
|
113 exts[ename] = doc.splitlines(0)[0].strip() |
|
114 |
|
115 return exts, maxlength |
|
116 |
|
117 def extensionslisting(header, exts, maxlength): |
42 def extensionslisting(header, exts, maxlength): |
118 '''Return a text listing of the given extensions''' |
43 '''Return a text listing of the given extensions''' |
119 result = '' |
44 result = '' |
120 |
45 |
121 if exts: |
46 if exts: |
158 hgext.bar = !/path/to/extension/bar.py |
83 hgext.bar = !/path/to/extension/bar.py |
159 # ditto, but no path was supplied for extension baz |
84 # ditto, but no path was supplied for extension baz |
160 hgext.baz = ! |
85 hgext.baz = ! |
161 ''') |
86 ''') |
162 |
87 |
163 exts, maxlength = enabledextensions() |
88 exts, maxlength = extensions.enabled() |
164 doc += extensionslisting(_('enabled extensions:'), exts, maxlength) |
89 doc += extensionslisting(_('enabled extensions:'), exts, maxlength) |
165 |
90 |
166 exts, maxlength = additionalextensions() |
91 exts, maxlength = extensions.disabled() |
167 doc += extensionslisting(_('non-enabled extensions:'), exts, maxlength) |
92 doc += extensionslisting(_('non-enabled extensions:'), exts, maxlength) |
168 |
93 |
169 return doc |
94 return doc |
170 |
95 |
171 helptable = ( |
96 helptable = ( |