go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/scripts/led-edit-recipe-wrapper.py (about)

     1  #!/usr/bin/env python3
     2  # Copyright 2021 The Fuchsia 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  """A script for testing recipe_wrapper using led.
     7  
     8  To launch a led job using your local modifications to recipe_wrapper, run this
     9  from the root of the infra/infra repo:
    10  
    11      led get-build -real-build ... | ./scripts/led-edit-recipe-wrapper.py | led launch
    12  """
    13  
    14  import json
    15  import os
    16  import subprocess
    17  import sys
    18  import tempfile
    19  
    20  
    21  def main():
    22      job = json.load(sys.stdin)
    23  
    24      # Clear the CIPD package info out of the `exe` field to force the build to
    25      # use the `luciexe` executable included in the CAS input instead.
    26      build = job["buildbucket"]["bbagent_args"]["build"]
    27      exe = build["exe"]
    28      exe.pop("cipd_package", None)
    29      exe.pop("cipd_version", None)
    30  
    31      # Make sure we build recipe_wrapper for the correct OS and arch used by
    32      # the build. This is best-effort, as it encodes infra implementation details
    33      # (names and allowed values of swarming dimensions) that may change in the
    34      # future.
    35      dimensions = {
    36          dim["key"]: dim["value"]
    37          for dim in build["infra"]["swarming"]["task_dimensions"]
    38      }
    39      os_dimension = dimensions.get("os", "Linux").lower()
    40      cpu_dimension = dimensions.get("cpu", "x64").lower()
    41      go_env = {
    42          "GOOS": {"linux": "linux", "mac": "darwin", "windows": "windows"}[os_dimension],
    43          "GOARCH": {"x64": "amd64", "arm64": "arm64"}[cpu_dimension],
    44          # Don't dynamically link against GLIBC.
    45          "CGO_ENABLED": "0",
    46      }
    47  
    48      infra_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    49  
    50      with tempfile.TemporaryDirectory() as build_dir:
    51          exe_name = "luciexe"
    52          if os_dimension == "windows":
    53              exe_name += ".exe"
    54          exe_path = os.path.join(build_dir, exe_name)
    55          subprocess.check_call(
    56              ["go", "build", "-mod=vendor", "-o", exe_path, "."],
    57              env={**os.environ, **go_env},
    58              # `go build` must be run from within the module containing
    59              # recipe_wrapper.
    60              cwd=os.path.join(infra_dir, "cmd", "recipe_wrapper"),
    61          )
    62  
    63          # `led edit-isolated` runs a command in a temporary directory that will
    64          # be used as the job's CAS input.
    65          subprocess.run(
    66              ["led", "edit-isolated", "mv", exe_path, "."],
    67              input=json.dumps(job),
    68              text=True,
    69              check=True,
    70          )
    71  
    72  
    73  if __name__ == "__main__":
    74      main()