diff options
Diffstat (limited to 'lib/spack/llnl/util/filesystem.py')
-rw-r--r-- | lib/spack/llnl/util/filesystem.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index bee6e1e43c..5e08273677 100644 --- a/lib/spack/llnl/util/filesystem.py +++ b/lib/spack/llnl/util/filesystem.py @@ -2,7 +2,6 @@ # Spack Project Developers. See the top-level COPYRIGHT file for details. # # SPDX-License-Identifier: (Apache-2.0 OR MIT) - import collections import errno import hashlib @@ -377,17 +376,17 @@ def install(src, dest): copy(src, dest, _permissions=True) -def resolve_link_target_relative_to_the_link(l): +def resolve_link_target_relative_to_the_link(link): """ os.path.isdir uses os.path.exists, which for links will check the existence of the link target. If the link target is relative to the link, we need to construct a pathname that is valid from our cwd (which may not be the same as the link's directory) """ - target = os.readlink(l) + target = os.readlink(link) if os.path.isabs(target): return target - link_dir = os.path.dirname(os.path.abspath(l)) + link_dir = os.path.dirname(os.path.abspath(link)) return os.path.join(link_dir, target) @@ -1571,6 +1570,19 @@ def can_access_dir(path): @memoized +def can_write_to_dir(path): + """Return True if the argument is a directory in which we can write. + + Args: + path: path to be tested + + Returns: + True if ``path`` is an writeable directory, else False + """ + return os.path.isdir(path) and os.access(path, os.R_OK | os.X_OK | os.W_OK) + + +@memoized def files_in(*search_paths): """Returns all the files in paths passed as arguments. @@ -1683,3 +1695,18 @@ def prefixes(path): pass return paths + + +def md5sum(file): + """Compute the MD5 sum of a file. + + Args: + file (str): file to be checksummed + + Returns: + MD5 sum of the file's content + """ + md5 = hashlib.md5() + with open(file, "rb") as f: + md5.update(f.read()) + return md5.digest() |