Skip to content

Commit

Permalink
Merge https://github.com/pypa/distutils into remove-lib2to3-usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 4, 2021
2 parents 41df8fe + aede9e0 commit d989cdb
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 133 deletions.
18 changes: 0 additions & 18 deletions docs/deprecated/distutils/apiref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1941,24 +1941,6 @@ Subclasses of :class:`Command` must define the following methods.

.. class:: build_py

.. class:: build_py_2to3

Alternative implementation of build_py which also runs the
2to3 conversion library on each .py file that is going to be
installed. To use this in a setup.py file for a distribution
that is designed to run with both Python 2.x and 3.x, add::

try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py

to your setup.py, and later::

cmdclass = {'build_py': build_py}

to the invocation of setup().


:mod:`distutils.command.build_scripts` --- Build the scripts of a package
=========================================================================
Expand Down
26 changes: 1 addition & 25 deletions setuptools/_distutils/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from distutils.core import Command
from distutils.errors import *
from distutils.util import convert_path, Mixin2to3
from distutils.util import convert_path
from distutils import log

class build_py (Command):
Expand Down Expand Up @@ -390,27 +390,3 @@ def byte_compile(self, files):
if self.optimize > 0:
byte_compile(files, optimize=self.optimize,
force=self.force, prefix=prefix, dry_run=self.dry_run)

class build_py_2to3(build_py, Mixin2to3):
def run(self):
self.updated_files = []

# Base class code
if self.py_modules:
self.build_modules()
if self.packages:
self.build_packages()
self.build_package_data()

# 2to3
self.run_2to3(self.updated_files)

# Remaining base class code
self.byte_compile(self.get_outputs(include_bytecode=0))

def build_module(self, module, module_file, package):
res = build_py.build_module(self, module, module_file, package)
if res[1]:
# file was copied
self.updated_files.append(res[0])
return res
10 changes: 1 addition & 9 deletions setuptools/_distutils/command/build_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from distutils import sysconfig
from distutils.core import Command
from distutils.dep_util import newer
from distutils.util import convert_path, Mixin2to3
from distutils.util import convert_path
from distutils import log
import tokenize

Expand Down Expand Up @@ -150,11 +150,3 @@ def copy_scripts(self):
os.chmod(file, newmode)
# XXX should we modify self.outfiles?
return outfiles, updated_files

class build_scripts_2to3(build_scripts, Mixin2to3):

def copy_scripts(self):
outfiles, updated_files = build_scripts.copy_scripts(self)
if not self.dry_run:
self.run_2to3(updated_files)
return outfiles, updated_files
81 changes: 0 additions & 81 deletions setuptools/_distutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,84 +533,3 @@ def rfc822_escape (header):
lines = header.split('\n')
sep = '\n' + 8 * ' '
return sep.join(lines)

# 2to3 support

def run_2to3(files, fixer_names=None, options=None, explicit=None):
"""Invoke 2to3 on a list of Python files.
The files should all come from the build area, as the
modification is done in-place. To reduce the build time,
only files modified since the last invocation of this
function should be passed in the files argument."""

if not files:
return

# Make this class local, to delay import of 2to3
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
class DistutilsRefactoringTool(RefactoringTool):
def log_error(self, msg, *args, **kw):
log.error(msg, *args)

def log_message(self, msg, *args):
log.info(msg, *args)

def log_debug(self, msg, *args):
log.debug(msg, *args)

if fixer_names is None:
fixer_names = get_fixers_from_package('lib2to3.fixes')
r = DistutilsRefactoringTool(fixer_names, options=options)
r.refactor(files, write=True)

def copydir_run_2to3(src, dest, template=None, fixer_names=None,
options=None, explicit=None):
"""Recursively copy a directory, only copying new and changed files,
running run_2to3 over all newly copied Python modules afterward.
If you give a template string, it's parsed like a MANIFEST.in.
"""
from distutils.dir_util import mkpath
from distutils.file_util import copy_file
from distutils.filelist import FileList
filelist = FileList()
curdir = os.getcwd()
os.chdir(src)
try:
filelist.findall()
finally:
os.chdir(curdir)
filelist.files[:] = filelist.allfiles
if template:
for line in template.splitlines():
line = line.strip()
if not line: continue
filelist.process_template_line(line)
copied = []
for filename in filelist.files:
outname = os.path.join(dest, filename)
mkpath(os.path.dirname(outname))
res = copy_file(os.path.join(src, filename), outname, update=1)
if res[1]: copied.append(outname)
run_2to3([fn for fn in copied if fn.lower().endswith('.py')],
fixer_names=fixer_names, options=options, explicit=explicit)
return copied

class Mixin2to3:
'''Mixin class for commands that run 2to3.
To configure 2to3, setup scripts may either change
the class variables, or inherit from individual commands
to override how 2to3 is invoked.'''

# provide list of fixers to run;
# defaults to all from lib2to3.fixers
fixer_names = None

# options dictionary
options = None

# list of fixers to invoke even though they are marked as explicit
explicit = None

def run_2to3(self, files):
return run_2to3(files, self.fixer_names, self.options, self.explicit)

0 comments on commit d989cdb

Please sign in to comment.