| # Library to interact with the active hscloud checkout. This supersedes the |
| # hscloud_root environment variable once used in hscloud. |
| # |
| # Some of this could be implemented in Python instead of shelling out to a Go |
| # binary - but that way we have a single source of truth, even if it's janky. |
| # |
| # To use: |
| # |
| # from tools.hscloud import lib as hscloud |
| # |
| # And specify deps = [ "//tools/hscloud:python" ] in py_binary. |
| |
| import subprocess |
| |
| from rules_python.python.runfiles import runfiles |
| |
| |
| r = runfiles.Create() |
| |
| |
| def tool_location(): |
| """ |
| Return an absolute path to a built //tools/hscloud binary, ready to run. |
| """ |
| rloc = r.Rlocation("hscloud/tools/hscloud/hscloud_/hscloud") |
| if rloc is None: |
| raise Exception("Could not find location of hscloud - are you in a valid checkout?") |
| return rloc.strip() |
| |
| |
| def workspace_location(): |
| """Return an absolute path to the hscloud checkout.""" |
| return subprocess.check_output([tool_location(), "workspace"]).decode().strip() |
| |
| |
| def must_rlocation(runfile): |
| """Return an absolute path to a runfile, eg. a data depndency in sh_binary.""" |
| rloc = r.Rlocation(runfile) |
| if rloc is None: |
| msg = f"Could not find runfile {runfile}" |
| manifest = os.environ.get("RUNFILES_MANIFEST_FILE", "") |
| if manifest != "": |
| msg += f"; manifest file: {manifest}" |
| raise Exception(msg) |
| return rloc.strip() |