summaryrefslogtreecommitdiff
path: root/src/buildstream/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildstream/utils.py')
-rw-r--r--src/buildstream/utils.py78
1 files changed, 17 insertions, 61 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index ec57b7f7f..1f16837df 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -250,9 +250,7 @@ def sha256sum(filename: str) -> str:
h.update(chunk)
except OSError as e:
- raise UtilError(
- "Failed to get a checksum of file '{}': {}".format(filename, e)
- ) from e
+ raise UtilError("Failed to get a checksum of file '{}': {}".format(filename, e)) from e
return h.hexdigest()
@@ -277,9 +275,7 @@ def safe_copy(src: str, dest: str, *, result: Optional[FileListResult] = None) -
os.unlink(dest)
except OSError as e:
if e.errno != errno.ENOENT:
- raise UtilError(
- "Failed to remove destination file '{}': {}".format(dest, e)
- ) from e
+ raise UtilError("Failed to remove destination file '{}': {}".format(dest, e)) from e
shutil.copyfile(src, dest)
try:
@@ -298,9 +294,7 @@ def safe_copy(src: str, dest: str, *, result: Optional[FileListResult] = None) -
raise UtilError("Failed to copy '{} -> {}': {}".format(src, dest, e)) from e
-def safe_link(
- src: str, dest: str, *, result: Optional[FileListResult] = None, _unlink=False
-) -> None:
+def safe_link(src: str, dest: str, *, result: Optional[FileListResult] = None, _unlink=False) -> None:
"""Try to create a hardlink, but resort to copying in the case of cross device links.
Args:
@@ -318,9 +312,7 @@ def safe_link(
os.unlink(dest)
except OSError as e:
if e.errno != errno.ENOENT:
- raise UtilError(
- "Failed to remove destination file '{}': {}".format(dest, e)
- ) from e
+ raise UtilError("Failed to remove destination file '{}': {}".format(dest, e)) from e
# If we can't link it due to cross-device hardlink, copy
try:
@@ -493,9 +485,7 @@ def get_host_tool(name: str) -> str:
program_path = shutil.which(name, path=search_path)
if not program_path:
- raise ProgramNotFoundError(
- "Did not find '{}' in PATH: {}".format(name, search_path)
- )
+ raise ProgramNotFoundError("Did not find '{}' in PATH: {}".format(name, search_path))
return program_path
@@ -532,12 +522,7 @@ def get_bst_version() -> Tuple[int, int]:
)
-def move_atomic(
- source: Union[Path, str],
- destination: Union[Path, str],
- *,
- ensure_parents: bool = True
-) -> None:
+def move_atomic(source: Union[Path, str], destination: Union[Path, str], *, ensure_parents: bool = True) -> None:
"""Move the source to the destination using atomic primitives.
This uses `os.rename` to move a file or directory to a new destination.
@@ -613,9 +598,7 @@ def save_file_atomic(
# This feature has been proposed for upstream Python in the past, e.g.:
# https://bugs.python.org/issue8604
- assert os.path.isabs(
- filename
- ), "The utils.save_file_atomic() parameter ``filename`` must be an absolute path"
+ assert os.path.isabs(filename), "The utils.save_file_atomic() parameter ``filename`` must be an absolute path"
if tempdir is None:
tempdir = os.path.dirname(filename)
fd, tempname = tempfile.mkstemp(dir=tempdir)
@@ -639,9 +622,7 @@ def save_file_atomic(
except FileNotFoundError:
pass
except OSError as e:
- raise UtilError(
- "Failed to cleanup temporary file {}: {}".format(tempname, e)
- ) from e
+ raise UtilError("Failed to cleanup temporary file {}: {}".format(tempname, e)) from e
try:
with _signals.terminator(cleanup_tempfile):
@@ -702,9 +683,7 @@ def _get_volume_size(path):
try:
usage = shutil.disk_usage(path)
except OSError as e:
- raise UtilError(
- "Failed to retrieve stats on volume for path '{}': {}".format(path, e)
- ) from e
+ raise UtilError("Failed to retrieve stats on volume for path '{}': {}".format(path, e)) from e
return usage.total, usage.free
@@ -794,11 +773,7 @@ def _force_rmtree(rootpath, **kwargs):
try:
os.chmod(path, 0o755)
except OSError as e:
- raise UtilError(
- "Failed to ensure write permission on file '{}': {}".format(
- path, e
- )
- )
+ raise UtilError("Failed to ensure write permission on file '{}': {}".format(path, e))
try:
shutil.rmtree(rootpath, **kwargs)
@@ -824,10 +799,7 @@ def _copy_directories(srcdir, destdir, target):
os.makedirs(new_dir)
yield (new_dir, mode)
else:
- raise UtilError(
- "Source directory tree has file where "
- "directory expected: {}".format(old_dir)
- )
+ raise UtilError("Source directory tree has file where " "directory expected: {}".format(old_dir))
else:
if not os.access(new_dir, os.W_OK):
# If the destination directory is not writable, change permissions to make it
@@ -862,9 +834,7 @@ def _ensure_real_directory(root, path):
else:
filetype = "special file"
- raise UtilError(
- "Destination is a {}, not a directory: {}".format(filetype, relpath)
- )
+ raise UtilError("Destination is a {}, not a directory: {}".format(filetype, relpath))
except FileNotFoundError:
os.makedirs(destpath)
@@ -886,13 +856,7 @@ def _ensure_real_directory(root, path):
#
#
def _process_list(
- srcdir,
- destdir,
- actionfunc,
- result,
- filter_callback=None,
- ignore_missing=False,
- report_written=False,
+ srcdir, destdir, actionfunc, result, filter_callback=None, ignore_missing=False, report_written=False,
):
# Keep track of directory permissions, since these need to be set
@@ -976,9 +940,7 @@ def _process_list(
else:
# Unsupported type.
- raise UtilError(
- "Cannot extract {} into staging-area. Unsupported type.".format(srcpath)
- )
+ raise UtilError("Cannot extract {} into staging-area. Unsupported type.".format(srcpath))
# Write directory permissions now that all files have been written
for d, perms in permissions:
@@ -1085,9 +1047,7 @@ def _tempdir(suffix="", prefix="tmp", dir=None): # pylint: disable=redefined-bu
# on SIGTERM.
#
@contextmanager
-def _tempnamedfile(
- suffix="", prefix="tmp", dir=None
-): # pylint: disable=redefined-builtin
+def _tempnamedfile(suffix="", prefix="tmp", dir=None): # pylint: disable=redefined-builtin
temp = None
def close_tempfile():
@@ -1261,9 +1221,7 @@ def _call(*popenargs, terminate=False, **kwargs):
group_id = os.getpgid(process.pid)
os.killpg(group_id, signal.SIGCONT)
- with _signals.suspendable(suspend_proc, resume_proc), _signals.terminator(
- kill_proc
- ):
+ with _signals.suspendable(suspend_proc, resume_proc), _signals.terminator(kill_proc):
process = subprocess.Popen( # pylint: disable=subprocess-popen-preexec-fn
*popenargs, preexec_fn=preexec_fn, universal_newlines=True, **kwargs
)
@@ -1469,9 +1427,7 @@ def _get_compression(tar):
if suffix == ".tar":
raise UtilError(
"Expected compression with unknown file extension ('{}'), "
- "supported extensions are ('.tar'), ('.gz'), ('.xz'), ('.bz2')".format(
- ext
- )
+ "supported extensions are ('.tar'), ('.gz'), ('.xz'), ('.bz2')".format(ext)
)
# Assume just an unconventional name was provided, default to uncompressed