From a1e3d240ef250e768611070a3f253bb9617d58cb Mon Sep 17 00:00:00 2001 From: utahta Date: Tue, 17 May 2011 18:39:49 +0900 Subject: [PATCH] support python3 --- pythonbrew/curl.py | 5 ++-- pythonbrew/define.py | 13 +++++++---- pythonbrew/downloader.py | 7 +++--- pythonbrew/installer/pythonbrewinstaller.py | 4 +++- pythonbrew/util.py | 26 +++++++++++++++++---- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pythonbrew/curl.py b/pythonbrew/curl.py index 3d72495..62bccba 100644 --- a/pythonbrew/curl.py +++ b/pythonbrew/curl.py @@ -3,6 +3,7 @@ import re import subprocess from subprocess import Popen, PIPE from pythonbrew.log import logger +from pythonbrew.util import u class Curl(object): def __init__(self): @@ -25,11 +26,11 @@ class Curl(object): raise respinfo = {} for line in p.stdout: - line = line.strip() + line = u(line.strip()) if re.match('^HTTP.*? 200 OK$', line): break for line in p.stdout: - line = line.strip().split(":", 1) + line = u(line.strip()).split(":", 1) if len(line) == 2: respinfo[line[0].strip().lower()] = line[1].strip() return respinfo diff --git a/pythonbrew/define.py b/pythonbrew/define.py index c8eb16d..184243f 100644 --- a/pythonbrew/define.py +++ b/pythonbrew/define.py @@ -1,14 +1,19 @@ import os import re -import ConfigParser +try: + import ConfigParser +except: + import configparser as ConfigParser +# pythonbrew version VERSION = "0.7.2" -if os.environ.has_key("PYTHONBREW_ROOT"): - ROOT = os.environ["PYTHONBREW_ROOT"] -else: +# pythonbrew root path +ROOT = os.environ.get("PYTHONBREW_ROOT") +if not ROOT: ROOT = os.path.join(os.environ["HOME"],".pythonbrew") +# pythonbrew installer root path INSTALLER_ROOT = os.path.dirname(os.path.abspath(__file__)) # directories diff --git a/pythonbrew/downloader.py b/pythonbrew/downloader.py index 5ea72f6..f5266a1 100644 --- a/pythonbrew/downloader.py +++ b/pythonbrew/downloader.py @@ -2,6 +2,7 @@ from pythonbrew.define import PYTHON_VERSION_URL, PYTHONBREW_STABLE_VERSION_URL, PYTHONBREW_UPDATE_URL_PYPI, PYTHONBREW_UPDATE_URL_HEAD from pythonbrew.log import logger from pythonbrew.curl import Curl +from pythonbrew.util import u def get_headerinfo_from_url(url): c = Curl() @@ -9,7 +10,7 @@ def get_headerinfo_from_url(url): def get_stable_version(): c = Curl() - return c.read(PYTHONBREW_STABLE_VERSION_URL).strip() + return u(c.read(PYTHONBREW_STABLE_VERSION_URL).strip()) class Downloader(object): def download(self, msg, url, path): @@ -24,6 +25,4 @@ def get_pythonbrew_update_url(version): return PYTHONBREW_UPDATE_URL_PYPI % (version) def get_python_version_url(version): - if PYTHON_VERSION_URL.has_key(version): - return PYTHON_VERSION_URL[version] - return None + return PYTHON_VERSION_URL.get(version) diff --git a/pythonbrew/installer/pythonbrewinstaller.py b/pythonbrew/installer/pythonbrewinstaller.py index 10c980e..bc17bc3 100644 --- a/pythonbrew/installer/pythonbrewinstaller.py +++ b/pythonbrew/installer/pythonbrewinstaller.py @@ -8,6 +8,7 @@ from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\ PATH_SCRIPTS_PYTHONBREW_COMMANDS, PATH_BIN_PYTHONBREW,\ ROOT, PATH_LOG, PATH_PATCHES, PATH_ETC_CONFIG,\ PATH_SCRIPTS_PYTHONBREW_INSTALLER +import stat class PythonbrewInstaller(object): """pythonbrew installer: @@ -55,7 +56,8 @@ if __name__ == "__main__": %s %s/pythonbrew_main.py "$@" """ % (sys.executable, PATH_SCRIPTS)) fp.close() - os.chmod(PATH_BIN_PYTHONBREW, 0755) + # mode 0755 + os.chmod(PATH_BIN_PYTHONBREW, stat.S_IRUSR|stat.S_IWUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH) # create a bashrc for pythonbrew fp = open(os.path.join(PATH_ETC,'bashrc'), 'w') diff --git a/pythonbrew/util.py b/pythonbrew/util.py index 956ea68..32f3fb2 100644 --- a/pythonbrew/util.py +++ b/pythonbrew/util.py @@ -11,6 +11,7 @@ from subprocess import PIPE, Popen from pythonbrew.define import PATH_BIN, PATH_PYTHONS, PATH_ETC_CURRENT from pythonbrew.exceptions import ShellCommandException from pythonbrew.log import logger +import sys def size_format(b): kb = 1000 @@ -76,8 +77,9 @@ def is_python26(version): def makedirs(path): try: os.makedirs(path) - except OSError, (e, es): - if errno.EEXIST != e: + except OSError: + e = sys.exc_info()[1] + if errno.EEXIST != e.errno: raise def symlink(src, dst): @@ -89,8 +91,9 @@ def symlink(src, dst): def unlink(path): try: os.unlink(path) - except OSError, (e, es): - if errno.ENOENT != e: + except OSError: + e = sys.exc_info()[1] + if errno.ENOENT != e.errno: raise def rm_r(path): @@ -166,7 +169,8 @@ def untar_file(filename, location): else: try: fp = tar.extractfile(member) - except (KeyError, AttributeError), e: + except (KeyError, AttributeError): + e = sys.exc_info()[1] # Some corrupt tar files seem to produce this # (specifically bad symlinks) logger.error('In the tar file %s the member %s is invalid: %s' @@ -218,6 +222,18 @@ def fileurl_to_path(url): url = '/' + url[len('file:'):].lstrip('/') return urllib.unquote(url) +def u(val): + """to unicode + """ + try: + # for python3 + if type(val) == bytes: + return val.decode() + except: + if type(val) == str: + return val.decode("utf-8") + return val + class Subprocess(object): def __init__(self, log=None, shell=True, cwd=None, print_cmd=False): self._log = log