Show More
@@ -9,8 +9,10 from __future__ import absolute_import | |||||
9 |
|
9 | |||
10 | import struct |
|
10 | import struct | |
11 |
|
11 | |||
12 | from . import pycompat |
|
12 | from . import policy, pycompat | |
13 | stringio = pycompat.stringio |
|
13 | stringio = pycompat.stringio | |
|
14 | modulepolicy = policy.policy | |||
|
15 | policynocffi = policy.policynocffi | |||
14 |
|
16 | |||
15 | class mpatchError(Exception): |
|
17 | class mpatchError(Exception): | |
16 | """error raised when a delta cannot be decoded |
|
18 | """error raised when a delta cannot be decoded | |
@@ -125,3 +127,44 def patchedsize(orig, delta): | |||||
125 |
|
127 | |||
126 | outlen += orig - last |
|
128 | outlen += orig - last | |
127 | return outlen |
|
129 | return outlen | |
|
130 | ||||
|
131 | if modulepolicy not in policynocffi: | |||
|
132 | try: | |||
|
133 | from _mpatch_cffi import ffi, lib | |||
|
134 | except ImportError: | |||
|
135 | if modulepolicy == 'cffi': # strict cffi import | |||
|
136 | raise | |||
|
137 | else: | |||
|
138 | @ffi.def_extern() | |||
|
139 | def cffi_get_next_item(arg, pos): | |||
|
140 | all, bins = ffi.from_handle(arg) | |||
|
141 | container = ffi.new("struct mpatch_flist*[1]") | |||
|
142 | to_pass = ffi.new("char[]", str(bins[pos])) | |||
|
143 | all.append(to_pass) | |||
|
144 | r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) | |||
|
145 | if r < 0: | |||
|
146 | return ffi.NULL | |||
|
147 | return container[0] | |||
|
148 | ||||
|
149 | def patches(text, bins): | |||
|
150 | lgt = len(bins) | |||
|
151 | all = [] | |||
|
152 | if not lgt: | |||
|
153 | return text | |||
|
154 | arg = (all, bins) | |||
|
155 | patch = lib.mpatch_fold(ffi.new_handle(arg), | |||
|
156 | lib.cffi_get_next_item, 0, lgt) | |||
|
157 | if not patch: | |||
|
158 | raise mpatchError("cannot decode chunk") | |||
|
159 | outlen = lib.mpatch_calcsize(len(text), patch) | |||
|
160 | if outlen < 0: | |||
|
161 | lib.mpatch_lfree(patch) | |||
|
162 | raise mpatchError("inconsistency detected") | |||
|
163 | buf = ffi.new("char[]", outlen) | |||
|
164 | if lib.mpatch_apply(buf, text, len(text), patch) < 0: | |||
|
165 | lib.mpatch_lfree(patch) | |||
|
166 | raise mpatchError("error applying patches") | |||
|
167 | res = ffi.buffer(buf, outlen)[:] | |||
|
168 | lib.mpatch_lfree(patch) | |||
|
169 | return res | |||
|
170 |
@@ -318,7 +318,8 class hgbuildpy(build_py): | |||||
318 | if self.distribution.pure: |
|
318 | if self.distribution.pure: | |
319 | self.distribution.ext_modules = [] |
|
319 | self.distribution.ext_modules = [] | |
320 | elif self.distribution.cffi: |
|
320 | elif self.distribution.cffi: | |
321 | exts = [] |
|
321 | import setup_mpatch_cffi | |
|
322 | exts = [setup_mpatch_cffi.ffi.distutils_extension()] | |||
322 | # cffi modules go here |
|
323 | # cffi modules go here | |
323 | if sys.platform == 'darwin': |
|
324 | if sys.platform == 'darwin': | |
324 | import setup_osutil_cffi |
|
325 | import setup_osutil_cffi |
General Comments 0
You need to be logged in to leave comments.
Login now