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 May 31, 2024
1 parent 4549de1 commit 6a54b19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
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

0 comments on commit 6a54b19

Please sign in to comment.