def __init__(self, entry): self.entry = entry self.is_root = (self.entry.name == "." and self.entry.container == '/') # Get name if self.is_root: self.name = u"Home" else: _, self.name = split(self.entry.container) self.path = self.entry.container self.container, _ = split(self.path) self.uuid = self.entry.uuid self.create_ts = self.entry.container_create_ts
def put_data_object(self, path): # Check if a collection with the name exists collection = Collection.find(path) if collection: # Try to put a data_object when a collection of the same name # already exists self.logger.info(u"Impossible to create a new resource, the collection '{}' already exists, try to update it".format(path)) return self.put_container(path) parent, name = split(path) # Check if the resource already exists resource = Resource.find(path) # Check permissions if resource: # Update Resource if not resource.user_can(self.user, "edit"): self.logger.warning(u"User {} tried to modify resource at '{}'".format(self.user, path)) return Response(status=HTTP_403_FORBIDDEN) else: # Create Resource parent_collection = Collection.find(parent) if not parent_collection: self.logger.info(u"Fail to create a resource at '{}', collection doesn't exist".format(path)) return Response(status=HTTP_404_NOT_FOUND) # Check if user can create a new resource in the collection if not parent_collection.user_can(self.user, "write"): self.logger.warning(u"User {} tried to create new resource at '{}'".format(self.user, path)) return Response(status=HTTP_403_FORBIDDEN) # All permissions are checked, we can proceed to create/update if self.http_mode: return self.put_data_object_http(parent, name, resource) else: return self.put_data_object_cdmi(parent, name, resource)
def find(cls, path): """Return a resource from a path""" coll_name, resc_name = split(path) entries = TreeEntry.objects.filter(container=coll_name, name=resc_name) if not entries: return None else: return cls(entries.first())
def local_file_dict(path): """Return a dictionary with the information guessed from the file on the filesystem""" t, _ = guess_type(path) _, name = split(path) _, ext = os.path.splitext(path) return { "mimetype": t, "size": os.path.getsize(path), "name": name, "type": ext[1:].upper(), }
def create_resource(self, path): container, name = split(path) Collection.create(name, container)
def do_work(self): # if self.include_pattern: # include_pattern = self.include_pattern.lower() # matcher = lambda x: include_pattern in x.lower() # else: # matcher = lambda x: True timer = TimerCounter() root_collection = Collection.get_root() self.collection_cache["/"] = root_collection t0 = time.time() if self.include_pattern: start_dir = None for (path, dirs, files) in os.walk(self.folder, topdown=True, followlinks=True): for k in dirs: if self.include_pattern.lower() in k.lower(): start_dir = os.path.join(path, k) break if start_dir: break else: start_dir = self.folder if start_dir: print "STARTING AT ", start_dir else: print "No start dir matching {0} found ... giving up ".format( self.include_pattern) return None for (path, dirs, files) in os.walk(start_dir, topdown=True, followlinks=True): if '/.' in path: continue # Ignore .paths path = path.replace(self.folder, '') parent_path, name = split(path) t1 = time.time() msg = "Processing {0} - '{1}' Previous={2:02f}s , this={3} files".format( path, name, t1 - t0, len(files)) t0 = t1 logger.info("Processing {} - '{}'".format(path, name)) print(msg) if name: timer.enter('get-collection') # parent = self.get_collection(parent_path) current_collection = self.get_collection(path) if not current_collection: current_collection = self.create_collection( parent_path, # parent.path, name, path) timer.exit('get-collection') else: current_collection = root_collection # Now we can add the resources from self.folder + path for filename in files: fullpath = self.folder + path + '/' + filename if filename.startswith("."): continue if filename.endswith(SKIP): continue if not os.path.isfile(fullpath): continue # Extract information needed to create the new resources resc_dict = local_file_dict(fullpath) resc_dict["read_access"] = self.groups resc_dict["write_access"] = self.groups resc_dict["container"] = current_collection.path resc_dict['compress'] = self.compress context = { "fullpath": fullpath, "local_ip": self.local_ip, "path": path, "filename": filename, "user": self.user.name } timer.enter('push') self.create_entry(resc_dict, context, self.is_reference) timer.exit('push') timer.summary()
def put_container(self, path): # Check if the container already exists collection = Collection.find(path) if collection: # Update if not collection.user_can(self.user, "edit"): self.logger.warning(u"User {} tried to modify collection at '{}'".format(self.user, path)) return Response(status=HTTP_403_FORBIDDEN) if self.http_mode: # HTTP Request, unsupported self.logger.warning(u"Update collection '{}' using HTTP is undefined".format(path)) return Response(status=HTTP_406_NOT_ACCEPTABLE) res = self.put_container_metadata(collection) return Response(status=res) # Create Collection parent, name = split(path) if name.startswith("cdmi_"): return Response("cdmi_ prefix is not a valid name for a container", status=HTTP_400_BAD_REQUEST) parent_collection = Collection.find(parent) if not parent_collection: self.logger.info(u"Fail to create a collection at '{}', parent collection doesn't exist".format(path)) return Response(status=HTTP_404_NOT_FOUND) # Check if user can create a new collection in the collection if not parent_collection.user_can(self.user, "write"): self.logger.warning(u"User {} tried to create new collection at '{}'".format(self.user, path)) return Response(status=HTTP_403_FORBIDDEN) body = OrderedDict() try: collection = Collection.create(name=name, container=parent) except ResourceConflictError: return Response(status=HTTP_409_CONFLICT) cdmi_container = CDMIContainer(collection, self.api_root) delayed = False res = self.put_container_metadata(collection) if res != HTTP_204_NO_CONTENT: return Response(status=res) if self.http_mode: # Specification states that: # # A response message body may be provided as per RFC 2616. # # Send the CDMI response but with Content-Type = application/json content_type = 'application/json' # Mandatory completionStatus # Does not accept CDMI - cannot return "202 Accepted" # Try to wait until complete while not path_exists(path): # Wait for 5 seconds time.sleep(5) response_status = "201 Created" body['completionStatus'] = "Complete" else: # CDMI mode for field, value in FIELDS_CONTAINER.items(): get_field = getattr(cdmi_container, 'get_{}'.format(field)) body[field] = get_field() if delayed: response_status = HTTP_202_ACCEPTED body['completionStatus'] = "Processing" else: response_status = HTTP_201_CREATED body['completionStatus'] = "Complete" return JsonResponse(body, content_type=body['objectType'], status=response_status)