Show More
@@ -1,54 +1,57 b'' | |||
|
1 | 1 | # highlight extension implementation file |
|
2 | 2 | # |
|
3 | 3 | # The original module was split in an interface and an implementation |
|
4 | 4 | # file to defer pygments loading and speedup extension setup. |
|
5 | 5 | |
|
6 | 6 | from mercurial import demandimport |
|
7 | 7 | demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',]) |
|
8 | 8 | |
|
9 | 9 | from mercurial import util |
|
10 | 10 | from mercurial.templatefilters import filters |
|
11 | 11 | |
|
12 | 12 | from pygments import highlight |
|
13 | 13 | from pygments.util import ClassNotFound |
|
14 | 14 | from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer |
|
15 | 15 | from pygments.formatters import HtmlFormatter |
|
16 | 16 | |
|
17 | 17 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' |
|
18 | 18 | 'type="text/css" />') |
|
19 | 19 | |
|
20 | 20 | def pygmentize(field, fctx, style, tmpl): |
|
21 | 21 | |
|
22 | 22 | # append a <link ...> to the syntax highlighting css |
|
23 | 23 | old_header = ''.join(tmpl('header')) |
|
24 | 24 | if SYNTAX_CSS not in old_header: |
|
25 | 25 | new_header = old_header + SYNTAX_CSS |
|
26 | 26 | tmpl.cache['header'] = new_header |
|
27 | 27 | |
|
28 | 28 | text = fctx.data() |
|
29 | 29 | if util.binary(text): |
|
30 | 30 | return |
|
31 | 31 | |
|
32 | # avoid UnicodeDecodeError in pygments | |
|
33 | text = util.tolocal(text) | |
|
34 | ||
|
32 | 35 | # To get multi-line strings right, we can't format line-by-line |
|
33 | 36 | try: |
|
34 | 37 | lexer = guess_lexer_for_filename(fctx.path(), text[:1024], |
|
35 | 38 | encoding=util._encoding) |
|
36 | 39 | except (ClassNotFound, ValueError): |
|
37 | 40 | try: |
|
38 | 41 | lexer = guess_lexer(text[:1024], encoding=util._encoding) |
|
39 | 42 | except (ClassNotFound, ValueError): |
|
40 | 43 | lexer = TextLexer(encoding=util._encoding) |
|
41 | 44 | |
|
42 | 45 | formatter = HtmlFormatter(style=style, encoding=util._encoding) |
|
43 | 46 | |
|
44 | 47 | colorized = highlight(text, lexer, formatter) |
|
45 | 48 | # strip wrapping div |
|
46 | 49 | colorized = colorized[:colorized.find('\n</pre>')] |
|
47 | 50 | colorized = colorized[colorized.find('<pre>')+5:] |
|
48 | 51 | coloriter = iter(colorized.splitlines()) |
|
49 | 52 | |
|
50 | 53 | filters['colorize'] = lambda x: coloriter.next() |
|
51 | 54 | |
|
52 | 55 | oldl = tmpl.cache[field] |
|
53 | 56 | newl = oldl.replace('line|escape', 'line|colorize') |
|
54 | 57 | tmpl.cache[field] = newl |
@@ -1,82 +1,90 b'' | |||
|
1 | 1 | #!/bin/sh |
|
2 | 2 | |
|
3 | 3 | "$TESTDIR/hghave" pygments || exit 80 |
|
4 | 4 | |
|
5 | 5 | cat <<EOF >> $HGRCPATH |
|
6 | 6 | [extensions] |
|
7 | 7 | hgext.highlight = |
|
8 | 8 | [web] |
|
9 | 9 | pygments_style = friendly |
|
10 | 10 | EOF |
|
11 | 11 | |
|
12 | 12 | hg init test |
|
13 | 13 | cd test |
|
14 | 14 | cp $TESTDIR/get-with-headers.py ./ |
|
15 | ||
|
16 | # check for UnicodeDecodeError with iso-8859-1 file contents | |
|
17 | python -c 'fp = open("isolatin", "w"); fp.write("h\xFCbsch\n"); fp.close();' | |
|
18 | ||
|
15 | 19 | hg ci -Ama |
|
16 | 20 | |
|
17 | 21 | echo % hg serve |
|
18 | 22 | hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log |
|
19 | 23 | cat hg.pid >> $DAEMON_PIDS |
|
20 | 24 | |
|
21 | 25 | echo % hgweb filerevision, html |
|
22 | 26 | ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py') \ |
|
23 | 27 | | sed "s/[0-9]* years ago/long ago/g" | sed "s/class=\"k\"/class=\"kn\"/g" |
|
24 | 28 | |
|
29 | echo % hgweb filerevision, html | |
|
30 | ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/isolatin') \ | |
|
31 | | sed "s/[0-9]* years ago/long ago/g" | sed "s/class=\"k\"/class=\"kn\"/g" | |
|
32 | ||
|
25 | 33 | echo % hgweb fileannotate, html |
|
26 | 34 | ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py') \ |
|
27 | 35 | | sed "s/[0-9]* years ago/long ago/g" | sed "s/class=\"k\"/class=\"kn\"/g" |
|
28 | 36 | |
|
29 | 37 | echo % hgweb fileannotate, raw |
|
30 | 38 | ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py?style=raw') \ |
|
31 | 39 | | sed "s/test@//" > a |
|
32 | 40 | |
|
33 | 41 | echo "200 Script output follows" > b |
|
34 | 42 | echo "" >> b |
|
35 | 43 | echo "" >> b |
|
36 | 44 | hg annotate "get-with-headers.py" >> b |
|
37 | 45 | echo "" >> b |
|
38 | 46 | echo "" >> b |
|
39 | 47 | echo "" >> b |
|
40 | 48 | echo "" >> b |
|
41 | 49 | |
|
42 | 50 | diff -u b a |
|
43 | 51 | |
|
44 | 52 | echo |
|
45 | 53 | echo % hgweb filerevision, raw |
|
46 | 54 | ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py?style=raw') \ |
|
47 | 55 | > a |
|
48 | 56 | |
|
49 | 57 | echo "200 Script output follows" > b |
|
50 | 58 | echo "" >> b |
|
51 | 59 | hg cat get-with-headers.py >> b |
|
52 | 60 | |
|
53 | 61 | diff -u b a |
|
54 | 62 | |
|
55 | 63 | echo |
|
56 | 64 | echo % hgweb highlightcss friendly |
|
57 | 65 | "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out |
|
58 | 66 | head -n 4 out |
|
59 | 67 | rm out |
|
60 | 68 | |
|
61 | 69 | echo % errors encountered |
|
62 | 70 | cat errors.log |
|
63 | 71 | kill `cat hg.pid` |
|
64 | 72 | |
|
65 | 73 | # Change the pygments style |
|
66 | 74 | cat > .hg/hgrc <<EOF |
|
67 | 75 | [web] |
|
68 | 76 | pygments_style = fruity |
|
69 | 77 | EOF |
|
70 | 78 | |
|
71 | 79 | echo % hg serve again |
|
72 | 80 | hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log |
|
73 | 81 | cat hg.pid >> $DAEMON_PIDS |
|
74 | 82 | |
|
75 | 83 | echo % hgweb highlightcss fruity |
|
76 | 84 | "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out |
|
77 | 85 | head -n 4 out |
|
78 | 86 | rm out |
|
79 | 87 | |
|
80 | 88 | echo % errors encountered |
|
81 | 89 | cat errors.log |
|
82 | 90 |
@@ -1,149 +1,213 b'' | |||
|
1 | 1 | adding get-with-headers.py |
|
2 | adding isolatin | |
|
2 | 3 | % hg serve |
|
3 | 4 | % hgweb filerevision, html |
|
4 | 5 | 200 Script output follows |
|
5 | 6 | |
|
6 | 7 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
7 | 8 | <html> |
|
8 | 9 | <head> |
|
9 | 10 | <link rel="icon" href="/static/hgicon.png" type="image/png"> |
|
10 | 11 | <meta name="robots" content="index, nofollow" /> |
|
11 | 12 | <link rel="stylesheet" href="/static/style.css" type="text/css" /> |
|
12 | 13 | |
|
13 | 14 | <link rel="stylesheet" href="/highlightcss" type="text/css" /> |
|
14 | 15 | <title>test:get-with-headers.py</title> |
|
15 | 16 | </head> |
|
16 | 17 | <body> |
|
17 | 18 | |
|
18 | 19 | <div class="buttons"> |
|
19 | 20 | <a href="/log/0">changelog</a> |
|
20 | 21 | <a href="/shortlog/0">shortlog</a> |
|
21 | 22 | <a href="/graph">graph</a> |
|
22 | 23 | <a href="/tags">tags</a> |
|
23 |
<a href="/rev/ |
|
|
24 |
<a href="/file/ |
|
|
25 |
<a href="/log/ |
|
|
26 |
<a href="/annotate/ |
|
|
27 |
<a href="/raw-file/ |
|
|
24 | <a href="/rev/7697c52ca9b0">changeset</a> | |
|
25 | <a href="/file/7697c52ca9b0/">files</a> | |
|
26 | <a href="/log/7697c52ca9b0/get-with-headers.py">revisions</a> | |
|
27 | <a href="/annotate/7697c52ca9b0/get-with-headers.py">annotate</a> | |
|
28 | <a href="/raw-file/7697c52ca9b0/get-with-headers.py">raw</a> | |
|
28 | 29 | </div> |
|
29 | 30 | |
|
30 | 31 | <h2>get-with-headers.py</h2> |
|
31 | 32 | |
|
32 | 33 | <table> |
|
33 | 34 | <tr> |
|
34 | 35 | <td class="metatag">changeset 0:</td> |
|
35 |
<td><a href="/rev/ |
|
|
36 | <td><a href="/rev/7697c52ca9b0">7697c52ca9b0</a></td></tr> | |
|
36 | 37 | |
|
37 | 38 | |
|
38 | 39 | <tr> |
|
39 | 40 | <td class="metatag">author:</td> |
|
40 | 41 | <td>test</td></tr> |
|
41 | 42 | <tr> |
|
42 | 43 | <td class="metatag">date:</td> |
|
43 | 44 | <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td></tr> |
|
44 | 45 | <tr> |
|
45 | 46 | <td class="metatag">permissions:</td> |
|
46 | 47 | <td>-rwxr-xr-x</td></tr> |
|
47 | 48 | <tr> |
|
48 | 49 | <td class="metatag">description:</td> |
|
49 | 50 | <td>a</td> |
|
50 | 51 | </tr> |
|
51 | 52 | </table> |
|
52 | 53 | |
|
53 | 54 | <pre> |
|
54 | 55 | <div class="parity0"><a class="lineno" href="#l1" id="l1"> 1</a><span class="c">#!/usr/bin/env python</span></div><div class="parity1"><a class="lineno" href="#l2" id="l2"> 2</a></div><div class="parity0"><a class="lineno" href="#l3" id="l3"> 3</a><span class="n">__doc__</span> <span class="o">=</span> <span class="s">"""This does HTTP get requests given a host:port and path and returns</span></div><div class="parity1"><a class="lineno" href="#l4" id="l4"> 4</a><span class="s">a subset of the headers plus the body of the result."""</span></div><div class="parity0"><a class="lineno" href="#l5" id="l5"> 5</a></div><div class="parity1"><a class="lineno" href="#l6" id="l6"> 6</a><span class="kn">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></div><div class="parity0"><a class="lineno" href="#l7" id="l7"> 7</a></div><div class="parity1"><a class="lineno" href="#l8" id="l8"> 8</a><span class="kn">try</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l9" id="l9"> 9</a> <span class="kn">import</span> <span class="nn">msvcrt</span><span class="o">,</span> <span class="nn">os</span></div><div class="parity1"><a class="lineno" href="#l10" id="l10"> 10</a> <span class="n">msvcrt</span><span class="o">.</span><span class="n">setmode</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">os</span><span class="o">.</span><span class="n">O_BINARY</span><span class="p">)</span></div><div class="parity0"><a class="lineno" href="#l11" id="l11"> 11</a> <span class="n">msvcrt</span><span class="o">.</span><span class="n">setmode</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">os</span><span class="o">.</span><span class="n">O_BINARY</span><span class="p">)</span></div><div class="parity1"><a class="lineno" href="#l12" id="l12"> 12</a><span class="kn">except</span> <span class="ne">ImportError</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l13" id="l13"> 13</a> <span class="kn">pass</span></div><div class="parity1"><a class="lineno" href="#l14" id="l14"> 14</a></div><div class="parity0"><a class="lineno" href="#l15" id="l15"> 15</a><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="kn">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></div><div class="parity1"><a class="lineno" href="#l16" id="l16"> 16</a><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></div><div class="parity0"><a class="lineno" href="#l17" id="l17"> 17</a><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">"GET"</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></div><div class="parity1"><a class="lineno" href="#l18" id="l18"> 18</a><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></div><div class="parity0"><a class="lineno" href="#l19" id="l19"> 19</a><span class="kn">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></div><div class="parity1"><a class="lineno" href="#l20" id="l20"> 20</a><span class="kn">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l21" id="l21"> 21</a> <span class="kn">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></div><div class="parity1"><a class="lineno" href="#l22" id="l22"> 22</a> <span class="kn">print</span> <span class="s">"</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></div><div class="parity0"><a class="lineno" href="#l23" id="l23"> 23</a><span class="kn">print</span></div><div class="parity1"><a class="lineno" href="#l24" id="l24"> 24</a><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></div><div class="parity0"><a class="lineno" href="#l25" id="l25"> 25</a></div><div class="parity1"><a class="lineno" href="#l26" id="l26"> 26</a><span class="kn">if</span> <span class="mf">200</span> <span class="o"><=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o"><=</span> <span class="mf">299</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l27" id="l27"> 27</a> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></div><div class="parity1"><a class="lineno" href="#l28" id="l28"> 28</a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></div> |
|
55 | 56 | </pre> |
|
56 | 57 | |
|
57 | 58 | |
|
58 | 59 | <div class="logo"> |
|
59 | 60 | <a href="http://www.selenic.com/mercurial/"> |
|
60 | 61 | <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a> |
|
61 | 62 | </div> |
|
62 | 63 | |
|
63 | 64 | </body> |
|
64 | 65 | </html> |
|
65 | 66 | |
|
67 | % hgweb filerevision, html | |
|
68 | 200 Script output follows | |
|
69 | ||
|
70 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
|
71 | <html> | |
|
72 | <head> | |
|
73 | <link rel="icon" href="/static/hgicon.png" type="image/png"> | |
|
74 | <meta name="robots" content="index, nofollow" /> | |
|
75 | <link rel="stylesheet" href="/static/style.css" type="text/css" /> | |
|
76 | ||
|
77 | <link rel="stylesheet" href="/highlightcss" type="text/css" /> | |
|
78 | <title>test:isolatin</title> | |
|
79 | </head> | |
|
80 | <body> | |
|
81 | ||
|
82 | <div class="buttons"> | |
|
83 | <a href="/log/0">changelog</a> | |
|
84 | <a href="/shortlog/0">shortlog</a> | |
|
85 | <a href="/graph">graph</a> | |
|
86 | <a href="/tags">tags</a> | |
|
87 | <a href="/rev/7697c52ca9b0">changeset</a> | |
|
88 | <a href="/file/7697c52ca9b0/">files</a> | |
|
89 | <a href="/log/7697c52ca9b0/isolatin">revisions</a> | |
|
90 | <a href="/annotate/7697c52ca9b0/isolatin">annotate</a> | |
|
91 | <a href="/raw-file/7697c52ca9b0/isolatin">raw</a> | |
|
92 | </div> | |
|
93 | ||
|
94 | <h2>isolatin</h2> | |
|
95 | ||
|
96 | <table> | |
|
97 | <tr> | |
|
98 | <td class="metatag">changeset 0:</td> | |
|
99 | <td><a href="/rev/7697c52ca9b0">7697c52ca9b0</a></td></tr> | |
|
100 | ||
|
101 | ||
|
102 | <tr> | |
|
103 | <td class="metatag">author:</td> | |
|
104 | <td>test</td></tr> | |
|
105 | <tr> | |
|
106 | <td class="metatag">date:</td> | |
|
107 | <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td></tr> | |
|
108 | <tr> | |
|
109 | <td class="metatag">permissions:</td> | |
|
110 | <td>-rw-r--r--</td></tr> | |
|
111 | <tr> | |
|
112 | <td class="metatag">description:</td> | |
|
113 | <td>a</td> | |
|
114 | </tr> | |
|
115 | </table> | |
|
116 | ||
|
117 | <pre> | |
|
118 | <div class="parity0"><a class="lineno" href="#l1" id="l1"> 1</a>h?bsch</div> | |
|
119 | </pre> | |
|
120 | ||
|
121 | ||
|
122 | <div class="logo"> | |
|
123 | <a href="http://www.selenic.com/mercurial/"> | |
|
124 | <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a> | |
|
125 | </div> | |
|
126 | ||
|
127 | </body> | |
|
128 | </html> | |
|
129 | ||
|
66 | 130 | % hgweb fileannotate, html |
|
67 | 131 | 200 Script output follows |
|
68 | 132 | |
|
69 | 133 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
70 | 134 | <html> |
|
71 | 135 | <head> |
|
72 | 136 | <link rel="icon" href="/static/hgicon.png" type="image/png"> |
|
73 | 137 | <meta name="robots" content="index, nofollow" /> |
|
74 | 138 | <link rel="stylesheet" href="/static/style.css" type="text/css" /> |
|
75 | 139 | |
|
76 | 140 | <link rel="stylesheet" href="/highlightcss" type="text/css" /> |
|
77 | 141 | <title>test: get-with-headers.py annotate</title> |
|
78 | 142 | </head> |
|
79 | 143 | <body> |
|
80 | 144 | |
|
81 | 145 | <div class="buttons"> |
|
82 | 146 | <a href="/log/0">changelog</a> |
|
83 | 147 | <a href="/shortlog/0">shortlog</a> |
|
84 | 148 | <a href="/graph">graph</a> |
|
85 | 149 | <a href="/tags">tags</a> |
|
86 |
<a href="/rev/ |
|
|
87 |
<a href="/file/ |
|
|
88 |
<a href="/file/ |
|
|
89 |
<a href="/log/ |
|
|
90 |
<a href="/raw-annotate/ |
|
|
150 | <a href="/rev/7697c52ca9b0">changeset</a> | |
|
151 | <a href="/file/7697c52ca9b0/">files</a> | |
|
152 | <a href="/file/7697c52ca9b0/get-with-headers.py">file</a> | |
|
153 | <a href="/log/7697c52ca9b0/get-with-headers.py">revisions</a> | |
|
154 | <a href="/raw-annotate/7697c52ca9b0/get-with-headers.py">raw</a> | |
|
91 | 155 | </div> |
|
92 | 156 | |
|
93 | 157 | <h2>Annotate get-with-headers.py</h2> |
|
94 | 158 | |
|
95 | 159 | <table> |
|
96 | 160 | <tr> |
|
97 | 161 | <td class="metatag">changeset 0:</td> |
|
98 |
<td><a href="/rev/ |
|
|
162 | <td><a href="/rev/7697c52ca9b0">7697c52ca9b0</a></td></tr> | |
|
99 | 163 | |
|
100 | 164 | |
|
101 | 165 | <tr> |
|
102 | 166 | <td class="metatag">author:</td> |
|
103 | 167 | <td>test</td></tr> |
|
104 | 168 | <tr> |
|
105 | 169 | <td class="metatag">date:</td> |
|
106 | 170 | <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td> |
|
107 | 171 | </tr> |
|
108 | 172 | <tr> |
|
109 | 173 | <td class="metatag">permissions:</td> |
|
110 | 174 | <td>-rwxr-xr-x</td> |
|
111 | 175 | </tr> |
|
112 | 176 | <tr> |
|
113 | 177 | <td class="metatag">description:</td> |
|
114 | 178 | <td>a</td> |
|
115 | 179 | </tr> |
|
116 | 180 | </table> |
|
117 | 181 | |
|
118 | 182 | <br/> |
|
119 | 183 | |
|
120 | 184 | <table cellspacing="0" cellpadding="0"> |
|
121 |
<tr class="parity0"><td class="annotate"><a href="/annotate/ |
|
|
185 | <tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l1" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l1" id="l1"> 1</a></td><td><pre><span class="c">#!/usr/bin/env python</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l2" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l2" id="l2"> 2</a></td><td><pre></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l3" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l3" id="l3"> 3</a></td><td><pre><span class="n">__doc__</span> <span class="o">=</span> <span class="s">"""This does HTTP get requests given a host:port and path and returns</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l4" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l4" id="l4"> 4</a></td><td><pre><span class="s">a subset of the headers plus the body of the result."""</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l5" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l5" id="l5"> 5</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l6" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l6" id="l6"> 6</a></td><td><pre><span class="kn">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l7" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l7" id="l7"> 7</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l8" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l8" id="l8"> 8</a></td><td><pre><span class="kn">try</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l9" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l9" id="l9"> 9</a></td><td><pre> <span class="kn">import</span> <span class="nn">msvcrt</span><span class="o">,</span> <span class="nn">os</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l10" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l10" id="l10"> 10</a></td><td><pre> <span class="n">msvcrt</span><span class="o">.</span><span class="n">setmode</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">os</span><span class="o">.</span><span class="n">O_BINARY</span><span class="p">)</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l11" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l11" id="l11"> 11</a></td><td><pre> <span class="n">msvcrt</span><span class="o">.</span><span class="n">setmode</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">os</span><span class="o">.</span><span class="n">O_BINARY</span><span class="p">)</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l12" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l12" id="l12"> 12</a></td><td><pre><span class="kn">except</span> <span class="ne">ImportError</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l13" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l13" id="l13"> 13</a></td><td><pre> <span class="kn">pass</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l14" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l14" id="l14"> 14</a></td><td><pre></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l15" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l15" id="l15"> 15</a></td><td><pre><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="kn">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l16" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l16" id="l16"> 16</a></td><td><pre><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l17" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l17" id="l17"> 17</a></td><td><pre><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">"GET"</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l18" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l18" id="l18"> 18</a></td><td><pre><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l19" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l19" id="l19"> 19</a></td><td><pre><span class="kn">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l20" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l20" id="l20"> 20</a></td><td><pre><span class="kn">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l21" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l21" id="l21"> 21</a></td><td><pre> <span class="kn">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l22" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l22" id="l22"> 22</a></td><td><pre> <span class="kn">print</span> <span class="s">"</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l23" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l23" id="l23"> 23</a></td><td><pre><span class="kn">print</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l24" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l24" id="l24"> 24</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l25" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l25" id="l25"> 25</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l26" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l26" id="l26"> 26</a></td><td><pre><span class="kn">if</span> <span class="mf">200</span> <span class="o"><=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o"><=</span> <span class="mf">299</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l27" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l27" id="l27"> 27</a></td><td><pre> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/7697c52ca9b0/get-with-headers.py#l28" title="7697c52ca9b0: a">test@0</a></td><td><a class="lineno" href="#l28" id="l28"> 28</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></pre></td></tr> | |
|
122 | 186 | </table> |
|
123 | 187 | |
|
124 | 188 | |
|
125 | 189 | <div class="logo"> |
|
126 | 190 | <a href="http://www.selenic.com/mercurial/"> |
|
127 | 191 | <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a> |
|
128 | 192 | </div> |
|
129 | 193 | |
|
130 | 194 | </body> |
|
131 | 195 | </html> |
|
132 | 196 | |
|
133 | 197 | % hgweb fileannotate, raw |
|
134 | 198 | |
|
135 | 199 | % hgweb filerevision, raw |
|
136 | 200 | |
|
137 | 201 | % hgweb highlightcss friendly |
|
138 | 202 | 200 Script output follows |
|
139 | 203 | |
|
140 | 204 | /* pygments_style = friendly */ |
|
141 | 205 | |
|
142 | 206 | % errors encountered |
|
143 | 207 | % hg serve again |
|
144 | 208 | % hgweb highlightcss fruity |
|
145 | 209 | 200 Script output follows |
|
146 | 210 | |
|
147 | 211 | /* pygments_style = fruity */ |
|
148 | 212 | |
|
149 | 213 | % errors encountered |
General Comments 0
You need to be logged in to leave comments.
Login now