Exemplo n.º 1
0
def PrepareBinhostUploads(input_proto, output_proto, config):
    """Return a list of files to upload to the binhost.

  See BinhostService documentation in api/proto/binhost.proto.

  Args:
    input_proto (PrepareBinhostUploadsRequest): The input proto.
    output_proto (PrepareBinhostUploadsResponse): The output proto.
    config (api_config.ApiConfig): The API call config.
  """
    if input_proto.sysroot.build_target.name:
        build_target_msg = input_proto.sysroot.build_target
    else:
        build_target_msg = input_proto.build_target
    sysroot_path = input_proto.sysroot.path

    if not sysroot_path and not build_target_msg.name:
        cros_build_lib.Die('Sysroot.path is required.')

    build_target = controller_util.ParseBuildTarget(build_target_msg)
    chroot = controller_util.ParseChroot(input_proto.chroot)

    if not sysroot_path:
        sysroot_path = build_target.root
    sysroot = sysroot_lib.Sysroot(sysroot_path)

    uri = input_proto.uri
    # For now, we enforce that all input URIs are Google Storage buckets.
    if not gs.PathIsGs(uri):
        raise ValueError('Upload URI %s must be Google Storage.' % uri)

    if config.validate_only:
        return controller.RETURN_CODE_VALID_INPUT

    parsed_uri = urllib.parse.urlparse(uri)
    upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
    upload_path = parsed_uri.path.lstrip('/')

    # Read all packages and update the index. The index must be uploaded to the
    # binhost for Portage to use it, so include it in upload_targets.
    uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
    index_path = binhost.UpdatePackageIndex(uploads_dir,
                                            upload_uri,
                                            upload_path,
                                            sudo=True)
    upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
    assert index_path.startswith(uploads_dir), (
        'expected index_path to start with uploads_dir')
    upload_targets.append(index_path[len(uploads_dir):])

    output_proto.uploads_dir = uploads_dir
    for upload_target in upload_targets:
        output_proto.upload_targets.add().path = upload_target.strip('/')
    def testGetPrebuiltsFilesBadFile(self):
        """GetPrebuiltsFiles dies if archive file does not exist."""
        packages_content = """\
ARCH: amd64
URI: gs://foo_prebuilts

CPV: package/prebuilt
    """
        osutils.WriteFile(os.path.join(self.root, 'Packages'),
                          packages_content)

        with self.assertRaises(LookupError):
            binhost.GetPrebuiltsFiles(self.root)
    def testGetPrebuiltsFiles(self):
        """GetPrebuiltsFiles returns all archives for all packages."""
        packages_content = """\
ARCH: amd64
URI: gs://foo_prebuilts

CPV: package/prebuilt_a

CPV: package/prebuilt_b
    """
        osutils.WriteFile(os.path.join(self.root, 'Packages'),
                          packages_content)
        osutils.WriteFile(os.path.join(self.root, 'package/prebuilt_a.tbz2'),
                          'a',
                          makedirs=True)
        osutils.WriteFile(os.path.join(self.root, 'package/prebuilt_b.tbz2'),
                          'b')

        actual = binhost.GetPrebuiltsFiles(self.root)
        expected = ['package/prebuilt_a.tbz2', 'package/prebuilt_b.tbz2']
        self.assertEqual(actual, expected)
    def testGetPrebuiltsFilesWithDebugSymbols(self):
        """GetPrebuiltsFiles returns debug symbols archive if specified in index."""
        packages_content = """\
ARCH: amd64
URI: gs://foo_prebuilts

CPV: package/prebuilt
DEBUG_SYMBOLS: yes
    """
        osutils.WriteFile(os.path.join(self.root, 'Packages'),
                          packages_content)
        osutils.WriteFile(os.path.join(self.root, 'package/prebuilt.tbz2'),
                          'foo',
                          makedirs=True)
        osutils.WriteFile(os.path.join(self.root,
                                       'package/prebuilt.debug.tbz2'),
                          'debug',
                          makedirs=True)

        actual = binhost.GetPrebuiltsFiles(self.root)
        expected = ['package/prebuilt.tbz2', 'package/prebuilt.debug.tbz2']
        self.assertEqual(actual, expected)