Skip to content

Commit

Permalink
remotetool: adding option to upload directory (bazelbuild#532)
Browse files Browse the repository at this point in the history
remotetool: adding option to upload directory
  • Loading branch information
ola-rozenfeld authored Feb 12, 2024
1 parent 6dd3970 commit 46b7334
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion go/cmd/remotetool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This tool supports common debugging operations concerning remotely executed
// actions:
// 1. Download a file or directory from remote cache by its digest.
// 1. Download/upload a file or directory from/to remote cache by its digest.
// 2. Display details of a remotely executed action.
// 3. Download action results by the action digest.
// 4. Re-execute remote action (with optional inputs override).
Expand Down Expand Up @@ -46,6 +46,7 @@ const (
checkDeterminism OpType = "check_determinism"
uploadBlob OpType = "upload_blob"
uploadBlobV2 OpType = "upload_blob_v2"
uploadDir OpType = "upload_dir"
)

var supportedOps = []OpType{
Expand All @@ -57,6 +58,7 @@ var supportedOps = []OpType{
executeAction,
checkDeterminism,
uploadBlob,
uploadDir,
}

var (
Expand Down Expand Up @@ -142,6 +144,11 @@ func main() {
log.Exitf("error uploading blob for digest %v: %v", getDigestFlag(), err)
}

case uploadDir:
if err := c.UploadDirectory(ctx, getPathFlag()); err != nil {
log.Exitf("error uploading directory for path %s: %v", getPathFlag(), err)
}

default:
log.Exitf("unsupported operation %v. Supported operations:\n%v", *operation, supportedOps)
}
Expand Down
14 changes: 14 additions & 0 deletions go/pkg/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ func (c *Client) DownloadDirectory(ctx context.Context, rootDigest, path string)
return err
}

// UploadDirectory uploads a directory from the specified path as a Merkle-tree to the remote cache.
func (c *Client) UploadDirectory(ctx context.Context, path string) error {
log.Infof("Computing Merkle tree rooted at %s", path)
root, blobs, stats, err := c.GrpcClient.ComputeMerkleTree(ctx, path, "", "", &command.InputSpec{Inputs: []string{"."}}, filemetadata.NewNoopCache())
if err != nil {
return err
}
log.Infof("Directory root digest: %v", root)
log.Infof("Directory stats: %d files, %d directories, %d symlinks, %d total bytes", stats.InputFiles, stats.InputDirectories, stats.InputSymlinks, stats.TotalInputBytes)
log.Infof("Uploading directory %v rooted at %s to CAS.", root, path)
_, _, err = c.GrpcClient.UploadIfMissing(ctx, blobs...)
return err
}

func (c *Client) writeProto(m proto.Message, baseName string) error {
f, err := os.Create(baseName)
if err != nil {
Expand Down

0 comments on commit 46b7334

Please sign in to comment.