github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/git/git_configure.bzl (about)

     1  """Repository rule for Git autoconfiguration.
     2  
     3  `git_configure` depends on the following environment variables:
     4  
     5    * `PYTHON_BIN_PATH`: location of python binary.
     6  """
     7  
     8  _PYTHON_BIN_PATH = "PYTHON_BIN_PATH"
     9  
    10  def _fail(msg):
    11      """Output failure message when auto configuration fails."""
    12      red = "\033[0;31m"
    13      no_color = "\033[0m"
    14      fail("%sGit Configuration Error:%s %s\n" % (red, no_color, msg))
    15  
    16  def _get_python_bin(repository_ctx):
    17      """Gets the python bin path."""
    18      python_bin = repository_ctx.os.environ.get(_PYTHON_BIN_PATH)
    19      if python_bin != None:
    20          return python_bin
    21      python_bin_path = repository_ctx.which("python")
    22      if python_bin_path != None:
    23          return str(python_bin_path)
    24      _fail("Cannot find python in PATH, please make sure " +
    25            "python is installed and add its directory in PATH, or --define " +
    26            "%s='/something/else'.\nPATH=%s" % (
    27                _PYTHON_BIN_PATH,
    28                repository_ctx.os.environ.get("PATH", ""),
    29            ))
    30  
    31  def _git_conf_impl(repository_ctx):
    32      repository_ctx.template(
    33          "BUILD",
    34          Label("//third_party/git:BUILD.tpl"),
    35      )
    36  
    37      tensorflow_root_path = str(repository_ctx.path(
    38          Label("@org_tensorflow//:BUILD"),
    39      ))[:-len("BUILD")]
    40      python_script_path = repository_ctx.path(
    41          Label("@org_tensorflow//tensorflow/tools/git:gen_git_source.py"),
    42      )
    43      generated_files_path = repository_ctx.path("gen")
    44  
    45      r = repository_ctx.execute(
    46          ["test", "-f", "%s/.git/logs/HEAD" % tensorflow_root_path],
    47      )
    48      if r.return_code == 0:
    49          unused_var = repository_ctx.path(Label("//:.git/HEAD"))  # pylint: disable=unused-variable
    50  
    51      result = repository_ctx.execute([
    52          _get_python_bin(repository_ctx),
    53          python_script_path,
    54          "--configure",
    55          tensorflow_root_path,
    56          "--gen_root_path",
    57          generated_files_path,
    58      ], quiet = False)
    59  
    60      if not result.return_code == 0:
    61          _fail(result.stderr)
    62  
    63  git_configure = repository_rule(
    64      implementation = _git_conf_impl,
    65      environ = [
    66          _PYTHON_BIN_PATH,
    67      ],
    68  )