Created
May 16, 2022 14:24
-
-
Save thejcannon/9ae12baf0fd3876f58e5a91285947139 to your computer and use it in GitHub Desktop.
gitstate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import subprocess | |
from pants.base.build_root import BuildRoot | |
from pants.core.target_types import FileSourceField | |
from pants.engine.fs import CreateDigest, FileContent, Snapshot | |
from pants.engine.rules import Get, _uncacheable_rule, collect_rules | |
from pants.engine.target import ( | |
GeneratedSources, | |
GenerateSourcesRequest, | |
OptionalSingleSourceField, | |
Target, | |
) | |
from pants.engine.unions import UnionRule | |
from pants.util.logging import LogLevel | |
logger = logging.getLogger(__name__) | |
class GitStateSourceField(OptionalSingleSourceField): | |
alias = "gitstate_source" | |
expected_num_files = 0 | |
class GenerateFileFromGitStateRequest(GenerateSourcesRequest): | |
input = GitStateSourceField | |
output = FileSourceField | |
class GitState(Target): | |
alias = "gitstate" | |
help = "A file containing the commit SHA." | |
core_fields = (GitStateSourceField,) | |
@_uncacheable_rule(level=LogLevel.DEBUG) | |
async def generate_commit_file( | |
request: GenerateFileFromGitStateRequest, | |
build_root: BuildRoot, | |
) -> GeneratedSources: | |
result = subprocess.run( | |
["git", "rev-parse", "HEAD"], | |
cwd=build_root.path, | |
check=True, | |
capture_output=True, | |
text=True, | |
) | |
snapshot = await Get( | |
Snapshot, | |
CreateDigest( | |
[ | |
FileContent( | |
"gitcommit", | |
result.stdout.encode("ascii"), | |
) | |
] | |
), | |
) | |
return GeneratedSources(snapshot) | |
def target_types(): | |
return [ | |
GitState, | |
] | |
def rules(): | |
return ( | |
*collect_rules(), | |
UnionRule(GenerateSourcesRequest, GenerateFileFromGitStateRequest), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment