From 106561ef716efaddc33638d3d2cea477151085b9 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Fri, 31 May 2024 11:28:56 +0100 Subject: [PATCH] PERF: remove isdir() check in copy_file(), about 10% of the run time was checking if the file to copy is a directory. --- distutils/command/install_data.py | 6 ++++-- distutils/command/install_headers.py | 5 ++++- distutils/file_util.py | 16 +++------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/distutils/command/install_data.py b/distutils/command/install_data.py index b63a1af2..cc6ac697 100644 --- a/distutils/command/install_data.py +++ b/distutils/command/install_data.py @@ -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 @@ -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): diff --git a/distutils/command/install_headers.py b/distutils/command/install_headers.py index 085272c1..b7fb4b41 100644 --- a/distutils/command/install_headers.py +++ b/distutils/command/install_headers.py @@ -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 @@ -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): diff --git a/distutils/file_util.py b/distutils/file_util.py index 960def9c..e2239ac7 100644 --- a/distutils/file_util.py +++ b/distutils/file_util.py @@ -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 @@ -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) @@ -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)