Skip to content

Commit

Permalink
feat: more complicated runenv stubs. python runenv stub
Browse files Browse the repository at this point in the history
  • Loading branch information
DenKoren committed Aug 30, 2024
1 parent f64b374 commit f3d5014
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 43 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 32 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",

Expand All @@ -32,7 +32,7 @@
"windows-x64": "./build/windows-x64/hello-world"
},
"entrypoints": {
"hello-world": { "cmd": [ "{pkg}/main" ] }
"hello-world": { "cmd": [ "{pkg}/hello-world" ] }
}
}
},
Expand All @@ -49,7 +49,7 @@
"windows-x64": "./build/windows-x64/sleep"
},
"entrypoints": {
"sleep": { "cmd": [ "{pkg}/main" ] }
"sleep": { "cmd": [ "{pkg}/sleep" ] }
}
}
},
Expand All @@ -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" ] }
}
}
},
Expand All @@ -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"
}
}
}
},
Expand All @@ -111,6 +128,6 @@
],
"license": "UNLICENSED",
"devDependencies": {
"@milaboratory/pl-package-builder": "^2.6.1"
"@milaboratory/pl-package-builder": "^2.6.4"
}
}
18 changes: 16 additions & 2 deletions runenv-java-stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
41 changes: 41 additions & 0 deletions runenv-python-stub.go
Original file line number Diff line number Diff line change
@@ -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)
}
48 changes: 28 additions & 20 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit f3d5014

Please sign in to comment.