def file_name_to_object_name(p): """Given a file path, construct its object name. Any relative or absolute directory markers at the beginning of the path will be stripped, for example:: ../../my_file -> my_file ./my_dir -> my_dir .hidden_dir/file -> .hidden_dir/file /absolute_dir -> absolute_dir Note that windows paths will have their back slashes changed to forward slashes:: C:\\my\\windows\\file -> my/windows/file Args: p (str): The input path Returns: PosixPath: The object name. An empty path will be returned in the case of the input path only consisting of absolute or relative directory markers (i.e. '/' -> '', './' -> '') """ from stor import Path from stor.posix import PosixPath p_parts = Path(p).expand().splitdrive()[1].split(os.path.sep) obj_start = next((i for i, part in enumerate(p_parts) if part not in ('', '..', '.')), None) return PosixPath.parts_class('/'.join(p_parts[obj_start:]) if obj_start is not None else '')
def download_objects(self, dest, objects): def is_parent_dir(possible_parent, possible_child): """Checks if possible_child is a sub-path of possible_parent""" if not possible_parent.resource: return possible_child.resource and \ possible_child.project == possible_parent.project return possible_child.startswith( utils.with_trailing_slash(possible_parent)) source = self if source == (self.drive + self.project): # need to convert dx://proj to dx://proj: source = DXPath(self + ':') for obj in objects: if utils.is_dx_path(obj) and not is_parent_dir( source, DXPath(obj)): raise ValueError('"%s" must be child of download path "%s"' % (obj, self)) # Convert requested download objects to full object paths objs_to_download = { obj: DXPath(obj) if utils.is_dx_path(obj) else source / obj for obj in objects } results = {} for obj, dx_obj in objs_to_download.items(): dest_resource = dx_obj[len(source):].lstrip('/') dest_obj = PosixPath(dest) / dest_resource dx_obj.copy(dest_obj) results[obj] = dest_obj return results
def test_walkfiles_posix(self, mock_walkfiles): mock_walkfiles.return_value = ['./a/b.txt', './c.txt', './d.txt'] self.parse_args('stor walkfiles -p=*.txt .') self.assertEquals(sys.stdout.getvalue(), './a/b.txt\n' './c.txt\n' './d.txt\n') mock_walkfiles.assert_called_once_with(PosixPath('.'), pattern='*.txt')