Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 27 additions & 37 deletions distutils/archive_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def make_tarball(
base_dir: str | os.PathLike[str],
compress: Literal["gzip", "bzip2", "xz"] | None = "gzip",
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str:
Expand Down Expand Up @@ -96,7 +95,7 @@ def make_tarball(
archive_name = base_name + '.tar'
archive_name += compress_ext.get(compress, '')

mkpath(os.path.dirname(archive_name), dry_run=dry_run)
mkpath(os.path.dirname(archive_name))

# creating the tarball
import tarfile # late import so Python build itself doesn't break
Expand All @@ -115,21 +114,19 @@ def _set_uid_gid(tarinfo):
tarinfo.uname = owner
return tarinfo

if not dry_run:
tar = tarfile.open(archive_name, f'w|{tar_compression[compress]}')
try:
tar.add(base_dir, filter=_set_uid_gid)
finally:
tar.close()
tar = tarfile.open(archive_name, f'w|{tar_compression[compress]}')
try:
tar.add(base_dir, filter=_set_uid_gid)
finally:
tar.close()

return archive_name


def make_zipfile( # noqa: C901
def make_zipfile(
base_name: str,
base_dir: str | os.PathLike[str],
verbose: bool = False,
dry_run: bool = False,
) -> str:
"""Create a zip file from all the files under 'base_dir'.

Expand All @@ -140,7 +137,7 @@ def make_zipfile( # noqa: C901
file.
"""
zip_filename = base_name + ".zip"
mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
mkpath(os.path.dirname(zip_filename))

# If zipfile module is not available, try spawning an external
# 'zip' command.
Expand All @@ -151,7 +148,7 @@ def make_zipfile( # noqa: C901
zipoptions = "-rq"

try:
spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
spawn(["zip", zipoptions, zip_filename, base_dir])
except DistutilsExecError:
# XXX really should distinguish between "couldn't find
# external 'zip' command" and "zip failed".
Expand All @@ -164,29 +161,26 @@ def make_zipfile( # noqa: C901
else:
log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)

if not dry_run:
try:
zip = zipfile.ZipFile(
zip_filename, "w", compression=zipfile.ZIP_DEFLATED
)
except RuntimeError:
zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_STORED)

with zip:
if base_dir != os.curdir:
path = os.path.normpath(os.path.join(base_dir, ''))
try:
zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_DEFLATED)
except RuntimeError:
zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_STORED)

with zip:
if base_dir != os.curdir:
path = os.path.normpath(os.path.join(base_dir, ''))
zip.write(path, path)
log.info("adding '%s'", path)
for dirpath, dirnames, filenames in os.walk(base_dir):
for name in dirnames:
path = os.path.normpath(os.path.join(dirpath, name, ''))
zip.write(path, path)
log.info("adding '%s'", path)
for dirpath, dirnames, filenames in os.walk(base_dir):
for name in dirnames:
path = os.path.normpath(os.path.join(dirpath, name, ''))
for name in filenames:
path = os.path.normpath(os.path.join(dirpath, name))
if os.path.isfile(path):
zip.write(path, path)
log.info("adding '%s'", path)
for name in filenames:
path = os.path.normpath(os.path.join(dirpath, name))
if os.path.isfile(path):
zip.write(path, path)
log.info("adding '%s'", path)

return zip_filename

Expand Down Expand Up @@ -219,7 +213,6 @@ def make_archive(
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes] | None = None,
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str: ...
Expand All @@ -230,7 +223,6 @@ def make_archive(
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes],
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str: ...
Expand All @@ -240,7 +232,6 @@ def make_archive(
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes] | None = None,
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str:
Expand All @@ -264,13 +255,12 @@ def make_archive(
if root_dir is not None:
log.debug("changing into '%s'", root_dir)
base_name = os.path.abspath(base_name)
if not dry_run:
os.chdir(root_dir)
os.chdir(root_dir)

if base_dir is None:
base_dir = os.curdir

kwargs = {'dry_run': dry_run}
kwargs: dict[str, bool | None] = {}

try:
format_info = ARCHIVE_FORMATS[format]
Expand Down
31 changes: 6 additions & 25 deletions distutils/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ def __init__(self, dist: Distribution) -> None:

# Per-command versions of the global flags, so that the user can
# customize Distutils' behaviour command-by-command and let some
# commands fall back on the Distribution's behaviour. None means
# "not defined, check self.distribution's copy", while 0 or 1 mean
# false and true (duh). Note that this means figuring out the real
# value of each flag is a touch complicated -- hence "self._dry_run"
# will be handled by __getattr__, below.
# XXX This needs to be fixed.
self._dry_run = None
# commands fall back on the Distribution's behaviour. None means
# "not defined, check self.distribution's copy".

# verbose is largely ignored, but needs to be set for
# backwards compatibility (I think)?
Expand All @@ -119,17 +114,6 @@ def __init__(self, dist: Distribution) -> None:
# always calls 'finalize_options()', to respect/update it.
self.finalized = False

# XXX A more explicit way to customize dry_run would be better.
def __getattr__(self, attr):
if attr == 'dry_run':
myval = getattr(self, "_" + attr)
if myval is None:
return getattr(self.distribution, attr)
else:
return myval
else:
raise AttributeError(attr)

def ensure_finalized(self) -> None:
if not self.finalized:
self.finalize_options()
Expand Down Expand Up @@ -381,10 +365,10 @@ def execute(
msg: object = None,
level: int = 1,
) -> None:
util.execute(func, args, msg, dry_run=self.dry_run)
util.execute(func, args, msg)

def mkpath(self, name: str, mode: int = 0o777) -> None:
dir_util.mkpath(name, mode, dry_run=self.dry_run)
dir_util.mkpath(name, mode)

@overload
def copy_file(
Expand Down Expand Up @@ -425,7 +409,6 @@ def copy_file(
preserve_times,
not self.force,
link,
dry_run=self.dry_run,
)

def copy_tree(
Expand All @@ -447,7 +430,6 @@ def copy_tree(
preserve_times,
preserve_symlinks,
not self.force,
dry_run=self.dry_run,
)

@overload
Expand All @@ -465,15 +447,15 @@ def move_file(
level: int = 1,
) -> str | os.PathLike[str] | bytes | os.PathLike[bytes]:
"""Move a file respecting dry-run flag."""
return file_util.move_file(src, dst, dry_run=self.dry_run)
return file_util.move_file(src, dst)

def spawn(
self, cmd: MutableSequence[str], search_path: bool = True, level: int = 1
) -> None:
"""Spawn an external command respecting dry-run flag."""
from distutils.spawn import spawn

spawn(cmd, search_path, dry_run=self.dry_run)
spawn(cmd, search_path)

@overload
def make_archive(
Expand Down Expand Up @@ -509,7 +491,6 @@ def make_archive(
format,
root_dir,
base_dir,
dry_run=self.dry_run,
owner=owner,
group=group,
)
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/bdist_dumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ def run(self):
self.distribution.dist_files.append(('bdist_dumb', pyversion, filename))

if not self.keep_temp:
remove_tree(self.bdist_dir, dry_run=self.dry_run)
remove_tree(self.bdist_dir)
47 changes: 23 additions & 24 deletions distutils/command/bdist_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,30 +378,29 @@ def run(self) -> None: # noqa: C901

self.spawn(rpm_cmd)

if not self.dry_run:
if self.distribution.has_ext_modules():
pyversion = get_python_version()
else:
pyversion = 'any'

if not self.binary_only:
srpm = os.path.join(rpm_dir['SRPMS'], source_rpm)
assert os.path.exists(srpm)
self.move_file(srpm, self.dist_dir)
filename = os.path.join(self.dist_dir, source_rpm)
self.distribution.dist_files.append(('bdist_rpm', pyversion, filename))

if not self.source_only:
for rpm in binary_rpms:
rpm = os.path.join(rpm_dir['RPMS'], rpm)
if os.path.exists(rpm):
self.move_file(rpm, self.dist_dir)
filename = os.path.join(self.dist_dir, os.path.basename(rpm))
self.distribution.dist_files.append((
'bdist_rpm',
pyversion,
filename,
))
if self.distribution.has_ext_modules():
pyversion = get_python_version()
else:
pyversion = 'any'

if not self.binary_only:
srpm = os.path.join(rpm_dir['SRPMS'], source_rpm)
assert os.path.exists(srpm)
self.move_file(srpm, self.dist_dir)
filename = os.path.join(self.dist_dir, source_rpm)
self.distribution.dist_files.append(('bdist_rpm', pyversion, filename))

if not self.source_only:
for rpm in binary_rpms:
rpm = os.path.join(rpm_dir['RPMS'], rpm)
if os.path.exists(rpm):
self.move_file(rpm, self.dist_dir)
filename = os.path.join(self.dist_dir, os.path.basename(rpm))
self.distribution.dist_files.append((
'bdist_rpm',
pyversion,
filename,
))

def _dist_path(self, path):
return os.path.join(self.dist_dir, os.path.basename(path))
Expand Down
4 changes: 1 addition & 3 deletions distutils/command/build_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def run(self) -> None:
if not self.libraries:
return

self.compiler = new_compiler(
compiler=self.compiler, dry_run=self.dry_run, force=self.force
)
self.compiler = new_compiler(compiler=self.compiler, force=self.force)
customize_compiler(self.compiler)

if self.include_dirs is not None:
Expand Down
1 change: 0 additions & 1 deletion distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ def run(self) -> None: # noqa: C901
self.compiler = new_compiler(
compiler=self.compiler,
verbose=self.verbose,
dry_run=self.dry_run,
force=self.force,
)
customize_compiler(self.compiler)
Expand Down
5 changes: 1 addition & 4 deletions distutils/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,11 @@ def byte_compile(self, files) -> None:
# method of the "install_lib" command, except for the determination
# of the 'prefix' string. Hmmm.
if self.compile:
byte_compile(
files, optimize=0, force=self.force, prefix=prefix, dry_run=self.dry_run
)
byte_compile(files, optimize=0, force=self.force, prefix=prefix)
if self.optimize > 0:
byte_compile(
files,
optimize=self.optimize,
force=self.force,
prefix=prefix,
dry_run=self.dry_run,
)
36 changes: 13 additions & 23 deletions distutils/command/build_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,24 @@ def _copy_script(self, script, outfiles, updated_files):

# Always open the file, but ignore failures in dry-run mode
# in order to attempt to copy directly.
try:
f = tokenize.open(script)
except OSError:
if not self.dry_run:
raise
f = None
else:
first_line = f.readline()
if not first_line:
self.warn(f"{script} is an empty file (skipping)")
return
f = tokenize.open(script)

first_line = f.readline()
if not first_line:
self.warn(f"{script} is an empty file (skipping)")
return

shebang_match = shebang_pattern.match(first_line)
shebang_match = shebang_pattern.match(first_line)

updated_files.append(outfile)
if shebang_match:
log.info("copying and adjusting %s -> %s", script, self.build_dir)
if not self.dry_run:
post_interp = shebang_match.group(1) or ''
shebang = f"#!python{post_interp}\n"
self._validate_shebang(shebang, f.encoding)
with open(outfile, "w", encoding=f.encoding) as outf:
outf.write(shebang)
outf.writelines(f.readlines())
post_interp = shebang_match.group(1) or ''
shebang = f"#!python{post_interp}\n"
self._validate_shebang(shebang, f.encoding)
with open(outfile, "w", encoding=f.encoding) as outf:
outf.write(shebang)
outf.writelines(f.readlines())
if f:
f.close()
else:
Expand All @@ -126,10 +120,6 @@ def _change_modes(self, outfiles):
self._change_mode(file)

def _change_mode(self, file):
if self.dry_run:
log.info("changing mode of %s", file)
return

oldmode = os.stat(file)[ST_MODE] & 0o7777
newmode = (oldmode | 0o555) & 0o7777
if newmode != oldmode:
Expand Down
Loading
Loading