def mkdir(path): """Create a directory.""" conn = get_storage() if conn is None: return None dirname, basename = common.split_path(path) coll = irodsCollection(conn) coll.openCollection(dirname) # see ls() if coll.getId() < 0: raise NotFoundException('Path does not exist or is not a directory: %s' % (coll.getCollName())) err = coll.createCollection(basename) if err != 0: if err == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME: raise ConflictException('Target already exists: %s' % (path)) elif err == CAT_INSUFFICIENT_PRIVILEGE_LEVEL: raise NotAuthorizedException('Target creation not allowed: %s' % (path)) else: raise StorageException('Unknown storage exception: %s: %s' % (path, __getErrorName(err))) return True, ''
def rmdir(path): """Delete a directory. Be careful: it also deletes subdirectories without asking. """ conn = get_storage() if conn is None: return None dirname, basename = common.split_path(path) coll = irodsCollection(conn) coll.openCollection(dirname) # see ls() if coll.getId() < 0: raise NotFoundException('Path does not exist or is not a directory: %s' % (coll.getCollName())) err = coll.deleteCollection(basename) if err != 0: if err == USER_FILE_DOES_NOT_EXIST: raise NotFoundException( 'Path does not exist or is not a directory: %s' % (path)) elif err == CAT_INSUFFICIENT_PRIVILEGE_LEVEL: raise NotAuthorizedException('Target creation not allowed: %s' % (path)) else: raise StorageException('Unknown storage exception: %s: %s' % (path, __getErrorName(err))) return True, ''
def rmdir(path, force=False, conn=None): """Delete a directory. Be careful: it also deletes subdirectories without asking. """ if conn is None: return None objinfo = stat(path) if not objinfo['type'] == DIR: raise IsFileException('Path is a file') if not force and objinfo['children'] > 0: raise ConflictException('Directory is not empty') dirname, basename = common.split_path(path) coll = irodsCollection(conn) coll.openCollection(dirname) err = coll.deleteCollection(basename) if err != 0: _handle_irodserror(path, err) return True, ''
def _create_dirlist_gen(dir_gen, path): """Returns a list with the directory entries.""" nav_links = [storage.StorageDir('.', path), storage.StorageDir('..', common.split_path(path)[0])] return imap(lambda x: (x.name, json.dumps( {'name': x.name, 'path': x.path, 'metadata': _safe_stat(x.path, True) })), chain(nav_links, dir_gen))
def create_dirlist_dict(dir_list, path): """Returns a dictionary with the directory entries.""" def make_abs_link(name, path): return urljoin(path, name) nav_links = [storage.StorageDir('.', path), storage.StorageDir('..', common.split_path(path)[0])] return map(lambda x: {'name': x.name, 'path': x.path, 'metadata': metadata.stat(x.path, True)}, nav_links + dir_list)
def mkdir(path, conn=None): """Create a directory.""" if conn is None: return None dirname, basename = common.split_path(path) coll = irodsCollection(conn) coll.openCollection(dirname) err = coll.createCollection(basename) if err != 0: _handle_irodserror(path, err) return True, ''
def create_dirlist_dict(dir_list, path): """Returns a dictionary with the directory entries.""" def make_abs_link(name, path): return urljoin(path, name) nav_links = [ storage.StorageDir('.', path), storage.StorageDir('..', common.split_path(path)[0]) ] return map( lambda x: { 'name': x.name, 'path': x.path, 'metadata': metadata.stat(x.path, True) }, nav_links + dir_list)
def stat(path, metadata=None, conn=None): """Return detailed information about the object. The metadata argument specifies if and which user-specified metadata should be read. For this, we first have to check if we're reading a dir or a file to ask to the right information. For the future, there should be a standard what stat() returns. """ if conn is None: return None obj_info = dict() obj_handle, path_is_dir = _get_irods_obj_handle(conn, path) base, name = common.split_path(path) obj_info['base'] = base obj_info['name'] = name if path_is_dir: obj_info['type'] = DIR # -1 because irods counts the current dir obj_info['children'] = (obj_handle.getLenSubCollections() - 1 + obj_handle.getLenObjects()) obj_info['ID'] = obj_handle.getId() else: obj_info['type'] = FILE obj_info['size'] = obj_handle.getSize() obj_info['resc'] = obj_handle.getResourceName() obj_info['repl_num'] = obj_handle.getReplNumber() if metadata is not None: user_metadata = _get_user_metadata(conn, obj_handle, path, metadata) obj_info['user_metadata'] = user_metadata try: _close(obj_handle) except AttributeError: pass # obj is a collection, which cannot be closed return obj_info
def get_cdmi_dir_obj(path): """Get a directory entry through CDMI. Get the listing of a directory. TODO: find a way to stream the listing. """ cdmi_filters = [] if request_wants(ContentTypes.cdmi_object): try: cdmi_filters = _get_cdmi_filters(request.args) except MalformedArgumentValueException as e: return e.msg, 400 try: dir_gen = storage.ls(path) except storage.NotFoundException as e: return e.msg, 404 except storage.NotAuthorizedException as e: return e.msg, 401 except storage.StorageException as e: return e.msg, 500 except storage.MalformedPathException as e: return e.msg, 400 if request_wants(ContentTypes.cdmi_object): cdmi_json_gen = _get_cdmi_json_dir_generator(path, dir_gen) if cdmi_filters: filtered_gen = ((a, b(cdmi_filters[a])) for a, b in cdmi_json_gen if a in cdmi_filters) else: filtered_gen = ((a, b()) for a, b in cdmi_json_gen) json_stream_wrapper = _wrap_with_json_generator(filtered_gen) return Response(stream_with_context(json_stream_wrapper)) elif request_wants(ContentTypes.json): dir_gen_wrapper = _create_dirlist_gen(dir_gen, path) json_stream_wrapper = _wrap_with_json_generator(dir_gen_wrapper) return Response(stream_with_context(json_stream_wrapper)) else: return render_template('dirlisting.html', dirlist=dir_gen, path=path, path_links=create_path_links(path), parent_path=common.split_path(path)[0])
def stat(path, metadata=None): """Return detailed information about the object. The metadata argument specifies if and which user-specified metadata should be read. For this, we first have to check if we're reading a dir or a file to ask to the right information. For the future, there should be a standard what stat() returns. """ conn = get_storage() if conn is None: return None obj_info = dict() obj_handle, path_is_dir = __get_irods_obj_handle(conn, path) base, name = common.split_path(path) obj_info['base'] = base obj_info['name'] = name if path_is_dir: obj_info['children'] = (obj_handle.getLenSubCollections() + obj_handle.getLenObjects()) obj_info['ID'] = obj_handle.getId() else: obj_info['size'] = obj_handle.getSize() obj_info['resc'] = obj_handle.getResourceName() obj_info['repl_num'] = obj_handle.getReplNumber() if metadata is not None: user_metadata = __get_user_metadata(conn, obj_handle, path, metadata) obj_info['user_metadata'] = user_metadata try: obj_handle.close() except AttributeError: pass # obj is a collection, which cannot be closed return obj_info
def get_cdmi_dir_obj(path): """Get a directory entry through CDMI. Get the listing of a directory. TODO: find a way to stream the listing. """ cdmi_filters = [] if request_wants_cdmi_object(): try: cdmi_filters = get_cdmi_filters(request.args) except MalformedArgumentValueException as e: return e.msg, 400 try: dir_list = [x for x in storage.ls(path)] except storage.NotFoundException as e: return e.msg, 404 except storage.NotAuthorizedException as e: return e.msg, 401 except storage.StorageException as e: return e.msg, 500 if request_wants_cdmi_object(): cdmi_json_gen = get_cdmi_json_dir_generator(path, dir_list) if cdmi_filters: filtered_gen = ((a, b(cdmi_filters[a])) for a, b in cdmi_json_gen if a in cdmi_filters) else: filtered_gen = ((a, b()) for a, b in cdmi_json_gen) json_stream_wrapper = wrap_with_json_generator(filtered_gen) return Response(stream_with_context(json_stream_wrapper)) elif request_wants_json(): return flask_jsonify(dirlist=create_dirlist_dict(dir_list, path)) else: return render_template('dirlisting.html', dirlist=dir_list, path=path, parent_path=common.split_path(path)[0])