From: Arfrever Frehtes Taifersar Arahesis Date: Sun, 20 Sep 2009 16:22:08 +0000 (-0000) Subject: Update system imports for compatibility with Python 3. X-Git-Tag: v2.2_rc42~108 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8243a08d6d2121e4c1e92201c9d4361df42e5d8f;p=portage.git Update system imports for compatibility with Python 3. svn path=/main/trunk/; revision=14294 --- diff --git a/bin/dispatch-conf b/bin/dispatch-conf index 38359c5b3..5247cdb60 100755 --- a/bin/dispatch-conf +++ b/bin/dispatch-conf @@ -19,7 +19,13 @@ if not hasattr(__builtins__, "set"): from stat import * from random import * -import atexit, commands, re, shutil, stat, sys +import atexit, re, shutil, stat, sys +try: + from subprocess import getoutput as subprocess_getoutput + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getoutput as subprocess_getoutput + from commands import getstatusoutput as subprocess_getstatusoutput try: import portage except ImportError: @@ -131,11 +137,11 @@ class dispatch: else: mrgfail = portage.dispatch_conf.file_archive(archive, conf['current'], conf['new'], mrgconf) if os.path.exists(archive + '.dist'): - unmodified = len(commands.getoutput(DIFF_CONTENTS % (conf['current'], archive + '.dist'))) == 0 + unmodified = len(subprocess_getoutput(DIFF_CONTENTS % (conf['current'], archive + '.dist'))) == 0 else: unmodified = 0 if os.path.exists(mrgconf): - if mrgfail or len(commands.getoutput(DIFF_CONTENTS % (conf['new'], mrgconf))) == 0: + if mrgfail or len(subprocess_getoutput(DIFF_CONTENTS % (conf['new'], mrgconf))) == 0: os.unlink(mrgconf) newconf = conf['new'] else: @@ -146,13 +152,13 @@ class dispatch: if newconf == mrgconf and \ self.options.get('ignore-previously-merged') != 'yes' and \ os.path.exists(archive+'.dist') and \ - len(commands.getoutput(DIFF_CONTENTS % (archive+'.dist', conf['new']))) == 0: + len(subprocess_getoutput(DIFF_CONTENTS % (archive+'.dist', conf['new']))) == 0: # The current update is identical to the archived .dist # version that has previously been merged. os.unlink(mrgconf) newconf = conf['new'] - mystatus, myoutput = commands.getstatusoutput( + mystatus, myoutput = subprocess_getstatusoutput( DIFF_CONTENTS % (conf ['current'], newconf)) same_file = 0 == len(myoutput) if mystatus >> 8 == 2: @@ -160,9 +166,9 @@ class dispatch: same_cvs = False same_wsc = False else: - same_cvs = 0 == len(commands.getoutput( + same_cvs = 0 == len(subprocess_getoutput( DIFF_CVS_INTERP % (conf ['current'], newconf))) - same_wsc = 0 == len(commands.getoutput( + same_wsc = 0 == len(subprocess_getoutput( DIFF_WSCOMMENTS % (conf ['current'], newconf))) # Do options permit? diff --git a/bin/glsa-check b/bin/glsa-check index 6516a1d91..e9c28f9e6 100755 --- a/bin/glsa-check +++ b/bin/glsa-check @@ -268,7 +268,7 @@ if mode == "test": # mail mode as requested by solar if mode == "mail": import portage.mail, socket - from StringIO import StringIO + from io import StringIO from email.mime.text import MIMEText # color doesn't make any sense for mail diff --git a/bin/repoman b/bin/repoman index c849a22f7..613e1ec14 100755 --- a/bin/repoman +++ b/bin/repoman @@ -10,7 +10,10 @@ from __future__ import print_function import codecs -import commands +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput import errno import formatter import logging @@ -23,14 +26,10 @@ import tempfile import time import platform +from io import StringIO from itertools import chain, izip from stat import S_ISDIR, ST_CTIME -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO - if not hasattr(__builtins__, "set"): from sets import Set as set @@ -1136,7 +1135,7 @@ for x in scanlist: (checkdir, y, m.group(0))) if desktop_file_validate and desktop_pattern.match(y): - status, cmd_output = commands.getstatusoutput( + status, cmd_output = subprocess_getstatusoutput( "'%s' '%s'" % (desktop_file_validate, full_path)) if os.WIFEXITED(status) and os.WEXITSTATUS(status) != os.EX_OK: # Note: in the future we may want to grab the @@ -1186,7 +1185,7 @@ for x in scanlist: if xmllint_capable and not metadata_bad: # xmlint can produce garbage output even on success, so only dump # the ouput when it fails. - st, out = commands.getstatusoutput( + st, out = subprocess_getstatusoutput( "xmllint --nonet --noout --dtdvalid '%s' '%s'" % \ (metadata_dtd, os.path.join(checkdir, "metadata.xml"))) if st != os.EX_OK: @@ -1983,7 +1982,7 @@ else: if myfile not in expansion: continue - myout = commands.getstatusoutput("egrep -q "+headerstring+" "+myfile) + myout = subprocess_getstatusoutput("egrep -q "+headerstring+" "+myfile) if myout[0] == 0: myheaders.append(myfile) diff --git a/pym/_emerge/BinpkgFetcher.py b/pym/_emerge/BinpkgFetcher.py index e72799a1d..1f244cb1a 100644 --- a/pym/_emerge/BinpkgFetcher.py +++ b/pym/_emerge/BinpkgFetcher.py @@ -3,7 +3,10 @@ # $Id$ from _emerge.SpawnProcess import SpawnProcess -import urlparse +try: + from urllib.parse import urlparse as urllib_parse_urlparse +except ImportError: + from urlparse import urlparse as urllib_parse_urlparse import sys import portage from portage import os @@ -61,7 +64,7 @@ class BinpkgFetcher(SpawnProcess): self.wait() return - protocol = urlparse.urlparse(uri)[0] + protocol = urllib_parse_urlparse(uri)[0] fcmd_prefix = "FETCHCOMMAND" if resume: fcmd_prefix = "RESUMECOMMAND" diff --git a/pym/_emerge/JobStatusDisplay.py b/pym/_emerge/JobStatusDisplay.py index 98724e8a7..dd72a2971 100644 --- a/pym/_emerge/JobStatusDisplay.py +++ b/pym/_emerge/JobStatusDisplay.py @@ -5,11 +5,7 @@ import formatter import sys import time - -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO +from io import StringIO import portage from portage import os diff --git a/pym/_emerge/actions.py b/pym/_emerge/actions.py index 08f11836e..fda9f3d81 100644 --- a/pym/_emerge/actions.py +++ b/pym/_emerge/actions.py @@ -2,7 +2,10 @@ # Distributed under the terms of the GNU General Public License v2 # $Id$ -import commands +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput import errno import logging import platform @@ -1240,7 +1243,7 @@ def action_info(settings, trees, myopts, myfiles): else: print("Unknown") - output=commands.getstatusoutput("distcc --version") + output=subprocess_getstatusoutput("distcc --version") if not output[0]: print(str(output[1].split("\n",1)[0]), end=' ') if "distcc" in settings.features: @@ -1248,7 +1251,7 @@ def action_info(settings, trees, myopts, myfiles): else: print("[disabled]") - output=commands.getstatusoutput("ccache -V") + output=subprocess_getstatusoutput("ccache -V") if not output[0]: print(str(output[1].split("\n",1)[0]), end=' ') if "ccache" in settings.features: @@ -2661,16 +2664,16 @@ def getgccversion(chost): "!!! other terminals also.\n" ) - mystatus, myoutput = commands.getstatusoutput("gcc-config -c") + mystatus, myoutput = subprocess_getstatusoutput("gcc-config -c") if mystatus == os.EX_OK and myoutput.startswith(chost + "-"): return myoutput.replace(chost + "-", gcc_ver_prefix, 1) - mystatus, myoutput = commands.getstatusoutput( + mystatus, myoutput = subprocess_getstatusoutput( chost + "-" + gcc_ver_command) if mystatus == os.EX_OK: return gcc_ver_prefix + myoutput - mystatus, myoutput = commands.getstatusoutput(gcc_ver_command) + mystatus, myoutput = subprocess_getstatusoutput(gcc_ver_command) if mystatus == os.EX_OK: return gcc_ver_prefix + myoutput diff --git a/pym/_emerge/main.py b/pym/_emerge/main.py index cf3a4e416..950314662 100644 --- a/pym/_emerge/main.py +++ b/pym/_emerge/main.py @@ -7,12 +7,16 @@ import signal import sys import textwrap import platform +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput import portage from portage import os from portage import _encodings from portage import _unicode_decode import _emerge.help -import portage.xpak, commands, errno, re, time +import portage.xpak, errno, re, time from portage.output import colorize, xtermTitle, xtermTitleReset from portage.output import create_color_func good = create_color_func("GOOD") @@ -142,7 +146,7 @@ def chk_updated_info_files(root, infodirs, prev_mtimes, retval): raise del e processed_count += 1 - myso=commands.getstatusoutput("LANG=C LANGUAGE=C /usr/bin/install-info --dir-file="+inforoot+"/dir "+inforoot+"/"+x)[1] + myso=subprocess_getstatusoutput("LANG=C LANGUAGE=C /usr/bin/install-info --dir-file="+inforoot+"/dir "+inforoot+"/"+x)[1] existsstr="already exists, for file `" if myso!="": if re.search(existsstr,myso): diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index e6e5f5bda..deb7b2ff9 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -28,7 +28,10 @@ try: import pickle import stat - import commands + try: + from subprocess import getstatusoutput as subprocess_getstatusoutput + except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput from time import sleep from random import shuffle from itertools import chain, izip @@ -434,7 +437,7 @@ if platform.system() in ('FreeBSD',): @classmethod def chflags(cls, path, flags, opts=""): cmd = 'chflags %s %o %s' % (opts, flags, _shell_quote(path)) - status, output = commands.getstatusoutput(cmd) + status, output = subprocess_getstatusoutput(cmd) if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: return # Try to generate an ENOENT error if appropriate. @@ -1990,7 +1993,10 @@ class config(object): self._local_repo_configs = {} self._local_repo_conf_path = \ os.path.join(abs_user_config, 'repos.conf') - from ConfigParser import SafeConfigParser, ParsingError + try: + from configparser import SafeConfigParser, ParsingError + except ImportError: + from ConfigParser import SafeConfigParser, ParsingError repo_conf_parser = SafeConfigParser() try: repo_conf_parser.readfp( diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py index 01637b1a6..ee2e51c71 100644 --- a/pym/portage/checksum.py +++ b/pym/portage/checksum.py @@ -12,7 +12,10 @@ from portage import _unicode_encode import errno import stat import tempfile -import commands +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput #dict of all available hash functions hashfunc_map = {} @@ -112,7 +115,7 @@ hashfunc_map["size"] = getsize prelink_capable = False if os.path.exists(PRELINK_BINARY): - results = commands.getstatusoutput(PRELINK_BINARY+" --version > /dev/null 2>&1") + results = subprocess_getstatusoutput(PRELINK_BINARY+" --version > /dev/null 2>&1") if (results[0] >> 8) == 0: prelink_capable=1 del results diff --git a/pym/portage/data.py b/pym/portage/data.py index 0ce760a92..c30a3688a 100644 --- a/pym/portage/data.py +++ b/pym/portage/data.py @@ -100,7 +100,10 @@ userpriv_groups = [portage_gid] if secpass >= 2: # Get a list of group IDs for the portage user. Do not use grp.getgrall() # since it is known to trigger spurious SIGPIPE problems with nss_ldap. - from commands import getstatusoutput + try: + from subprocess import getstatusoutput + except ImportError: + from commands import getstatusoutput mystatus, myoutput = getstatusoutput("id -G portage") if mystatus == os.EX_OK: for x in myoutput.split(): diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py index e020415ab..bc91152a6 100644 --- a/pym/portage/dbapi/bintree.py +++ b/pym/portage/dbapi/bintree.py @@ -660,7 +660,10 @@ class binarytree(object): base_url = self.settings["PORTAGE_BINHOST"] from portage.const import CACHE_PATH - from urlparse import urlparse + try: + from urllib.parse import urlparse + except ImportError: + from urlparse import urlparse urldata = urlparse(base_url) pkgindex_file = os.path.join(self.settings["ROOT"], CACHE_PATH, "binhost", urldata[1] + urldata[2], "Packages") @@ -678,13 +681,16 @@ class binarytree(object): if e.errno != errno.ENOENT: raise local_timestamp = pkgindex.header.get("TIMESTAMP", None) - import urllib, urlparse + try: + from urllib.request import urlopen as urllib_request_urlopen + except ImportError: + from urllib import urlopen as urllib_request_urlopen rmt_idx = self._new_pkgindex() try: # urlparse.urljoin() only works correctly with recognized # protocols and requires the base url to have a trailing # slash, so join manually... - f = urllib.urlopen(base_url.rstrip("/") + "/Packages") + f = urllib_request_urlopen(base_url.rstrip("/") + "/Packages") try: rmt_idx.readHeader(f) remote_timestamp = rmt_idx.header.get("TIMESTAMP", None) @@ -1082,7 +1088,10 @@ class binarytree(object): mydest = os.path.dirname(self.getname(pkgname)) self._ensure_dir(mydest) - from urlparse import urlparse + try: + from urllib.parse import urlparse + except ImportError: + from urlparse import urlparse # urljoin doesn't work correctly with unrecognized protocols like sftp if self._remote_has_index: rel_url = self._remotepkgs[pkgname].get("PATH") diff --git a/pym/portage/dispatch_conf.py b/pym/portage/dispatch_conf.py index e18eb34fa..971f9eb5f 100644 --- a/pym/portage/dispatch_conf.py +++ b/pym/portage/dispatch_conf.py @@ -7,7 +7,11 @@ # Library by Wayne Davison , derived from code # written by Jeremy Wohl (http://igmus.org) -import os, sys, commands, shutil +import os, sys, shutil +try: + from subprocess import getoutput as subprocess_getoutput +except ImportError: + from commands import getoutput as subprocess_getoutput import portage from portage.localization import _ @@ -110,7 +114,7 @@ def file_archive(archive, curconf, newconf, mrgconf): # Archive the current config file if it isn't already saved if os.path.exists(archive) \ - and len(commands.getoutput("diff -aq '%s' '%s'" % (curconf,archive))) != 0: + and len(subprocess_getoutput("diff -aq '%s' '%s'" % (curconf,archive))) != 0: suf = 1 while suf < 9 and os.path.exists(archive + '.' + str(suf)): suf += 1 diff --git a/pym/portage/getbinpkg.py b/pym/portage/getbinpkg.py index adab28fab..c3994d26d 100644 --- a/pym/portage/getbinpkg.py +++ b/pym/portage/getbinpkg.py @@ -11,13 +11,21 @@ from portage import os from portage import _encodings from portage import _unicode_encode -import HTMLParser import sys import socket import time import tempfile import base64 -import urllib2 + +try: + from html.parser import HTMLParser as html_parser_HTMLParser +except ImportError: + from HTMLParser import HTMLParser as html_parser_HTMLParser + +try: + from urllib.parse import unquote as urllib_parse_unquote +except: + from urllib2 import unquote as urllib_parse_unquote try: import cPickle as pickle @@ -30,9 +38,18 @@ except ImportError as e: sys.stderr.write(colorize("BAD","!!! CANNOT IMPORT FTPLIB: ")+str(e)+"\n") try: - import httplib + try: + from http.client import HTTPConnection as http_client_HTTPConnection + from http.client import HTTPSConnection as http_client_HTTPConnection + from http.client import BadStatusLine as http_client_HTTPConnection + from http.client import ResponseNotReady as http_client_HTTPConnection + except ImportError: + from httplib import HTTPConnection as http_client_HTTPConnection + from httplib import HTTPSConnection as http_client_HTTPConnection + from httplib import BadStatusLine as http_client_HTTPConnection + from httplib import ResponseNotReady as http_client_HTTPConnection except ImportError as e: - sys.stderr.write(colorize("BAD","!!! CANNOT IMPORT HTTPLIB: ")+str(e)+"\n") + sys.stderr.write(colorize("BAD","!!! CANNOT IMPORT HTTP.CLIENT: ")+str(e)+"\n") def make_metadata_dict(data): myid,myglob = data @@ -43,12 +60,12 @@ def make_metadata_dict(data): return mydict -class ParseLinks(HTMLParser.HTMLParser): +class ParseLinks(html_parser_HTMLParser): """Parser class that overrides HTMLParser to grab all anchors from an html page and provide suffix and prefix limitors""" def __init__(self): self.PL_anchors = [] - HTMLParser.HTMLParser.__init__(self) + html_parser_HTMLParser.__init__(self) def get_anchors(self): return self.PL_anchors @@ -77,7 +94,7 @@ class ParseLinks(HTMLParser.HTMLParser): for x in attrs: if x[0] == 'href': if x[1] not in self.PL_anchors: - self.PL_anchors.append(urllib2.unquote(x[1])) + self.PL_anchors.append(urllib_parse_unquote(x[1])) def create_conn(baseurl,conn=None): @@ -132,9 +149,9 @@ def create_conn(baseurl,conn=None): if not conn: if protocol == "https": - conn = httplib.HTTPSConnection(host) + conn = http_client_HTTPSConnection(host) elif protocol == "http": - conn = httplib.HTTPConnection(host) + conn = http_client_HTTPConnection(host) elif protocol == "ftp": passive = 1 if(host[-1] == "*"): @@ -637,14 +654,14 @@ def dir_get_metadata(baseurl, conn=None, chunk_size=3000, verbose=1, usingcache= "/".join((baseurl.rstrip("/"), x.lstrip("/"))), conn, chunk_size) break - except httplib.BadStatusLine: + except http_client_BadStatusLine: # Sometimes this error is thrown from conn.getresponse() in # make_http_request(). The docstring for this error in # httplib.py says "Presumably, the server closed the # connection before sending a valid response". conn, protocol, address, params, headers = create_conn( baseurl) - except httplib.ResponseNotReady: + except http_client_ResponseNotReady: # With some http servers this error is known to be thrown # from conn.getresponse() in make_http_request() when the # remote file does not have appropriate read permissions. diff --git a/pym/portage/glsa.py b/pym/portage/glsa.py index 95c3c813c..1f061b231 100644 --- a/pym/portage/glsa.py +++ b/pym/portage/glsa.py @@ -4,7 +4,10 @@ import codecs import sys -import urllib +try: + from urllib.request import urlopen as urllib_request_urlopen +except ImportError: + from urllib import urlopen as urllib_request_urlopen import re import xml.dom.minidom @@ -470,7 +473,7 @@ class Glsa: myurl = "file://"+self.nr else: myurl = repository + "glsa-%s.xml" % str(self.nr) - self.parse(urllib.urlopen(myurl)) + self.parse(urllib_request_urlopen(myurl)) return None def parse(self, myfile): diff --git a/pym/portage/output.py b/pym/portage/output.py index 06073df71..5170d70aa 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -5,7 +5,10 @@ __docformat__ = "epytext" import codecs -import commands +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput import errno import formatter import re @@ -405,7 +408,7 @@ def get_term_size(): pass except ImportError: pass - st, out = commands.getstatusoutput('stty size') + st, out = subprocess_getstatusoutput('stty size') if st == os.EX_OK: out = out.split() if len(out) == 2: diff --git a/pym/portage/sets/__init__.py b/pym/portage/sets/__init__.py index 6c5f850ad..20923ca2e 100644 --- a/pym/portage/sets/__init__.py +++ b/pym/portage/sets/__init__.py @@ -5,8 +5,11 @@ __all__ = ["SETPREFIX", "get_boolean", "SetConfigError", "SetConfig", "load_default_config"] +try: + from configparser import SafeConfigParser, NoOptionError +except ImportError: + from ConfigParser import SafeConfigParser, NoOptionError from portage import os -from ConfigParser import SafeConfigParser, NoOptionError from portage import load_mod from portage.const import USER_CONFIG_PATH, GLOBAL_CONFIG_PATH from portage.exception import PackageSetNotFound diff --git a/pym/portage/util.py b/pym/portage/util.py index 7b7c2a6f1..1401febae 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -12,7 +12,10 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions', 'stack_dicts', 'stack_lists', 'unique_array', 'varexpand', 'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout'] -import commands +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput import codecs import errno import logging @@ -21,6 +24,7 @@ import shlex import stat import string import sys +from io import StringIO import portage from portage import os @@ -40,11 +44,6 @@ try: except ImportError: import pickle -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO - noiselimit = 0 def initialize_logger(level=logging.WARN): @@ -1402,7 +1401,7 @@ def find_updated_config_files(target_root, config_protect): mycommand = "find '%s' -maxdepth 1 -name '._cfg????_%s'" % \ os.path.split(x.rstrip(os.path.sep)) mycommand += " ! -name '.*~' ! -iname '.*.bak' -print0" - a = commands.getstatusoutput(mycommand) + a = subprocess_getstatusoutput(mycommand) if a[0] == 0: files = a[1].split('\0') diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py index a65e57d51..fcabd599d 100644 --- a/pym/repoman/utilities.py +++ b/pym/repoman/utilities.py @@ -19,11 +19,14 @@ __all__ = [ ] import codecs -import commands import errno import itertools import logging import sys +try: + from subprocess import getstatusoutput as subprocess_getstatusoutput +except ImportError: + from commands import getstatusoutput as subprocess_getstatusoutput from xml.dom import minidom from xml.dom import NotFoundErr @@ -59,13 +62,13 @@ def detect_vcs_conflicts(options, vcs): if vcs == 'cvs': logging.info("Performing a " + output.green("cvs -n up") + \ " with a little magic grep to check for updates.") - retval = commands.getstatusoutput("cvs -n up 2>&1 | " + \ + retval = subprocess_getstatusoutput("cvs -n up 2>&1 | " + \ "egrep '^[^\?] .*' | " + \ "egrep -v '^. .*/digest-[^/]+|^cvs server: .* -- ignored$'") if vcs == 'svn': logging.info("Performing a " + output.green("svn status -u") + \ " with a little magic grep to check for updates.") - retval = commands.getstatusoutput("svn status -u 2>&1 | " + \ + retval = subprocess_getstatusoutput("svn status -u 2>&1 | " + \ "egrep -v '^. +.*/digest-[^/]+' | " + \ "head -n-1")