Skip to content

Commit

Permalink
Port to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
vsajip authored and vbabiy committed Mar 15, 2011
1 parent 6c82a1b commit 680b5df
Show file tree
Hide file tree
Showing 43 changed files with 699 additions and 393 deletions.
3 changes: 1 addition & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Copyright (c) 2008-2010 Ian Bicking and Contributors
Copyright (c) 2011 The virtualenv developers
Copyright (c) 2008-2011 The pip developers (see AUTHORS.txt file)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
4 changes: 0 additions & 4 deletions docs/news.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ tip (unreleased)

* Fixed issue #204 - rmtree undefined in mercurial.py. Thanks Kelsey Hightower

* Fixed bug in Git vcs backend that would break during reinstallation.

* Fixed bug in Mercurial vcs backend related to pip freeze and branch/tag resolution.

* Fixed bug in version string parsing related to the suffix "-dev".

0.8.2
-----
Expand Down
15 changes: 8 additions & 7 deletions pip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pip.exceptions import InstallationError
from pip.log import logger
from pip.util import get_installed_distributions
from pip.backwardcompat import walk_packages
from pip.backwardcompat import u, walk_packages


def autocomplete():
Expand Down Expand Up @@ -53,30 +53,30 @@ def autocomplete():
# if there are no dists installed, fall back to option completion
if installed:
for dist in installed:
print dist
print(dist)
sys.exit(1)
subcommand = command_dict.get(subcommand_name)
options += [(opt.get_opt_string(), opt.nargs)
for opt in subcommand.parser.option_list
if opt.help != optparse.SUPPRESS_HELP]
# filter out previously specified options from available options
prev_opts = [x.split('=')[0] for x in cwords[1:cword-1]]
options = filter(lambda (x, v): x not in prev_opts, options)
options = [(x, v) for (x, v) in options if x not in prev_opts]
# filter options by current input
options = [(k, v) for k, v in options if k.startswith(current)]
for option in options:
opt_label = option[0]
# append '=' to options which require args
if option[1]:
opt_label += '='
print opt_label
print(opt_label)
else:
# show options of main parser only when necessary
if current.startswith('-') or current.startswith('--'):
subcommands += [opt.get_opt_string()
for opt in parser.option_list
if opt.help != optparse.SUPPRESS_HELP]
print ' '.join(filter(lambda x: x.startswith(current), subcommands))
print(' '.join([x for x in subcommands if x.startswith(current)]))
sys.exit(1)


Expand Down Expand Up @@ -213,15 +213,16 @@ def call_subprocess(cmd, show_stdout=True,
proc = subprocess.Popen(
cmd, stderr=subprocess.STDOUT, stdin=None, stdout=stdout,
cwd=cwd, env=env)
except Exception, e:
except Exception:
e = sys.exc_info()[1]
logger.fatal(
"Error %s while executing command %s" % (e, command_desc))
raise
all_output = []
if stdout is not None:
stdout = proc.stdout
while 1:
line = stdout.readline()
line = u(stdout.readline())
if not line:
break
line = line.rstrip()
Expand Down
8 changes: 5 additions & 3 deletions pip/_pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import imp
import os.path
from types import ModuleType
from pip.backwardcompat import string_types

__all__ = [
'get_importer', 'iter_importers', 'get_loader', 'find_loader',
Expand Down Expand Up @@ -324,7 +325,7 @@ def get_filename(self, fullname=None):
from zipimport import zipimporter

def iter_zipimport_modules(importer, prefix=''):
dirlist = zipimport._zip_directory_cache[importer.archive].keys()
dirlist = list(zipimport._zip_directory_cache[importer.archive].keys())
dirlist.sort()
_prefix = importer.prefix
plen = len(_prefix)
Expand Down Expand Up @@ -523,7 +524,7 @@ def extend_path(path, name):
path = path[:] # Start with a copy of the existing path

for dir in sys.path:
if not isinstance(dir, basestring) or not os.path.isdir(dir):
if not isinstance(dir, string_types) or not os.path.isdir(dir):
continue
subdir = os.path.join(dir, pname)
# XXX This may still add duplicate entries to path on
Expand All @@ -537,7 +538,8 @@ def extend_path(path, name):
if os.path.isfile(pkgfile):
try:
f = open(pkgfile)
except IOError, msg:
except IOError:
msg = sys.exc_info()[1]
sys.stderr.write("Can't open %s: %s\n" %
(pkgfile, msg))
else:
Expand Down
60 changes: 56 additions & 4 deletions pip/backwardcompat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Stuff that isn't in some old versions of Python"""
"""Stuff that differs in different Python versions"""

import sys
import os
Expand All @@ -9,7 +9,9 @@
try:
WindowsError = WindowsError
except NameError:
WindowsError = None
class NeverUsedException(Exception):
"""this exception should never be raised"""
WindowsError = NeverUsedException
try:
from hashlib import md5
except ImportError:
Expand All @@ -20,7 +22,7 @@
from pkgutil import walk_packages
except ImportError:
# let's fall back as long as we can
from _pkgutil import walk_packages
from pip._pkgutil import walk_packages

try:
any = any
Expand All @@ -32,6 +34,56 @@ def any(seq):
return True
return False

if sys.version_info >= (3,):
from io import StringIO
from functools import reduce
from urllib.error import URLError, HTTPError
from queue import Queue, Empty
from urllib.request import url2pathname
from urllib.request import urlretrieve
from email import message as emailmessage
import urllib.parse as urllib
import urllib.request as urllib2
import configparser as ConfigParser
import xmlrpc.client as xmlrpclib
import urllib.parse as urlparse
import http.client as httplib
def cmp(a, b):
return (a > b) - (a < b)
def b(s):
return s.encode('utf-8')
def u(s):
return s.decode('utf-8')
string_types = (str,)
raw_input = input
else:
from cStringIO import StringIO
from urllib2 import URLError, HTTPError
from Queue import Queue, Empty
from urllib import url2pathname, urlretrieve
from email import Message as emailmessage
import urllib
import urllib2
import urlparse
import ConfigParser
import xmlrpclib
import httplib
def b(s):
return s
def u(s):
return s
string_types = (basestring,)
reduce = reduce
cmp = cmp
raw_input = raw_input

try:
from email.parser import FeedParser
except ImportError:
# python lesser than 2.5
from email.FeedParser import FeedParser

from distutils.sysconfig import get_python_lib, get_python_version

def copytree(src, dst):
if sys.version_info < (2, 5):
Expand All @@ -47,7 +99,7 @@ def copytree(src, dst):
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
pools = list(map(tuple, args)) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
Expand Down
13 changes: 7 additions & 6 deletions pip/basecommand.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Base Command class, and related routines"""

from cStringIO import StringIO
import os
import socket
import sys
Expand All @@ -13,7 +12,7 @@
from pip.download import urlopen
from pip.exceptions import BadCommand, InstallationError, UninstallationError
from pip.venv import restart_in_venv
from pip.backwardcompat import walk_packages
from pip.backwardcompat import StringIO, urllib, urllib2, walk_packages

__all__ = ['command_dict', 'Command', 'load_all_commands',
'load_command', 'command_names']
Expand Down Expand Up @@ -125,11 +124,13 @@ def main(self, complete_args, args, initial_options):
exit = 0
try:
self.run(options, args)
except (InstallationError, UninstallationError), e:
except (InstallationError, UninstallationError):
e = sys.exc_info()[1]
logger.fatal(str(e))
logger.info('Exception information:\n%s' % format_exc())
exit = 1
except BadCommand, e:
except BadCommand:
e = sys.exc_info()[1]
logger.fatal(str(e))
logger.info('Exception information:\n%s' % format_exc())
exit = 1
Expand Down Expand Up @@ -174,8 +175,8 @@ def open_logfile(filename, mode='a'):

log_fp = open(filename, mode)
if exists:
print >> log_fp, '-'*60
print >> log_fp, '%s run on %s' % (sys.argv[0], time.strftime('%c'))
log_fp.write('%s\n' % ('-'*60))
log_fp.write('%s run on %s\n' % (sys.argv[0], time.strftime('%c')))
return log_fp


Expand Down
13 changes: 7 additions & 6 deletions pip/baseparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import sys
import optparse
import pkg_resources
import ConfigParser
import os
from distutils.util import strtobool
from pip.backwardcompat import ConfigParser, string_types
from pip.locations import default_config_file, default_log_file


Expand Down Expand Up @@ -50,7 +50,7 @@ def update_defaults(self, defaults):
# 2. environmental variables
config.update(dict(self.get_environ_vars()))
# Then set the options with those values
for key, val in config.iteritems():
for key, val in config.items():
key = key.replace('_', '-')
if not key.startswith('--'):
key = '--%s' % key # only prefer long opts
Expand All @@ -68,8 +68,9 @@ def update_defaults(self, defaults):
val = strtobool(val)
try:
val = option.convert_value(key, val)
except optparse.OptionValueError, e:
print ("An error occured during configuration: %s" % e)
except optparse.OptionValueError:
e = sys.exc_info()[1]
print("An error occured during configuration: %s" % e)
sys.exit(3)
defaults[option.dest] = val
return defaults
Expand All @@ -82,7 +83,7 @@ def get_config_section(self, name):

def get_environ_vars(self, prefix='PIP_'):
"""Returns a generator with all environmental vars with prefix PIP_"""
for key, val in os.environ.iteritems():
for key, val in os.environ.items():
if key.startswith(prefix):
yield (key.replace(prefix, '').lower(), val)

Expand All @@ -96,7 +97,7 @@ def get_default_values(self):
defaults = self.update_defaults(self.defaults.copy()) # ours
for option in self._get_all_options():
default = defaults.get(option.dest)
if isinstance(default, basestring):
if isinstance(default, string_types):
opt_str = option.get_opt_string()
defaults[option.dest] = option.check_value(opt_str, default)
return optparse.Values(defaults)
Expand Down
2 changes: 1 addition & 1 deletion pip/commands/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def run(self, options, args):
shell_options = ['--'+shell for shell in sorted(shells)]
if options.shell in shells:
script = COMPLETION_SCRIPTS.get(options.shell, '')
print BASE_COMPLETION % {'script': script, 'shell': options.shell}
print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
else:
sys.stderr.write('ERROR: You must pass %s\n' % ' or '.join(shell_options))

Expand Down
5 changes: 2 additions & 3 deletions pip/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ def run(self, options, args):
command.parser.print_help()
return
parser.print_help()
print
print 'Commands available:'
print('\nCommands available:')
commands = list(set(command_dict.values()))
commands.sort(key=lambda x: x.name)
for command in commands:
if command.hidden:
continue
print ' %s: %s' % (command.name, command.summary)
print(' %s: %s' % (command.name, command.summary))


HelpCommand()
4 changes: 2 additions & 2 deletions pip/commands/search.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
import xmlrpclib
import textwrap
import pkg_resources
import pip.download
from pip.basecommand import Command
from pip.util import get_terminal_size
from pip.log import logger
from pip.backwardcompat import xmlrpclib, reduce, cmp
from distutils.version import StrictVersion, LooseVersion


Expand Down Expand Up @@ -69,7 +69,7 @@ def transform_hits(hits):
packages[name]['score'] = score

# each record has a unique name now, so we will convert the dict into a list sorted by score
package_list = sorted(packages.values(), lambda x, y: cmp(y['score'], x['score']))
package_list = sorted(packages.values(), key=lambda x: x['score'], reverse=True)
return package_list


Expand Down
2 changes: 1 addition & 1 deletion pip/commands/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def remove_filename_from_pth(self, filename):
if lines != new_lines:
logger.info('Removing reference to %s from .pth file %s'
% (display_path(filename), display_path(pth)))
if not filter(None, new_lines):
if not [line for line in new_lines if line]:
logger.info('%s file would be empty: deleting' % display_path(pth))
if not self.simulate:
os.unlink(pth)
Expand Down
Loading

0 comments on commit 680b5df

Please sign in to comment.