[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -2,10 +2,11 @@
Utility functions for creating archive files (tarballs, zip files,
that sort of thing)."""
from __future__ import annotations
import os
from warnings import warn
import sys
from warnings import warn
try:
import zipfile
@ -28,6 +29,7 @@ try:
except ImportError:
getgrnam = None
def _get_gid(name):
"""Returns a gid, given a group name."""
if getgrnam is None or name is None:
@ -40,6 +42,7 @@ def _get_gid(name):
return result[2]
return None
def _get_uid(name):
"""Returns an uid, given a user name."""
if getpwnam is None or name is None:
@ -52,8 +55,11 @@ def _get_uid(name):
return result[2]
return None
def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
owner=None, group=None):
def make_tarball(
base_name, base_dir, compress='gzip', verbose=0, dry_run=0,
owner=None, group=None,
):
"""Create a (possibly compressed) tar file from all the files under
'base_dir'.
@ -69,16 +75,21 @@ def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
Returns the output filename.
"""
tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '',
'compress': ''}
compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz',
'compress': '.Z'}
tar_compression = {
'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '',
'compress': '',
}
compress_ext = {
'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz',
'compress': '.Z',
}
# flags for compression program, each element of list will be an argument
if compress is not None and compress not in compress_ext.keys():
raise ValueError(
"bad value for 'compress': must be None, 'gzip', 'bzip2', "
"'xz' or 'compress'")
"bad value for 'compress': must be None, 'gzip', 'bzip2', "
"'xz' or 'compress'",
)
archive_name = base_name + '.tar'
if compress != 'compress':
@ -124,6 +135,7 @@ def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
return archive_name
def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
"""Create a zip file from all the files under 'base_dir'.
@ -133,38 +145,50 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
available, raises DistutilsExecError. Returns the name of the output zip
file.
"""
zip_filename = base_name + ".zip"
zip_filename = base_name + '.zip'
mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
# If zipfile module is not available, try spawning an external
# 'zip' command.
if zipfile is None:
if verbose:
zipoptions = "-r"
zipoptions = '-r'
else:
zipoptions = "-rq"
zipoptions = '-rq'
try:
spawn(["zip", zipoptions, zip_filename, base_dir],
dry_run=dry_run)
spawn(
['zip', zipoptions, zip_filename, base_dir],
dry_run=dry_run,
)
except DistutilsExecError:
# XXX really should distinguish between "couldn't find
# external 'zip' command" and "zip failed".
raise DistutilsExecError(("unable to create zip file '%s': "
"could neither import the 'zipfile' module nor "
"find a standalone zip utility") % zip_filename)
raise DistutilsExecError(
(
"unable to create zip file '%s': "
"could neither import the 'zipfile' module nor "
'find a standalone zip utility'
) % zip_filename,
)
else:
log.info("creating '%s' and adding '%s' to it",
zip_filename, base_dir)
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)
zip = zipfile.ZipFile(
zip_filename, 'w',
compression=zipfile.ZIP_DEFLATED,
)
except RuntimeError:
zip = zipfile.ZipFile(zip_filename, "w",
compression=zipfile.ZIP_STORED)
zip = zipfile.ZipFile(
zip_filename, 'w',
compression=zipfile.ZIP_STORED,
)
with zip:
if base_dir != os.curdir:
@ -184,14 +208,16 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
return zip_filename
ARCHIVE_FORMATS = {
'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"),
'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
'zip': (make_zipfile, [],"ZIP file")
}
'ztar': (make_tarball, [('compress', 'compress')], 'compressed tar file'),
'tar': (make_tarball, [('compress', None)], 'uncompressed tar file'),
'zip': (make_zipfile, [], 'ZIP file'),
}
def check_archive_formats(formats):
"""Returns the first format from the 'format' list that is unknown.
@ -203,8 +229,11 @@ def check_archive_formats(formats):
return format
return None
def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0, owner=None, group=None):
def make_archive(
base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0, owner=None, group=None,
):
"""Create an archive file (eg. zip or tar).
'base_name' is the name of the file to create, minus any format-specific