refactor(make): better checks for tool existence and paths (#327)

After all the refactoring done so far, we can run checks directly
inside of `make`, because we have auto-cleanup, temporary environments
and we don't need wrapper scripts anymore.

Part of https://github.com/ooni/probe/issues/1466.
This commit is contained in:
Simone Basso 2021-05-05 11:33:51 +02:00 committed by GitHub
parent 4bd1533c86
commit 5738c07aff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 64 deletions

View File

@ -1,16 +0,0 @@
#!/bin/sh
# print the fullpath of the go we're using so that it's part
# of the build logs and we can easily increase our confidence
# that we are using the right go binary.
echo -n "$0: checking for go... "
go=`command -v go`
if [ -z $go ]; then
echo "not found"
exit 1
fi
echo "$go"
set -x
$go "$@"

View File

@ -1,24 +0,0 @@
#!/bin/sh
# print the fullpath of the go/gomobile we're using so that it's part
# of the build logs and we can easily increase our confidence
# that we are using the right go/gomobile binary.
echo -n "$0: checking for go... "
go=`command -v go`
if [ -z $go ]; then
echo "not found"
exit 1
fi
echo "$go"
echo -n "$0: checking for gomobile... "
gomobile=`command -v gomobile`
if [ -z $go ]; then
echo "not found"
exit 1
fi
echo "$gomobile"
set -x
$gomobile "$@"

50
make
View File

@ -329,9 +329,13 @@ class CommandExecutor:
def require(self, *executable: str) -> None: def require(self, *executable: str) -> None:
"""require implements Engine.require.""" """require implements Engine.require."""
self._dry_runner.require(*executable) for exc in executable:
# Nothing else to do, because executable is fully self._dry_runner.require(exc)
# implemented by CommandDryRunner. fullpath = shutil.which(exc)
if not fullpath:
log("checking for {}... not found".format(exc))
sys.exit(1)
log("checking for {}... {}".format(exc, fullpath))
def run( def run(
self, self,
@ -345,13 +349,13 @@ class CommandExecutor:
def setenv(self, key: str, value: str) -> Optional[str]: def setenv(self, key: str, value: str) -> Optional[str]:
"""setenv implements Engine.setenv.""" """setenv implements Engine.setenv."""
# Nothing else to do, because executable is fully # Nothing else to do, because setenv is fully
# implemented by CommandDryRunner. # implemented by CommandDryRunner.
return self._dry_runner.setenv(key, value) return self._dry_runner.setenv(key, value)
def unsetenv(self, key: str) -> None: def unsetenv(self, key: str) -> None:
"""unsetenv implements Engine.unsetenv.""" """unsetenv implements Engine.unsetenv."""
# Nothing else to do, because executable is fully # Nothing else to do, because unsetenv is fully
# implemented by CommandDryRunner. # implemented by CommandDryRunner.
self._dry_runner.unsetenv(key) self._dry_runner.unsetenv(key)
@ -362,9 +366,6 @@ 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 __init__(self):
self.__cmdcache: Dict[str, str] = {}
def backticks( def backticks(
self, self,
output_variable: str, output_variable: str,
@ -396,17 +397,9 @@ class CommandDryRunner:
def require(self, *executable: str) -> None: def require(self, *executable: str) -> None:
"""require implements Engine.require.""" """require implements Engine.require."""
# implemented here because we want to see the results
# of checks when we're doing a dry run
for exc in executable: for exc in executable:
if exc in self.__cmdcache: log(f"./make: echo -n 'checking for {exc}... '")
continue # do not print checks more than once log("./make: command -v %s || { echo 'not found'; exit 1 }" % exc)
fullpath = shutil.which(exc)
if not fullpath:
log("./make: checking for {}... not found".format(exc))
sys.exit(1)
log("./make: checking for {}... {}".format(exc, fullpath))
self.__cmdcache[exc] = fullpath
def run( def run(
self, self,
@ -710,7 +703,7 @@ class OONIMKAllAAR:
# TODO(bassosimone): find a way to run this command without # TODO(bassosimone): find a way to run this command without
# adding extra dependencies to go.mod and go.sum. # adding extra dependencies to go.mod and go.sum.
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "go")) cmdline.append("go")
cmdline.append("get") cmdline.append("get")
cmdline.append("-u") cmdline.append("-u")
if options.verbose(): if options.verbose():
@ -720,6 +713,7 @@ class OONIMKAllAAR:
cmdline.append("golang.org/x/mobile/cmd/gomobile@latest") cmdline.append("golang.org/x/mobile/cmd/gomobile@latest")
with Environ(engine, "GOPATH", gopath()): with Environ(engine, "GOPATH", gopath()):
with AugmentedPath(engine, oonigo.binpath()): with AugmentedPath(engine, oonigo.binpath()):
engine.require("go")
engine.run(cmdline) engine.run(cmdline)
def _gomobile_init( def _gomobile_init(
@ -729,12 +723,13 @@ class OONIMKAllAAR:
android: SDKAndroid, android: SDKAndroid,
) -> None: ) -> None:
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "gomobile")) cmdline.append("gomobile")
cmdline.append("init") cmdline.append("init")
with Environ(engine, "ANDROID_HOME", android.home()): with Environ(engine, "ANDROID_HOME", android.home()):
with Environ(engine, "ANDROID_NDK_HOME", android.ndk_home()): with Environ(engine, "ANDROID_NDK_HOME", android.ndk_home()):
with AugmentedPath(engine, oonigo.binpath()): with AugmentedPath(engine, oonigo.binpath()):
with AugmentedPath(engine, os.path.join(gopath(), "bin")): with AugmentedPath(engine, os.path.join(gopath(), "bin")):
engine.require("gomobile", "go")
engine.run(cmdline) engine.run(cmdline)
def _gomobile_bind( def _gomobile_bind(
@ -745,7 +740,7 @@ class OONIMKAllAAR:
android: SDKAndroid, android: SDKAndroid,
) -> None: ) -> None:
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "gomobile")) cmdline.append("gomobile")
cmdline.append("bind") cmdline.append("bind")
if options.verbose(): if options.verbose():
cmdline.append("-v") cmdline.append("-v")
@ -765,6 +760,7 @@ class OONIMKAllAAR:
with Environ(engine, "ANDROID_NDK_HOME", android.ndk_home()): with Environ(engine, "ANDROID_NDK_HOME", android.ndk_home()):
with AugmentedPath(engine, oonigo.binpath()): with AugmentedPath(engine, oonigo.binpath()):
with AugmentedPath(engine, os.path.join(gopath(), "bin")): with AugmentedPath(engine, os.path.join(gopath(), "bin")):
engine.require("gomobile", "go")
engine.run(cmdline) engine.run(cmdline)
@ -881,7 +877,7 @@ class OONIMKAllFramework:
# TODO(bassosimone): find a way to run this command without # TODO(bassosimone): find a way to run this command without
# adding extra dependencies to go.mod and go.sum. # adding extra dependencies to go.mod and go.sum.
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "go")) cmdline.append("go")
cmdline.append("get") cmdline.append("get")
cmdline.append("-u") cmdline.append("-u")
if options.verbose(): if options.verbose():
@ -891,6 +887,7 @@ class OONIMKAllFramework:
cmdline.append("golang.org/x/mobile/cmd/gomobile@latest") cmdline.append("golang.org/x/mobile/cmd/gomobile@latest")
with AugmentedPath(engine, gogo.binpath()): with AugmentedPath(engine, gogo.binpath()):
with Environ(engine, "GOPATH", gopath()): with Environ(engine, "GOPATH", gopath()):
engine.require("go")
engine.run(cmdline) engine.run(cmdline)
def _gomobile_init( def _gomobile_init(
@ -899,10 +896,11 @@ class OONIMKAllFramework:
gogo: SDKGolangGo, gogo: SDKGolangGo,
) -> None: ) -> None:
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "gomobile")) cmdline.append("gomobile")
cmdline.append("init") cmdline.append("init")
with AugmentedPath(engine, os.path.join(gopath(), "bin")): with AugmentedPath(engine, os.path.join(gopath(), "bin")):
with AugmentedPath(engine, gogo.binpath()): with AugmentedPath(engine, gogo.binpath()):
engine.require("gomobile", "go")
engine.run(cmdline) engine.run(cmdline)
def _gomobile_bind( def _gomobile_bind(
@ -912,7 +910,7 @@ class OONIMKAllFramework:
gogo: SDKGolangGo, gogo: SDKGolangGo,
) -> None: ) -> None:
cmdline: List[str] = [] cmdline: List[str] = []
cmdline.append(os.path.join(".", "MOBILE", "gomobile")) cmdline.append("gomobile")
cmdline.append("bind") cmdline.append("bind")
if options.verbose(): if options.verbose():
cmdline.append("-v") cmdline.append("-v")
@ -930,6 +928,7 @@ class OONIMKAllFramework:
cmdline.append("./pkg/oonimkall") cmdline.append("./pkg/oonimkall")
with AugmentedPath(engine, os.path.join(gopath(), "bin")): with AugmentedPath(engine, os.path.join(gopath(), "bin")):
with AugmentedPath(engine, gogo.binpath()): with AugmentedPath(engine, gogo.binpath()):
engine.require("gomobile", "go")
engine.run(cmdline) engine.run(cmdline)
@ -980,6 +979,7 @@ 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
engine.require("cat", "sed")
output = engine.backticks("RELEASE", ["git", "describe", "--tags"]) output = engine.backticks("RELEASE", ["git", "describe", "--tags"])
release = output.decode("utf-8").strip() 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")
@ -1041,6 +1041,7 @@ class MiniOONIDarwinOrWindows:
with Environ(engine, "GOARCH", self.__arch): with Environ(engine, "GOARCH", self.__arch):
with Environ(engine, "CGO_ENABLED", "0"): with Environ(engine, "CGO_ENABLED", "0"):
with AugmentedPath(engine, gogo.binpath()): with AugmentedPath(engine, gogo.binpath()):
engine.require("go")
engine.run(cmdline) engine.run(cmdline)
@ -1089,6 +1090,7 @@ class MiniOONILinux:
with Environ(engine, "GOARCH", self.__arch): with Environ(engine, "GOARCH", self.__arch):
with Environ(engine, "CGO_ENABLED", "0"): with Environ(engine, "CGO_ENABLED", "0"):
with AugmentedPath(engine, gogo.binpath()): with AugmentedPath(engine, gogo.binpath()):
engine.require("go")
engine.run(cmdline) engine.run(cmdline)