2011-03-15 20:49:48 +01:00
|
|
|
"""Stuff that differs in different Python versions"""
|
2009-11-20 08:10:46 +01:00
|
|
|
|
2010-06-03 20:44:34 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2010-07-26 19:31:49 +02:00
|
|
|
__all__ = ['any', 'WindowsError', 'md5', 'copytree']
|
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
|
|
|
class NeverUsedException(Exception):
|
|
|
|
"""this exception should never be raised"""
|
|
|
|
WindowsError = NeverUsedException
|
2009-11-20 20:04:33 +01:00
|
|
|
try:
|
|
|
|
from hashlib import md5
|
|
|
|
except ImportError:
|
|
|
|
import md5 as md5_module
|
|
|
|
md5 = md5_module.new
|
2009-11-20 08:10:46 +01:00
|
|
|
|
2010-07-26 19:31:49 +02:00
|
|
|
try:
|
|
|
|
from pkgutil import walk_packages
|
|
|
|
except ImportError:
|
|
|
|
# let's fall back as long as we can
|
2011-03-15 20:49:48 +01:00
|
|
|
from pip._pkgutil import walk_packages
|
2010-07-26 19:31:49 +02:00
|
|
|
|
2009-11-20 08:10:46 +01:00
|
|
|
try:
|
2009-11-20 22:03:14 +01:00
|
|
|
any = any
|
2009-11-20 08:10:46 +01:00
|
|
|
except NameError:
|
2010-07-29 18:56:03 +02:00
|
|
|
|
2009-11-20 08:10:46 +01:00
|
|
|
def any(seq):
|
|
|
|
for item in seq:
|
|
|
|
if item:
|
|
|
|
return True
|
|
|
|
return False
|
2010-06-03 20:44:34 +02:00
|
|
|
|
2011-03-23 00:24:25 +01:00
|
|
|
console_encoding = sys.__stdout__.encoding
|
|
|
|
|
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
|
2012-06-03 15:40:59 +02:00
|
|
|
from urllib.request import pathname2url
|
2011-03-15 20:49:48 +01:00
|
|
|
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
|
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')
|
|
|
|
|
2011-12-27 18:26:33 +01:00
|
|
|
def fwrite(f, s):
|
|
|
|
f.buffer.write(b(s))
|
|
|
|
|
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
|
2012-06-03 15:40:59 +02:00
|
|
|
from urllib import pathname2url, url2pathname, urlretrieve
|
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
|
|
|
|
|
|
|
def fwrite(f, s):
|
|
|
|
f.write(s)
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2010-07-29 18:56:03 +02:00
|
|
|
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2010-06-03 20:44:34 +02:00
|
|
|
def copytree(src, dst):
|
2010-06-03 21:17:46 +02:00
|
|
|
if sys.version_info < (2, 5):
|
2010-06-03 20:44:34 +02:00
|
|
|
before_last_dir = os.path.dirname(dst)
|
|
|
|
if not os.path.exists(before_last_dir):
|
|
|
|
os.makedirs(before_last_dir)
|
|
|
|
shutil.copytree(src, dst)
|
|
|
|
shutil.copymode(src, dst)
|
|
|
|
else:
|
|
|
|
shutil.copytree(src, dst)
|
2010-07-26 18:53:59 +02:00
|
|
|
|
|
|
|
|
2010-08-16 01:46:23 +02:00
|
|
|
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
|
2011-03-15 20:49:48 +01:00
|
|
|
pools = list(map(tuple, args)) * kwds.get('repeat', 1)
|
2010-08-16 01:46:23 +02:00
|
|
|
result = [[]]
|
|
|
|
for pool in pools:
|
|
|
|
result = [x+[y] for x in result for y in pool]
|
|
|
|
for prod in result:
|
|
|
|
yield tuple(prod)
|