Skip to content
Snippets Groups Projects
Commit 725db807 authored by Martin Blanchard's avatar Martin Blanchard
Browse files

utils.py: Remove CAS download helpers

#79
parent 9b787728
No related branches found
No related tags found
Loading
......@@ -18,87 +18,6 @@ import os
from buildgrid.settings import HASH
from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
from buildgrid._protos.google.bytestream import bytestream_pb2
def gen_fetch_blob(stub, digest, instance_name=""):
""" Generates byte stream from a fetch blob request
"""
resource_name = os.path.join(instance_name, 'blobs', digest.hash, str(digest.size_bytes))
request = bytestream_pb2.ReadRequest(resource_name=resource_name,
read_offset=0)
for response in stub.Read(request):
yield response.data
def write_fetch_directory(root_directory, stub, digest, instance_name=None):
"""Locally replicates a directory from CAS.
Args:
root_directory (str): local directory to populate.
stub (): gRPC stub for CAS communication.
digest (Digest): digest for the directory to fetch from CAS.
instance_name (str, optional): farm instance name to query data from.
"""
if not os.path.isabs(root_directory):
root_directory = os.path.abspath(root_directory)
if not os.path.exists(root_directory):
os.makedirs(root_directory, exist_ok=True)
directory = parse_to_pb2_from_fetch(remote_execution_pb2.Directory(),
stub, digest, instance_name)
for directory_node in directory.directories:
child_path = os.path.join(root_directory, directory_node.name)
write_fetch_directory(child_path, stub, directory_node.digest, instance_name)
for file_node in directory.files:
child_path = os.path.join(root_directory, file_node.name)
with open(child_path, 'wb') as child_file:
write_fetch_blob(child_file, stub, file_node.digest, instance_name)
for symlink_node in directory.symlinks:
child_path = os.path.join(root_directory, symlink_node.name)
if os.path.isabs(symlink_node.target):
continue # No out of temp-directory links for now.
target_path = os.path.join(root_directory, symlink_node.target)
os.symlink(child_path, target_path)
def write_fetch_blob(target_file, stub, digest, instance_name=None):
"""Extracts a blob from CAS into a local file.
Args:
target_file (str): local file to write.
stub (): gRPC stub for CAS communication.
digest (Digest): digest for the blob to fetch from CAS.
instance_name (str, optional): farm instance name to query data from.
"""
for stream in gen_fetch_blob(stub, digest, instance_name):
target_file.write(stream)
target_file.flush()
assert digest.size_bytes == os.fstat(target_file.fileno()).st_size
def parse_to_pb2_from_fetch(pb2, stub, digest, instance_name=""):
""" Fetches stream and parses it into given pb2
"""
stream_bytes = b''
for stream in gen_fetch_blob(stub, digest, instance_name):
stream_bytes += stream
pb2.ParseFromString(stream_bytes)
return pb2
def create_digest(bytes_to_digest):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment