refactor(make): backticks: return output directly ()

The previous fix allowed us to return values directly from
implementations of Engine. Here we take advantage of this
possibility by immediately refactoring how backticks work.

Part of https://github.com/ooni/probe/issues/1466.
This commit is contained in:
Simone Basso 2021-05-05 10:42:39 +02:00 committed by GitHub
parent 80ef3e4b1b
commit 42dd9cb55e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

23
make

@ -258,9 +258,9 @@ class Engine(Protocol):
self, self,
output_variable: str, output_variable: str,
cmdline: List[str], cmdline: List[str],
output: List[bytes], ) -> bytes:
) -> None: """backticks executes output_variable=`*cmdline` and returns
"""backticks executes output_variable=`*cmdline`.""" the output emitted by the command to the caller."""
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
@ -300,12 +300,12 @@ class CommandExecutor:
self, self,
output_variable: str, output_variable: str,
cmdline: List[str], cmdline: List[str],
output: List[bytes], ) -> bytes:
) -> None:
"""backticks implements Engine.backticks""" """backticks implements Engine.backticks"""
self._dry_runner.backticks(output_variable, cmdline, output) 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
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
@ -371,8 +371,7 @@ class CommandDryRunner:
self, self,
output_variable: str, output_variable: str,
cmdline: List[str], cmdline: List[str],
output: List[bytes], ) -> bytes:
) -> None:
"""backticks implements Engine.backticks""" """backticks implements Engine.backticks"""
log("./make: {}=`{}`".format(output_variable, shlex.join(cmdline))) log("./make: {}=`{}`".format(output_variable, shlex.join(cmdline)))
# implemented here because we want to see the result of backticks # implemented here because we want to see the result of backticks
@ -381,7 +380,7 @@ class CommandDryRunner:
stdout = popen.communicate()[0] stdout = popen.communicate()[0]
if popen.returncode != 0: if popen.returncode != 0:
raise RuntimeError(popen.returncode) raise RuntimeError(popen.returncode)
output.append(stdout) return stdout
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
@ -437,7 +436,6 @@ class CommandDryRunner:
log("./make: unset {}".format(key)) log("./make: unset {}".format(key))
def new_engine(options: Options) -> Engine: def new_engine(options: Options) -> Engine:
"""new_engine creates a new engine instance""" """new_engine creates a new engine instance"""
out: Engine = CommandDryRunner() out: Engine = CommandDryRunner()
@ -1008,9 +1006,8 @@ class OONIMKAllPodspec:
if os.path.isfile(self.__name) and not options.dry_run(): if os.path.isfile(self.__name) and not options.dry_run():
log("./make: {}: already built".format(self.__name)) log("./make: {}: already built".format(self.__name))
return return
output: List[bytes] = [] output = engine.backticks("RELEASE", ["git", "describe", "--tags"])
engine.backticks("RELEASE", ["git", "describe", "--tags"], output) release = output.decode("utf-8").strip()
release = output[0].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)],