2013-11-25 00:26:22 +01:00
|
|
|
"""Stuff that differs in different Python versions and platform
|
|
|
|
distributions."""
|
2009-11-20 08:10:46 +01:00
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
# flake8: noqa
|
|
|
|
|
2012-08-05 03:48:16 +02:00
|
|
|
import os
|
2012-08-19 00:26:17 +02:00
|
|
|
import imp
|
2010-06-03 20:44:34 +02:00
|
|
|
import sys
|
2012-06-11 03:16:48 +02:00
|
|
|
import site
|
2010-06-03 20:44:34 +02:00
|
|
|
|
2012-06-06 16:20:22 +02:00
|
|
|
__all__ = ['WindowsError']
|
2009-11-20 20:04:33 +01:00
|
|
|
|
2013-02-16 19:02:41 +01:00
|
|
|
uses_pycache = hasattr(imp, 'cache_from_source')
|
2012-08-19 00:26:17 +02:00
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2013-03-04 07:55:54 +01:00
|
|
|
class NeverUsedException(Exception):
|
|
|
|
"""this exception should never be raised"""
|
|
|
|
|
2009-11-20 20:04:33 +01:00
|
|
|
try:
|
2010-01-21 13:27:02 +01:00
|
|
|
WindowsError = WindowsError
|
2009-11-20 20:04:33 +01:00
|
|
|
except NameError:
|
2011-03-15 20:49:48 +01:00
|
|
|
WindowsError = NeverUsedException
|
2009-11-20 08:10:46 +01:00
|
|
|
|
2013-03-04 07:55:54 +01:00
|
|
|
try:
|
|
|
|
#new in Python 3.3
|
|
|
|
PermissionError = PermissionError
|
|
|
|
except NameError:
|
|
|
|
PermissionError = NeverUsedException
|
2010-06-03 20:44:34 +02:00
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
console_encoding = sys.__stdout__.encoding
|
|
|
|
|
2014-01-27 20:40:32 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
|
|
|
|
def binary(s):
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
return s.encode('ascii')
|
|
|
|
return s
|
|
|
|
except NameError:
|
|
|
|
def binary(s):
|
|
|
|
if isinstance(s, str):
|
|
|
|
return s.encode('ascii')
|
|
|
|
|
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
if sys.version_info >= (3,):
|
2011-06-03 16:26:21 +02:00
|
|
|
from io import StringIO, BytesIO
|
2011-03-15 20:49:48 +01:00
|
|
|
from functools import reduce
|
|
|
|
from urllib.error import URLError, HTTPError
|
|
|
|
from queue import Queue, Empty
|
2013-11-21 00:36:09 +01:00
|
|
|
from urllib.request import url2pathname, urlretrieve, pathname2url
|
2011-03-15 20:49:48 +01:00
|
|
|
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
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
def cmp(a, b):
|
|
|
|
return (a > b) - (a < b)
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
def b(s):
|
|
|
|
return s.encode('utf-8')
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
def u(s):
|
|
|
|
return s.decode('utf-8')
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
def console_to_str(s):
|
2011-10-26 06:56:36 +02:00
|
|
|
try:
|
|
|
|
return s.decode(console_encoding)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return s.decode('utf_8')
|
|
|
|
|
2013-03-29 17:29:11 +01:00
|
|
|
def get_http_message_param(http_message, param, default_value):
|
|
|
|
return http_message.get_param(param, default_value)
|
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
bytes = bytes
|
2011-03-15 20:49:48 +01:00
|
|
|
string_types = (str,)
|
|
|
|
raw_input = input
|
|
|
|
else:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
from urllib2 import URLError, HTTPError
|
|
|
|
from Queue import Queue, Empty
|
2013-11-21 00:36:09 +01:00
|
|
|
from urllib import url2pathname, urlretrieve, pathname2url
|
2011-03-15 20:49:48 +01:00
|
|
|
from email import Message as emailmessage
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import urlparse
|
|
|
|
import ConfigParser
|
|
|
|
import xmlrpclib
|
|
|
|
import httplib
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
def b(s):
|
|
|
|
return s
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
def u(s):
|
|
|
|
return s
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
def console_to_str(s):
|
|
|
|
return s
|
2011-12-27 18:26:33 +01:00
|
|
|
|
2013-03-29 17:29:11 +01:00
|
|
|
def get_http_message_param(http_message, param, default_value):
|
2013-03-30 18:31:36 +01:00
|
|
|
result = http_message.getparam(param)
|
|
|
|
return result or default_value
|
2013-03-29 17:29:11 +01:00
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
bytes = str
|
2011-03-15 20:49:48 +01:00
|
|
|
string_types = (basestring,)
|
|
|
|
reduce = reduce
|
|
|
|
cmp = cmp
|
|
|
|
raw_input = raw_input
|
2011-06-03 16:26:21 +02:00
|
|
|
BytesIO = StringIO
|
2011-03-15 20:49:48 +01:00
|
|
|
|
|
|
|
|
2013-11-25 00:26:22 +01:00
|
|
|
def get_path_uid(path):
|
|
|
|
"""
|
|
|
|
Return path's uid.
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
Does not follow symlinks:
|
|
|
|
https://github.com/pypa/pip/pull/935#discussion_r5307003
|
2013-11-25 00:26:22 +01:00
|
|
|
|
2014-05-14 05:10:06 +02:00
|
|
|
Placed this function in compat due to differences on AIX and
|
2014-01-27 15:07:10 +01:00
|
|
|
Jython, that should eventually go away.
|
2013-11-25 00:26:22 +01:00
|
|
|
|
|
|
|
:raises OSError: When path is a symlink or can't be read.
|
|
|
|
"""
|
|
|
|
if hasattr(os, 'O_NOFOLLOW'):
|
|
|
|
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
|
|
|
|
file_uid = os.fstat(fd).st_uid
|
|
|
|
os.close(fd)
|
|
|
|
else: # AIX and Jython
|
|
|
|
# WARNING: time of check vulnerabity, but best we can do w/o NOFOLLOW
|
|
|
|
if not os.path.islink(path):
|
|
|
|
# older versions of Jython don't have `os.fstat`
|
|
|
|
file_uid = os.stat(path).st_uid
|
|
|
|
else:
|
|
|
|
# raise OSError for parity with os.O_NOFOLLOW above
|
2014-01-27 15:07:10 +01:00
|
|
|
raise OSError(
|
|
|
|
"%s is a symlink; Will not return uid for symlinks" % path
|
|
|
|
)
|
2013-11-25 00:26:22 +01:00
|
|
|
return file_uid
|
2014-03-01 05:09:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
# packages in the stdlib that may have installation metadata, but should not be
|
|
|
|
# considered 'installed'. this theoretically could be determined based on
|
|
|
|
# dist.location (py27:`sysconfig.get_paths()['stdlib']`,
|
|
|
|
# py26:sysconfig.get_config_vars('LIBDEST')), but fear platform variation may
|
|
|
|
# make this ineffective, so hard-coding
|
|
|
|
stdlib_pkgs = ['python', 'wsgiref']
|
|
|
|
if sys.version_info >= (2, 7):
|
|
|
|
stdlib_pkgs.extend(['argparse'])
|