Esempio n. 1
0
File: dpack.py Progetto: BeauJoh/phd
def CreatePackageArchiveSidecar(archive_path: pathlib.Path,
                                manifest: dpack_pb2.DataPackage,
                                sidecar_path: pathlib.Path) -> None:
    """Create a sidecar manifest to accompany an archive.

  Args:
    archive_path: The path of the archive tarball.
    manifest: A DataPackage manifest instance.
    sidecar_path: The path of the sidecar to create

  Raises:
    OSError: If sidecar_path already exists, or archive_path does not.
  """
    if sidecar_path.exists():
        raise OSError(f'Refusing to overwrite {sidecar_path}.')
    if not archive_path.is_file():
        raise OSError(f'Archive {archive_path} does not exist')

    sidecar = dpack_pb2.DataPackage()
    sidecar.CopyFrom(manifest)
    # Clear the file attributes. Only the file names and comments are stored in the sidecar.
    for f in sidecar.file:
        if not f.comment:
            f.ClearField("comment")
        f.ClearField("size_in_bytes")
        f.ClearField("checksum_hash")
        f.ClearField("checksum")
    sidecar.checksum_hash = dpack_pb2.SHA256
    sidecar.checksum = crypto.sha256_file(archive_path)
    pbutil.ToFile(sidecar, sidecar_path)
    logging.info('Wrote %s', sidecar_path.absolute())
Esempio n. 2
0
File: dpack.py Progetto: BeauJoh/phd
def SetDataPackageFileAttributes(package_root: pathlib.Path,
                                 relpath: pathlib.Path,
                                 f: dpack_pb2.DataPackageFile) -> None:
    """Set the file attributes of a DataPackageFile message.

  Args:
    package_root: The root of the package.
    relpath: The path to the file, relative to package_root.
    f: A DataPackageFile instance.
  """
    abspath = package_root / relpath
    f.relative_path = str(relpath)
    f.size_in_bytes = abspath.stat().st_size
    f.checksum = crypto.sha256_file(abspath)
    f.checksum_hash = dpack_pb2.SHA256
Esempio n. 3
0
def test_sha256_file_empty():
    assert ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
            == crypto.sha256_file("lib/labm8/data/test/empty_file"))
Esempio n. 4
0
def test_sha256_file_hello_world():
    assert ("d9014c4624844aa5bac314773d6b689ad467fa4e1d1a50a1b8a99d5a95f72ff5"
            == crypto.sha256_file("lib/labm8/data/test/hello_world"))