[OE-core] [PATCH 3/6] lib/oe/patch: handle encoding differences in patch files

Khem Raj raj.khem at gmail.com
Thu Jul 7 00:40:03 UTC 2016


On Wed, Jul 6, 2016 at 4:57 PM, Paul Eggleton
<paul.eggleton at linux.intel.com> wrote:
> With Python 3, the encoding of a file is significant; several recipes in
> OE-Core have patches which are not fully utf-8 decodable e.g. man,
> lrzsz, and gstreamer1.0-libav, leading to errors when using devtool's
> modify, upgrade or extract subcommands on these recipes. To work around
> this, try reading the patch file as utf-8 first and if that fails try
> latin-1 before giving up.


while this makes devtool robust. I think it will also be a good
opportunity to inform
the user/developer of the utf-8 non compliant file and may be a hint
on how to fix it if possible

>
> Signed-off-by: Paul Eggleton <paul.eggleton at linux.intel.com>
> ---
>  meta/lib/oe/patch.py | 100 +++++++++++++++++++++++++++++----------------------
>  1 file changed, 57 insertions(+), 43 deletions(-)
>
> diff --git a/meta/lib/oe/patch.py b/meta/lib/oe/patch.py
> index 4a0d3f7..af3adec 100644
> --- a/meta/lib/oe/patch.py
> +++ b/meta/lib/oe/patch.py
> @@ -117,43 +117,50 @@ class PatchSet(object):
>                  return None
>              return os.sep.join(filesplit[striplevel:])
>
> -        copiedmode = False
> -        filelist = []
> -        with open(patchfile) as f:
> -            for line in f:
> -                if line.startswith('--- '):
> -                    patchpth = patchedpath(line)
> -                    if not patchpth:
> -                        break
> -                    if copiedmode:
> -                        addedfile = patchpth
> -                    else:
> -                        removedfile = patchpth
> -                elif line.startswith('+++ '):
> -                    addedfile = patchedpath(line)
> -                    if not addedfile:
> -                        break
> -                elif line.startswith('*** '):
> -                    copiedmode = True
> -                    removedfile = patchedpath(line)
> -                    if not removedfile:
> -                        break
> -                else:
> -                    removedfile = None
> -                    addedfile = None
> -
> -                if addedfile and removedfile:
> -                    if removedfile == '/dev/null':
> -                        mode = 'A'
> -                    elif addedfile == '/dev/null':
> -                        mode = 'D'
> -                    else:
> -                        mode = 'M'
> -                    if srcdir:
> -                        fullpath = os.path.abspath(os.path.join(srcdir, addedfile))
> -                    else:
> -                        fullpath = addedfile
> -                    filelist.append((fullpath, mode))
> +        for encoding in ['utf-8', 'latin-1']:
> +            try:
> +                copiedmode = False
> +                filelist = []
> +                with open(patchfile) as f:
> +                    for line in f:
> +                        if line.startswith('--- '):
> +                            patchpth = patchedpath(line)
> +                            if not patchpth:
> +                                break
> +                            if copiedmode:
> +                                addedfile = patchpth
> +                            else:
> +                                removedfile = patchpth
> +                        elif line.startswith('+++ '):
> +                            addedfile = patchedpath(line)
> +                            if not addedfile:
> +                                break
> +                        elif line.startswith('*** '):
> +                            copiedmode = True
> +                            removedfile = patchedpath(line)
> +                            if not removedfile:
> +                                break
> +                        else:
> +                            removedfile = None
> +                            addedfile = None
> +
> +                        if addedfile and removedfile:
> +                            if removedfile == '/dev/null':
> +                                mode = 'A'
> +                            elif addedfile == '/dev/null':
> +                                mode = 'D'
> +                            else:
> +                                mode = 'M'
> +                            if srcdir:
> +                                fullpath = os.path.abspath(os.path.join(srcdir, addedfile))
> +                            else:
> +                                fullpath = addedfile
> +                            filelist.append((fullpath, mode))
> +            except UnicodeDecodeError:
> +                continue
> +            break
> +        else:
> +            raise PatchError('Unable to decode %s' % patchfile)
>
>          return filelist
>
> @@ -280,12 +287,19 @@ class GitApplyTree(PatchTree):
>          """
>          Extract just the header lines from the top of a patch file
>          """
> -        lines = []
> -        with open(patchfile, 'r') as f:
> -            for line in f.readlines():
> -                if line.startswith('Index: ') or line.startswith('diff -') or line.startswith('---'):
> -                    break
> -                lines.append(line)
> +        for encoding in ['utf-8', 'latin-1']:
> +            lines = []
> +            try:
> +                with open(patchfile, 'r', encoding=encoding) as f:
> +                    for line in f:
> +                        if line.startswith('Index: ') or line.startswith('diff -') or line.startswith('---'):
> +                            break
> +                        lines.append(line)
> +            except UnicodeDecodeError:
> +                continue
> +            break
> +        else:
> +            raise PatchError('Unable to find a character encoding to decode %s' % patchfile)
>          return lines
>
>      @staticmethod
> --
> 2.5.5
>
> --
> _______________________________________________
> Openembedded-core mailing list
> Openembedded-core at lists.openembedded.org
> http://lists.openembedded.org/mailman/listinfo/openembedded-core



More information about the Openembedded-core mailing list