diff options
author | Abderrahim Kitouni <akitouni@gnome.org> | 2020-07-16 11:30:24 +0100 |
---|---|---|
committer | Abderrahim Kitouni <akitouni@gnome.org> | 2020-08-26 12:47:05 +0100 |
commit | ae6c12b74bd693e7f26b25db950e989c543d099f (patch) | |
tree | 55f8a4fd1605e2796cc7889070c844c814f81237 | |
parent | 7eed6826d8c578d2cdc4c7d8d7e8386a03473e4e (diff) | |
download | buildstream-ae6c12b74bd693e7f26b25db950e989c543d099f.tar.gz |
_casbaseddirectory.py: Replace isinstance() with _FileType checks
Using isinstance() with concrete classes should typically be avoided,
and it's also more expensive than necessary.
Based on f6176579f122d370c4ecaba0df1512cea079e0f0
-rw-r--r-- | buildstream/storage/_casbaseddirectory.py | 63 |
1 files changed, 30 insertions, 33 deletions
diff --git a/buildstream/storage/_casbaseddirectory.py b/buildstream/storage/_casbaseddirectory.py index 039b00f67..b6199303b 100644 --- a/buildstream/storage/_casbaseddirectory.py +++ b/buildstream/storage/_casbaseddirectory.py @@ -126,13 +126,13 @@ class _Resolver(): if name not in directory.index: return None index_entry = directory.index[name] - if isinstance(index_entry.buildstream_object, Directory): - return index_entry.buildstream_object - elif isinstance(index_entry.pb_object, remote_execution_pb2.FileNode): + if index_entry.type == _FileType.DIRECTORY: + return index_entry.get_directory(directory) + elif index_entry.type == _FileType.REGULAR_FILE: return index_entry.pb_object # Now we must be dealing with a symlink. - assert isinstance(index_entry.pb_object, remote_execution_pb2.SymlinkNode) + assert index_entry.type == _FileType.SYMLINK symlink_object = index_entry.pb_object if symlink_object in self.seen_objects: @@ -348,16 +348,15 @@ class CasBasedDirectory(Directory): self._add_new_link_direct(filename, os.readlink(os.path.join(basename, filename))) def _add_new_link_direct(self, name, target): - existing_link = self._find_pb2_entry(name) - if existing_link: - symlinknode = existing_link + entry = self.index.get(name) + if entry: + symlinknode = entry.pb_object else: symlinknode = self.pb2_directory.symlinks.add() - assert isinstance(symlinknode, remote_execution_pb2.SymlinkNode) symlinknode.name = name # A symlink node has no digest. symlinknode.target = target - self.index[name] = IndexEntry(symlinknode, _FileType.SYMLINK, modified=(existing_link is not None)) + self.index[name] = IndexEntry(symlinknode, _FileType.SYMLINK, modified=(entry is not None)) def delete_entry(self, name): for collection in [self.pb2_directory.files, self.pb2_directory.symlinks, self.pb2_directory.directories]: @@ -400,9 +399,10 @@ class CasBasedDirectory(Directory): return self if subdirectory_spec[0] in self.index: - entry = self.index[subdirectory_spec[0]].buildstream_object - if isinstance(entry, CasBasedDirectory): - return entry.descend(subdirectory_spec[1:], create) + entry = self.index[subdirectory_spec[0]] + if entry.type == _FileType.DIRECTORY: + subdir = entry.buildstream_object + return subdir.descend(subdirectory_spec[1:], create) else: # May be a symlink target = self._resolve(subdirectory_spec[0], force_create=create) @@ -441,16 +441,11 @@ class CasBasedDirectory(Directory): Returns 'True' if the import should go ahead. fileListResult.overwritten and fileListResult.ignore are updated depending on the result. """ - existing_entry = self._find_pb2_entry(name) + existing_entry = self.index.get(name) relative_pathname = os.path.join(path_prefix, name) if existing_entry is None: return True - if (isinstance(existing_entry, - (remote_execution_pb2.FileNode, remote_execution_pb2.SymlinkNode))): - self.delete_entry(name) - fileListResult.overwritten.append(relative_pathname) - return True - elif isinstance(existing_entry, remote_execution_pb2.DirectoryNode): + elif existing_entry.type == _FileType.DIRECTORY: # If 'name' maps to a DirectoryNode, then there must be an entry in index # pointing to another Directory. if self.index[name].buildstream_object.is_empty(): @@ -461,9 +456,10 @@ class CasBasedDirectory(Directory): # We can't overwrite a non-empty directory, so we just ignore it. fileListResult.ignored.append(relative_pathname) return False - assert False, ("Entry '{}' is not a recognised file/link/directory and not None; it is {}" - .format(name, type(existing_entry))) - return False # In case asserts are disabled + else: + self.delete_entry(name) + fileListResult.overwritten.append(relative_pathname) + return True def _replace_anything_with_dir(self, name, path_prefix, overwritten_files_list): self.delete_entry(name) @@ -578,7 +574,7 @@ class CasBasedDirectory(Directory): file_list_required=file_list_required) result.combine(import_result) processed_directories.add(dirname) - elif isinstance(source_directory.index[f].buildstream_object, CasBasedDirectory): + elif source_directory.index[f].type == _FileType.DIRECTORY: # The thing in the input file list is a directory on # its own. We don't need to do anything other than create it if it doesn't exist. # If we already have an entry with the same name that isn't a directory, that @@ -589,13 +585,14 @@ class CasBasedDirectory(Directory): # We're importing a file or symlink - replace anything with the same name. importable = self._check_replacement(f, path_prefix, result) if importable: - item = source_directory.index[f].pb_object - if isinstance(item, remote_execution_pb2.FileNode): + entry = source_directory.index[f] + item = entry.pb_object + if entry.type == _FileType.REGULAR_FILE: filenode = self.pb2_directory.files.add(digest=item.digest, name=f, is_executable=item.is_executable) self.index[f] = IndexEntry(filenode, _FileType.REGULAR_FILE, modified=True) else: - assert isinstance(item, remote_execution_pb2.SymlinkNode) + assert entry.type == _FileType.SYMLINK self._add_new_link_direct(name=f, target=item.target) result.files_written.append(os.path.join(path_prefix, f)) else: @@ -735,7 +732,7 @@ class CasBasedDirectory(Directory): # Marks all entries in this directory and all child directories as unmodified. for i in self.index.values(): i.modified = False - if isinstance(i.buildstream_object, CasBasedDirectory): + if i.type == _FileType.DIRECTORY: i.buildstream_object._mark_directory_unmodified() def _mark_entry_unmodified(self, name): @@ -771,7 +768,7 @@ class CasBasedDirectory(Directory): for component in path_components[:-1]: if component not in directory.index: return None - if isinstance(directory.index[component].buildstream_object, CasBasedDirectory): + if directory.index[component].type == _FileType.DIRECTORY: directory = directory.index[component].buildstream_object else: return None @@ -795,11 +792,11 @@ class CasBasedDirectory(Directory): Return value: List(str) - list of all paths """ - symlink_list = filter(lambda i: isinstance(i[1].pb_object, remote_execution_pb2.SymlinkNode), + symlink_list = filter(lambda i: i[1].type == _FileType.SYMLINK, self.index.items()) - file_list = list(filter(lambda i: isinstance(i[1].pb_object, remote_execution_pb2.FileNode), + file_list = list(filter(lambda i: i[1].type == _FileType.REGULAR_FILE, self.index.items())) - directory_list = filter(lambda i: isinstance(i[1].buildstream_object, CasBasedDirectory), + directory_list = filter(lambda i: i[1].type == _FileType.DIRECTORY, self.index.items()) # We need to mimic the behaviour of os.walk, in which symlinks @@ -825,9 +822,9 @@ class CasBasedDirectory(Directory): def get_size(self): total = len(self.pb2_directory.SerializeToString()) for i in self.index.values(): - if isinstance(i.buildstream_object, CasBasedDirectory): + if i.type == _FileType.DIRECTORY: total += i.buildstream_object.get_size() - elif isinstance(i.pb_object, remote_execution_pb2.FileNode): + elif i.type == _FileType.REGULAR_FILE: src_name = self.cas_cache.objpath(i.pb_object.digest) filesize = os.stat(src_name).st_size total += filesize |