Openembedded Core Discussions
 help / color / mirror / Atom feed
* [Patch 0/1 V2] Update relocate_sdk.py to work with Python 2.4
@ 2013-10-16 15:44 Konrad Scherer
  2013-10-16 15:44 ` [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3 Konrad Scherer
  0 siblings, 1 reply; 5+ messages in thread
From: Konrad Scherer @ 2013-10-16 15:44 UTC (permalink / raw)
  To: openembedded-core

As a bootstrap script, relocate_sdk.py needs to support as many
versions of python as possible. Since python 2.4.3 is default python
on RedHat/CentOS 5.x, the recent updates to support python 3 broke
these platforms. This is the cleanest way I could find to support all
pythons 2.4 to 3. I have tested the patch on 2.4.3, 2.6.5, 2.7.3 and
3.2.3.

V2: Detect the system encoding before converting

---
Konrad Scherer, MTS, Linux Products Group, Wind River


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3.
  2013-10-16 15:44 [Patch 0/1 V2] Update relocate_sdk.py to work with Python 2.4 Konrad Scherer
@ 2013-10-16 15:44 ` Konrad Scherer
  2013-10-17  6:54   ` Laurentiu Palcu
  0 siblings, 1 reply; 5+ messages in thread
From: Konrad Scherer @ 2013-10-16 15:44 UTC (permalink / raw)
  To: openembedded-core

From: Konrad Scherer <Konrad.Scherer@windriver.com>

Python 2.4 does not support the 'b' string literal or the
keyword 'as' in exception handling. Python 3 does not accept
the old method of exception handling and defaults to unicode.
The b() function converts strings to bytes on Python 3 and
using sys.exc_info() avoids the exception handling syntax.

Signed-off-by: Konrad Scherer <Konrad.Scherer@windriver.com>
---
 scripts/relocate_sdk.py | 43 ++++++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index fe6e4e0..a15302d 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -31,7 +31,15 @@ import os
 import re
 import errno
 
-old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
+if sys.version < '3':
+    def b(x):
+        return x
+else:
+    import codecs
+    def b(x):
+        return x.encode(sys.getfilesystemencoding())
+
+old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
 
 def get_arch():
     f.seek(0)
@@ -92,15 +100,15 @@ def change_interpreter(elf_file_name):
             # External SDKs with mixed pre-compiled binaries should not get
             # relocated so look for some variant of /lib
             fname = f.read(11)
-            if fname.startswith(b"/lib/") or fname.startswith(b"/lib64/") or \
-               fname.startswith(b"/lib32/") or fname.startswith(b"/usr/lib32/") or \
-               fname.startswith(b"/usr/lib32/") or fname.startswith(b"/usr/lib64/"):
+            if fname.startswith(b("/lib/")) or fname.startswith(b("/lib64/")) or \
+               fname.startswith(b("/lib32/")) or fname.startswith(b("/usr/lib32/")) or \
+               fname.startswith(b("/usr/lib32/")) or fname.startswith(b("/usr/lib64/")):
                 break
             if (len(new_dl_path) >= p_filesz):
                 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
                     % (elf_file_name, p_memsz, len(new_dl_path) + 1))
                 break
-            dl_path = new_dl_path + b"\0" * (p_filesz - len(new_dl_path))
+            dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path))
             f.seek(p_offset)
             f.write(dl_path)
             break
@@ -132,40 +140,40 @@ def change_dl_sysdirs():
         sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
             sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr)
 
-        name = sh_strtab[sh_name:sh_strtab.find(b"\0", sh_name)]
+        name = sh_strtab[sh_name:sh_strtab.find(b("\0"), sh_name)]
 
         """ look only into SHT_PROGBITS sections """
         if sh_type == 1:
             f.seek(sh_offset)
             """ default library paths cannot be changed on the fly because  """
             """ the string lengths have to be changed too.                  """
-            if name == b".sysdirs":
+            if name == b(".sysdirs"):
                 sysdirs = f.read(sh_size)
                 sysdirs_off = sh_offset
                 sysdirs_sect_size = sh_size
-            elif name == b".sysdirslen":
+            elif name == b(".sysdirslen"):
                 sysdirslen = f.read(sh_size)
                 sysdirslen_off = sh_offset
-            elif name == b".ldsocache":
+            elif name == b(".ldsocache"):
                 ldsocache_path = f.read(sh_size)
                 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
                 # pad with zeros
-                new_ldsocache_path += b"\0" * (sh_size - len(new_ldsocache_path))
+                new_ldsocache_path += b("\0") * (sh_size - len(new_ldsocache_path))
                 # write it back
                 f.seek(sh_offset)
                 f.write(new_ldsocache_path)
 
     if sysdirs != "" and sysdirslen != "":
-        paths = sysdirs.split(b"\0")
-        sysdirs = b""
-        sysdirslen = b""
+        paths = sysdirs.split(b("\0"))
+        sysdirs = b("")
+        sysdirslen = b("")
         for path in paths:
             """ exit the loop when we encounter first empty string """
-            if path == b"":
+            if path == b(""):
                 break
 
             new_path = old_prefix.sub(new_prefix, path)
-            sysdirs += new_path + b"\0"
+            sysdirs += new_path + b("\0")
 
             if arch == 32:
                 sysdirslen += struct.pack("<L", len(new_path))
@@ -173,7 +181,7 @@ def change_dl_sysdirs():
                 sysdirslen += struct.pack("<Q", len(new_path))
 
         """ pad with zeros """
-        sysdirs += b"\0" * (sysdirs_sect_size - len(sysdirs))
+        sysdirs += b("\0") * (sysdirs_sect_size - len(sysdirs))
 
         """ write the sections back """
         f.seek(sysdirs_off)
@@ -205,7 +213,8 @@ for e in executables_list:
 
     try:
         f = open(e, "r+b")
-    except IOError as ioex:
+    except IOError:
+        exctype, ioex = sys.exc_info()[:2]
         if ioex.errno == errno.ETXTBSY:
             print("Could not open %s. File used by another process.\nPlease "\
                   "make sure you exit all processes that might use any SDK "\
-- 
1.8.1.2



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3.
  2013-10-16 15:44 ` [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3 Konrad Scherer
@ 2013-10-17  6:54   ` Laurentiu Palcu
  2013-10-17  7:01     ` Laurentiu Palcu
  0 siblings, 1 reply; 5+ messages in thread
From: Laurentiu Palcu @ 2013-10-17  6:54 UTC (permalink / raw)
  To: Konrad Scherer; +Cc: openembedded-core

On Wed, Oct 16, 2013 at 11:44:59AM -0400, Konrad Scherer wrote:
> From: Konrad Scherer <Konrad.Scherer@windriver.com>
> 
> Python 2.4 does not support the 'b' string literal or the
> keyword 'as' in exception handling. Python 3 does not accept
> the old method of exception handling and defaults to unicode.
> The b() function converts strings to bytes on Python 3 and
> using sys.exc_info() avoids the exception handling syntax.
> 
> Signed-off-by: Konrad Scherer <Konrad.Scherer@windriver.com>
> ---
>  scripts/relocate_sdk.py | 43 ++++++++++++++++++++++++++-----------------
>  1 file changed, 26 insertions(+), 17 deletions(-)
> 
> diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
> index fe6e4e0..a15302d 100755
> --- a/scripts/relocate_sdk.py
> +++ b/scripts/relocate_sdk.py
> @@ -31,7 +31,15 @@ import os
>  import re
>  import errno
>  
> -old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
> +if sys.version < '3':
> +    def b(x):
> +        return x
> +else:
> +    import codecs
I can drop this import.

Thanks,
Laurentiu

> +    def b(x):
> +        return x.encode(sys.getfilesystemencoding())
> +
> +old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
>  



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3.
  2013-10-17  6:54   ` Laurentiu Palcu
@ 2013-10-17  7:01     ` Laurentiu Palcu
  0 siblings, 0 replies; 5+ messages in thread
From: Laurentiu Palcu @ 2013-10-17  7:01 UTC (permalink / raw)
  To: Konrad Scherer; +Cc: openembedded-core

On Thu, Oct 17, 2013 at 09:54:11AM +0300, Laurentiu Palcu wrote:
> On Wed, Oct 16, 2013 at 11:44:59AM -0400, Konrad Scherer wrote:
> > From: Konrad Scherer <Konrad.Scherer@windriver.com>
> > 
> > Python 2.4 does not support the 'b' string literal or the
> > keyword 'as' in exception handling. Python 3 does not accept
> > the old method of exception handling and defaults to unicode.
> > The b() function converts strings to bytes on Python 3 and
> > using sys.exc_info() avoids the exception handling syntax.
> > 
> > Signed-off-by: Konrad Scherer <Konrad.Scherer@windriver.com>
> > ---
> >  scripts/relocate_sdk.py | 43 ++++++++++++++++++++++++++-----------------
> >  1 file changed, 26 insertions(+), 17 deletions(-)
> > 
> > diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
> > index fe6e4e0..a15302d 100755
> > --- a/scripts/relocate_sdk.py
> > +++ b/scripts/relocate_sdk.py
> > @@ -31,7 +31,15 @@ import os
> >  import re
> >  import errno
> >  
> > -old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
> > +if sys.version < '3':
> > +    def b(x):
> > +        return x
> > +else:
> > +    import codecs
> I meant, you can drop this import. Too early in the morning! :)
> 
> Thanks,
> Laurentiu
> 
> > +    def b(x):
> > +        return x.encode(sys.getfilesystemencoding())
> > +
> > +old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
> >  
> 
> _______________________________________________
> Openembedded-core mailing list
> Openembedded-core@lists.openembedded.org
> http://lists.openembedded.org/mailman/listinfo/openembedded-core


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3.
  2013-10-17 14:17 [Patch 0/1 V3] Update relocate_sdk.py to work with Python 2.4 Konrad Scherer
@ 2013-10-17 14:17 ` Konrad Scherer
  0 siblings, 0 replies; 5+ messages in thread
From: Konrad Scherer @ 2013-10-17 14:17 UTC (permalink / raw)
  To: openembedded-core

From: Konrad Scherer <Konrad.Scherer@windriver.com>

Python 2.4 does not support the 'b' string literal or the
keyword 'as' in exception handling. Python 3 does not accept
the old method of exception handling and defaults to unicode.
The b() function converts strings to bytes on Python 3 and
using sys.exc_info() avoids the exception handling syntax.

Signed-off-by: Konrad Scherer <Konrad.Scherer@windriver.com>
---
 scripts/relocate_sdk.py | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index fe6e4e0..05d9fd6 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -31,7 +31,14 @@ import os
 import re
 import errno
 
-old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
+if sys.version < '3':
+    def b(x):
+        return x
+else:
+    def b(x):
+        return x.encode(sys.getfilesystemencoding())
+
+old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
 
 def get_arch():
     f.seek(0)
@@ -92,15 +99,15 @@ def change_interpreter(elf_file_name):
             # External SDKs with mixed pre-compiled binaries should not get
             # relocated so look for some variant of /lib
             fname = f.read(11)
-            if fname.startswith(b"/lib/") or fname.startswith(b"/lib64/") or \
-               fname.startswith(b"/lib32/") or fname.startswith(b"/usr/lib32/") or \
-               fname.startswith(b"/usr/lib32/") or fname.startswith(b"/usr/lib64/"):
+            if fname.startswith(b("/lib/")) or fname.startswith(b("/lib64/")) or \
+               fname.startswith(b("/lib32/")) or fname.startswith(b("/usr/lib32/")) or \
+               fname.startswith(b("/usr/lib32/")) or fname.startswith(b("/usr/lib64/")):
                 break
             if (len(new_dl_path) >= p_filesz):
                 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
                     % (elf_file_name, p_memsz, len(new_dl_path) + 1))
                 break
-            dl_path = new_dl_path + b"\0" * (p_filesz - len(new_dl_path))
+            dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path))
             f.seek(p_offset)
             f.write(dl_path)
             break
@@ -132,40 +139,40 @@ def change_dl_sysdirs():
         sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
             sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr)
 
-        name = sh_strtab[sh_name:sh_strtab.find(b"\0", sh_name)]
+        name = sh_strtab[sh_name:sh_strtab.find(b("\0"), sh_name)]
 
         """ look only into SHT_PROGBITS sections """
         if sh_type == 1:
             f.seek(sh_offset)
             """ default library paths cannot be changed on the fly because  """
             """ the string lengths have to be changed too.                  """
-            if name == b".sysdirs":
+            if name == b(".sysdirs"):
                 sysdirs = f.read(sh_size)
                 sysdirs_off = sh_offset
                 sysdirs_sect_size = sh_size
-            elif name == b".sysdirslen":
+            elif name == b(".sysdirslen"):
                 sysdirslen = f.read(sh_size)
                 sysdirslen_off = sh_offset
-            elif name == b".ldsocache":
+            elif name == b(".ldsocache"):
                 ldsocache_path = f.read(sh_size)
                 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
                 # pad with zeros
-                new_ldsocache_path += b"\0" * (sh_size - len(new_ldsocache_path))
+                new_ldsocache_path += b("\0") * (sh_size - len(new_ldsocache_path))
                 # write it back
                 f.seek(sh_offset)
                 f.write(new_ldsocache_path)
 
     if sysdirs != "" and sysdirslen != "":
-        paths = sysdirs.split(b"\0")
-        sysdirs = b""
-        sysdirslen = b""
+        paths = sysdirs.split(b("\0"))
+        sysdirs = b("")
+        sysdirslen = b("")
         for path in paths:
             """ exit the loop when we encounter first empty string """
-            if path == b"":
+            if path == b(""):
                 break
 
             new_path = old_prefix.sub(new_prefix, path)
-            sysdirs += new_path + b"\0"
+            sysdirs += new_path + b("\0")
 
             if arch == 32:
                 sysdirslen += struct.pack("<L", len(new_path))
@@ -173,7 +180,7 @@ def change_dl_sysdirs():
                 sysdirslen += struct.pack("<Q", len(new_path))
 
         """ pad with zeros """
-        sysdirs += b"\0" * (sysdirs_sect_size - len(sysdirs))
+        sysdirs += b("\0") * (sysdirs_sect_size - len(sysdirs))
 
         """ write the sections back """
         f.seek(sysdirs_off)
@@ -205,7 +212,8 @@ for e in executables_list:
 
     try:
         f = open(e, "r+b")
-    except IOError as ioex:
+    except IOError:
+        exctype, ioex = sys.exc_info()[:2]
         if ioex.errno == errno.ETXTBSY:
             print("Could not open %s. File used by another process.\nPlease "\
                   "make sure you exit all processes that might use any SDK "\
-- 
1.8.1.2



^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-10-17 14:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-16 15:44 [Patch 0/1 V2] Update relocate_sdk.py to work with Python 2.4 Konrad Scherer
2013-10-16 15:44 ` [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3 Konrad Scherer
2013-10-17  6:54   ` Laurentiu Palcu
2013-10-17  7:01     ` Laurentiu Palcu
  -- strict thread matches above, loose matches on Subject: below --
2013-10-17 14:17 [Patch 0/1 V3] Update relocate_sdk.py to work with Python 2.4 Konrad Scherer
2013-10-17 14:17 ` [PATCH] relocate_sdk.py: Allow script to work with Python 2.4 and 3 Konrad Scherer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox