lib/oe/patch: handle non-UTF8 encoding when reading patches

When extracting patches from a git repository with PATCHTOOL = "git" we
cannot assume that all patches will be UTF-8 formatted, so as with other
places in this module, try latin-1 if utf-8 fails.

This fixes UnicodeDecodeError running devtool update-recipe or devtool
finish on the openssl recipe.

(From OE-Core rev: 579e4d54a212d04cfece2c9fc0635d7ac1644058)

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Paul Eggleton
2016-08-31 15:28:46 +12:00
committed by Richard Purdie
parent ead0545626
commit 94aefd9a39

View File

@@ -415,16 +415,24 @@ class GitApplyTree(PatchTree):
out = runcmd(["sh", "-c", " ".join(shellcmd)], tree) out = runcmd(["sh", "-c", " ".join(shellcmd)], tree)
if out: if out:
for srcfile in out.split(): for srcfile in out.split():
patchlines = [] for encoding in ['utf-8', 'latin-1']:
outfile = None patchlines = []
with open(srcfile, 'r') as f: outfile = None
for line in f: try:
if line.startswith(GitApplyTree.patch_line_prefix): with open(srcfile, 'r', encoding=encoding) as f:
outfile = line.split()[-1].strip() for line in f:
continue if line.startswith(GitApplyTree.patch_line_prefix):
if line.startswith(GitApplyTree.ignore_commit_prefix): outfile = line.split()[-1].strip()
continue continue
patchlines.append(line) if line.startswith(GitApplyTree.ignore_commit_prefix):
continue
patchlines.append(line)
except UnicodeDecodeError:
continue
break
else:
raise PatchError('Unable to find a character encoding to decode %s' % srcfile)
if not outfile: if not outfile:
outfile = os.path.basename(srcfile) outfile = os.path.basename(srcfile)
with open(os.path.join(outdir, outfile), 'w') as of: with open(os.path.join(outdir, outfile), 'w') as of: