Make the setup.py runnable by Python 3 and run 2to3 automatically.

This commit is contained in:
Valentin Lorentz 2013-01-30 20:10:48 +01:00
parent cdb54613bd
commit f42ac86d91
3 changed files with 59 additions and 34 deletions

30
2to3/run.py Normal file → Executable file
View File

@ -1,6 +1,32 @@
#! /usr/bin/python2.7 #!/usr/bin/env python
import os
import sys 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 import fix_def_iteritems, fix_def_itervalues, fix_def_iterkeys, fix_reload

View File

@ -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 "$@"

View File

@ -32,28 +32,8 @@
import os import os
import sys 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 import subprocess
plugins = [s for s in os.listdir('plugins') if
os.path.exists(os.path.join('plugins', s, 'plugin.py'))]
version = None version = None
try: try:
proc = subprocess.Popen('git show HEAD --format=%ci', shell=True, proc = subprocess.Popen('git show HEAD --format=%ci', shell=True,
@ -62,7 +42,6 @@ try:
.strip() \ .strip() \
.replace(' +', '+') \ .replace(' +', '+') \
.replace(' ', 'T') .replace(' ', 'T')
except: except:
pass pass
if not version: 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.write("version = '0.83.4.1+limnoria %s'\n" % version)
fd.close() 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): def normalizeWhitespace(s):
return ' '.join(s.split()) return ' '.join(s.split())
try: try:
from distutils.core import setup from distutils.core import setup
from distutils.sysconfig import get_python_lib from distutils.sysconfig import get_python_lib
except ImportError, e: except ImportError as e:
s = normalizeWhitespace("""Supybot requires the distutils package to s = normalizeWhitespace("""Supybot requires the distutils package to
install. This package is normally included with Python, but for some install. This package is normally included with Python, but for some
unfathomable reason, many distributions to take it out of standard Python 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') previousInstall = os.path.join(get_python_lib(), 'supybot')
if os.path.exists(previousInstall): if os.path.exists(previousInstall):
try: try:
print 'Removing current installation.' print('Removing current installation.')
shutil.rmtree(previousInstall) shutil.rmtree(previousInstall)
except Exception, e: except Exception as e:
print 'Couldn\'t remove former installation: %s' % e print('Couldn\'t remove former installation: %s' % e)
sys.exit(-1) sys.exit(-1)
packages = ['supybot', packages = ['supybot',