From f3d5014627ce554afb01c9995305be73ddcc1e60 Mon Sep 17 00:00:00 2001 From: Korenevskiy Denis Date: Fri, 30 Aug 2024 13:53:12 +0300 Subject: [PATCH] feat: more complicated runenv stubs. python runenv stub --- package-lock.json | 12 +++++------ package.json | 47 ++++++++++++++++++++++++++++-------------- runenv-java-stub.go | 18 ++++++++++++++-- runenv-python-stub.go | 41 ++++++++++++++++++++++++++++++++++++ scripts/build.sh | 48 +++++++++++++++++++++++++------------------ 5 files changed, 123 insertions(+), 43 deletions(-) create mode 100644 runenv-python-stub.go diff --git a/package-lock.json b/package-lock.json index b2d7ab6..16f95e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@milaboratory/small-binaries", - "version": "1.7.3", + "version": "1.8.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@milaboratory/small-binaries", - "version": "1.7.3", + "version": "1.8.0", "license": "UNLICENSED", "devDependencies": { - "@milaboratory/pl-package-builder": "^2.6.1" + "@milaboratory/pl-package-builder": "^2.6.4" } }, "node_modules/@aws-crypto/crc32": { @@ -1039,9 +1039,9 @@ } }, "node_modules/@milaboratory/pl-package-builder": { - "version": "2.6.1", - "resolved": "https://npm.pkg.github.com/download/@milaboratory/pl-package-builder/2.6.1/e06473141c6ec994f922deeb4b710ece9d4287d4", - "integrity": "sha512-a3meDyVIU3zqF0JDta938K1wapPcAZBVMC4Ug8SUFh0YUeRKodBKm9YtECOM6pzKB4u+UwoEk7ZMV6WHBoFq2A==", + "version": "2.6.4", + "resolved": "https://npm.pkg.github.com/download/@milaboratory/pl-package-builder/2.6.4/96272574effa7d2ed198fd401d29ac29b73baaa0", + "integrity": "sha512-+lFzvT3ZuPjU6vRN+N02ZP2zT8L02MxLcPpawgpBBVb6fxp+UXgi/2KrFp3XP6vSQtHjn7pdPt3sVrJTNlj7sQ==", "dev": true, "dependencies": { "@aws-sdk/client-s3": "^3.623.0", diff --git a/package.json b/package.json index c34cb3e..4e31a4c 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "@milaboratory/small-binaries", - "version": "1.7.3", + "version": "1.8.0", "description": "Small cross-platform binaries, like 'sleep' or 'hello-world', suitable for test needs", "scripts": { "cleanup": "rm -rf ./pkg-*.tgz && rm -rf ./build/ && rm -rf ./dist/", "build": "npm run pkg:build", "pkg:build": "./scripts/build.sh", - "pkg:sign": "pl-pkg sign --all-platforms --sign-util='gcloud-kms-sign'", + "pkg:sign": "pl-pkg sign --all-platforms --sign-command=[\"gcloud-kms-sign\", \"{pkg}\", \"{pkg}.sig\"]", "pkg:publish": "pl-pkg publish packages --all-platforms", "pkg:release": "npm run pkg:build && npm run pkg:sign && npm run pkg:publish", @@ -32,7 +32,7 @@ "windows-x64": "./build/windows-x64/hello-world" }, "entrypoints": { - "hello-world": { "cmd": [ "{pkg}/main" ] } + "hello-world": { "cmd": [ "{pkg}/hello-world" ] } } } }, @@ -49,7 +49,7 @@ "windows-x64": "./build/windows-x64/sleep" }, "entrypoints": { - "sleep": { "cmd": [ "{pkg}/main" ] } + "sleep": { "cmd": [ "{pkg}/sleep" ] } } } }, @@ -66,24 +66,24 @@ "windows-x64": "./build/windows-x64/guided-command" }, "entrypoints": { - "guided-command": { "cmd": [ "{pkg}/main" ] } + "guided-command": { "cmd": [ "{pkg}/guided-command" ] } } } }, - "read-file-to-stdout-with-sleep": { + "read-with-sleep": { "binary": { "registry": { "name": "milaboratories" }, - "name": "common/read-file-to-stdout-with-sleep", + "name": "common/read-with-sleep", "version": "1.6.3", "roots": { - "linux-x64": "./build/linux-x64/read-file-to-stdout-with-sleep", - "linux-aarch64": "./build/linux-aarch64/read-file-to-stdout-with-sleep", - "macosx-x64": "./build/macosx-x64/read-file-to-stdout-with-sleep", - "macosx-aarch64": "./build/macosx-aarch64/read-file-to-stdout-with-sleep", - "windows-x64": "./build/windows-x64/read-file-to-stdout-with-sleep" + "linux-x64": "./build/linux-x64/read-with-sleep", + "linux-aarch64": "./build/linux-aarch64/read-with-sleep", + "macosx-x64": "./build/macosx-x64/read-with-sleep", + "macosx-aarch64": "./build/macosx-aarch64/read-with-sleep", + "windows-x64": "./build/windows-x64/read-with-sleep" }, "entrypoints": { - "read-file-to-stdout-with-sleep": { "cmd": [ "{pkg}/main" ] } + "read-file-to-stdout-with-sleep": { "cmd": [ "{pkg}/read-with-sleep" ] } } } }, @@ -100,9 +100,26 @@ "macosx-aarch64": "./build/macosx-aarch64/runenv-java-stub", "windows-x64": "./build/windows-x64/runenv-java-stub" }, - "binDir": ".", + "binDir": "bin/", "entrypointName": "runenv-java-stub" } + }, + "runenv-python-stub": { + "environment": { + "registry": { "name": "milaboratories" }, + "name": "common/runenv-python-stub", + "version": "1.0.0", + "type": "python", + "roots": { + "linux-x64": "./build/linux-x64/runenv-python-stub", + "linux-aarch64": "./build/linux-aarch64/runenv-python-stub", + "macosx-x64": "./build/macosx-x64/runenv-python-stub", + "macosx-aarch64": "./build/macosx-aarch64/runenv-python-stub", + "windows-x64": "./build/windows-x64/runenv-python-stub" + }, + "binDir": "bin/", + "entrypointName": "runenv-python-stub" + } } } }, @@ -111,6 +128,6 @@ ], "license": "UNLICENSED", "devDependencies": { - "@milaboratory/pl-package-builder": "^2.6.1" + "@milaboratory/pl-package-builder": "^2.6.4" } } diff --git a/runenv-java-stub.go b/runenv-java-stub.go index 8e73bc1..c65ad76 100644 --- a/runenv-java-stub.go +++ b/runenv-java-stub.go @@ -4,12 +4,26 @@ import ( "fmt" "os" "strings" + "time" ) func main() { + cmdName := os.Args[0] + if len(os.Args) == 1 { - fmt.Fprintf(os.Stdout, "run environment was started with no command and arguments") + fmt.Fprintf(os.Stdout, "%q of java run environment was started with no command and arguments", cmdName) + } + + const columnWidth = 8 + + argsReport := strings.Builder{} + + fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, "cmd", cmdName) + for i, a := range os.Args[1:] { + argStr := fmt.Sprintf("arg[%d]", i) + fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, argStr, a) } + fmt.Fprintf(&argsReport, "%*s = %d", columnWidth, "time", time.Now().Unix()) - fmt.Fprint(os.Stdout, strings.Join(os.Args[1:], "\n")) + fmt.Fprint(os.Stdout, argsReport.String()+"\n") } diff --git a/runenv-python-stub.go b/runenv-python-stub.go new file mode 100644 index 0000000..651457b --- /dev/null +++ b/runenv-python-stub.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "time" +) + +func main() { + cmdName := os.Args[0] + if len(os.Args) == 1 { + fmt.Fprintf(os.Stdout, "%q of python run environment was started with no command and arguments", cmdName) + } + + const venvDir = "./venv" + const columnWidth = 8 + + err := os.MkdirAll(venvDir, 0o750) + if err != nil { + panic(fmt.Errorf("python stub: failed to create %q dir: %w", venvDir, err)) + } + + argsReport := strings.Builder{} + + fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, "cmd", cmdName) + for i, a := range os.Args[1:] { + argStr := fmt.Sprintf("arg[%d]", i) + fmt.Fprintf(&argsReport, "%*s = %q\n", columnWidth, argStr, a) + } + fmt.Fprintf(&argsReport, "%*s = %d", columnWidth, "time", time.Now().Unix()) + + contentFileValue := argsReport.String() + "\n" + + contentFilePath := filepath.Join(venvDir, cmdName+".txt") + err = os.WriteFile(contentFilePath, []byte(contentFileValue), 0o640) + + fmt.Fprintf(os.Stdout, "file %q created\n", contentFilePath) + fmt.Fprint(os.Stdout, contentFileValue) +} diff --git a/scripts/build.sh b/scripts/build.sh index a6776a3..f6ca652 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -10,24 +10,28 @@ script_dir="$(cd "$(dirname "${0}")" && pwd)" cd "${script_dir}/.." build_binary() { - local _bin_name="${1}" - local _os_go="${2}" - local _arch_go="${3}" - local _os_reg="${4}" - local _arch_reg="${5}" - local _ext="${6:-}" + local _pkg_name="${1}" + local _go_name="${2}" + local _bin_name="${3}" + local _os_go="${4}" + local _arch_go="${5}" + local _os_reg="${6}" + local _arch_reg="${7}" + local _ext="${8:-}" local _platform="${_os_reg}-${_arch_reg}" printf "## os='%s', arch='%s':\n" "${_os_go}" "${_arch_go}" env GOOS="${_os_go}" GOARCH="${_arch_go}" \ go build \ - -o "${BUILD_DIR}/${_platform}/${_bin_name}/main${_ext}" \ - "./${_bin_name}.go" + -o "${BUILD_DIR}/${_platform}/${_pkg_name}/${_bin_name}${_ext}" \ + "./${_go_name}" } build_binaries() { - local _bin_name="${1}" + local _pkg_name="${1}" + local _go_name="${2}" + local _bin_name="${3}" # OS names mapping: # darwin -> macosx @@ -36,24 +40,28 @@ build_binaries() { # amd64 -> x64 # arm64 -> aarch64 - printf "\n# Building '%s'...\n" "${_bin_name}" + printf "\n# Building package '%s'. go='%s' bin='%s'...\n" "${_pkg_name}" "${_go_name}" "${_bin_name}" - build_binary "${_bin_name}" "windows" "amd64" "windows" "x64" ".exe" + build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "windows" "amd64" "windows" "x64" ".exe" - build_binary "${_bin_name}" "linux" "amd64" "linux" "x64" - build_binary "${_bin_name}" "linux" "arm64" "linux" "aarch64" + build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "linux" "amd64" "linux" "x64" + build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "linux" "arm64" "linux" "aarch64" - build_binary "${_bin_name}" "darwin" "amd64" "macosx" "x64" - build_binary "${_bin_name}" "darwin" "arm64" "macosx" "aarch64" + build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "darwin" "amd64" "macosx" "x64" + build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "darwin" "arm64" "macosx" "aarch64" } rm -rf "${script_dir}/${BUILD_DIR}" -build_binaries "runenv-java-stub" -build_binaries "hello-world" -build_binaries "guided-command" -build_binaries "sleep" -build_binaries "read-file-to-stdout-with-sleep" +build_binaries "runenv-java-stub" "runenv-java-stub.go" "bin/java" + +build_binaries "runenv-python-stub" "runenv-python-stub.go" "bin/python" +build_binaries "runenv-python-stub" "runenv-python-stub.go" "bin/pip" + +build_binaries "hello-world" "hello-world.go" "hello-world" +build_binaries "guided-command" "guided-command.go" "guided-command" +build_binaries "sleep" "sleep.go" "sleep" +build_binaries "read-with-sleep" "read-file-to-stdout-with-sleep.go" "read-with-sleep" pl-pkg build packages --all-platforms