def _collect_actions_existing_dest(self, source, dest): """ _collect_actions() helper to collect required actions to perform a stow command when the destination already exists """ if utils.is_same_file(dest, source): if dest.is_symlink() or self.is_unfolding: self._are_same_file(source, dest) else: self.errors.add( error.SourceIsSameAsDest(self.subcmd, dest.parent)) elif dest.is_dir() and source.is_dir: self._are_directories(source, dest) else: self.errors.add( error.ConflictsWithExistingFile(self.subcmd, source, dest))
def should_ignore(self, source): """ check if a source should be ignored, based on the ignore patterns in self.patterns This checks if the ignore patterns match either the file exactly or its parents """ for pattern in self.patterns: try: files = sorted(source.parent.glob(pattern)) except IndexError: # the glob result was empty continue for file in files: if utils.is_same_file(file, source) or source in file.parents: return True return False
def _collect_folding_actions(self): """ find candidates for folding i.e. when a directory contains symlinks to files that all share the same parent directory """ for parent in self.actions.get_unlink_target_parents(): items = utils.get_directory_contents(parent) other_links_parents = [] other_links = [] source_parent = None is_normal_files_detected = False for item in items: if item not in self.actions.get_unlink_targets(): does_item_exist = False try: does_item_exist = item.exists() except PermissionError: self.errors.add( error.PermissionDenied(self.subcmd, item)) return if does_item_exist and item.is_symlink(): source_parent = item.resolve().parent other_links_parents.append(item.resolve().parent) other_links.append(item) else: is_normal_files_detected = True break if not is_normal_files_detected: other_links_parent_count = len(Counter(other_links_parents)) if other_links_parent_count == 1: assert source_parent is not None if utils.is_same_files( utils.get_directory_contents(source_parent), other_links): self._fold(source_parent, parent) elif (other_links_parent_count == 0 and not utils.is_same_file(parent, self.dest_input)): self.actions.add( actions.RemoveDirectory(self.subcmd, parent))
def _collect_actions(self, source, dest): """ Concrete method to collect required actions to perform a link sub-command """ if dest.exists(): if utils.is_same_file(dest, source): self.actions.add( actions.AlreadyLinked(self.subcmd, source, dest)) else: self.errors.add( error.ConflictsWithExistingFile(self.subcmd, source, dest)) elif dest.is_symlink(): self.errors.add( error.ConflictsWithExistingLink(self.subcmd, source, dest)) elif not dest.parent.exists(): self.errors.add( error.NoSuchDirectoryToSubcmdInto(self.subcmd, dest.parent)) else: self.actions.add(actions.SymbolicLink(self.subcmd, source, dest))