migrate to python 3
PATCH 1/2 migrate to python 3
In reply to Alejandro del CastilloGeneral 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)
--- 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(-)
arfile.py +8 -2
@@ -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
makePackage +2 -0
@@ -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
opkg-compare-indexes +2 -0
@@ -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
opkg-graph-deps +21 -18
@@ -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)
opkg-list-fields +2 -0
@@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import absolute_import +from __future__ import print_function import sys, opkg
opkg-make-index +2 -0
@@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import absolute_import +from __future__ import print_function import sys, os, posixpath import subprocess
opkg-show-deps +2 -0
@@ -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
opkg-unbuild +2 -0
@@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import absolute_import +from __future__ import print_function import sys, os, re
opkg-update-index +1 -0
@@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import import sys, os from glob import glob
opkg.py +10 -7
@@ -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 = {}
Signature · 1 line
1.9.1
PATCH 2/2 opkg.py/write_packages_file: call __str__ instead of __repr__
In reply to Alejandro del CastilloCommit b9eec00a67f13ad9f9526caf591578b40d73a391 correctly renamed __repr__to __str__. A call site in write_packages_file was missed.
--- opkg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
opkg.py +1 -1
@@ -533,7 +533,7 @@ class Packages(object): names = list(self.packages.keys()) names.sort() for name in names: - f.write(self.packages[name].__repr__()) + f.write(self.packages[name].__str__()) return def keys(self):
Signature · 1 line
1.9.1
cover migrate to python 3
First patch migrates all opkg-utils scrips to python 3. opkg-graph-deps doesn't work on python 3 since it depends on pydot, which hasn't migrated to python 3. Second patch fixes opkg-update-index, which was broken.
Alejandro del Castillo (2):
migrate to python 3
opkg.py/write_packages_file: call __str__ instead of __repr__
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 | 19 +++++++++++-------- 10 files changed, 53 insertions(+), 28 deletions(-)
Signature · 1 line
1.9.1