def delete_file(attr, value): """ Delete files in GridFS Args: attr (str): The attribute to retrieve. vale (str): Value of the attribute. Returns: success - True not found - False Raises: Exception >>> delete_file('name', 'test') """ try: gridfs = connect_gridfs() except Exception as e: logger.debug(e) raise else: files = gridfs.find({attr: value}) if files.count() > 0: for f in files: gridfs.delete(f._id) return True else: return False
def store_result_for_apk(self, apk, script): ''' See doc of :py:meth:`.ResultWritingInterface.store_result_for_apk`. Returns ------- tuple<str, bool> First component is the id of the entry and the second a boolean indication if the result has been stored in gridfs. None If an error occurred. ''' try: # escape keys for mongodb insert res_obj_dict = escape_keys(script.result_dict(gen_id = False)) _id = script.gen_unique_id() # if data is to big or custom result object used -> store with gridfs if script.uses_custom_result_object() or script.is_big_res(): log.debug("storing results for %s, %s in %s (id: %s)", apk.short_description(), script, self.grid_fs, _id) result = self.get_custom_res_obj_representation(script) gridfs = self.grid_fs # gridfs doesn't have an update method -> delete and insert if gridfs.exists(**{RESOBJ_ID : _id}): # delete by _id gridfs.delete(_id) # store file together with metadata from `ResultObject` gridfs.put(result, metadata = res_obj_dict, filename = script.get_file_name(), _id = _id) # return id return _id, True # normal json data else: log.debug("storing results for %s, %s in %s db(id: %s)", apk.short_description(), script, self.res_coll, _id) # set id so we don't have multiple results for same script and apk res_obj_dict[RESOBJ_ID] = _id # update or insert if not existing self.res_coll.update({RESOBJ_ID : _id}, res_obj_dict, upsert = True) # return id return _id, False except (PyMongoError, BSONError) as e: raise DatabaseStoreException(self, "script: %s" % script, caused_by = e), None, sys.exc_info()[2]