Martin Blanchard pushed to branch mablanch/61-bazel-support at BuildGrid / buildgrid
Commits:
-
54824a95
by Martin Blanchard at 2018-09-10T11:58:24Z
-
4a3f2c7c
by Martin Blanchard at 2018-09-10T11:58:25Z
-
2c0d9b59
by Martin Blanchard at 2018-09-10T11:58:25Z
-
fe578726
by Martin Blanchard at 2018-09-10T12:02:33Z
-
da041ae6
by Martin Blanchard at 2018-09-10T12:02:34Z
4 changed files:
- buildgrid/_app/bots/temp_directory.py
- + buildgrid/client/cas.py
- buildgrid/server/execution/execution_service.py
- buildgrid/utils.py
Changes:
... | ... | @@ -19,71 +19,94 @@ 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.client.cas import upload
|
|
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 |
+from buildgrid.utils import write_fetch_directory, parse_to_pb2_from_fetch
|
|
26 |
+from buildgrid.utils import output_file_maker, output_directory_maker
|
|
25 | 27 |
|
26 | 28 |
|
27 | 29 |
def work_temp_directory(context, lease):
|
28 |
- """ Bot downloads directories and files into a temp directory,
|
|
29 |
- then uploads results back to CAS
|
|
30 |
+ """Executes a lease for a build action, using host tools.
|
|
30 | 31 |
"""
|
31 | 32 |
|
32 |
- parent = context.parent
|
|
33 | 33 |
stub_bytestream = bytestream_pb2_grpc.ByteStreamStub(context.cas_channel)
|
34 |
+ instance_name = context.parent
|
|
35 |
+ logger = context.logger
|
|
34 | 36 |
|
35 | 37 |
action_digest = remote_execution_pb2.Digest()
|
36 | 38 |
lease.payload.Unpack(action_digest)
|
37 | 39 |
|
38 |
- action = remote_execution_pb2.Action()
|
|
40 |
+ action = parse_to_pb2_from_fetch(remote_execution_pb2.Action(),
|
|
41 |
+ stub_bytestream, action_digest, instance_name)
|
|
39 | 42 |
|
40 |
- action = parse_to_pb2_from_fetch(action, stub_bytestream, action_digest, parent)
|
|
43 |
+ with tempfile.TemporaryDirectory() as temp_directory:
|
|
44 |
+ command = parse_to_pb2_from_fetch(remote_execution_pb2.Command(),
|
|
45 |
+ stub_bytestream, action.command_digest, instance_name)
|
|
41 | 46 |
|
42 |
- with tempfile.TemporaryDirectory() as temp_dir:
|
|
47 |
+ write_fetch_directory(temp_directory, stub_bytestream,
|
|
48 |
+ action.input_root_digest, instance_name)
|
|
43 | 49 |
|
44 |
- command = remote_execution_pb2.Command()
|
|
45 |
- command = parse_to_pb2_from_fetch(command, stub_bytestream, action.command_digest, parent)
|
|
46 |
- |
|
47 |
- arguments = "cd {} &&".format(temp_dir)
|
|
50 |
+ environment = os.environ.copy()
|
|
51 |
+ for variable in command.environment_variables:
|
|
52 |
+ if variable.name not in ['PATH', 'PWD']:
|
|
53 |
+ environment[variable.name] = variable.value
|
|
48 | 54 |
|
55 |
+ command_line = list()
|
|
49 | 56 |
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, parent)
|
|
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=parent,
|
|
79 |
- requests=requests)
|
|
80 |
- |
|
81 |
- stub_cas = remote_execution_pb2_grpc.ContentAddressableStorageStub(context.cas_channel)
|
|
82 |
- stub_cas.BatchUpdateBlobs(request)
|
|
83 |
- |
|
84 |
- result_any = any_pb2.Any()
|
|
85 |
- result_any.Pack(result)
|
|
86 |
- |
|
87 |
- lease.result.CopyFrom(result_any)
|
|
57 |
+ command_line.append(argument.strip())
|
|
58 |
+ |
|
59 |
+ working_directory = None
|
|
60 |
+ if command.working_directory:
|
|
61 |
+ working_directory = os.path.join(temp_directory,
|
|
62 |
+ command.working_directory)
|
|
63 |
+ os.makedirs(working_directory, exist_ok=True)
|
|
64 |
+ else:
|
|
65 |
+ working_directory = temp_directory
|
|
66 |
+ |
|
67 |
+ # Ensure that output files structure exists:
|
|
68 |
+ for output_path in command.output_files:
|
|
69 |
+ directory_path = os.path.join(working_directory,
|
|
70 |
+ os.path.dirname(output_path))
|
|
71 |
+ os.makedirs(directory_path, exist_ok=True)
|
|
72 |
+ |
|
73 |
+ logger.debug(' '.join(command_line))
|
|
74 |
+ |
|
75 |
+ process = subprocess.Popen(command_line,
|
|
76 |
+ cwd=working_directory,
|
|
77 |
+ universal_newlines=True,
|
|
78 |
+ env=environment,
|
|
79 |
+ stdin=subprocess.PIPE,
|
|
80 |
+ stdout=subprocess.PIPE)
|
|
81 |
+ # TODO: Should return the stdout and stderr in the ActionResult.
|
|
82 |
+ process.communicate()
|
|
83 |
+ |
|
84 |
+ action_result = remote_execution_pb2.ActionResult()
|
|
85 |
+ |
|
86 |
+ with upload(context.cas_channel, instance=instance_name) as cas:
|
|
87 |
+ for output_path in command.output_files:
|
|
88 |
+ file_path = os.path.join(working_directory, output_path)
|
|
89 |
+ # Missing outputs should simply be omitted in ActionResult:
|
|
90 |
+ if not os.path.isfile(file_path):
|
|
91 |
+ continue
|
|
92 |
+ |
|
93 |
+ output_file = output_file_maker(file_path, working_directory, cas=cas)
|
|
94 |
+ action_result.output_files.extend([output_file])
|
|
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 = output_directory_maker(directory_path, working_directory, cas=cas)
|
|
104 |
+ |
|
105 |
+ action_result.output_directories.extend([output_directory])
|
|
106 |
+ |
|
107 |
+ action_result_any = any_pb2.Any()
|
|
108 |
+ action_result_any.Pack(action_result)
|
|
109 |
+ |
|
110 |
+ lease.result.CopyFrom(action_result_any)
|
|
88 | 111 |
|
89 | 112 |
return lease
|
1 |
+# Copyright (C) 2018 Bloomberg LP
|
|
2 |
+#
|
|
3 |
+# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
+# you may not use this file except in compliance with the License.
|
|
5 |
+# You may obtain a copy of the License at
|
|
6 |
+#
|
|
7 |
+# <http://www.apache.org/licenses/LICENSE-2.0>
|
|
8 |
+#
|
|
9 |
+# Unless required by applicable law or agreed to in writing, software
|
|
10 |
+# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
+# See the License for the specific language governing permissions and
|
|
13 |
+# limitations under the License.
|
|
14 |
+ |
|
15 |
+ |
|
16 |
+from contextlib import contextmanager
|
|
17 |
+import uuid
|
|
18 |
+import os
|
|
19 |
+ |
|
20 |
+from buildgrid.settings import HASH
|
|
21 |
+from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
|
|
22 |
+from buildgrid._protos.google.bytestream import bytestream_pb2, bytestream_pb2_grpc
|
|
23 |
+ |
|
24 |
+ |
|
25 |
+@contextmanager
|
|
26 |
+def upload(channel, instance=None, u_uid=None):
|
|
27 |
+ uploader = Uploader(channel, instance=instance, u_uid=u_uid)
|
|
28 |
+ try:
|
|
29 |
+ yield uploader
|
|
30 |
+ finally:
|
|
31 |
+ uploader.flush()
|
|
32 |
+ |
|
33 |
+ |
|
34 |
+class Uploader:
|
|
35 |
+ """Remote CAS files, directories and messages upload helper.
|
|
36 |
+ |
|
37 |
+ The :class:`Uploader` class comes with a generator factory function that can
|
|
38 |
+ be used together with the `with` statement for context management::
|
|
39 |
+ |
|
40 |
+ with upload(channel, instance='build') as cas:
|
|
41 |
+ cas.upload_file('/path/to/local/file')
|
|
42 |
+ """
|
|
43 |
+ |
|
44 |
+ def __init__(self, channel, instance=None, u_uid=None):
|
|
45 |
+ """Initializes a new :class:`Uploader` instance.
|
|
46 |
+ |
|
47 |
+ Args:
|
|
48 |
+ channel (grpc.Channel): A gRPC channel to the CAS endpoint.
|
|
49 |
+ instance (str, optional): the targeted instance's name.
|
|
50 |
+ u_uid (str, optional): a UUID for CAS transactions.
|
|
51 |
+ """
|
|
52 |
+ self.channel = channel
|
|
53 |
+ |
|
54 |
+ self.instance_name = instance
|
|
55 |
+ if u_uid is not None:
|
|
56 |
+ self.u_uid = u_uid
|
|
57 |
+ else:
|
|
58 |
+ self.u_uid = str(uuid.uuid4())
|
|
59 |
+ |
|
60 |
+ self.__bytestream_stub = bytestream_pb2_grpc.ByteStreamStub(self.channel)
|
|
61 |
+ self.__cas_stub = remote_execution_pb2_grpc.ContentAddressableStorageStub(self.channel)
|
|
62 |
+ |
|
63 |
+ self.__file_size_threshold = 1 * 1024 * 1024
|
|
64 |
+ self.__max_request_size = 2 * 1024 * 1024
|
|
65 |
+ |
|
66 |
+ self.__requests = dict()
|
|
67 |
+ self.__request_size = 0
|
|
68 |
+ |
|
69 |
+ def upload_file(self, file_path, queue=True):
|
|
70 |
+ """Stores a local file into the remote CAS storage.
|
|
71 |
+ |
|
72 |
+ If queuing is allowed (`queue=True`), the upload request **may** be
|
|
73 |
+ defer. An explicit call to :method:`flush` can force the request to be
|
|
74 |
+ send immediately (allong with the rest of the queued batch).
|
|
75 |
+ |
|
76 |
+ Args:
|
|
77 |
+ file_path (str): absolute or relative path to a local file.
|
|
78 |
+ queue (bool, optional): wheter or not the upload request may be
|
|
79 |
+ queued and submitted as part of a batch upload request. Defaults
|
|
80 |
+ to True.
|
|
81 |
+ |
|
82 |
+ Returns:
|
|
83 |
+ :obj:`Digest`: The digest of the file's content.
|
|
84 |
+ |
|
85 |
+ Raises:
|
|
86 |
+ OSError: If `file_path` does not exist or is not readable.
|
|
87 |
+ """
|
|
88 |
+ if not os.path.isabs(file_path):
|
|
89 |
+ file_path = os.path.abspath(file_path)
|
|
90 |
+ |
|
91 |
+ with open(file_path, 'rb') as bytes_steam:
|
|
92 |
+ file_bytes = bytes_steam.read()
|
|
93 |
+ |
|
94 |
+ if not queue or len(file_bytes) > self.__file_size_threshold:
|
|
95 |
+ blob_digest = self._send_blob(file_bytes)
|
|
96 |
+ else:
|
|
97 |
+ blob_digest = self._queue_blob(file_bytes)
|
|
98 |
+ |
|
99 |
+ return blob_digest
|
|
100 |
+ |
|
101 |
+ def upload_directory(self, directory, queue=True):
|
|
102 |
+ """Stores a :obj:`Directory` into the remote CAS storage.
|
|
103 |
+ |
|
104 |
+ If queuing is allowed (`queue=True`), the upload request **may** be
|
|
105 |
+ defer. An explicit call to :method:`flush` can force the request to be
|
|
106 |
+ send immediately (allong with the rest of the queued batch).
|
|
107 |
+ |
|
108 |
+ Args:
|
|
109 |
+ directory (:obj:`Directory`): a :obj:`Directory` object.
|
|
110 |
+ queue (bool, optional): wheter or not the upload request may be
|
|
111 |
+ queued and submitted as part of a batch upload request. Defaults
|
|
112 |
+ to True.
|
|
113 |
+ |
|
114 |
+ Returns:
|
|
115 |
+ :obj:`Digest`: The digest of the :obj:`Directory`.
|
|
116 |
+ """
|
|
117 |
+ if not isinstance(directory, remote_execution_pb2.Directory):
|
|
118 |
+ raise TypeError
|
|
119 |
+ |
|
120 |
+ if not queue:
|
|
121 |
+ return self._send_blob(directory.SerializeToString())
|
|
122 |
+ else:
|
|
123 |
+ return self._queue_blob(directory.SerializeToString())
|
|
124 |
+ |
|
125 |
+ def send_message(self, message):
|
|
126 |
+ """Stores a message into the remote CAS storage.
|
|
127 |
+ |
|
128 |
+ Args:
|
|
129 |
+ message (:obj:`Message`): a protobuf message object.
|
|
130 |
+ |
|
131 |
+ Returns:
|
|
132 |
+ :obj:`Digest`: The digest of the message.
|
|
133 |
+ """
|
|
134 |
+ return self._send_blob(message.SerializeToString())
|
|
135 |
+ |
|
136 |
+ def flush(self):
|
|
137 |
+ """Ensures any queued request gets sent."""
|
|
138 |
+ if self.__requests:
|
|
139 |
+ self._send_batch()
|
|
140 |
+ |
|
141 |
+ def _queue_blob(self, blob):
|
|
142 |
+ """Queues a memory block for later batch upload"""
|
|
143 |
+ blob_digest = remote_execution_pb2.Digest()
|
|
144 |
+ blob_digest.hash = HASH(blob).hexdigest()
|
|
145 |
+ blob_digest.size_bytes = len(blob)
|
|
146 |
+ |
|
147 |
+ if self.__request_size + len(blob) > self.__max_request_size:
|
|
148 |
+ self._send_batch()
|
|
149 |
+ |
|
150 |
+ update_request = remote_execution_pb2.BatchUpdateBlobsRequest.Request()
|
|
151 |
+ update_request.digest.CopyFrom(blob_digest)
|
|
152 |
+ update_request.data = blob
|
|
153 |
+ |
|
154 |
+ update_request_size = update_request.ByteSize()
|
|
155 |
+ if self.__request_size + update_request_size > self.__max_request_size:
|
|
156 |
+ self._send_batch()
|
|
157 |
+ |
|
158 |
+ self.__requests[update_request.digest.hash] = update_request
|
|
159 |
+ self.__request_size += update_request_size
|
|
160 |
+ |
|
161 |
+ return blob_digest
|
|
162 |
+ |
|
163 |
+ def _send_blob(self, blob):
|
|
164 |
+ """Sends a memory block using ByteStream.Write()"""
|
|
165 |
+ blob_digest = remote_execution_pb2.Digest()
|
|
166 |
+ blob_digest.hash = HASH(blob).hexdigest()
|
|
167 |
+ blob_digest.size_bytes = len(blob)
|
|
168 |
+ |
|
169 |
+ if self.instance_name is not None:
|
|
170 |
+ resource_name = '/'.join([self.instance_name, 'uploads', self.u_uid, 'blobs',
|
|
171 |
+ blob_digest.hash, str(blob_digest.size_bytes)])
|
|
172 |
+ else:
|
|
173 |
+ resource_name = '/'.join(['uploads', self.u_uid, 'blobs',
|
|
174 |
+ blob_digest.hash, str(blob_digest.size_bytes)])
|
|
175 |
+ |
|
176 |
+ def __write_request_stream(resource, content):
|
|
177 |
+ offset = 0
|
|
178 |
+ finished = False
|
|
179 |
+ remaining = len(content)
|
|
180 |
+ while not finished:
|
|
181 |
+ chunk_size = min(remaining, 64 * 1024)
|
|
182 |
+ remaining -= chunk_size
|
|
183 |
+ |
|
184 |
+ request = bytestream_pb2.WriteRequest()
|
|
185 |
+ request.resource_name = resource
|
|
186 |
+ request.data = content[offset:offset + chunk_size]
|
|
187 |
+ request.write_offset = offset
|
|
188 |
+ request.finish_write = remaining <= 0
|
|
189 |
+ |
|
190 |
+ yield request
|
|
191 |
+ |
|
192 |
+ offset += chunk_size
|
|
193 |
+ finished = request.finish_write
|
|
194 |
+ |
|
195 |
+ write_resquests = __write_request_stream(resource_name, blob)
|
|
196 |
+ # TODO: Handle connection loss/recovery using QueryWriteStatus()
|
|
197 |
+ write_response = self.__bytestream_stub.Write(write_resquests)
|
|
198 |
+ |
|
199 |
+ assert write_response.committed_size == blob_digest.size_bytes
|
|
200 |
+ |
|
201 |
+ return blob_digest
|
|
202 |
+ |
|
203 |
+ def _send_batch(self):
|
|
204 |
+ """Sends queued data using ContentAddressableStorage.BatchUpdateBlobs()"""
|
|
205 |
+ batch_request = remote_execution_pb2.BatchUpdateBlobsRequest()
|
|
206 |
+ batch_request.requests.extend(self.__requests.values())
|
|
207 |
+ if self.instance_name is not None:
|
|
208 |
+ batch_request.instance_name = self.instance_name
|
|
209 |
+ |
|
210 |
+ batch_response = self.__cas_stub.BatchUpdateBlobs(batch_request)
|
|
211 |
+ |
|
212 |
+ for response in batch_response.responses:
|
|
213 |
+ assert response.digest.hash in self.__requests
|
|
214 |
+ assert response.status.code is 0
|
|
215 |
+ |
|
216 |
+ self.__requests.clear()
|
|
217 |
+ self.__request_size = 0
|
... | ... | @@ -86,6 +86,11 @@ class ExecutionService(remote_execution_pb2_grpc.ExecutionServicer): |
86 | 86 |
yield operations_pb2.Operation()
|
87 | 87 |
|
88 | 88 |
def _get_instance(self, name):
|
89 |
+ # If client does not support multiple instances, it may omit the
|
|
90 |
+ # instance name request parameter, so better map our default:
|
|
91 |
+ if not name and len(self._instances) == 1:
|
|
92 |
+ name = next(iter(self._instances))
|
|
93 |
+ |
|
89 | 94 |
try:
|
90 | 95 |
return self._instances[name]
|
91 | 96 |
|
... | ... | @@ -13,6 +13,7 @@ |
13 | 13 |
# limitations under the License.
|
14 | 14 |
|
15 | 15 |
|
16 |
+from operator import attrgetter
|
|
16 | 17 |
import os
|
17 | 18 |
|
18 | 19 |
from buildgrid.settings import HASH
|
... | ... | @@ -99,7 +100,15 @@ def parse_to_pb2_from_fetch(pb2, stub, digest, instance_name=""): |
99 | 100 |
|
100 | 101 |
|
101 | 102 |
def create_digest(bytes_to_digest):
|
102 |
- """ Creates a hash based on the hex digest and returns the digest
|
|
103 |
+ """Computes the :obj:`Digest` of a piece of data.
|
|
104 |
+ |
|
105 |
+ The :obj:`Digest` of a data is a function of its hash **and** size.
|
|
106 |
+ |
|
107 |
+ Args:
|
|
108 |
+ bytes_to_digest (bytes): byte data to digest.
|
|
109 |
+ |
|
110 |
+ Returns:
|
|
111 |
+ :obj:`Digest`: The gRPC :obj:`Digest` for the given byte data.
|
|
103 | 112 |
"""
|
104 | 113 |
return remote_execution_pb2.Digest(hash=HASH(bytes_to_digest).hexdigest(),
|
105 | 114 |
size_bytes=len(bytes_to_digest))
|
... | ... | @@ -136,6 +145,197 @@ def file_maker(file_path, file_digest): |
136 | 145 |
is_executable=os.access(file_path, os.X_OK))
|
137 | 146 |
|
138 | 147 |
|
139 |
-def read_file(read):
|
|
140 |
- with open(read, 'rb') as f:
|
|
141 |
- return f.read()
|
|
148 |
+def directory_maker(directory_path, child_directories=None, cas=None, upload_directories=True):
|
|
149 |
+ """Creates a :obj:`Directory` from a local directory and possibly upload it.
|
|
150 |
+ |
|
151 |
+ Args:
|
|
152 |
+ directory_path (str): absolute or relative path to a local directory.
|
|
153 |
+ child_directories (list): output list of of children :obj:`Directory`
|
|
154 |
+ objects.
|
|
155 |
+ cas (:obj:`Uploader`): a CAS client uploader.
|
|
156 |
+ upload_directories (bool): wheter or not to upload the :obj:`Directory`
|
|
157 |
+ objects along with the files.
|
|
158 |
+ |
|
159 |
+ Returns:
|
|
160 |
+ :obj:`Directory`, :obj:`Digest`: Tuple of a new gRPC :obj:`Directory`
|
|
161 |
+ for the local directory pointed by `directory_path` and the digest
|
|
162 |
+ for that object.
|
|
163 |
+ """
|
|
164 |
+ if not os.path.isabs(directory_path):
|
|
165 |
+ directory_path = os.path.abspath(directory_path)
|
|
166 |
+ |
|
167 |
+ files, directories, symlinks = list(), list(), list()
|
|
168 |
+ for directory_entry in os.scandir(directory_path):
|
|
169 |
+ # Create a FileNode and corresponding BatchUpdateBlobsRequest:
|
|
170 |
+ if directory_entry.is_file(follow_symlinks=False):
|
|
171 |
+ if cas is not None:
|
|
172 |
+ node_digest = cas.upload_file(directory_entry.path)
|
|
173 |
+ else:
|
|
174 |
+ node_digest = create_digest(read_file(directory_entry.path))
|
|
175 |
+ |
|
176 |
+ node = remote_execution_pb2.FileNode()
|
|
177 |
+ node.name = directory_entry.name
|
|
178 |
+ node.digest.CopyFrom(node_digest)
|
|
179 |
+ node.is_executable = os.access(directory_entry.path, os.X_OK)
|
|
180 |
+ |
|
181 |
+ files.append(node)
|
|
182 |
+ |
|
183 |
+ # Create a DirectoryNode and corresponding BatchUpdateBlobsRequest:
|
|
184 |
+ elif directory_entry.is_dir(follow_symlinks=False):
|
|
185 |
+ node_directory, node_digest = directory_maker(directory_entry.path,
|
|
186 |
+ child_directories=child_directories,
|
|
187 |
+ upload_directories=upload_directories,
|
|
188 |
+ cas=cas)
|
|
189 |
+ |
|
190 |
+ node = remote_execution_pb2.DirectoryNode()
|
|
191 |
+ node.name = directory_entry.name
|
|
192 |
+ node.digest.CopyFrom(node_digest)
|
|
193 |
+ |
|
194 |
+ directories.append(node)
|
|
195 |
+ |
|
196 |
+ # Create a SymlinkNode if necessary;
|
|
197 |
+ elif os.path.islink(directory_entry.path):
|
|
198 |
+ node_target = os.readlink(directory_entry.path)
|
|
199 |
+ |
|
200 |
+ node = remote_execution_pb2.SymlinkNode()
|
|
201 |
+ node.name = directory_entry.name
|
|
202 |
+ node.target = node_target
|
|
203 |
+ |
|
204 |
+ symlinks.append(node)
|
|
205 |
+ |
|
206 |
+ files.sort(key=attrgetter('name'))
|
|
207 |
+ directories.sort(key=attrgetter('name'))
|
|
208 |
+ symlinks.sort(key=attrgetter('name'))
|
|
209 |
+ |
|
210 |
+ directory = remote_execution_pb2.Directory()
|
|
211 |
+ directory.files.extend(files)
|
|
212 |
+ directory.directories.extend(directories)
|
|
213 |
+ directory.symlinks.extend(symlinks)
|
|
214 |
+ |
|
215 |
+ if child_directories is not None:
|
|
216 |
+ child_directories.append(directory)
|
|
217 |
+ |
|
218 |
+ if cas is not None and upload_directories:
|
|
219 |
+ directory_digest = cas.upload_directory(directory)
|
|
220 |
+ else:
|
|
221 |
+ directory_digest = create_digest(directory.SerializeToString())
|
|
222 |
+ |
|
223 |
+ return directory, directory_digest
|
|
224 |
+ |
|
225 |
+ |
|
226 |
+def tree_maker(directory_path, cas=None):
|
|
227 |
+ """Creates a :obj:`Tree` from a local directory and possibly upload it.
|
|
228 |
+ |
|
229 |
+ If `cas` is specified, the local directory content will be uploded/stored
|
|
230 |
+ in remote CAS (the :obj:`Tree` message won't).
|
|
231 |
+ |
|
232 |
+ Args:
|
|
233 |
+ directory_path (str): absolute or relative path to a local directory.
|
|
234 |
+ cas (:obj:`Uploader`): a CAS client uploader.
|
|
235 |
+ |
|
236 |
+ Returns:
|
|
237 |
+ :obj:`Tree`, :obj:`Digest`: Tuple of a new gRPC :obj:`Tree` for the
|
|
238 |
+ local directory pointed by `directory_path` and the digest for that
|
|
239 |
+ object.
|
|
240 |
+ """
|
|
241 |
+ if not os.path.isabs(directory_path):
|
|
242 |
+ directory_path = os.path.abspath(directory_path)
|
|
243 |
+ |
|
244 |
+ child_directories = list()
|
|
245 |
+ directory, _ = directory_maker(directory_path, children=child_directories, cas=cas)
|
|
246 |
+ |
|
247 |
+ tree = remote_execution_pb2.Tree()
|
|
248 |
+ tree.children.extend(child_directories)
|
|
249 |
+ tree.root.CopyFrom(directory)
|
|
250 |
+ |
|
251 |
+ if cas is not None:
|
|
252 |
+ tree_digest = cas.send_message(tree)
|
|
253 |
+ else:
|
|
254 |
+ tree_digest = create_digest(tree.SerializeToString())
|
|
255 |
+ |
|
256 |
+ return tree, tree_digest
|
|
257 |
+ |
|
258 |
+ |
|
259 |
+def read_file(file_path):
|
|
260 |
+ """Loads raw file content in memory.
|
|
261 |
+ |
|
262 |
+ Args:
|
|
263 |
+ file_path (str): path to the target file.
|
|
264 |
+ |
|
265 |
+ Returns:
|
|
266 |
+ bytes: Raw file's content until EOF.
|
|
267 |
+ |
|
268 |
+ Raises:
|
|
269 |
+ OSError: If `file_path` does not exist or is not readable.
|
|
270 |
+ """
|
|
271 |
+ with open(file_path, 'rb') as byte_file:
|
|
272 |
+ return byte_file.read()
|
|
273 |
+ |
|
274 |
+ |
|
275 |
+def output_file_maker(file_path, input_path, cas=None):
|
|
276 |
+ """Creates an :obj:`OutputFile` from a local file and possibly upload it.
|
|
277 |
+ |
|
278 |
+ If `cas` is specified, the local file will be uploded/stored in remote CAS
|
|
279 |
+ (the :obj:`OutputFile` message won't).
|
|
280 |
+ |
|
281 |
+ Note:
|
|
282 |
+ `file_path` **must** point inside or be relative to `input_path`.
|
|
283 |
+ |
|
284 |
+ Args:
|
|
285 |
+ file_path (str): absolute or relative path to a local file.
|
|
286 |
+ input_path (str): absolute or relative path to the input root directory.
|
|
287 |
+ cas (:obj:`Uploader`): a CAS client uploader.
|
|
288 |
+ |
|
289 |
+ Returns:
|
|
290 |
+ :obj:`OutputFile`: a new gRPC :obj:`OutputFile` object for the file
|
|
291 |
+ pointed by `file_path`.
|
|
292 |
+ """
|
|
293 |
+ if not os.path.isabs(file_path):
|
|
294 |
+ file_path = os.path.abspath(file_path)
|
|
295 |
+ if not os.path.isabs(input_path):
|
|
296 |
+ input_path = os.path.abspath(input_path)
|
|
297 |
+ |
|
298 |
+ if cas is not None:
|
|
299 |
+ file_digest = cas.upload_file(file_path)
|
|
300 |
+ else:
|
|
301 |
+ file_digest = create_digest(read_file(file_path))
|
|
302 |
+ |
|
303 |
+ output_file = remote_execution_pb2.OutputFile()
|
|
304 |
+ output_file.digest.CopyFrom(file_digest)
|
|
305 |
+ # OutputFile.path should be relative to the working direcory:
|
|
306 |
+ output_file.path = os.path.relpath(file_path, start=input_path)
|
|
307 |
+ output_file.is_executable = os.access(file_path, os.X_OK)
|
|
308 |
+ |
|
309 |
+ return output_file
|
|
310 |
+ |
|
311 |
+ |
|
312 |
+def output_directory_maker(directory_path, working_path, cas=None):
|
|
313 |
+ """Creates an :obj:`OutputDirectory` from a local directory.
|
|
314 |
+ |
|
315 |
+ If `cas` is specified, the local directory content will be uploded/stored
|
|
316 |
+ in remote CAS (the :obj:`OutputDirectory` message won't).
|
|
317 |
+ |
|
318 |
+ Note:
|
|
319 |
+ `directory_path` **must** point inside or be relative to `input_path`.
|
|
320 |
+ |
|
321 |
+ Args:
|
|
322 |
+ directory_path (str): absolute or relative path to a local directory.
|
|
323 |
+ working_path (str): absolute or relative path to the working directory.
|
|
324 |
+ cas (:obj:`Uploader`): a CAS client uploader.
|
|
325 |
+ |
|
326 |
+ Returns:
|
|
327 |
+ :obj:`OutputDirectory`: a new gRPC :obj:`OutputDirectory` for the
|
|
328 |
+ directory pointed by `directory_path`.
|
|
329 |
+ """
|
|
330 |
+ if not os.path.isabs(directory_path):
|
|
331 |
+ directory_path = os.path.abspath(directory_path)
|
|
332 |
+ if not os.path.isabs(working_path):
|
|
333 |
+ working_path = os.path.abspath(working_path)
|
|
334 |
+ |
|
335 |
+ _, tree_digest = tree_maker(directory_path, cas=cas)
|
|
336 |
+ |
|
337 |
+ output_directory = remote_execution_pb2.OutputDirectory()
|
|
338 |
+ output_directory.tree_digest.CopyFrom(tree_digest)
|
|
339 |
+ output_directory.path = os.path.relpath(directory_path, start=working_path)
|
|
340 |
+ |
|
341 |
+ return output_directory
|