Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ola-rozenfeld committed Jul 5, 2023
1 parent 131ca1f commit 1ab18ae
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions go/pkg/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ func (c *Client) prepCommand(ctx context.Context, client *rexec.Client, actionDi
if _, err := c.GrpcClient.ReadProto(ctx, cmdDg, commandProto); err != nil {
return nil, err
}
var inputRoot string
var nodeProperties map[string]*repb.NodeProperties
if actionRoot == "" {
fetchInputs := actionRoot == ""
if fetchInputs {
curTime := time.Now().Format(time.RFC3339)
actionRoot = filepath.Join(os.TempDir(), acDg.Hash+"_"+curTime)
inputRoot = filepath.Join(actionRoot, "input")
}
inputRoot := filepath.Join(actionRoot, "input")
var nodeProperties map[string]*repb.NodeProperties
if fetchInputs {
dg, err := digest.NewFromProto(actionProto.GetInputRootDigest())
if err != nil {
return nil, err
Expand All @@ -122,22 +124,8 @@ func (c *Client) prepCommand(ctx context.Context, client *rexec.Client, actionDi
nodeProperties[path] = t.NodeProperties
}
}
} else {
inputRoot = filepath.Join(actionRoot, "input")
npPath := filepath.Join(actionRoot, "input_node_properties.textproto")
if _, err := os.Stat(npPath); err == nil {
inTxt, err := os.ReadFile(npPath)
if err != nil {
return nil, fmt.Errorf("error reading input node properties from file: %v", err)
}
ipb := &cpb.InputSpec{}
if err := prototext.Unmarshal(inTxt, ipb); err != nil {
return nil, fmt.Errorf("error unmarshalling input node properties from file %s: %v", npPath, err)
}
nodeProperties = ipb.InputNodeProperties
} else if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("error accessing input node properties file: %v", err)
}
} else if nodeProperties, err = readNodePropertiesFromFile(filepath.Join(actionRoot, "input_node_properties.textproto")); err != nil {
return nil, err
}
contents, err := os.ReadDir(inputRoot)
if err != nil {
Expand All @@ -158,6 +146,24 @@ func (c *Client) prepCommand(ctx context.Context, client *rexec.Client, actionDi
return cmd, nil
}

func readNodePropertiesFromFile(path string) (nps map[string]*repb.NodeProperties, err error) {
if _, err = os.Stat(path); err == nil {
inTxt, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading input node properties from file: %v", err)
}
ipb := &cpb.InputSpec{}
if err := prototext.Unmarshal(inTxt, ipb); err != nil {
return nil, fmt.Errorf("error unmarshalling input node properties from file %s: %v", path, err)
}
return ipb.InputNodeProperties, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("error accessing input node properties file: %v", err)
}
return nil, nil
}

// DownloadActionResult downloads the action result of the given action digest
// if it exists in the remote cache.
func (c *Client) DownloadActionResult(ctx context.Context, actionDigest, pathPrefix string) error {
Expand Down Expand Up @@ -371,11 +377,11 @@ func (c *Client) DownloadAction(ctx context.Context, actionDigest, outputPath st
if err != nil {
return err
}
is := &cpb.InputSpec{InputNodeProperties: make(map[string]*repb.NodeProperties)}
ts, _, err := c.GrpcClient.DownloadDirectory(ctx, rDg, rootPath, filemetadata.NewNoopCache())
if err != nil {
return fmt.Errorf("error fetching input tree: %v", err)
}
is := &cpb.InputSpec{InputNodeProperties: make(map[string]*repb.NodeProperties)}
for path, t := range ts {
if t.NodeProperties != nil {
is.InputNodeProperties[path] = t.NodeProperties
Expand Down

0 comments on commit 1ab18ae

Please sign in to comment.