コード例 #1
0
 def addFile(self, file_name, mets_filegroup):
     # reload(sys)
     # sys.setdefaultencoding('utf8')
     file_url = "./%s" % os.path.relpath(file_name, self.root_path)
     file_mimetype, _ = self.mime.guess_type(file_url)
     file_checksum = get_sha256_hash(file_name)
     file_size = os.path.getsize(file_name)
     file_cdate = get_file_ctime_iso_date_str(file_name,
                                              DT_ISO_FMT_SEC_PREC)
     file_id = "ID" + uuid.uuid4().__str__()
     mets_file = M.file({
         "MIMETYPE": file_mimetype,
         "CHECKSUMTYPE": "SHA-256",
         "CREATED": file_cdate,
         "CHECKSUM": file_checksum,
         "USE": "Datafile",
         "ID": file_id,
         "SIZE": file_size
     })
     mets_filegroup.append(mets_file)
     # _,fname = os.path.split(file_name)
     mets_FLocat = M.FLocat({
         q(XLINK_NS, 'href'): file_url,
         "LOCTYPE": "URL",
         q(XLINK_NS, 'type'): 'simple'
     })
     mets_file.append(mets_FLocat)
     return file_id
コード例 #2
0
def secure_force_copy_file(source_file, target_file):
    """
    Secure file copy (checksum test)
    :param source_file:
    :param target_file:
    :return: True if target file exists and checksum verification was successful
    :raises: IOError if the checksum verification failed
    """
    if os.path.isfile(source_file):
        shutil.copy2(source_file, target_file)
        from eatb.storage.checksum import get_sha256_hash
        if not os.path.exists(target_file) or not get_sha256_hash(
                source_file) == get_sha256_hash(target_file):
            raise IOError(
                "File copy operation failed  (checksums not equal: %s vs. %s)."
                % (source_file, target_file))
        return os.path.exists(target_file)
コード例 #3
0
ファイル: metsgenerator.py プロジェクト: MatteoRomiti/eatb
 def make_mdref(self, path, file, id, mdtype):
     mimetype, _ = self.mime.guess_type(os.path.join(path, file))
     rel_path = "file://./%s" % os.path.relpath(os.path.join(path, file),
                                                self.root_path)
     mets_mdref = {
         "LOCTYPE": "URL",
         "MIMETYPE": mimetype,
         "CREATED": current_timestamp(),
         q(XLINK_NS, "type"): "simple",
         q(XLINK_NS, "href"): rel_path,
         "CHECKSUMTYPE": "SHA-256",
         "CHECKSUM": get_sha256_hash(os.path.join(path, file)),
         "ID": id,
         "MDTYPE": mdtype
     }
     return mets_mdref