def setValueFile(self, value, **kwargs): """Copy file value on filesystem""" # Get filename of value file name = kwargs['name'] path = self.getValueDirectoryPath(**kwargs) # Remove old files (value and rdf files) file_path = self.getValueFilePath(**kwargs) if os.path.exists(file_path): rm_file(file_path) rdf_path = self.getRDFFilePath(**kwargs) if os.path.exists(rdf_path): rm_file(rdf_path) self.unsetFilenameInConfigFile(path, name) # Process new filename filename = self.getValueFilename(**kwargs) # Update config file self.createSubDirectories(path, self.storage_path) self.setFilenameInConfigFile(path, name, filename) # Store value FlatStorageStrategy.setValueFile(self, value, **kwargs)
def unsetRDFFile(self, **kwargs): """Remove rdf file if exists""" # This is a cut/paste operation. There is no need to delete file if kwargs.get('is_moved', False): return rdf_path = self.getRDFFilePath(**kwargs) if not os.path.exists(rdf_path): return rm_file(rdf_path)
def setValueFile(self, value, **kwargs): """Copy file value on filesystem""" # Remove all files in value directory except rdf file path = self.getValueDirectoryPath(**kwargs) if os.path.exists(path): rdf_filename = self.getRDFFilename(**kwargs) for filename in os.listdir(path): if filename == rdf_filename: continue file_path = os.path.join(path, filename) rm_file(file_path) DirectoryStorageStrategy.setValueFile(self, value, **kwargs)
def removeBackups(self, max_days): """ Remove backups older than specified days """ backup_brains = self.getBackupBrains() valid_backups = [x for x in backup_brains if x['path'] is None] current_time = time.time() for item in valid_backups: one_day = 86400 # One day 86400 seconds modified = item['modified'] seconds = int(current_time) - int(modified.timeTime()) days = int(seconds/one_day) if days >= max_days: rm_file(item['fs_path'])
def removeBackups(self, max_days): """ Remove backups older than specified days """ backup_brains = self.getBackupBrains() valid_backups = [x for x in backup_brains if x['path'] is None] current_time = time.time() for item in valid_backups: one_day = 86400 # One day 86400 seconds modified = item['modified'] seconds = int(current_time) - int(modified.timeTime()) days = int(seconds / one_day) if days >= max_days: rm_file(item['fs_path'])
def unsetFilenameInConfigFile(self, root_path, name): """Remove filename field association in config file @root_path: Path where config file is stored @param name: Field name """ path = os.path.join(root_path, self.cfg_filename) # Config file doesn't exists if not os.path.exists(path): return # Initialize config file config = RawConfigParser() # Read old config file fd = open(path, 'r') try: # Read file config.readfp(fd) finally: fd.close() # Update config file if config.has_section(self.cfg_filename_section): config.remove_option(self.cfg_filename_section, name) if not config.options(self.cfg_filename_section): # Section is empty so remove config file rm_file(path) else: fd = open(path, 'w') try: # Write file config.write(fd) finally: fd.close()
def set(self, name, instance, value, **kwargs): """Set value of a field""" # Ignore initialize process initializing = kwargs.get('_initializing_', False) if initializing: return # Remove acquisition wrappers value = aq_base(value) # Create File System Storage Info info = self.setFSSInfo(name, instance, value, **kwargs) # Wrap value if objectImplements(IObjectField, value): value = value.getRaw(self.instance) if objectImplements(IBaseUnit, value): value = value.getRaw() elif isinstance(value, File): value = value.data else: value = str(value) # Copy file on filesystem strategy = self.getStorageStrategy(name, instance) props = self.getStorageStrategyProperties(name, instance, info) if isinstance(value, FSSPdata): ## put all in temporory file fd, pathtemp = tempfile.mkstemp(prefix="tempfss") copy_file(value, pathtemp) value = filestream_iterator(pathtemp, mode='rb') strategy.setValueFile(value, **props) value.close() os.close(fd) rm_file(pathtemp) elif isinstance(value, Pdata): fd, pathtemp = tempfile.mkstemp(prefix="tempfss") f = open(pathtemp, 'wb') data = value while data is not None: f.write(data.data) data = data.next f.seek(0, 0) f.close() f = open(pathtemp, 'rb') strategy.setValueFile(f, **props) f.close() os.close(fd) rm_file(pathtemp) else: strategy.setValueFile(value, **props) # Create RDF file conf = self.getConf() is_rdf_enabled = conf.isRDFEnabled() rdf_script = conf.getRDFScript() if is_rdf_enabled: # Replace rdf file rdf_value = info.getRDFValue(name, instance, rdf_script) strategy.setRDFFile(rdf_value, **props)
def set(self, name, instance, value, **kwargs): """Set value of a field""" # Ignore initialize process initializing = kwargs.get('_initializing_', False) if initializing: return # Remove acquisition wrappers value = aq_base(value) # Create File System Storage Info info = self.setFSSInfo(name, instance, value, **kwargs) # Wrap value if objectImplements(IObjectField, value): value = value.getRaw(self.instance) if objectImplements(IBaseUnit, value): value = value.getRaw() elif isinstance(value, File): value = value.data else: value = str(value) # Copy file on filesystem strategy = self.getStorageStrategy(name, instance) props = self.getStorageStrategyProperties(name, instance, info) if isinstance(value, FSSPdata): ## put all in temporory file fd, pathtemp = tempfile.mkstemp(prefix="tempfss") copy_file(value, pathtemp) value = filestream_iterator(pathtemp, mode='rb') strategy.setValueFile(value, **props) value.close() os.close(fd) rm_file(pathtemp) elif isinstance(value, Pdata): fd, pathtemp = tempfile.mkstemp(prefix="tempfss") f = open(pathtemp,'wb') data = value while data is not None: f.write(data.data) data = data.next f.seek(0, 0) f.close() f = open(pathtemp,'rb') strategy.setValueFile(f, **props) f.close() os.close(fd) rm_file(pathtemp) else: strategy.setValueFile(value, **props) # Create RDF file conf = self.getConf() is_rdf_enabled = conf.isRDFEnabled() rdf_script = conf.getRDFScript() if is_rdf_enabled: # Replace rdf file rdf_value = info.getRDFValue(name, instance, rdf_script) strategy.setRDFFile(rdf_value, **props)