Discussion:
Script for automatic 2to3-ing during develooment?
Lennart Regebro
2009-02-18 13:45:59 UTC
Permalink
When porting you constantly need two copies of the code, one that has
been 2to3'd and one not. Before I start writing a script that syncs
the two and runs 2to3 on the updates scripts I thought I better check
if somebody else already have done this.
--
Lennart Regebro: Pythonista, Barista, Notsotrista.
http://regebro.wordpress.com/
+33 661 58 14 64
Martin v. Löwis
2009-02-18 21:19:57 UTC
Permalink
Post by Lennart Regebro
When porting you constantly need two copies of the code, one that has
been 2to3'd and one not. Before I start writing a script that syncs
the two and runs 2to3 on the updates scripts I thought I better check
if somebody else already have done this.
In distutils' build_py command, there is support for running 2to3 at
installation time.

Regards,
Martin
Lennart Regebro
2009-02-18 23:29:44 UTC
Permalink
Post by Martin v. Löwis
Post by Lennart Regebro
When porting you constantly need two copies of the code, one that has
been 2to3'd and one not. Before I start writing a script that syncs
the two and runs 2to3 on the updates scripts I thought I better check
if somebody else already have done this.
In distutils' build_py command, there is support for running 2to3 at
installation time.
Yeah, but I don't want to *install* it. I want to run the tests under
Python 3. :) Maybe I can run a command that installs it into a
specific prefix, and then runs the tests from there. Hmmm.
--
Lennart Regebro: Pythonista, Barista, Notsotrista.
http://regebro.wordpress.com/
+33 661 58 14 64
Martin v. Löwis
2009-02-18 23:35:18 UTC
Permalink
Post by Lennart Regebro
Yeah, but I don't want to *install* it. I want to run the tests under
Python 3. :) Maybe I can run a command that installs it into a
specific prefix, and then runs the tests from there. Hmmm.
Then you should be able to recycle the logic that performs the
translation, namely distutils.utils.run_2to3 (after making a copy
of the sources to be converted).

HTH,
Martin
Lennart Regebro
2009-02-18 23:36:36 UTC
Permalink
Post by Martin v. Löwis
Post by Lennart Regebro
Yeah, but I don't want to *install* it. I want to run the tests under
Python 3. :) Maybe I can run a command that installs it into a
specific prefix, and then runs the tests from there. Hmmm.
Then you should be able to recycle the logic that performs the
translation, namely distutils.utils.run_2to3 (after making a copy
of the sources to be converted).
OK, I'll look at that, thanks.
--
Lennart Regebro: Pythonista, Barista, Notsotrista.
http://regebro.wordpress.com/
+33 661 58 14 64
Mark Hammond
2009-02-19 09:11:43 UTC
Permalink
Post by Lennart Regebro
Post by Martin v. Löwis
Post by Lennart Regebro
When porting you constantly need two copies of the code, one that has
been 2to3'd and one not. Before I start writing a script that syncs
the two and runs 2to3 on the updates scripts I thought I better check
if somebody else already have done this.
In distutils' build_py command, there is support for running 2to3 at
installation time.
Yeah, but I don't want to *install* it. I want to run the tests under
Python 3. :)
pywin32 takes the approach outlined by Martin - so *does* actually
install from the source dir, and the tests are run in the install dir.

If the tests are simple, I use the following "run2.py" script a'la:

% python30 run2.py some_script_in_py2_syntax.py etc...

It is far from perfect, but works-for-me...

Cheers,

Mark

-- run2.py --
# This is a Python 3.x script to execute a python 2.x script by
# 2to3'ing it.
import sys
from lib2to3.refactor import RefactoringTool, get_fixers_from_package

fixers = get_fixers_from_package('lib2to3.fixes')
options = dict(doctests_only=False, fix=[], list_fixes=[],
print_function=False, verbose=False,
write=True)
r = RefactoringTool(fixers, options)
script = sys.argv[1]
data = open(script).read()
print("Converting...")
got = r.refactor_string(data, script)
print("Executing...")
# nuke ourselves from argv
del sys.argv[0]
# patch our env
__file__ = sys.argv[0]
# and go
exec(str(got))
Lennart Regebro
2009-02-19 09:31:45 UTC
Permalink
OK, thanks. I'll take a look at this and report back.
Post by Lennart Regebro
Post by Martin v. Löwis
Post by Lennart Regebro
When porting you constantly need two copies of the code, one that has
been 2to3'd and one not. Before I start writing a script that syncs
the two and runs 2to3 on the updates scripts I thought I better check
if somebody else already have done this.
In distutils' build_py command, there is support for running 2to3 at
installation time.
Yeah, but I don't want to *install* it. I want to run the tests under
Python 3. :)
pywin32 takes the approach outlined by Martin - so *does* actually install
from the source dir, and the tests are run in the install dir.
% python30 run2.py some_script_in_py2_syntax.py etc...
It is far from perfect, but works-for-me...
Cheers,
Mark
-- run2.py --
# This is a Python 3.x script to execute a python 2.x script by
# 2to3'ing it.
import sys
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
fixers = get_fixers_from_package('lib2to3.fixes')
options = dict(doctests_only=False, fix=[], list_fixes=[],
print_function=False, verbose=False,
write=True)
r = RefactoringTool(fixers, options)
script = sys.argv[1]
data = open(script).read()
print("Converting...")
got = r.refactor_string(data, script)
print("Executing...")
# nuke ourselves from argv
del sys.argv[0]
# patch our env
__file__ = sys.argv[0]
# and go
exec(str(got))
--
Lennart Regebro: Pythonista, Barista, Notsotrista.
http://regebro.wordpress.com/
+33 661 58 14 64
Loading...