[yocto] [opkg-utils PATCH 1/2] migrate to python 3

Alejandro del Castillo alejandro.delcastillo at ni.com
Fri May 27 14:26:42 PDT 2016


General fixes
* Add __future__ imports
* Use print function instead of print statement
* Use new style classes
* Iterate over list, instead of over object.keys()
* Use six.iteritems  instead of iteritems()

opkg.py:
* Add conditional to decode string if needed in Package:read_control

arfile.py
* Add seekable method to FileSection class (needed by tarfile)

Signed-off-by: Alejandro del Castillo <alejandro.delcastillo at ni.com>
---
 arfile.py            | 10 ++++++++--
 makePackage          |  2 ++
 opkg-compare-indexes |  2 ++
 opkg-graph-deps      | 39 +++++++++++++++++++++------------------
 opkg-list-fields     |  2 ++
 opkg-make-index      |  2 ++
 opkg-show-deps       |  2 ++
 opkg-unbuild         |  2 ++
 opkg-update-index    |  1 +
 opkg.py              | 17 ++++++++++-------
 10 files changed, 52 insertions(+), 27 deletions(-)

diff --git a/arfile.py b/arfile.py
index 88ace46..4bab526 100644
--- a/arfile.py
+++ b/arfile.py
@@ -5,12 +5,15 @@ Copyright (c) 2006-7 Paul Sokolovsky
 This file is released under the terms 
 of GNU General Public License v2 or later.
 """
+from __future__ import absolute_import
+from __future__ import print_function
+from builtins import object
 import sys
 import os
 import tarfile 
 
 
-class FileSection:
+class FileSection(object):
     "A class which allows to treat portion of file as separate file object."
 
     def __init__(self, f, offset, size):
@@ -30,6 +33,9 @@ class FileSection:
         else:
             assert False
 
+    def seekable(self):
+        return True 
+
     def tell(self):
 #        print("tell()")
         return self.f.tell() - self.offset
@@ -38,7 +44,7 @@ class FileSection:
 #        print("read(%d)" % size)
         return self.f.read(size)
 
-class ArFile:
+class ArFile(object):
 
     def __init__(self, f, fn):
         self.f = f
diff --git a/makePackage b/makePackage
index ec76338..4bdfc56 100755
--- a/makePackage
+++ b/makePackage
@@ -5,6 +5,8 @@
 #   Insert the filename, size, and md5 lines before the description.
 # Call it like this:
 #  find . -name \*.opk | xargs -n 1 makePackage > Packages
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys
 import opkg
diff --git a/opkg-compare-indexes b/opkg-compare-indexes
index 7bb3c69..b60d20a 100755
--- a/opkg-compare-indexes
+++ b/opkg-compare-indexes
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys, os
 from glob import glob
diff --git a/opkg-graph-deps b/opkg-graph-deps
index d7f4fd2..6653fd5 100755
--- a/opkg-graph-deps
+++ b/opkg-graph-deps
@@ -1,18 +1,21 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys
 import os
 import getopt
 import pydot
 import opkg
+import six
 
 def usage(more=False):
-    print >>sys.stderr, ( 'Usage: opkg-graph-deps '
+    print(( 'Usage: opkg-graph-deps '
         '[-h] [-d] [-o feed.dot] '
         '[-u <Base_feed_URL>] '
-        '<Paths_to_Packages_files>' )
+        '<Paths_to_Packages_files>' ), file=sys.stderr)
     if more:
-        print >>sys.stderr, '\n'.join( [
+        print('\n'.join( [
 '',
 'Generates a dot formatted dependency graph of an IPK feed.',
 '',
@@ -56,7 +59,7 @@ def usage(more=False):
 '  the alias != (to).',
 ' ipkBrokenDep: Set to "1" if (to) is missing from the feed.',
 '',
-        ] )
+        ] ), file=sys.stderr)
     exit(1)
 
 # optional args
@@ -76,19 +79,19 @@ for (optkey, optval) in opts:
         feed_url = optval
 
 if not index_files:
-    print >>sys.stderr, 'Must specify a path to at least one Packages file'
+    print('Must specify a path to at least one Packages file', file=sys.stderr)
     usage()
 
 def fatal_error(msg):
-    print >>sys.stderr, ('ERROR: ' + str(msg))
+    print(('ERROR: ' + str(msg)), file=sys.stderr)
     exit(1)
 
 def warn(msg):
-    print >>sys.stderr, str(msg)
+    print(str(msg), file=sys.stderr)
 
 def debug(msg):
     if enable_debug:
-        print >>sys.stderr, ('DEBUG: ' + str(msg))
+        print(('DEBUG: ' + str(msg)), file=sys.stderr)
 
 def split_dep_list(lst):
     '''
@@ -190,7 +193,7 @@ for indexFilePath in index_files:
     packages.read_packages_file(indexFilePath)
 
     # add each package
-    for pkgKey in packages.keys():
+    for pkgKey in list(packages.keys()):
         pkg = packages[pkgKey]
 
         # sanity check: verify important attributes are defined for
@@ -228,7 +231,7 @@ for indexFilePath in index_files:
 # provides the alias.
 # These packages are not added to the graph because their implementations
 # are already there.
-for pkgKey, pkg in real_pkg_map.iteritems():
+for pkgKey, pkg in six.iteritems(real_pkg_map):
     for alias in split_dep_list(pkg.provides):
         if alias not in active_pkg_map:
             # add it
@@ -260,7 +263,7 @@ for pkgKey, pkg in real_pkg_map.iteritems():
                 virt_pkg_map[alias].append(pkg)
 
 # Print alternatives for virtual packages
-for pkgKey, pkgList in virt_pkg_map.iteritems():
+for pkgKey, pkgList in six.iteritems(virt_pkg_map):
     if len(pkgList) > 1:
         pkgNameList = ','.join( [x.package for x in pkgList] )
         debug("%s alternate implementations of package %s: %s" % (len(pkgList), pkgKey, pkgNameList))
@@ -271,7 +274,7 @@ for pkgKey, pkgList in virt_pkg_map.iteritems():
 
 # Create stub packages in missing_pkg_map and active_pkg_map for broken
 # dependencies, and add them to the graph.
-for pkgKey, pkg in real_pkg_map.iteritems():
+for pkgKey, pkg in six.iteritems(real_pkg_map):
     for depName in split_dep_list(pkg.depends):
         if not depName in active_pkg_map:
             warn("Broken dependency: %s --> %s (missing)" % (
@@ -288,7 +291,7 @@ for pkgKey, pkg in real_pkg_map.iteritems():
 
 # process dependencies
 # add edges to graph
-for pkgKey, pkg in real_pkg_map.iteritems():
+for pkgKey, pkg in six.iteritems(real_pkg_map):
     for depName in split_dep_list(pkg.depends):
         depPkg = active_pkg_map[depName]
 
@@ -297,10 +300,10 @@ for pkgKey, pkg in real_pkg_map.iteritems():
             broken=(depPkg.package in missing_pkg_map) )
 
 # Results
-print "%s total packages are referenced in the feed" % len(active_pkg_map)
-print " %s real packages (%s collisions)" % ( len(real_pkg_map), real_pkg_replace_count )
-print " %s virtual packages" % len(virt_pkg_map)
-print " %s missing packages" % len(missing_pkg_map)
+print("%s total packages are referenced in the feed" % len(active_pkg_map))
+print(" %s real packages (%s collisions)" % ( len(real_pkg_map), real_pkg_replace_count ))
+print(" %s virtual packages" % len(virt_pkg_map))
+print(" %s missing packages" % len(missing_pkg_map))
 
 # sanity check
 if len(active_pkg_map) != (len(real_pkg_map) + len(virt_pkg_map) + len(missing_pkg_map)):
@@ -308,4 +311,4 @@ if len(active_pkg_map) != (len(real_pkg_map) + len(virt_pkg_map) + len(missing_p
 
 # Write the graph
 graph.write(path=dot_filename)
-print "Graphed at %s" % dot_filename
+print("Graphed at %s" % dot_filename)
diff --git a/opkg-list-fields b/opkg-list-fields
index 1fb7fd1..c14a90f 100755
--- a/opkg-list-fields
+++ b/opkg-list-fields
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys, opkg
 
diff --git a/opkg-make-index b/opkg-make-index
index 2fdf95c..7897918 100755
--- a/opkg-make-index
+++ b/opkg-make-index
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys, os, posixpath
 import subprocess
diff --git a/opkg-show-deps b/opkg-show-deps
index 4694579..153f21e 100755
--- a/opkg-show-deps
+++ b/opkg-show-deps
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys, os, posixpath
 from glob import glob
diff --git a/opkg-unbuild b/opkg-unbuild
index 35a387f..4f36bec 100755
--- a/opkg-unbuild
+++ b/opkg-unbuild
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
+from __future__ import print_function
 
 import sys, os, re
 
diff --git a/opkg-update-index b/opkg-update-index
index 353ea69..341c1c2 100755
--- a/opkg-update-index
+++ b/opkg-update-index
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+from __future__ import absolute_import
 
 import sys, os
 from glob import glob
diff --git a/opkg.py b/opkg.py
index ae0fbcd..fe6b9e0 100644
--- a/opkg.py
+++ b/opkg.py
@@ -31,6 +31,8 @@
 #        people to say "./control.tar.gz" or "./control" when they package files.
 #        It would be much better to require ./control or disallow ./control (either)
 #        rather than letting people pick.  Some freedoms aren't worth their cost.
+from __future__ import absolute_import
+from __future__ import print_function
 
 import tempfile
 import os
@@ -44,7 +46,7 @@ import arfile
 import tarfile
 import textwrap
 
-class Version:
+class Version(object):
     """A class for holding parsed package version information."""
     def __init__(self, epoch, version):
         self.epoch = epoch
@@ -113,7 +115,7 @@ def parse_version(versionstr):
         epoch = int(epochstr)
     return Version(epoch, versionstr)
 
-class Package:
+class Package(object):
     """A class for creating objects to manipulate (e.g. create) opkg
        packages."""
 
@@ -165,7 +167,6 @@ class Package:
             ar = arfile.ArFile(f, fn)
             tarStream = ar.open("control.tar.gz")
             tarf = tarfile.open("control.tar.gz", "r", tarStream)
-
             try:
                 control = tarf.extractfile("control")
             except KeyError:
@@ -175,7 +176,6 @@ class Package:
             except TypeError as e:
                 sys.stderr.write("Cannot read control file '%s' - %s\n" % (fn, e))
             control.close()
-
         self.scratch_dir = None
         self.file_dir = None
         self.meta_dir = None
@@ -217,8 +217,11 @@ class Package:
         line = control.readline()
         while 1:
             if not line: break
+            # Decode if stream has byte strings
+            if not isinstance(line, str):
+                line = line.decode()
             line = line.rstrip()
-            lineparts = re.match(r'([\w-]*?):\s*(.*)', str(line))
+            lineparts = re.match(r'([\w-]*?):\s*(.*)', line)
             if lineparts:
                 name = lineparts.group(1).lower()
                 value = lineparts.group(2)
@@ -378,7 +381,7 @@ class Package:
         tarStream = ar.open("data.tar.gz")
         tarf = tarfile.open("data.tar.gz", "r", tarStream)
         self.file_list = tarf.getnames()
-        self.file_list = map(lambda a: ["./", ""][a.startswith("./")] + a, self.file_list)
+        self.file_list = [["./", ""][a.startswith("./")] + a for a in self.file_list]
 
         f.close()
         return self.file_list
@@ -490,7 +493,7 @@ class Package:
         #       are being destroyed?  -- a7r
         pass
 
-class Packages:
+class Packages(object):
     """A currently unimplemented wrapper around the opkg utility."""
     def __init__(self):
         self.packages = {}
-- 
1.9.1




More information about the yocto mailing list