from distantrec.helpers import get_option
import grpc
from buildgrid.client.cas import Uploader, Downloader
from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
from google import auth as google_auth
from google.auth.transport import grpc as google_auth_transport_grpc
from google.auth.transport import requests as google_auth_transport_requests
from distantrec.helpers import get_option

class RAC:
    def __init__(self, uri, instance):
        if(get_option('SETUP','USERBE') == 'yes'):
            credentials, _ = google_auth.default()
            request = google_auth_transport_requests.Request()
            self.channel = google_auth_transport_grpc.secure_authorized_channel(credentials, request, 'remotebuildexecution.googleapis.com:443')
        else:
            self.channel = grpc.insecure_channel(uri)
        self.instname = instance
        self.uploader = Uploader(self.channel, instance=self.instname)
        if instance == None:
           nm = "unknown--"+uri
        else:
           nm = instance
        print("Running on instance {}".format(nm))

    def upload_action(self, commands, input_root, output_file, cache=True):
        command_handler = remote_execution_pb2.Command()

        for arg in commands:
            command_handler.arguments.extend([arg])

        for ofile in output_file:
            command_handler.output_files.extend([ofile])

        if get_option('SETUP', 'PROPNAME') and get_option('SETUP', 'PROPVALUE') and get_option('SETUP','USERBE') == 'yes':
            new_property = command_handler.platform.properties.add()
            new_property.name = get_option('SETUP', 'PROPNAME')
            new_property.value = get_option('SETUP', 'PROPVALUE')

        command_digest = self.uploader.put_message(command_handler, queue=True)

        input_root_digest = self.uploader.upload_directory(input_root + "/" + get_option('SETUP','BUILDDIR'),queue=False)

        action = remote_execution_pb2.Action(command_digest=command_digest,
                input_root_digest = input_root_digest,
                do_not_cache=not cache)

        action_digest = self.uploader.put_message(action, queue=False)

        return action_digest

    def run_command(self, action_digest, cache=True):
        stub = remote_execution_pb2_grpc.ExecutionStub(self.channel)

        request = remote_execution_pb2.ExecuteRequest(instance_name=get_option('SETUP','INSTANCE'),
                action_digest=action_digest,
                skip_cache_lookup=not cache)

        response = stub.Execute(request)

        stream = None


        for stream in response:
            continue


        execute_response = remote_execution_pb2.ExecuteResponse()
        stream.response.Unpack(execute_response)
        if execute_response.result.stdout_digest.hash:
            downloader = Downloader(self.channel, instance=self.instname)
            blob = downloader.get_blob(execute_response.result.stdout_digest)
            print(blob)
            downloader.close()

        if execute_response.result.stderr_raw != "":
            print(str(execute_response.result.stderr_raw, errors='ignore'))
        if execute_response.result.exit_code != 0:
            print("Compilation failed.")
            fail = 1
            return None

        if execute_response.cached_result:
            print("Served from cache!")

        return execute_response.result.output_files

    def action_run(self, cmd, input_dir, output_files):
        action_digest = self.upload_action(cmd, input_dir, output_files)
        self.uploader.flush()
        return self.run_command(action_digest)

@@ -3044,16 +3044,145 @@
 .close()
+%0A        if execute_response.result.stdout_raw != %22%22:%0A            print(str(execute_response.result.stdout_raw, errors='ignore'))
 %0A%0A      

Try to print stdout raw