[Piuparts-commits] [piuparts] 01/02: use six to handle py3 vs. py2 imports

Holger Levsen holger at moszumanska.debian.org
Tue Apr 28 12:29:56 UTC 2015


This is an automated email from the git hooks/post-receive script.

holger pushed a commit to branch develop
in repository piuparts.

commit 123d27832818bd7c33adc5fdac1ee3463e772940
Author: Börni <boerni at gmail.com>
Date:   Mon Apr 27 21:42:27 2015 +0200

    use six to handle py3 vs. py2 imports
---
 debian/control            |  1 +
 piuparts.py               | 12 ++++--------
 piupartslib/__init__.py   | 13 +++----------
 piupartslib/conf.py       | 23 +++++++----------------
 piupartslib/packagesdb.py | 10 +++++-----
 tests/unittests.py        |  9 +++------
 6 files changed, 23 insertions(+), 45 deletions(-)

diff --git a/debian/control b/debian/control
index d56041b..f6a62b9 100644
--- a/debian/control
+++ b/debian/control
@@ -117,6 +117,7 @@ Architecture: all
 Depends:
  python-apt,
  python-distro-info,
+ python-six,
  ${misc:Depends},
  ${python:Depends}
 Breaks:
diff --git a/piuparts.py b/piuparts.py
index c0f5297..53c2477 100644
--- a/piuparts.py
+++ b/piuparts.py
@@ -47,6 +47,7 @@ import shutil
 import os
 import tarfile
 import stat
+import six
 import re
 import pickle
 import subprocess
@@ -54,11 +55,6 @@ import uuid
 from signal import alarm, signal, SIGALRM, SIGTERM, SIGKILL
 
 try:
-    import urllib.parse as urllib_parse  # py3
-except ImportError:
-    from urllib import quote as urllib_parse  # py2
-
-try:
     from debian import deb822
 except ImportError:
     from debian_bundle import deb822
@@ -1693,7 +1689,7 @@ class VirtServ(Chroot):
                 if not isinstance(a, type(())):
                     return a
                 (a,) = a
-                return urllib_parse(a)
+                return six.moves.urllib.parse.quote(a)
             cmd = ' '.join(map(maybe_quote, cmd))
         logging.debug('adt-virt >> %s', cmd)
         print(cmd, file=self._vs.stdin)
@@ -1777,7 +1773,7 @@ class VirtServ(Chroot):
             exec 2>&1
             exec "$@"
                 ''', '<command>']
-        ca = ','.join(map(urllib_parse, prefix + cmdl))
+        ca = ','.join(map(six.moves.urllib.parse.quote, prefix + cmdl))
         stdout = '%s/cmd-stdout' % self._scratch
         stderr = '%s/cmd-stderr-base' % self._scratch
         cmd = ['execute', ca,
@@ -1879,7 +1875,7 @@ class VirtServ(Chroot):
                         break
                     if len(splut) >= 8:
                         self._fail('aaargh wrong output from find: %s' %
-                                   urllib_parse(line), repr(splut))
+                                   six.moves.urllib.parse.quote(line), repr(splut))
                     l = f.readline()
                     if not l:
                         if not line:
diff --git a/piupartslib/__init__.py b/piupartslib/__init__.py
index d48644d..bf815f5 100644
--- a/piupartslib/__init__.py
+++ b/piupartslib/__init__.py
@@ -19,16 +19,9 @@
 
 
 import bz2
+import six
 import zlib
 
-try:
-    # py3
-    from urllib.error import HTTPError
-    from urllib.request import urlopen
-except ImportError:
-    # py2
-    from urllib2 import HTTPError
-    from urllib2 import urlopen
 
 from . import conf
 from . import dependencyparser
@@ -89,8 +82,8 @@ def open_packages_url(url):
     socket = None
     for ext in ['.bz2', '.gz']:
         try:
-            socket = urlopen(url + ext)
-        except HTTPError as httperror:
+            socket = six.moves.urllib.request.urlopen(url + ext)
+        except six.moves.urllib.error.HTTPError as httperror:
             pass
         else:
             break
diff --git a/piupartslib/conf.py b/piupartslib/conf.py
index 51e67ec..75be7e6 100644
--- a/piupartslib/conf.py
+++ b/piupartslib/conf.py
@@ -24,19 +24,10 @@
 # in a configuration file, that's why).
 #
 
-try:
-    import configparser  # py3
-except ImportError:
-    import ConfigParser as configparser  # py2
-
-try:
-    from collections import UserDict  # py3
-except ImportError:
-    from UserDict import UserDict  # py2
-
 import subprocess
 import collections
 import re
+import six
 import distro_info
 from functools import reduce
 
@@ -55,10 +46,10 @@ class MissingMandatorySetting(Exception):
             (key, filename),
 
 
-class Config(UserDict):
+class Config(six.moves.UserDict):
 
     def __init__(self, section, defaults, mandatory=[], defaults_section=None):
-        UserDict.__init__(self)
+        six.moves.UserDict.__init__(self)
         self._section = section
         self._defaults_section = defaults_section
         for key, value in defaults.iteritems():
@@ -66,7 +57,7 @@ class Config(UserDict):
         self._mandatory = mandatory
 
     def read(self, filename):
-        cp = configparser.ConfigParser()
+        cp = six.moves.configparser.ConfigParser()
         cp.read(filename)
         if not cp.has_section(self._section):
             raise MissingSection(filename, self._section)
@@ -157,10 +148,10 @@ class Config(UserDict):
         return self["arch"]
 
 
-class DistroConfig(UserDict):
+class DistroConfig(six.moves.UserDict):
 
     def __init__(self, filename, mirror):
-        UserDict.__init__(self)
+        six.moves.UserDict.__init__(self)
         self._mirror = mirror
         self._defaults = {
             "uri": None,
@@ -170,7 +161,7 @@ class DistroConfig(UserDict):
                 "depends": None,
                 "candidates": None,
         }
-        cp = configparser.SafeConfigParser()
+        cp = six.moves.configparser.SafeConfigParser()
         cp.read(filename)
         for section in cp.sections():
             self[section] = dict(self._defaults)
diff --git a/piupartslib/packagesdb.py b/piupartslib/packagesdb.py
index 9fa5b25..ac7727f 100644
--- a/piupartslib/packagesdb.py
+++ b/piupartslib/packagesdb.py
@@ -31,9 +31,9 @@ import logging
 import os
 import random
 import stat
+import six
 import tempfile
 import time
-import UserDict
 import apt_pkg
 
 import piupartslib
@@ -55,10 +55,10 @@ def rfc822_like_header_parse(input):
     return headers
 
 
-class Package(UserDict.UserDict):
+class Package(six.moves.UserDict):
 
     def __init__(self, headers):
-        UserDict.UserDict.__init__(self)
+        six.moves.__init__(self)
         self.headers = headers
         for header in headers:
             name, value = header.split(":", 1)
@@ -127,10 +127,10 @@ class Package(UserDict.UserDict):
         output_file.write("".join(self.headers))
 
 
-class PackagesFile(UserDict.UserDict):
+class PackagesFile(six.moves.UserDict):
 
     def __init__(self):
-        UserDict.UserDict.__init__(self)
+        six.moves.UserDict.__init__(self)
         self._urllist = []
 
     def load_packages_urls(self, urls, restrict_packages=None):
diff --git a/tests/unittests.py b/tests/unittests.py
index 212839b..677b3d7 100644
--- a/tests/unittests.py
+++ b/tests/unittests.py
@@ -1,10 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import os
-try:
-    from io import StringIO  # py3
-except ImportError:
-    from StringIO import StringIO  # py2
+import six
 
 import unittest
 
@@ -44,7 +41,7 @@ class FakeLogDB(piupartslib.packagesdb.LogDB):
     def open_file(self, pathname, mode):
         vdir, base = self._parse(pathname)
         self.dict[vdir].append(base)
-        return StringIO()
+        return six.moves.StringIO()
 
     def remove_file(self, pathname):
         vdir, base = self._parse(pathname)
@@ -59,7 +56,7 @@ class PackagesDbTests(unittest.TestCase):
 
     def new_db(self, packages_file_contents):
         db = piupartslib.packagesdb.PackagesDB(FakeLogDB())
-        db.read_packages_file(StringIO(packages_file_contents))
+        db.read_packages_file(six.moves.StringIO(packages_file_contents))
         return db
 
     def reserve(self, packages_file_contents):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/piuparts/piuparts.git



More information about the Piuparts-commits mailing list