From f42ac86d91802a2a8a37a15268d2b8cf898dd55d Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 30 Jan 2013 20:10:48 +0100 Subject: [PATCH] Make the setup.py runnable by Python 3 and run 2to3 automatically. --- 2to3/run.py | 30 ++++++++++++++++++++++-- sandbox/run_2to3.sh | 7 ------ setup.py | 56 +++++++++++++++++++++++++-------------------- 3 files changed, 59 insertions(+), 34 deletions(-) mode change 100644 => 100755 2to3/run.py delete mode 100755 sandbox/run_2to3.sh diff --git a/2to3/run.py b/2to3/run.py old mode 100644 new mode 100755 index 4457f5603..90ed51daa --- a/2to3/run.py +++ b/2to3/run.py @@ -1,6 +1,32 @@ -#! /usr/bin/python2.7 +#!/usr/bin/env python +import os import sys -from lib2to3.main import main +import shutil +from glob import glob +try: + from lib2to3.main import main +except ImportError: + print('Error: you need the 2to3 tool to run this script.') +os.chdir(os.path.join(os.path.dirname(__file__), '..')) +try: + os.unlink('src/version.py') +except OSError: + pass +try: + shutil.rmtree('py3k') +except OSError: + pass +os.mkdir('py3k') +for dirname in ('locales', 'docs', 'plugins'): + shutil.copytree(dirname, os.path.join('py3k', dirname)) + +files = ['run.py', 'src', 'plugins', 'test', 'setup.py'] + glob('scripts/*') +args = ['-wWno', 'py3k'] +fixers = [] +for fix in ['all', 'def_iteritems', 'def_itervalues', 'def_iterkeys', 'reload']: + fixers += ['-f', fix] +sys.argv = files + args + fixers + sys.argv +sys.argc = len(sys.argv) import fix_def_iteritems, fix_def_itervalues, fix_def_iterkeys, fix_reload diff --git a/sandbox/run_2to3.sh b/sandbox/run_2to3.sh deleted file mode 100755 index 3780f1026..000000000 --- a/sandbox/run_2to3.sh +++ /dev/null @@ -1,7 +0,0 @@ -rm -f src/version.py # Prevent 2to3 from copying it, since py3k/src/version.py was probably written by root. -rm -rf py3k -mkdir py3k -cp -R locales/ py3k/locales -cp -R docs/ py3k/ -cp -R plugins/ py3k/plugins # copy plugins data -python 2to3/run.py src/ plugins/ test/ scripts/* setup.py -wWno py3k -f all -f def_iteritems -f def_itervalues -f def_iterkeys -f reload "$@" diff --git a/setup.py b/setup.py index ab77df709..cd56fb292 100644 --- a/setup.py +++ b/setup.py @@ -32,28 +32,8 @@ import os import sys - -if sys.version_info < (2, 6, 0): - sys.stderr.write("Supybot requires Python 2.6 or newer.") - sys.stderr.write(os.linesep) - sys.exit(-1) - -import textwrap - -clean = False -while '--clean' in sys.argv: - clean = True - sys.argv.remove('--clean') - -import glob -import shutil -import os import subprocess - -plugins = [s for s in os.listdir('plugins') if - os.path.exists(os.path.join('plugins', s, 'plugin.py'))] - version = None try: proc = subprocess.Popen('git show HEAD --format=%ci', shell=True, @@ -62,7 +42,6 @@ try: .strip() \ .replace(' +', '+') \ .replace(' ', 'T') - except: pass if not version: @@ -76,13 +55,40 @@ fd = open(os.path.join('src', 'version.py'), 'a') fd.write("version = '0.83.4.1+limnoria %s'\n" % version) fd.close() +if sys.version_info < (2, 6, 0): + sys.stderr.write("Supybot requires Python 2.6 or newer.") + sys.stderr.write(os.linesep) + sys.exit(-1) +elif sys.version_info[0] >= 3 and \ + not os.path.split(os.path.abspath(os.path.dirname(__file__)))[-1] == 'py3k': + # The second condition is used to prevent this script to run recursively + subprocess.Popen([sys.executable, os.path.join('2to3', 'run.py')]).wait() + os.chdir('py3k') + subprocess.Popen([sys.executable] + sys.argv).wait() + + +import textwrap + +clean = False +while '--clean' in sys.argv: + clean = True + sys.argv.remove('--clean') + +import glob +import shutil +import os + + +plugins = [s for s in os.listdir('plugins') if + os.path.exists(os.path.join('plugins', s, 'plugin.py'))] + def normalizeWhitespace(s): return ' '.join(s.split()) try: from distutils.core import setup from distutils.sysconfig import get_python_lib -except ImportError, e: +except ImportError as e: s = normalizeWhitespace("""Supybot requires the distutils package to install. This package is normally included with Python, but for some unfathomable reason, many distributions to take it out of standard Python @@ -103,10 +109,10 @@ if clean: previousInstall = os.path.join(get_python_lib(), 'supybot') if os.path.exists(previousInstall): try: - print 'Removing current installation.' + print('Removing current installation.') shutil.rmtree(previousInstall) - except Exception, e: - print 'Couldn\'t remove former installation: %s' % e + except Exception as e: + print('Couldn\'t remove former installation: %s' % e) sys.exit(-1) packages = ['supybot',