diff options
author | Jürg Billeter <j@bitron.ch> | 2020-04-22 09:34:07 +0200 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2020-04-27 08:32:32 +0000 |
commit | f48a4315442c7507c134bd3b1117674209361d5b (patch) | |
tree | 8a015fd8f256425ab32b22897d606b5183dbbe33 /src/buildstream | |
parent | c20e921158f033cf3efedfb9255867f6500d9e8d (diff) | |
download | buildstream-f48a4315442c7507c134bd3b1117674209361d5b.tar.gz |
directory.py: Add isfile(), isdir() and islink() methods
Diffstat (limited to 'src/buildstream')
-rw-r--r-- | src/buildstream/storage/directory.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/buildstream/storage/directory.py b/src/buildstream/storage/directory.py index 5d85cbe46..3bd80dc3e 100644 --- a/src/buildstream/storage/directory.py +++ b/src/buildstream/storage/directory.py @@ -33,6 +33,7 @@ See also: :ref:`sandboxing`. import os +import stat from typing import Callable, Optional, Union, List from .._exceptions import BstError @@ -221,6 +222,54 @@ class Directory: """ raise NotImplementedError() + def isfile(self, *paths: str, follow_symlinks: bool = False) -> bool: + """ Check whether the specified path is an existing regular file. + + Args: + *paths: A list of strings which are all path components. + follow_symlinks: True to follow symlinks. + + Returns: + True if the path is an existing regular file, False otherwise. + """ + try: + st = self.stat(*paths, follow_symlinks=follow_symlinks) + return stat.S_ISREG(st.st_mode) + except (VirtualDirectoryError, FileNotFoundError): + return False + + def isdir(self, *paths: str, follow_symlinks: bool = False) -> bool: + """ Check whether the specified path is an existing directory. + + Args: + *paths: A list of strings which are all path components. + follow_symlinks: True to follow symlinks. + + Returns: + True if the path is an existing directory, False otherwise. + """ + try: + st = self.stat(*paths, follow_symlinks=follow_symlinks) + return stat.S_ISDIR(st.st_mode) + except (VirtualDirectoryError, FileNotFoundError): + return False + + def islink(self, *paths: str, follow_symlinks: bool = False) -> bool: + """ Check whether the specified path is an existing symlink. + + Args: + *paths: A list of strings which are all path components. + follow_symlinks: True to follow symlinks. + + Returns: + True if the path is an existing symlink, False otherwise. + """ + try: + st = self.stat(*paths, follow_symlinks=follow_symlinks) + return stat.S_ISLNK(st.st_mode) + except (VirtualDirectoryError, FileNotFoundError): + return False + def open_file(self, *paths: str, mode: str = "r"): """ Open file and return a corresponding file object. In text mode, UTF-8 is used as encoding. |