def setRDFFile(self, value, **kwargs): """Copy rdf file on filesystem""" # Put value in buffer buffer = StringIO(value) copy_file(buffer, self.getRDFFilePath(**kwargs)) buffer.close()
def copyValueFile(self, **kwargs): """Duplicate file value on filesystem""" src_path = self.getValueFilePath(uid=kwargs['src_uid'], name=kwargs['name']) if not os.path.exists(src_path): return dst_path = self.getValueFilePath(**kwargs) copy_file(src_path, dst_path)
def copyValueFile(self, **kwargs): """Duplicate file value on filesystem""" src_path = self.getValueFilePath(uid=kwargs['src_uid'], path=kwargs['src_path'], name=kwargs['name'], title=kwargs.get('title', '')) if not os.path.exists(src_path): return dst_path = self.getValueFilePath(**kwargs) self.createSubDirectories(self.getValueDirectoryPath(**kwargs), self.storage_path) copy_file(src_path, dst_path)
def copyValueFile(self, **kwargs): """Duplicate file value on filesystem""" src_path = self.getValueFilePath(uid=kwargs['src_uid'], path=kwargs['src_path'], name=kwargs['name'], title=kwargs.get('title', '')) if not os.path.exists(src_path): return name = kwargs['name'] path = self.getValueDirectoryPath(**kwargs) # Process new filename filename = self.getValueFilename(**kwargs) # Update config file self.createSubDirectories(path, self.storage_path) self.setFilenameInConfigFile(path, name, filename) # Then copy file dst_path = self.getValueFilePath(**kwargs) copy_file(src_path, dst_path)
def getData(self): """ Returns data (as a string) stored on filesystem. """ # Use StringIO for large files virtual_file = StringIO() value = '' # Make sure file exists. If not returns empty string if not os.path.exists(self.path) or os.path.getsize(self.path) == 0: return '' # Copy all data in one pass try: copy_file(self.path, virtual_file) virtual_file.seek(0) value = virtual_file.getvalue() finally: virtual_file.close() return value
def build_fs_tree(src_path, dst_path, lib_path): """Build FS tree""" # Rdf imports sys.path.append(lib_path) from rdf import RDFReader from FileUtils import move_file, copy_file print "Build filesystem data in %s from %s" % (src_path, dst_path) sys_encoding = sys.getfilesystemencoding() # Store rdf files # List of dictionnary {'field': ..., 'uid': ...} rdf_files = [] # Walk into filesystem for root, dirs, files in os.walk(src_path): if root == src_path: # Loop on files for item in files: match = SEARCH_RDF_RE.match(item) if match is None: continue # Get field name and content uid uid = match.group('uid') field = match.group('field') rdf_files.append({'uid': uid, 'field': field}) # Processing collected rdf files print "Processing %s rdf files" % str(len(rdf_files)) file_paths = [] for rdf_file in rdf_files: uid = rdf_file['uid'] field = rdf_file['field'] # Get RDF file rdf_filename = '%s_%s.rdf' % (uid, field) rdf_path = os.path.join(src_path, rdf_filename) rdf_file = StringIO() rdf_text = '' try: copy_file(rdf_path, rdf_file) rdf_file.seek(0) rdf_text = rdf_file.getvalue() finally: rdf_file.close() # Read RDF properties try: rdf_reader = RDFReader(rdf_text) except: try: # XXX known bug to fix rdf_text = rdf_text.replace('&', '&') rdf_reader = RDFReader(rdf_text) except: print rdf_path print rdf_text raise field_url = rdf_reader.getFieldUrl() field_url = field_url.encode(sys_encoding, 'replace') # Create tree directories content_path_array = field_url.split('/')[:-2] content_path = dst_path for content_dir in content_path_array: content_path = os.path.join(content_path, content_dir) if os.path.exists(content_path): continue print "Create path: %s" % content_path os.mkdir(content_path) # Get source file src_filename = '%s_%s' % (uid, field) src_file_path = os.path.join(src_path, src_filename) if not os.path.exists(src_file_path): print "Source file doesn't exist, we continue: %s" % src_file_path continue # Get destination file dst_filename = field dst_filenames = rdf_reader.getFieldProperty('fss:filename') if dst_filenames: dst_filename = dst_filenames[0] if not dst_filename: dst_filename = field else: dst_filename = dst_filename.encode(sys_encoding, 'replace') dst_file_path = os.path.join(content_path, dst_filename) # In some cases, you can have a content having 2 fss fields with # 2 files with the same name orig_dst_filename = dst_filename dst_file_path_ok = False index = 0 while not dst_file_path_ok: if dst_file_path not in file_paths: dst_file_path_ok = True file_paths.append(dst_file_path) else: index += 1 dst_filename = '%s-%s' % (str(index), orig_dst_filename) print dst_filename dst_file_path = os.path.join(content_path, dst_filename) print "Create file: %s" % dst_file_path copy_file(src_file_path, dst_file_path) print "Create RDF file: %s.rdf" % dst_file_path copy_file(rdf_path, dst_file_path + '.rdf') print "Filesystem data built complete"