Skip to content

Commit

Permalink
feat(extractors): add a passthrough bazel extractor (#4354)
Browse files Browse the repository at this point in the history
* feat(extractors): add a passthrough bazel extractor

* chore: address comments
  • Loading branch information
shahms committed Feb 6, 2020
1 parent 939f3ea commit e22e87a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kythe/go/extractors/cmd/bazel/copy_kzip/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//tools:build_rules/shims.bzl", "go_binary")

package(default_visibility = ["//kythe:default_visibility"])

go_binary(
name = "copy_kzip",
srcs = ["copy_kzip.go"],
deps = [
"//kythe/go/extractors/bazel",
"//kythe/go/platform/vfs",
],
)
88 changes: 88 additions & 0 deletions kythe/go/extractors/cmd/bazel/copy_kzip/copy_kzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020 The Kythe Authors. All rights reserved.
*
* 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.
*/

// Program copy_kzip implements a Bazel extra action that directly copies a Kythe
// compilation record produced by a "spawn" action.
package main

import (
"context"
"flag"
"io"
"log"
"os"
"time"

"kythe.io/kythe/go/extractors/bazel"
"kythe.io/kythe/go/platform/vfs"
)

var (
outputPath = flag.String("output", "", "Path of output index file (required)")
extraAction = flag.String("extra_action", "", "Path of the SpawnAction file (required)")
)

func main() {
flag.Parse()

// Verify that required flags are set.
if *outputPath == "" {
log.Fatal("You must provide a non-empty --output file path")
}
if *extraAction == "" {
log.Fatal("You must provide a non-empty --extra_action file path")
}

info, err := bazel.LoadAction(*extraAction)
if err != nil {
log.Fatalf("Unable to load extra action: %v", err)
}
start := time.Now()
ai, err := bazel.SpawnAction(info)
if len(ai.Outputs) != 1 {
log.Fatalf("Can only copy a single output kzip, not %d", len(ai.Outputs))
}

if err := os.Link(ai.Outputs[0], *outputPath); err != nil {
log.Printf("Unable to link input to output, falling back to copy: %v", err)
if err := copyFile(ai.Outputs[0], *outputPath); err != nil {
log.Fatalf("Unable to copy input to output: %v", err)
}
}

log.Printf("Finished extracting [%v elapsed]", time.Since(start))
}

func copyFile(input, output string) error {
ctx := context.Background()
f, err := vfs.Open(ctx, input)
if err != nil {
return err
}
defer f.Close()

o, err := vfs.Create(ctx, output)
if err != nil {
return err
}

if _, err := io.Copy(o, f); err != nil {
o.Close()
return err
}

return o.Close()
}

0 comments on commit e22e87a

Please sign in to comment.