Skip to content

Commit

Permalink
PERF: remove isdir() check in copy_file(), about 10% of the run time …
Browse files Browse the repository at this point in the history
…was checking if the file to copy is a directory.
  • Loading branch information
rmmancom committed Jun 20, 2024
1 parent a37185d commit 745640e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
6 changes: 4 additions & 2 deletions distutils/command/bdist_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,13 @@ def run(self): # noqa: C901

source = sdist.get_archive_files()[0]
source_dir = rpm_dir['SOURCES']
self.copy_file(source, source_dir)
dest = os.path.join(source_dir, os.path.basename(source))
self.copy_file(source, dest)

if self.icon:
if os.path.exists(self.icon):
self.copy_file(self.icon, source_dir)
dest = os.path.join(source_dir, os.path.basename(self.icon))
self.copy_file(self.icon, dest)
else:
raise DistutilsFileError("icon file '%s' does not exist" % self.icon)

Expand Down
6 changes: 4 additions & 2 deletions distutils/command/install_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def run(self):
"setup script did not provide a directory for "
f"'{f}' -- installing right in '{self.install_dir}'"
)
(out, _) = self.copy_file(f, self.install_dir)
dst = os.path.join(self.install_dir, os.path.basename(f))
(out, _) = self.copy_file(f, dst)
self.outfiles.append(out)
else:
# it's a tuple with path to install to and a list of files
Expand All @@ -74,7 +75,8 @@ def run(self):
# Copy files, adding them to the list of output files.
for data in f[1]:
data = convert_path(data)
(out, _) = self.copy_file(data, dir)
dst = os.path.join(dir, os.path.basename(data))
(out, _) = self.copy_file(data, dst)
self.outfiles.append(out)

def get_inputs(self):
Expand Down
5 changes: 4 additions & 1 deletion distutils/command/install_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Implements the Distutils 'install_headers' command, to install C/C++ header
files to the Python include directory."""

import os

from ..core import Command


Expand Down Expand Up @@ -34,7 +36,8 @@ def run(self):

self.mkpath(self.install_dir)
for header in headers:
(out, _) = self.copy_file(header, self.install_dir)
dst = os.path.join(self.install_dir, os.path.basename(header))
(out, _) = self.copy_file(header, dst)
self.outfiles.append(out)

def get_inputs(self):
Expand Down
16 changes: 3 additions & 13 deletions distutils/file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ def copy_file( # noqa: C901
verbose=1,
dry_run=0,
):
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is
copied there with the same name; otherwise, it must be a filename. (If
the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
"""Copy a file 'src' to 'dst'.
(If the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
is true (the default), the file's mode (type and permission bits, or
whatever is analogous on the current platform) is copied. If
'preserve_times' is true (the default), the last-modified and
Expand Down Expand Up @@ -109,12 +108,6 @@ def copy_file( # noqa: C901
"can't copy '%s': doesn't exist or not a regular file" % src
)

if os.path.isdir(dst):
dir = dst
dst = os.path.join(dst, os.path.basename(src))
else:
dir = os.path.dirname(dst)

if update and not newer(src, dst):
if verbose >= 1:
log.debug("not copying %s (output up-to-date)", src)
Expand All @@ -126,10 +119,7 @@ def copy_file( # noqa: C901
raise ValueError("invalid value '%s' for 'link' argument" % link)

if verbose >= 1:
if os.path.basename(dst) == os.path.basename(src):
log.info("%s %s -> %s", action, src, dir)
else:
log.info("%s %s -> %s", action, src, dst)
log.info("%s %s -> %s", action, src, dst)

if dry_run:
return (dst, 1)
Expand Down
3 changes: 2 additions & 1 deletion distutils/tests/test_dir_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def test_copy_tree_verbosity(self, caplog):

mkpath(self.target, verbose=0)
a_file = path.Path(self.target) / 'ok.txt'
to_file = path.Path(self.target2) / 'ok.txt'
jaraco.path.build({'ok.txt': 'some content'}, self.target)

wanted = [f'copying {a_file} -> {self.target2}']
wanted = [f'copying {a_file} -> {to_file}']
copy_tree(self.target, self.target2, verbose=1)
assert caplog.messages == wanted

Expand Down

0 comments on commit 745640e

Please sign in to comment.