blob: 2ecf81c8c7d84fbc743f4007f78bea9f7ff28674 [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Portage Binhost operations."""
7
8from __future__ import print_function
9
10import urlparse
11
12from chromite.api.gen.chromite.api import binhost_pb2
13from chromite.lib import gs
14from chromite.service import binhost
15
16
17def PrepareBinhostUploads(input_proto, output_proto):
18 """Return a list of files to uplooad to the binhost.
19
20 See BinhostService documentation in api/proto/binhost.proto.
21
22 Args:
23 input_proto (PrepareBinhostUploadsRequest): The input proto.
24 output_proto (PrepareBinhostUploadsResponse): The output proto.
25 """
26 target = input_proto.build_target.name
27 uri = input_proto.uri
28
29 # For now, we enforce that all input URIs are Google Storage buckets.
30 if not gs.PathIsGs(uri):
31 raise ValueError('Upload URI %s must be Google Storage.' % uri)
32
33 parsed_uri = urlparse.urlparse(uri)
34 upload_uri = gs.GetGsURL(parsed_uri.netloc)
35 upload_path = parsed_uri.path
36
37 # Read all packages and update the index. The index must be uploaded to the
38 # binhost for Portage to use it, so include it in upload_targets.
39 uploads_dir = binhost.GetPrebuiltsRoot(target)
40 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
41 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path)
42 assert index_path.startswith(uploads_dir), (
43 'expected index_path to start with uploads_dir')
44 upload_targets.append(index_path[len(uploads_dir):])
45
46 output_proto.uploads_dir = uploads_dir
47 for upload_target in upload_targets:
48 output_proto.upload_targets.add().path = upload_target.strip('/')
49
50
51def SetBinhost(input_proto, output_proto):
52 """Set the URI for a given binhost key and build target.
53
54 See BinhostService documentation in api/proto/binhost.proto.
55
56 Args:
57 input_proto (SetBinhostRequest): The input proto.
58 output_proto (SetBinhostResponse): The output proto.
59 """
60 target = input_proto.build_target.name
61 key = binhost_pb2.BinhostKey.Name(input_proto.key)
62 uri = input_proto.uri
63 private = input_proto.private
64 output_proto.output_file = binhost.SetBinhost(target, key, uri,
65 private=private)