Skip to contentSkip to conversation

Make useradd-staticids.bbclass Python 3 compatible

PATCH New Peter Kjellerstedt 1 participant 3 messages Jun 10, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover Make useradd-staticids.bbclass Python 3 compatible

Avoid warnings with Python 3 due to files that are not closed in useradd-staticids.bbclass.

PATCHv2: Also avoid a FutureWarning about split() requiring a non-empty pattern. It occured for every recipe that inherited useradd (with useradd-staticids enabled), but with no context at all. It also did not end up in the tmp/log/cooker log...

//Peter

The following changes since commit 40e789d1fee7f0df2d23230fff38325119ad2935:

bitbake: lib/bb/main.py: Fix use of BBPOSTCONF and BBPRECONF (2016-06-09 18:00:58 +0100)

are available in the git repository at:

git://git.yoctoproject.org/poky-contrib pkj/useradd-staticids-py3
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=pkj/useradd-staticids-py3

Peter Kjellerstedt (2):
useradd-staticids.bbclass: Make sure opened files are closed useradd-staticids.bbclass: Avoid FutureWarning about split()

 meta/classes/useradd-staticids.bbclass | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
Signature · 1 line
2.8.3
#1 lore raw

PATCH v2 1/2 useradd-staticids.bbclass: Make sure opened files are closed

This avoids warnings about unclosed files with Python 3.

---
 meta/classes/useradd-staticids.bbclass | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
meta/classes/useradd-staticids.bbclass +17 -13
index a9b506d..440c0e3 100644
@@ -4,6 +4,7 @@ def update_useradd_static_config(d):
     import argparse
     import itertools
     import re
+    import errno
 
     class myArgumentParser( argparse.ArgumentParser ):
         def _print_message(self, message, file=None):
@@ -30,19 +31,22 @@ def update_useradd_static_config(d):
         are set)."""
         id_table = dict()
         for conf in file_list.split():
-            if os.path.exists(conf):
-                f = open(conf, "r")
-                for line in f:
-                    if line.startswith('#'):
-                        continue
-                    # Make sure there always are at least exp_fields elements in
-                    # the field list. This allows for leaving out trailing
-                    # colons in the files.
-                    fields = list_extend(line.rstrip().split(":"), exp_fields)
-                    if fields[0] not in id_table:
-                        id_table[fields[0]] = fields
-                    else:
-                        id_table[fields[0]] = list(itertools.imap(lambda x, y: x or y, fields, id_table[fields[0]]))
+            try:
+                with open(conf, "r") as f:
+                    for line in f:
+                        if line.startswith('#'):
+                            continue
+                        # Make sure there always are at least exp_fields
+                        # elements in the field list. This allows for leaving
+                        # out trailing colons in the files.
+                        fields = list_extend(line.rstrip().split(":"), exp_fields)
+                        if fields[0] not in id_table:
+                            id_table[fields[0]] = fields
+                        else:
+                            id_table[fields[0]] = list(itertools.imap(lambda x, y: x or y, fields, id_table[fields[0]]))
+            except IOError as e:
+                if e.errno == errno.ENOENT:
+                    pass
 
         return id_table
 
Signature · 1 line
2.8.3
#2 lore raw

PATCH v2 2/2 useradd-staticids.bbclass: Avoid FutureWarning about split()

In reply to Peter Kjellerstedt

This avoids the following warning with Python 3.5:

/usr/lib64/python3.5/re.py:203: FutureWarning: split() requires a non-empty pattern

---
 meta/classes/useradd-staticids.bbclass | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
meta/classes/useradd-staticids.bbclass +2 -2
index 440c0e3..d963f28 100644
@@ -97,7 +97,7 @@ def update_useradd_static_config(d):
             if not param:
                 continue
             try:
-                uaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param))
+                uaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param))
             except:
                 raise bb.build.FuncFailed("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param))
 
@@ -231,7 +231,7 @@ def update_useradd_static_config(d):
                 continue
             try:
                 # If we're processing multiple lines, we could have left over values here...
-                gaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param))
+                gaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param))
             except:
                 raise bb.build.FuncFailed("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param))
 
Signature · 1 line
2.8.3