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 stat(path, metadata=None, conn=None): catalog = conn.stack.getCatalog() xstat = None try: xstat = catalog.extendedStat(path, True) except pydmlite.DmException as e: raise NotFoundException('File not found') obj_info = dict() base, name = common.split_path(path) obj_info['base'] = base obj_info['name'] = name if xstat.stat.isDir(): obj_info['type'] = DIR obj_info['children'] = xstat.stat.st_size else: obj_info['type'] = FILE obj_info['size'] = xstat.stat.st_size if metadata is not None: user_metadata = get_user_metadata(path, metadata) obj_info['user_metadata'] = user_metadata return obj_info
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, flask_json.dumps( {'name': x.name, 'path': x.path, 'metadata': _safe_stat(x.path, True) })), chain(nav_links, dir_gen))
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 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 current_app.logger.debug('# of sub collections: %d' % obj_handle.getLenSubCollections()) # -1 because irods counts the current dir as subdir, too obj_info['children'] = (obj_handle.getLenSubCollections() + obj_handle.getLenObjects() - 1) 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_dir_obj(path): try: dir_gen = storage.ls(path) except storage.NotFoundException as e: return e.msg, 404 except storage.NotAuthorizedException as e: return e.msg, 403 except storage.StorageException as e: return e.msg, 500 except storage.MalformedPathException as e: return e.msg, 400 dirlist, filelist = tee(dir_gen) return render_template( 'html/dirlisting.html', dirlist=dirlist, filelist=filelist, path=path, path_links=common.create_path_links(path), parent_path=common.add_trailing_slash(common.split_path(path)[0]))
def get_dir_obj(path): try: dir_gen = storage.ls(path) except storage.NotFoundException as e: return e.msg, 404 except storage.NotAuthorizedException as e: return e.msg, 403 except storage.StorageException as e: return e.msg, 500 except storage.MalformedPathException as e: return e.msg, 400 dirlist, filelist = tee(dir_gen) return render_template('html/dirlisting.html', dirlist=dirlist, filelist=filelist, path=path, path_links=common.create_path_links(path), parent_path=common.add_trailing_slash( common.split_path(path)[0]))
def __init__(self, url, objtype, objinfo, exists=True, parent_exists=True, objectid=None): self.url = url # this must not be included in add_prefix, # since the root is determined without the prefix if self.url == '/': self.is_root = True self.objtype = objtype self.objinfo = objinfo self.exists = exists self.parent_exists = parent_exists self.path = self.url self.parent_url, self.name = split_path(self.url) self.parent_url = add_trailing_slash(self.parent_url) if objectid is not None: self.objectid = objectid else: self.objectid = binascii.b2a_hex(create_object_id_no_ctx(20))
def _get_cdmi_json_generator(path, obj_type, **data): base_uri, obj_name = common.split_path(path) meta = metadata.stat(path, user_metadata=True) parent_uri = common.add_trailing_slash(base_uri) current_app.logger.debug('get stat for name, base: %s, %s' % (obj_name, base_uri)) parent_object_id = None try: parent_meta = metadata.get_user_metadata(base_uri, user_metadata=['objectID']) current_app.logger.debug('got parent meta: %s' % parent_meta) parent_object_id = parent_meta.get('objectID', None) except storage.NotFoundException: pass def get_range(range_max, range_tuple=(0, None)): if range_tuple is None: range_start, range_end = 0, None else: range_start, range_end = range_tuple if range_end is None or range_end > range_max: range_end = range_max yield flask_json.dumps('%s-%s' % (range_start, range_end)) def wrap_json_string(gen): yield '"' for part in gen: yield b64encode(part) yield '"' def json_list_gen(iterable, func): yield '[\n' for i, el in enumerate(iterable): if i > 0: yield ',\n "%s"' % func(el) else: yield ' "%s"' % func(el) yield '\n ]' def get_hex_object_id_or_none(meta): user_meta = meta.get('user_metadata', None) if user_meta is not None: obj_id = user_meta.get('objectID', None) if obj_id is not None: # obj_id is already stored in hex return obj_id return None yield ('objectType', lambda x=None: 'application/cdmi-%s' % obj_type) yield ('objectID', lambda x=None: get_hex_object_id_or_none(meta)) yield ('objectName', lambda x=None: obj_name) yield ('parentURI', lambda x=None: parent_uri) yield ('parentID', lambda x=None: parent_object_id) yield ('domainURI', lambda x=None: '/cdmi_domains/%s/' % get_config_parameter('CDMI_DOMAIN', None)) yield ('capabilitiesURI', lambda x=None: '/cdmi_capabilities/%s/' % ('dataobject' if obj_type == 'object' else obj_type)) yield ('completionStatus', lambda x=None: 'Complete') #'percentComplete': '%s', # optional yield ('metadata', lambda x=None: meta['user_metadata']) #'exports': {}, # optional #'snapshots': [], # optional if obj_type == 'container': yield ('childrenrange', partial(get_range, meta.get('children', None))) yield ('children', lambda t=(0, None): json_list_gen( islice(data['dir_listing'], t[0], t[1]), lambda x: x.name)) if obj_type == 'object': yield ('mimetype', lambda x=None: 'mime') yield ('valuerange', partial(get_range, meta['size'])) yield ('valuetransferencoding', lambda x=None: 'base64') yield ('value', lambda x=None: wrap_json_string(data['value_gen']))
def add_prefix(self, prefix): self.url = '%s%s' % (prefix, self.url) self.path = self.url self.parent_url, self.name = split_path(self.url) self.parent_url = add_trailing_slash(self.parent_url)