##// END OF EJS Templates
gendoc: fall back to pure modules if C extensions are not available (issue1711)
Cédric Duval -
r9130:335f749c default
parent child Browse files
Show More
@@ -1,112 +1,114 b''
1 import sys, textwrap
1 import os, sys, textwrap
2 # import from the live mercurial repo
2 # import from the live mercurial repo
3 sys.path.insert(0, "..")
3 sys.path.insert(0, "..")
4 # fall back to pure modules if required C extensions are not available
5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
4 from mercurial import demandimport; demandimport.enable()
6 from mercurial import demandimport; demandimport.enable()
5 from mercurial.commands import table, globalopts
7 from mercurial.commands import table, globalopts
6 from mercurial.i18n import gettext, _
8 from mercurial.i18n import gettext, _
7 from mercurial.help import helptable
9 from mercurial.help import helptable
8
10
9 def get_desc(docstr):
11 def get_desc(docstr):
10 if not docstr:
12 if not docstr:
11 return "", ""
13 return "", ""
12 # sanitize
14 # sanitize
13 docstr = docstr.strip("\n")
15 docstr = docstr.strip("\n")
14 docstr = docstr.rstrip()
16 docstr = docstr.rstrip()
15 shortdesc = docstr.splitlines()[0].strip()
17 shortdesc = docstr.splitlines()[0].strip()
16
18
17 i = docstr.find("\n")
19 i = docstr.find("\n")
18 if i != -1:
20 if i != -1:
19 desc = docstr[i+2:]
21 desc = docstr[i+2:]
20 else:
22 else:
21 desc = " %s" % shortdesc
23 desc = " %s" % shortdesc
22 return (shortdesc, desc)
24 return (shortdesc, desc)
23
25
24 def get_opts(opts):
26 def get_opts(opts):
25 for shortopt, longopt, default, desc in opts:
27 for shortopt, longopt, default, desc in opts:
26 allopts = []
28 allopts = []
27 if shortopt:
29 if shortopt:
28 allopts.append("-%s" % shortopt)
30 allopts.append("-%s" % shortopt)
29 if longopt:
31 if longopt:
30 allopts.append("--%s" % longopt)
32 allopts.append("--%s" % longopt)
31 desc += default and _(" (default: %s)") % default or ""
33 desc += default and _(" (default: %s)") % default or ""
32 yield(", ".join(allopts), desc)
34 yield(", ".join(allopts), desc)
33
35
34 def get_cmd(cmd):
36 def get_cmd(cmd):
35 d = {}
37 d = {}
36 attr = table[cmd]
38 attr = table[cmd]
37 cmds = cmd.lstrip("^").split("|")
39 cmds = cmd.lstrip("^").split("|")
38
40
39 d['cmd'] = cmds[0]
41 d['cmd'] = cmds[0]
40 d['aliases'] = cmd.split("|")[1:]
42 d['aliases'] = cmd.split("|")[1:]
41 d['desc'] = get_desc(attr[0].__doc__)
43 d['desc'] = get_desc(attr[0].__doc__)
42 d['opts'] = list(get_opts(attr[1]))
44 d['opts'] = list(get_opts(attr[1]))
43
45
44 s = 'hg ' + cmds[0]
46 s = 'hg ' + cmds[0]
45 if len(attr) > 2:
47 if len(attr) > 2:
46 if not attr[2].startswith('hg'):
48 if not attr[2].startswith('hg'):
47 s += ' ' + attr[2]
49 s += ' ' + attr[2]
48 else:
50 else:
49 s = attr[2]
51 s = attr[2]
50 d['synopsis'] = s
52 d['synopsis'] = s
51
53
52 return d
54 return d
53
55
54 def show_doc(ui):
56 def show_doc(ui):
55 def bold(s, text=""):
57 def bold(s, text=""):
56 ui.write("%s\n%s\n%s\n" % (s, "="*len(s), text))
58 ui.write("%s\n%s\n%s\n" % (s, "="*len(s), text))
57 def underlined(s, text=""):
59 def underlined(s, text=""):
58 ui.write("%s\n%s\n%s\n" % (s, "-"*len(s), text))
60 ui.write("%s\n%s\n%s\n" % (s, "-"*len(s), text))
59
61
60 # print options
62 # print options
61 underlined(_("OPTIONS"))
63 underlined(_("OPTIONS"))
62 for optstr, desc in get_opts(globalopts):
64 for optstr, desc in get_opts(globalopts):
63 ui.write("%s::\n %s\n\n" % (optstr, desc))
65 ui.write("%s::\n %s\n\n" % (optstr, desc))
64
66
65 # print cmds
67 # print cmds
66 underlined(_("COMMANDS"))
68 underlined(_("COMMANDS"))
67 h = {}
69 h = {}
68 for c, attr in table.items():
70 for c, attr in table.items():
69 f = c.split("|")[0]
71 f = c.split("|")[0]
70 f = f.lstrip("^")
72 f = f.lstrip("^")
71 h[f] = c
73 h[f] = c
72 cmds = h.keys()
74 cmds = h.keys()
73 cmds.sort()
75 cmds.sort()
74
76
75 for f in cmds:
77 for f in cmds:
76 if f.startswith("debug"): continue
78 if f.startswith("debug"): continue
77 d = get_cmd(h[f])
79 d = get_cmd(h[f])
78 # synopsis
80 # synopsis
79 ui.write("[[%s]]\n" % d['cmd'])
81 ui.write("[[%s]]\n" % d['cmd'])
80 ui.write("%s::\n" % d['synopsis'].replace("hg ","", 1))
82 ui.write("%s::\n" % d['synopsis'].replace("hg ","", 1))
81 # description
83 # description
82 ui.write("%s\n\n" % d['desc'][1])
84 ui.write("%s\n\n" % d['desc'][1])
83 # options
85 # options
84 opt_output = list(d['opts'])
86 opt_output = list(d['opts'])
85 if opt_output:
87 if opt_output:
86 opts_len = max([len(line[0]) for line in opt_output])
88 opts_len = max([len(line[0]) for line in opt_output])
87 ui.write(_(" options:\n"))
89 ui.write(_(" options:\n"))
88 for optstr, desc in opt_output:
90 for optstr, desc in opt_output:
89 if desc:
91 if desc:
90 s = "%-*s %s" % (opts_len, optstr, desc)
92 s = "%-*s %s" % (opts_len, optstr, desc)
91 else:
93 else:
92 s = optstr
94 s = optstr
93 s = textwrap.fill(s, initial_indent=4 * " ",
95 s = textwrap.fill(s, initial_indent=4 * " ",
94 subsequent_indent=(6 + opts_len) * " ")
96 subsequent_indent=(6 + opts_len) * " ")
95 ui.write("%s\n" % s)
97 ui.write("%s\n" % s)
96 ui.write("\n")
98 ui.write("\n")
97 # aliases
99 # aliases
98 if d['aliases']:
100 if d['aliases']:
99 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
101 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
100
102
101 # print topics
103 # print topics
102 for names, section, doc in helptable:
104 for names, section, doc in helptable:
103 underlined(gettext(section).upper())
105 underlined(gettext(section).upper())
104 if callable(doc):
106 if callable(doc):
105 doc = doc()
107 doc = doc()
106 else:
108 else:
107 doc = gettext(doc)
109 doc = gettext(doc)
108 ui.write(doc)
110 ui.write(doc)
109 ui.write("\n")
111 ui.write("\n")
110
112
111 if __name__ == "__main__":
113 if __name__ == "__main__":
112 show_doc(sys.stdout)
114 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now