Skip to content

Instantly share code, notes, and snippets.

@benley
Created January 13, 2018 23:48
Show Gist options
  • Save benley/e9dfdfef516aa024d50816ec92b7bd9a to your computer and use it in GitHub Desktop.
Save benley/e9dfdfef516aa024d50816ec92b7bd9a to your computer and use it in GitHub Desktop.
def _check_call(repo_ctx, cmd_and_args):
"""
Execute a command with arguments, fail if it returns non-zero.
Similar to subprocess.check_call.
"""
result = repo_ctx.execute(cmd_and_args, timeout=10)
if result.return_code != 0:
fail(("Non-zero return code({1}) when executing '{0}':\n" + "Stdout: {2}\n"
+ "Stderr: {3}").format(" ".join(cmd_and_args), result.return_code,
result.stdout, result.stderr))
def _patched_http_archive_impl(repo_ctx):
"""Download the repository and apply a patch to its root"""
repo_ctx.download_and_extract(
repo_ctx.attr.urls,
sha256=repo_ctx.attr.sha256,
stripPrefix=repo_ctx.attr.strip_prefix)
cmd = [
"patch", "-p1", "-d", repo_ctx.path("."),
"-i", repo_ctx.path(repo_ctx.attr.patch_file)
]
_check_call(repo_ctx, cmd)
patched_http_archive = repository_rule(
implementation = _patched_http_archive_impl,
attrs = {
"patch_file": attr.label(),
"urls": attr.string_list(default = []),
"sha256": attr.string(default = ""),
"strip_prefix": attr.string(default = ""),
},
)
"Download a http archive, unpack it, apply a patch."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment