[Notes] [Git][BuildGrid/buildgrid][mablanch/61-bazel-support] 4 commits: utils.py: New gRPC OutputFile maker helper



Title: GitLab

Martin Blanchard pushed to branch mablanch/61-bazel-support at BuildGrid / buildgrid

Commits:

2 changed files:

Changes:

  • buildgrid/_app/bots/temp_directory.py
    ... ... @@ -19,7 +19,7 @@ import tempfile
    19 19
     
    
    20 20
     from google.protobuf import any_pb2
    
    21 21
     
    
    22
    -from buildgrid.utils import read_file, create_digest, write_fetch_directory, parse_to_pb2_from_fetch
    
    22
    +from buildgrid.utils import output_file_maker, write_fetch_directory, parse_to_pb2_from_fetch
    
    23 23
     from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
    
    24 24
     from buildgrid._protos.google.bytestream import bytestream_pb2_grpc
    
    25 25
     
    
    ... ... @@ -35,54 +35,81 @@ def work_temp_directory(context, lease):
    35 35
         action_digest = remote_execution_pb2.Digest()
    
    36 36
         lease.payload.Unpack(action_digest)
    
    37 37
     
    
    38
    -    action = remote_execution_pb2.Action()
    
    38
    +    action = parse_to_pb2_from_fetch(remote_execution_pb2.Action(),
    
    39
    +                                     stub_bytestream, action_digest, instance_name)
    
    39 40
     
    
    40
    -    action = parse_to_pb2_from_fetch(action, stub_bytestream, action_digest, instance_name)
    
    41
    +    with tempfile.TemporaryDirectory() as temp_directory:
    
    41 42
     
    
    42
    -    with tempfile.TemporaryDirectory() as temp_dir:
    
    43
    +        command = parse_to_pb2_from_fetch(remote_execution_pb2.Command(),
    
    44
    +                                          stub_bytestream, action.command_digest, instance_name)
    
    43 45
     
    
    44
    -        command = remote_execution_pb2.Command()
    
    45
    -        command = parse_to_pb2_from_fetch(command, stub_bytestream, action.command_digest, instance_name)
    
    46
    +        write_fetch_directory(temp_directory, stub_bytestream,
    
    47
    +                              action.input_root_digest, instance_name)
    
    46 48
     
    
    47
    -        arguments = "cd {} &&".format(temp_dir)
    
    49
    +        execution_envionment = os.environ.copy()
    
    50
    +        for variable in command.environment_variables:
    
    51
    +            if variable.name not in ['PATH', 'PWD']:
    
    52
    +                execution_envionment[variable.name] = variable.value
    
    48 53
     
    
    54
    +        command_arguments = list()
    
    49 55
             for argument in command.arguments:
    
    50
    -            arguments += " {}".format(argument)
    
    51
    -
    
    52
    -        context.logger.info(arguments)
    
    53
    -
    
    54
    -        write_fetch_directory(temp_dir, stub_bytestream, action.input_root_digest, instance_name)
    
    55
    -
    
    56
    -        proc = subprocess.Popen(arguments,
    
    57
    -                                shell=True,
    
    58
    -                                stdin=subprocess.PIPE,
    
    59
    -                                stdout=subprocess.PIPE)
    
    60
    -
    
    61
    -        # TODO: Should return the std_out to the user
    
    62
    -        proc.communicate()
    
    63
    -
    
    64
    -        result = remote_execution_pb2.ActionResult()
    
    65
    -        requests = []
    
    66
    -        for output_file in command.output_files:
    
    67
    -            path = os.path.join(temp_dir, output_file)
    
    68
    -            chunk = read_file(path)
    
    69
    -
    
    70
    -            digest = create_digest(chunk)
    
    71
    -
    
    72
    -            result.output_files.extend([remote_execution_pb2.OutputFile(path=output_file,
    
    73
    -                                                                        digest=digest)])
    
    74
    -
    
    75
    -            requests.append(remote_execution_pb2.BatchUpdateBlobsRequest.Request(
    
    76
    -                digest=digest, data=chunk))
    
    77
    -
    
    78
    -        request = remote_execution_pb2.BatchUpdateBlobsRequest(instance_name=instance_name,
    
    79
    -                                                               requests=requests)
    
    56
    +            command_arguments.append(argument.strip())
    
    57
    +
    
    58
    +        working_directory = None
    
    59
    +        if command.working_directory:
    
    60
    +            working_directory = os.path.join(temp_directory,
    
    61
    +                                             command.working_directory)
    
    62
    +            os.makedirs(working_directory, exist_ok=True)
    
    63
    +        else:
    
    64
    +            working_directory = temp_directory
    
    65
    +
    
    66
    +        # Ensure that output files structure exists:
    
    67
    +        for output_path in command.output_files:
    
    68
    +            directory_path = os.path.join(working_directory,
    
    69
    +                                          os.path.dirname(output_path))
    
    70
    +            os.makedirs(directory_path, exist_ok=True)
    
    71
    +
    
    72
    +        process = subprocess.Popen(command_arguments,
    
    73
    +                                   cwd=working_directory,
    
    74
    +                                   universal_newlines=True,
    
    75
    +                                   env=execution_envionment,
    
    76
    +                                   stdin=subprocess.PIPE,
    
    77
    +                                   stdout=subprocess.PIPE)
    
    78
    +        # TODO: Should return the stdout and stderr to the user.
    
    79
    +        process.communicate()
    
    80
    +
    
    81
    +        update_requests = remote_execution_pb2.BatchUpdateBlobsRequest(instance_name=instance_name)
    
    82
    +        action_result = remote_execution_pb2.ActionResult()
    
    83
    +
    
    84
    +        for output_path in command.output_files:
    
    85
    +            file_path = os.path.join(working_directory, output_path)
    
    86
    +            # Missing outputs should simply be omitted in ActionResult:
    
    87
    +            if not os.path.isfile(file_path):
    
    88
    +                continue
    
    89
    +
    
    90
    +            # OutputFile.path should be relative to the working direcory:
    
    91
    +            output_file, update_request = output_file_maker(file_path, working_directory)
    
    92
    +
    
    93
    +            action_result.output_files.extend([output_file])
    
    94
    +            update_requests.requests.extend([update_request])
    
    95
    +
    
    96
    +        for output_path in command.output_directories:
    
    97
    +            directory_path = os.path.join(working_directory, output_path)
    
    98
    +            # Missing outputs should simply be omitted in ActionResult:
    
    99
    +            if not os.path.isdir(directory_path):
    
    100
    +                continue
    
    101
    +
    
    102
    +            # OutputDirectory.path should be relative to the working direcory:
    
    103
    +            output_directory, update_request = output_directory_maker(directory_path, working_directory)
    
    104
    +
    
    105
    +            action_result.output_directories.extend([output_directory])
    
    106
    +            update_requests.requests.extend(update_request)
    
    80 107
     
    
    81 108
             stub_cas = remote_execution_pb2_grpc.ContentAddressableStorageStub(context.channel)
    
    82
    -        stub_cas.BatchUpdateBlobs(request)
    
    109
    +        stub_cas.BatchUpdateBlobs(update_requests)
    
    83 110
     
    
    84 111
             result_any = any_pb2.Any()
    
    85
    -        result_any.Pack(result)
    
    112
    +        result_any.Pack(action_result)
    
    86 113
     
    
    87 114
             lease.result.CopyFrom(result_any)
    
    88 115
     
    

  • buildgrid/utils.py
    ... ... @@ -99,7 +99,15 @@ def parse_to_pb2_from_fetch(pb2, stub, digest, instance_name=""):
    99 99
     
    
    100 100
     
    
    101 101
     def create_digest(bytes_to_digest):
    
    102
    -    """ Creates a hash based on the hex digest and returns the digest
    
    102
    +    """Computes the :obj:`Digest` of a piece of data.
    
    103
    +
    
    104
    +    The :obj:`Digest` of a data is a function of its hash **and** size.
    
    105
    +
    
    106
    +    Args:
    
    107
    +        bytes_to_digest (bytes): byte data to digest.
    
    108
    +
    
    109
    +    Returns:
    
    110
    +        :obj:`Digest`: The gRPC :obj:`Digest` for the given byte data.
    
    103 111
         """
    
    104 112
         return remote_execution_pb2.Digest(hash=HASH(bytes_to_digest).hexdigest(),
    
    105 113
                                            size_bytes=len(bytes_to_digest))
    
    ... ... @@ -136,6 +144,187 @@ def file_maker(file_path, file_digest):
    136 144
                                              is_executable=os.access(file_path, os.X_OK))
    
    137 145
     
    
    138 146
     
    
    139
    -def read_file(read):
    
    140
    -    with open(read, 'rb') as f:
    
    141
    -        return f.read()
    147
    +def directory_maker(directory_path):
    
    148
    +    """
    
    149
    +    """
    
    150
    +    if not os.path.isabs(directory_path):
    
    151
    +        directory_path = os.path.abspath(directory_path)
    
    152
    +
    
    153
    +    child_directories = list()
    
    154
    +    update_requests = list()
    
    155
    +
    
    156
    +    files, directories, symlinks = list(), list(), list()
    
    157
    +    for directory_entry in os.scandir(directory_path):
    
    158
    +        # Create a FileNode and corresponding BatchUpdateBlobsRequest:
    
    159
    +        if directory_entry.is_file(follow_symlinks=False):
    
    160
    +            node_blob = read_file(directory_entry.path)
    
    161
    +            node_digest = create_digest(node_blob)
    
    162
    +
    
    163
    +            node = remote_execution_pb2.FileNode()
    
    164
    +            node.name = directory_entry.name
    
    165
    +            node.digest = node_digest
    
    166
    +            node.is_executable = os.access(directory_entry.path, os.X_OK)
    
    167
    +
    
    168
    +            node_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=node_digest)
    
    169
    +            node_request.data = node_blob
    
    170
    +
    
    171
    +            update_requests.append(node_request)
    
    172
    +            files.append(node)
    
    173
    +
    
    174
    +        # Create a DirectoryNode and corresponding BatchUpdateBlobsRequest:
    
    175
    +        elif directory_entry.is_dir(follow_symlinks=False):
    
    176
    +            node_directory, _, node_requests = directory_maker(directory_entry.path)
    
    177
    +
    
    178
    +            node = remote_execution_pb2.DirectoryNode()
    
    179
    +            node.name = directory_entry.name
    
    180
    +            node.digest = node_requests[-1].digest
    
    181
    +
    
    182
    +            child_directories.append(node_directory)
    
    183
    +            update_requests.expend(node_requests)
    
    184
    +            directories.append(node)
    
    185
    +
    
    186
    +        # Create a SymlinkNode if necessary;
    
    187
    +        elif os.path.islink(directory_entry.path):
    
    188
    +            node_target = os.readlink(directory_entry.path)
    
    189
    +
    
    190
    +            node = remote_execution_pb2.SymlinkNode()
    
    191
    +            node.name = directory_entry.name
    
    192
    +            node.target = node_target
    
    193
    +
    
    194
    +            symlinks.append(node)
    
    195
    +
    
    196
    +    directory = remote_execution_pb2.Directory()
    
    197
    +    directory.files.extend(files.sort())
    
    198
    +    directory.directories.extend(directories.sort())
    
    199
    +    directory.symlinks.extend(symlinks.sort())
    
    200
    +
    
    201
    +    directory_blob = directory.SerializeToString()
    
    202
    +    directory_digest = create_digest(directory_blob)
    
    203
    +
    
    204
    +    update_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=directory_digest)
    
    205
    +    update_request.data = directory_blob
    
    206
    +
    
    207
    +    update_requests.append(update_request)
    
    208
    +
    
    209
    +    return directory, child_directories, update_requests
    
    210
    +
    
    211
    +
    
    212
    +def read_file(file_path):
    
    213
    +    """Loads raw file content in memory.
    
    214
    +
    
    215
    +    Returns:
    
    216
    +        bytes: Raw file's content until EOF.
    
    217
    +
    
    218
    +    Raises:
    
    219
    +        OSError: If `file_path` does not exist or is not readable.
    
    220
    +    """
    
    221
    +    with open(file_path, 'rb') as byte_file:
    
    222
    +        return byte_file.read()
    
    223
    +
    
    224
    +
    
    225
    +def output_file_maker(file_path, input_path):
    
    226
    +    """Creates an :obj:`OutputFile` from a local file.
    
    227
    +
    
    228
    +    `file_path` **must** point inside or be relative to `input_path`.
    
    229
    +
    
    230
    +    Args:
    
    231
    +        file_path (str): absolute or relative path to a local file.
    
    232
    +        input_path (str): absolute or relative path to the input root directory.
    
    233
    +
    
    234
    +    Returns:
    
    235
    +        :obj:`OutputFile`, :obj:`BatchUpdateBlobsRequest`: Tuple of a new gRPC
    
    236
    +        :obj:`OutputFile` object for the file pointed by `file_path` and the
    
    237
    +        corresponding :obj:`BatchUpdateBlobsRequest` for CAS upload.
    
    238
    +    """
    
    239
    +    if not os.path.isabs(file_path):
    
    240
    +        file_path = os.path.abspath(file_path)
    
    241
    +    if not os.path.isabs(input_path):
    
    242
    +        input_path = os.path.abspath(input_path)
    
    243
    +
    
    244
    +    file_blob = read_file(file_path)
    
    245
    +    file_digest = create_digest(file_blob)
    
    246
    +
    
    247
    +    output_file = remote_execution_pb2.OutputFile(digest=file_digest)
    
    248
    +    output_file.path = os.path.relpath(file_path, start=input_path)
    
    249
    +    output_file.is_executable = os.access(file_path, os.X_OK)
    
    250
    +
    
    251
    +    update_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=file_digest)
    
    252
    +    update_request.data = file_blob
    
    253
    +
    
    254
    +    return output_file, update_request
    
    255
    +
    
    256
    +
    
    257
    +def output_directory_maker(directory_path, working_path):
    
    258
    +    """Creates a gRPC :obj:`OutputDirectory` from a local directory.
    
    259
    +
    
    260
    +    `directory_path` **must** point inside or be relative to `input_path`.
    
    261
    +
    
    262
    +    Args:
    
    263
    +        directory_path (str): absolute or relative path to a local directory.
    
    264
    +        working_path (str): absolute or relative path to the working directory.
    
    265
    +
    
    266
    +    Returns:
    
    267
    +        :obj:`OutputDirectory`, :obj:`BatchUpdateBlobsRequest`: Tuple of a new
    
    268
    +        gRPC :obj:`OutputDirectory` for the directory pointed by
    
    269
    +        `directory_path` and the corresponding list of
    
    270
    +        :obj:`BatchUpdateBlobsRequest` for CAS upload.
    
    271
    +    """
    
    272
    +    if not os.path.isabs(directory_path):
    
    273
    +        directory_path = os.path.abspath(directory_path)
    
    274
    +    if not os.path.isabs(working_path):
    
    275
    +        working_path = os.path.abspath(working_path)
    
    276
    +
    
    277
    +    tree, update_requests = tree_maker(directory_path)
    
    278
    +
    
    279
    +    output_directory = remote_execution_pb2.OutputDirectory()
    
    280
    +    output_directory.tree_digest = update_requests[-1].digest
    
    281
    +    output_directory.path = os.path.relpath(directory_path, start=working_path)
    
    282
    +
    
    283
    +    output_directory_blob = output_directory.SerializeToString()
    
    284
    +    output_directory_digest = create_digest(output_directory_blob)
    
    285
    +
    
    286
    +    update_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=output_directory_digest)
    
    287
    +    update_request.data = output_directory_blob
    
    288
    +
    
    289
    +    update_requests.append(update_request)
    
    290
    +
    
    291
    +    return output_directory, update_requests
    
    292
    +
    
    293
    +
    
    294
    +def tree_maker(directory_path):
    
    295
    +    """Creates a gRPC :obj:`Tree` from a local directory.
    
    296
    +
    
    297
    +    Args:
    
    298
    +        directory_path (str): absolute or relative path to a local directory.
    
    299
    +
    
    300
    +    Returns:
    
    301
    +        :obj:`Tree`, :obj:`BatchUpdateBlobsRequest`: Tuple of a new
    
    302
    +        gRPC :obj:`Tree` for the directory pointed by `directory_path` and the
    
    303
    +        corresponding list of :obj:`BatchUpdateBlobsRequest` for CAS upload.
    
    304
    +
    
    305
    +        The :obj:`BatchUpdateBlobsRequest` list may come in any order. However,
    
    306
    +        its last element is guaranteed to be the :obj:`Tree`'s request.
    
    307
    +    """
    
    308
    +    if not os.path.isabs(directory_path):
    
    309
    +        directory_path = os.path.abspath(directory_path)
    
    310
    +
    
    311
    +    update_requests = list()
    
    312
    +    directory, update_request directory_maker(directory_path)
    
    313
    +
    
    314
    +
    
    315
    +        update_requests.expend(update_request)
    
    316
    +
    
    317
    +
    
    318
    +    tree = remote_execution_pb2.Tree()
    
    319
    +    tree.children.expend()
    
    320
    +    tree.root =
    
    321
    +
    
    322
    +    tree_blob = tree.SerializeToString()
    
    323
    +    tree_digest = create_digest(file_blob)
    
    324
    +
    
    325
    +    update_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=tree_digest)
    
    326
    +    update_request.data = tree_blob
    
    327
    +
    
    328
    +    update_requests.append(update_request)
    
    329
    +
    
    330
    +    return tree, update_requests



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]