Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

# Copyright (C) 2018 Bloomberg LP 

# 

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

# <http://www.apache.org/licenses/LICENSE-2.0> 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

 

 

from urllib.parse import urljoin 

from operator import attrgetter 

import os 

import socket 

import threading 

 

from buildgrid.settings import HASH, HASH_LENGTH, BROWSER_URL_FORMAT 

from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 

 

 

class BrowserURL: 

 

__url_markers = ( 

'%(instance)s', 

'%(type)s', 

'%(hash)s', 

'%(sizebytes)s', 

) 

 

def __init__(self, base_url, instance_name=None): 

"""Begins browser URL helper initialization.""" 

self.__base_url = base_url 

self.__initialized = False 

self.__url_spec = { 

'%(instance)s': instance_name or '', 

} 

 

def for_message(self, message_type, message_digest): 

"""Completes browser URL initialization for a protobuf message.""" 

if self.__initialized: 

return False 

 

self.__url_spec['%(type)s'] = message_type 

self.__url_spec['%(hash)s'] = message_digest.hash 

self.__url_spec['%(sizebytes)s'] = str(message_digest.size_bytes) 

 

self.__initialized = True 

return True 

 

def generate(self): 

"""Generates a browser URL string.""" 

if not self.__base_url or not self.__initialized: 

return None 

 

url_tail = BROWSER_URL_FORMAT 

 

for url_marker in self.__url_markers: 

if url_marker not in self.__url_spec: 

return None 

if url_marker not in url_tail: 

continue 

url_tail = url_tail.replace(url_marker, self.__url_spec[url_marker]) 

 

return urljoin(self.__base_url, url_tail) 

 

 

class Condition(threading.Condition): 

 

"""Subclass of ``threading.Condition`` with ``wait`` overridden. 

 

In this implementation, ``wait`` only releases the lock if other 

threads are actually waiting for the lock, otherwise it does nothing. 

 

""" 

 

def __init__(self, lock=None): 

super().__init__(lock=lock) 

self.thread_count = 0 

 

def __enter__(self): 

self.thread_count += 1 

return super().__enter__() 

 

def __exit__(self, *args): 

self.thread_count -= 1 

return super().__exit__(*args) 

 

def wait(self, timeout=None): 

"""Wait if other threads are trying to acquire the lock. 

 

If other threads have attempted to acquire the lock for this Condition 

using ``with``, this method behaves the same as ``wait`` on a regular 

``threading.Condition``. 

 

If only one thread has attempted to acquire the lock, then that thread 

must be the current thread. In that case, this method doesn't release 

the lock or wait at all, it simply returns ``True`` as if it had been 

woken by ``threading.Condition.notify``. 

 

""" 

if not super()._is_owned: 

raise RuntimeError("cannot wait on un-acquired lock") 

 

if self.thread_count > 1: 

return super().wait(timeout) 

return True 

 

 

def get_hostname(): 

"""Returns the hostname of the machine executing that function. 

 

Returns: 

str: Hostname for the current machine. 

""" 

return socket.gethostname() 

 

 

def get_hash_type(): 

"""Returns the hash type.""" 

hash_name = HASH().name 

if hash_name == "sha256": 

return remote_execution_pb2.DigestFunction.SHA256 

return remote_execution_pb2.DigestFunction.UNKNOWN 

 

 

def create_digest(bytes_to_digest): 

"""Computes the :obj:`Digest` of a piece of data. 

 

The :obj:`Digest` of a data is a function of its hash **and** size. 

 

Args: 

bytes_to_digest (bytes): byte data to digest. 

 

Returns: 

:obj:`Digest`: The :obj:`Digest` for the given byte data. 

""" 

return remote_execution_pb2.Digest(hash=HASH(bytes_to_digest).hexdigest(), 

size_bytes=len(bytes_to_digest)) 

 

 

def parse_digest(digest_string): 

"""Creates a :obj:`Digest` from a digest string. 

 

A digest string should alway be: ``{hash}/{size_bytes}``. 

 

Args: 

digest_string (str): the digest string. 

 

Returns: 

:obj:`Digest`: The :obj:`Digest` read from the string or None if 

`digest_string` is not a valid digest string. 

""" 

digest_hash, digest_size = digest_string.split('/') 

 

if len(digest_hash) == HASH_LENGTH and digest_size.isdigit(): 

return remote_execution_pb2.Digest(hash=digest_hash, 

size_bytes=int(digest_size)) 

 

return None 

 

 

def read_file(file_path): 

"""Loads raw file content in memory. 

 

Args: 

file_path (str): path to the target file. 

 

Returns: 

bytes: Raw file's content until EOF. 

 

Raises: 

OSError: If `file_path` does not exist or is not readable. 

""" 

with open(file_path, 'rb') as byte_file: 

return byte_file.read() 

 

 

def write_file(file_path, content): 

"""Dumps raw memory content to a file. 

 

Args: 

file_path (str): path to the target file. 

content (bytes): raw file's content. 

 

Raises: 

OSError: If `file_path` does not exist or is not writable. 

""" 

with open(file_path, 'wb') as byte_file: 

byte_file.write(content) 

byte_file.flush() 

 

 

def merkle_tree_maker(directory_path): 

"""Walks a local folder tree, generating :obj:`FileNode` and 

:obj:`DirectoryNode`. 

 

Args: 

directory_path (str): absolute or relative path to a local directory. 

 

Yields: 

:obj:`Message`, bytes, str: a tutple of either a :obj:`FileNode` or 

:obj:`DirectoryNode` message, the corresponding blob and the 

corresponding node path. 

""" 

directory_name = os.path.basename(directory_path) 

 

# Actual generator, yields recursively FileNodes and DirectoryNodes: 

def __merkle_tree_maker(directory_path, directory_name): 

if not os.path.isabs(directory_path): 

directory_path = os.path.abspath(directory_path) 

 

directory = remote_execution_pb2.Directory() 

 

files, directories, symlinks = [], [], [] 

for directory_entry in os.scandir(directory_path): 

node_name, node_path = directory_entry.name, directory_entry.path 

 

if directory_entry.is_file(follow_symlinks=False): 

node_blob = read_file(directory_entry.path) 

node_digest = create_digest(node_blob) 

 

node = remote_execution_pb2.FileNode() 

node.name = node_name 

node.digest.CopyFrom(node_digest) 

node.is_executable = os.access(node_path, os.X_OK) 

 

files.append(node) 

 

yield node, node_blob, node_path 

 

elif directory_entry.is_dir(follow_symlinks=False): 

node, node_blob, _ = yield from __merkle_tree_maker(node_path, node_name) 

 

directories.append(node) 

 

yield node, node_blob, node_path 

 

# Create a SymlinkNode; 

elif os.path.islink(directory_entry.path): 

node_target = os.readlink(directory_entry.path) 

 

node = remote_execution_pb2.SymlinkNode() 

node.name = directory_entry.name 

node.target = node_target 

 

symlinks.append(node) 

 

files.sort(key=attrgetter('name')) 

directories.sort(key=attrgetter('name')) 

symlinks.sort(key=attrgetter('name')) 

 

directory.files.extend(files) 

directory.directories.extend(directories) 

directory.symlinks.extend(symlinks) 

 

node_blob = directory.SerializeToString() 

node_digest = create_digest(node_blob) 

 

node = remote_execution_pb2.DirectoryNode() 

node.name = directory_name 

node.digest.CopyFrom(node_digest) 

 

return node, node_blob, directory_path 

 

node, node_blob, node_path = yield from __merkle_tree_maker(directory_path, 

directory_name) 

 

yield node, node_blob, node_path 

 

 

def output_file_maker(file_path, input_path, file_digest): 

"""Creates an :obj:`OutputFile` from a local file and possibly upload it. 

 

Note: 

`file_path` **must** point inside or be relative to `input_path`. 

 

Args: 

file_path (str): absolute or relative path to a local file. 

input_path (str): absolute or relative path to the input root directory. 

file_digest (:obj:`Digest`): the underlying file's digest. 

 

Returns: 

:obj:`OutputFile`: a new :obj:`OutputFile` object for the file pointed 

by `file_path`. 

""" 

if not os.path.isabs(file_path): 

file_path = os.path.abspath(file_path) 

if not os.path.isabs(input_path): 

input_path = os.path.abspath(input_path) 

 

output_file = remote_execution_pb2.OutputFile() 

output_file.digest.CopyFrom(file_digest) 

# OutputFile.path should be relative to the working directory 

output_file.path = os.path.relpath(file_path, start=input_path) 

output_file.is_executable = os.access(file_path, os.X_OK) 

 

return output_file 

 

 

def output_directory_maker(directory_path, working_path, tree_digest): 

"""Creates an :obj:`OutputDirectory` from a local directory. 

 

Note: 

`directory_path` **must** point inside or be relative to `input_path`. 

 

Args: 

directory_path (str): absolute or relative path to a local directory. 

working_path (str): absolute or relative path to the working directory. 

tree_digest (:obj:`Digest`): the underlying folder tree's digest. 

 

Returns: 

:obj:`OutputDirectory`: a new :obj:`OutputDirectory` for the directory 

pointed by `directory_path`. 

""" 

if not os.path.isabs(directory_path): 

directory_path = os.path.abspath(directory_path) 

if not os.path.isabs(working_path): 

working_path = os.path.abspath(working_path) 

 

output_directory = remote_execution_pb2.OutputDirectory() 

output_directory.tree_digest.CopyFrom(tree_digest) 

output_directory.path = os.path.relpath(directory_path, start=working_path) 

 

return output_directory