mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-13 14:24:18 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -2,18 +2,20 @@
|
|||
|
||||
Utility functions for operating on single files.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from distutils.errors import DistutilsFileError
|
||||
|
||||
from distutils import log
|
||||
from distutils.errors import DistutilsFileError
|
||||
|
||||
# for generating verbose output in 'copy_file()'
|
||||
_copy_action = { None: 'copying',
|
||||
'hard': 'hard linking',
|
||||
'sym': 'symbolically linking' }
|
||||
_copy_action = {None: 'copying',
|
||||
'hard': 'hard linking',
|
||||
'sym': 'symbolically linking', }
|
||||
|
||||
|
||||
def _copy_file_contents(src, dst, buffer_size=16*1024):
|
||||
def _copy_file_contents(src, dst, buffer_size=16 * 1024):
|
||||
"""Copy the file 'src' to 'dst'; both must be filenames. Any error
|
||||
opening either file, reading from 'src', or writing to 'dst', raises
|
||||
DistutilsFileError. Data is read/written in chunks of 'buffer_size'
|
||||
|
|
@ -28,27 +30,30 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
|
|||
try:
|
||||
fsrc = open(src, 'rb')
|
||||
except OSError as e:
|
||||
raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror))
|
||||
raise DistutilsFileError("could not open '{}': {}".format(src, e.strerror))
|
||||
|
||||
if os.path.exists(dst):
|
||||
try:
|
||||
os.unlink(dst)
|
||||
except OSError as e:
|
||||
raise DistutilsFileError(
|
||||
"could not delete '%s': %s" % (dst, e.strerror))
|
||||
"could not delete '{}': {}".format(dst, e.strerror),
|
||||
)
|
||||
|
||||
try:
|
||||
fdst = open(dst, 'wb')
|
||||
except OSError as e:
|
||||
raise DistutilsFileError(
|
||||
"could not create '%s': %s" % (dst, e.strerror))
|
||||
"could not create '{}': {}".format(dst, e.strerror),
|
||||
)
|
||||
|
||||
while True:
|
||||
try:
|
||||
buf = fsrc.read(buffer_size)
|
||||
except OSError as e:
|
||||
raise DistutilsFileError(
|
||||
"could not read from '%s': %s" % (src, e.strerror))
|
||||
"could not read from '{}': {}".format(src, e.strerror),
|
||||
)
|
||||
|
||||
if not buf:
|
||||
break
|
||||
|
|
@ -57,15 +62,19 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
|
|||
fdst.write(buf)
|
||||
except OSError as e:
|
||||
raise DistutilsFileError(
|
||||
"could not write to '%s': %s" % (dst, e.strerror))
|
||||
"could not write to '{}': {}".format(dst, e.strerror),
|
||||
)
|
||||
finally:
|
||||
if fdst:
|
||||
fdst.close()
|
||||
if fsrc:
|
||||
fsrc.close()
|
||||
|
||||
def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
||||
link=None, verbose=1, dry_run=0):
|
||||
|
||||
def copy_file(
|
||||
src, dst, preserve_mode=1, preserve_times=1, update=0,
|
||||
link=None, 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'
|
||||
|
|
@ -102,7 +111,8 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
|
||||
if not os.path.isfile(src):
|
||||
raise DistutilsFileError(
|
||||
"can't copy '%s': doesn't exist or not a regular file" % src)
|
||||
"can't copy '%s': doesn't exist or not a regular file" % src,
|
||||
)
|
||||
|
||||
if os.path.isdir(dst):
|
||||
dir = dst
|
||||
|
|
@ -112,7 +122,7 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
|
||||
if update and not newer(src, dst):
|
||||
if verbose >= 1:
|
||||
log.debug("not copying %s (output up-to-date)", src)
|
||||
log.debug('not copying %s (output up-to-date)', src)
|
||||
return (dst, 0)
|
||||
|
||||
try:
|
||||
|
|
@ -122,9 +132,9 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
|
||||
if verbose >= 1:
|
||||
if os.path.basename(dst) == os.path.basename(src):
|
||||
log.info("%s %s -> %s", action, src, dir)
|
||||
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)
|
||||
|
|
@ -163,10 +173,11 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
|
||||
|
||||
# XXX I suspect this is Unix-specific -- need porting help!
|
||||
def move_file (src, dst,
|
||||
verbose=1,
|
||||
dry_run=0):
|
||||
|
||||
def move_file(
|
||||
src, dst,
|
||||
verbose=1,
|
||||
dry_run=0,
|
||||
):
|
||||
"""Move a file 'src' to 'dst'. If 'dst' is a directory, the file will
|
||||
be moved into it with the same name; otherwise, 'src' is just renamed
|
||||
to 'dst'. Return the new full name of the file.
|
||||
|
|
@ -178,7 +189,7 @@ def move_file (src, dst,
|
|||
import errno
|
||||
|
||||
if verbose >= 1:
|
||||
log.info("moving %s -> %s", src, dst)
|
||||
log.info('moving %s -> %s', src, dst)
|
||||
|
||||
if dry_run:
|
||||
return dst
|
||||
|
|
@ -190,13 +201,15 @@ def move_file (src, dst,
|
|||
dst = os.path.join(dst, basename(src))
|
||||
elif exists(dst):
|
||||
raise DistutilsFileError(
|
||||
"can't move '%s': destination '%s' already exists" %
|
||||
(src, dst))
|
||||
"can't move '%s': destination '%s' already exists" %
|
||||
(src, dst),
|
||||
)
|
||||
|
||||
if not isdir(dirname(dst)):
|
||||
raise DistutilsFileError(
|
||||
"can't move '%s': destination '%s' not a valid path" %
|
||||
(src, dst))
|
||||
"can't move '%s': destination '%s' not a valid path" %
|
||||
(src, dst),
|
||||
)
|
||||
|
||||
copy_it = False
|
||||
try:
|
||||
|
|
@ -207,7 +220,8 @@ def move_file (src, dst,
|
|||
copy_it = True
|
||||
else:
|
||||
raise DistutilsFileError(
|
||||
"couldn't move '%s' to '%s': %s" % (src, dst, msg))
|
||||
"couldn't move '{}' to '{}': {}".format(src, dst, msg),
|
||||
)
|
||||
|
||||
if copy_it:
|
||||
copy_file(src, dst, verbose=verbose)
|
||||
|
|
@ -220,19 +234,20 @@ def move_file (src, dst,
|
|||
except OSError:
|
||||
pass
|
||||
raise DistutilsFileError(
|
||||
"couldn't move '%s' to '%s' by copy/delete: "
|
||||
"delete '%s' failed: %s"
|
||||
% (src, dst, src, msg))
|
||||
"couldn't move '%s' to '%s' by copy/delete: "
|
||||
"delete '%s' failed: %s"
|
||||
% (src, dst, src, msg),
|
||||
)
|
||||
return dst
|
||||
|
||||
|
||||
def write_file (filename, contents):
|
||||
def write_file(filename, contents):
|
||||
"""Create a file with the specified name and write 'contents' (a
|
||||
sequence of strings without line terminators) to it.
|
||||
"""
|
||||
f = open(filename, "w")
|
||||
f = open(filename, 'w')
|
||||
try:
|
||||
for line in contents:
|
||||
f.write(line + "\n")
|
||||
f.write(line + '\n')
|
||||
finally:
|
||||
f.close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue