summaryrefslogtreecommitdiff
path: root/buildstream/storage/_filebaseddirectory.py
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2019-02-26 08:29:33 +0100
committerJürg Billeter <j@bitron.ch>2019-03-06 10:31:06 +0100
commit9db874cd2d615cd51557dbd2ac2c39269d681a99 (patch)
treed672a85e86649f60c7427cdc070c123278206dfb /buildstream/storage/_filebaseddirectory.py
parent3d12d707cf885935350a5fe31a5d13b2e26b1fcf (diff)
downloadbuildstream-9db874cd2d615cd51557dbd2ac2c39269d681a99.tar.gz
storage: Use variable-length argument list for Directory.descend()
This provides an API in line with, e.g., os.path.join(), and eliminates isinstance checks.
Diffstat (limited to 'buildstream/storage/_filebaseddirectory.py')
-rw-r--r--buildstream/storage/_filebaseddirectory.py53
1 files changed, 25 insertions, 28 deletions
diff --git a/buildstream/storage/_filebaseddirectory.py b/buildstream/storage/_filebaseddirectory.py
index 4b0fd917b..742200379 100644
--- a/buildstream/storage/_filebaseddirectory.py
+++ b/buildstream/storage/_filebaseddirectory.py
@@ -46,35 +46,32 @@ class FileBasedDirectory(Directory):
def __init__(self, external_directory=None):
self.external_directory = external_directory
- def descend(self, subdirectory_spec, create=False):
+ def descend(self, *paths, create=False):
""" See superclass Directory for arguments """
- # It's very common to send a directory name instead of a list and this causes
- # bizarre errors, so check for it here
- if not isinstance(subdirectory_spec, list):
- subdirectory_spec = [subdirectory_spec]
-
- # Because of the way split works, it's common to get a list which begins with
- # an empty string. Detect these and remove them.
- while subdirectory_spec and subdirectory_spec[0] == "":
- subdirectory_spec.pop(0)
-
- if not subdirectory_spec:
- return self
-
- new_path = os.path.join(self.external_directory, subdirectory_spec[0])
- try:
- st = os.lstat(new_path)
- if not stat.S_ISDIR(st.st_mode):
- raise VirtualDirectoryError("Cannot descend into '{}': '{}' is not a directory"
- .format(subdirectory_spec[0], new_path))
- except FileNotFoundError:
- if create:
- os.mkdir(new_path)
- else:
- raise VirtualDirectoryError("Cannot descend into '{}': '{}' does not exist"
- .format(subdirectory_spec[0], new_path))
- return FileBasedDirectory(new_path).descend(subdirectory_spec[1:], create)
+ current_dir = self
+
+ for path in paths:
+ # Skip empty path segments
+ if not path:
+ continue
+
+ new_path = os.path.join(current_dir.external_directory, path)
+ try:
+ st = os.lstat(new_path)
+ if not stat.S_ISDIR(st.st_mode):
+ raise VirtualDirectoryError("Cannot descend into '{}': '{}' is not a directory"
+ .format(path, new_path))
+ except FileNotFoundError:
+ if create:
+ os.mkdir(new_path)
+ else:
+ raise VirtualDirectoryError("Cannot descend into '{}': '{}' does not exist"
+ .format(path, new_path))
+
+ current_dir = FileBasedDirectory(new_path)
+
+ return current_dir
def import_files(self, external_pathspec, *,
filter_callback=None,
@@ -160,7 +157,7 @@ class FileBasedDirectory(Directory):
tf.addfile(tarinfo, f)
elif tarinfo.isdir():
tf.addfile(tarinfo)
- self.descend(filename.split(os.path.sep)).export_to_tar(tf, arcname, mtime)
+ self.descend(*filename.split(os.path.sep)).export_to_tar(tf, arcname, mtime)
else:
tf.addfile(tarinfo)