refactor(make): simplify the implementation of backticks (#335)

See https://github.com/ooni/probe/issues/1466
This commit is contained in:
Simone Basso 2021-05-06 20:57:17 +02:00 committed by GitHub
parent 6b88730cc4
commit 1ea760cb5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

36
make
View File

@ -255,12 +255,8 @@ The third form of the command prints this help screen.
class Engine(Protocol): class Engine(Protocol):
"""Engine is an engine for building targets.""" """Engine is an engine for building targets."""
def backticks( def backticks(self, cmdline: List[str]) -> str:
self, """backticks executes the given command line and returns
output_variable: str,
cmdline: List[str],
) -> bytes:
"""backticks executes output_variable=`*cmdline` and returns
the output emitted by the command to the caller.""" the output emitted by the command to the caller."""
def cat_sed_redirect( def cat_sed_redirect(
@ -297,16 +293,11 @@ class CommandExecutor:
def __init__(self, dry_runner: Engine): def __init__(self, dry_runner: Engine):
self._dry_runner = dry_runner self._dry_runner = dry_runner
def backticks( def backticks(self, cmdline: List[str]) -> str:
self,
output_variable: str,
cmdline: List[str],
) -> bytes:
"""backticks implements Engine.backticks""" """backticks implements Engine.backticks"""
out = self._dry_runner.backticks(output_variable, cmdline)
# Nothing else to do, because backticks is fully # Nothing else to do, because backticks is fully
# implemented by CommandDryRunner. # implemented by CommandDryRunner.
return out return self._dry_runner.backticks(cmdline)
def cat_sed_redirect( def cat_sed_redirect(
self, patterns: List[Tuple[str, str]], source: str, dest: str self, patterns: List[Tuple[str, str]], source: str, dest: str
@ -366,20 +357,18 @@ class CommandDryRunner:
# Implementation note: here we try to log valid bash snippets # Implementation note: here we try to log valid bash snippets
# such that is really obvious what we are doing. # such that is really obvious what we are doing.
def backticks( def backticks(self, cmdline: List[str]) -> str:
self,
output_variable: str,
cmdline: List[str],
) -> bytes:
"""backticks implements Engine.backticks""" """backticks implements Engine.backticks"""
log("./make: {}=`{}`".format(output_variable, shlex.join(cmdline))) # The backticks command is used to gather information used by
# implemented here because we want to see the result of backticks # other commands. As such, it needs to always run. If it was not
# command invocations when we're doing a dry run # running, we could not correctly implement the `-n` flag. It's
# also a silent command, because it's not really part of the
# sequence of bash commands that are executed. ¯\_(ツ)_/¯
popen = subprocess.Popen(cmdline, stdout=subprocess.PIPE) popen = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
stdout = popen.communicate()[0] stdout = popen.communicate()[0]
if popen.returncode != 0: if popen.returncode != 0:
raise RuntimeError(popen.returncode) raise RuntimeError(popen.returncode)
return stdout return stdout.decode("utf-8").strip()
def cat_sed_redirect( def cat_sed_redirect(
self, patterns: List[Tuple[str, str]], source: str, dest: str self, patterns: List[Tuple[str, str]], source: str, dest: str
@ -989,8 +978,7 @@ class OONIMKAllPodspec:
log("./make: {}: already built".format(self._name)) log("./make: {}: already built".format(self._name))
return return
engine.require("cat", "sed") engine.require("cat", "sed")
output = engine.backticks("RELEASE", ["git", "describe", "--tags"]) release = engine.backticks(["git", "describe", "--tags"])
release = output.decode("utf-8").strip()
version = datetime.datetime.now().strftime("%Y.%m.%d-%H%M%S") version = datetime.datetime.now().strftime("%Y.%m.%d-%H%M%S")
engine.cat_sed_redirect( engine.cat_sed_redirect(
[("@VERSION@", version), ("@RELEASE@", release)], [("@VERSION@", version), ("@RELEASE@", release)],