diff --git a/runenv-java-stub.go b/dump-args.go similarity index 83% rename from runenv-java-stub.go rename to dump-args.go index c65ad76..c6b3618 100644 --- a/runenv-java-stub.go +++ b/dump-args.go @@ -11,7 +11,7 @@ func main() { cmdName := os.Args[0] if len(os.Args) == 1 { - fmt.Fprintf(os.Stdout, "%q of java run environment was started with no command and arguments", cmdName) + fmt.Fprintf(os.Stdout, "%q was started with no arguments", cmdName) } const columnWidth = 8 diff --git a/package-lock.json b/package-lock.json index 0a01978..9425552 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@milaboratory/small-binaries", - "version": "1.8.1", + "version": "1.9.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@milaboratory/small-binaries", - "version": "1.8.1", + "version": "1.9.0", "license": "UNLICENSED", "devDependencies": { "@milaboratory/pl-package-builder": "^2.8.0" diff --git a/package.json b/package.json index 0230367..e4f43eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@milaboratory/small-binaries", - "version": "1.8.1", + "version": "1.9.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/", @@ -11,7 +11,8 @@ "pkg:publish": "pl-pkg publish packages --all-platforms --skip-existing-packages", "pkg:release": "npm run pkg:build && npm run pkg:publish", - "descriptors:build": "pl-pkg build descriptors", + "descriptors:b:runenv": "pl-pkg build descriptors --entrypoint runenv-python-stub --entrypoint runenv-java-stub", + "descriptors:build": "npm run descriptors:b:runenv && pl-pkg build descriptors", "descriptors:publish": "pl-pkg publish descriptors", "descriptors:release": "npm run descriptors:build && npm run descriptors:publish", @@ -85,9 +86,9 @@ }, "binDir": "bin/" }, + "runenv-python-stub": { - "registry": { "name": "milaboratories" }, - "name": "common/runenv-python-stub", + "registry": "milaboratories" , "version": "1.0.0", "type": "environment", "runtime": "python", @@ -98,11 +99,20 @@ "macosx-aarch64": "./build/macosx-aarch64/runenv-python-stub", "windows-x64": "./build/windows-x64/runenv-python-stub" }, - "binDir": "bin/", - "entrypointName": "runenv-python-stub" + "binDir": "bin/" } }, "entrypoints": { + "runenv-java-stub": { + "environment": { + "artifact": "runenv-java-stub" + } + }, + "runenv-python-stub": { + "environment": { + "artifact": "runenv-python-stub" + } + }, "hello-world": { "binary": { "artifact": "hello-world", @@ -121,6 +131,22 @@ "cmd": [ "{pkg}/guided-command" ] } }, + "stub-python-script": { + "binary": { + "artifact": { + "type": "python", + "registry": "milaboratories", + "version": "1.0.0", + "environment": ":runenv-python-stub", + "dependencies": { + "toolset": "pip", + "requirements": "requirements.txt" + }, + "root": "./python-stub" + }, + "cmd": [ "python", "{pkg}/hello.py" ] + } + }, "read-file-to-stdout-with-sleep": { "binary": { "artifact": "read-with-sleep", diff --git a/python-stub/hello.py b/python-stub/hello.py new file mode 100644 index 0000000..382ea10 --- /dev/null +++ b/python-stub/hello.py @@ -0,0 +1 @@ +print("Hello from python!") diff --git a/python-stub/requirements.txt b/python-stub/requirements.txt new file mode 100644 index 0000000..bc7218b --- /dev/null +++ b/python-stub/requirements.txt @@ -0,0 +1 @@ +click >= 8.0.0 diff --git a/runenv-python-stub.go b/runenv-python-stub.go index 651457b..0c2bb1b 100644 --- a/runenv-python-stub.go +++ b/runenv-python-stub.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "path/filepath" "strings" @@ -14,14 +15,8 @@ func main() { 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) @@ -31,11 +26,71 @@ func main() { } fmt.Fprintf(&argsReport, "%*s = %d", columnWidth, "time", time.Now().Unix()) - contentFileValue := argsReport.String() + "\n" + fmt.Fprint(os.Stdout, argsReport.String()+"\n") + + if isVenvCreation(os.Args) { + createFakeVenv(os.Args) + fmt.Fprintf(os.Stdout, "venv directory created") + return + } +} + +func isVenvCreation(args []string) bool { + if len(args) < 3 { + return false + } + + return strings.HasSuffix(args[0], "python") && + args[1] == "-m" && args[2] == "venv" +} + +func createFakeVenv(args []string) { + venvDir := args[len(args)-1] + binDir := filepath.Join(venvDir, "bin") + + must( + os.MkdirAll(binDir, 0o750), + "python stub: failed to create %q dir", binDir, + ) + + must(copyFile(args[0], filepath.Join(binDir, "python")), "failed to put 'python' into %q", binDir) + must(copyFile(args[0], filepath.Join(binDir, "pip")), "failed to put 'pip' into %q", binDir) +} + +func copyFile(src, dst string) error { + sourceFile, err := os.Open(src) + if err != nil { + return err + } + defer sourceFile.Close() + + destFile, err := os.Create(dst) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, sourceFile) + if err != nil { + return err + } + + sourceInfo, err := os.Stat(src) + if err != nil { + return err + } + + err = os.Chmod(dst, sourceInfo.Mode()) + if err != nil { + return err + } - contentFilePath := filepath.Join(venvDir, cmdName+".txt") - err = os.WriteFile(contentFilePath, []byte(contentFileValue), 0o640) + return nil +} - fmt.Fprintf(os.Stdout, "file %q created\n", contentFilePath) - fmt.Fprint(os.Stdout, contentFileValue) +func must(err error, msg string, args ...any) { + if err != nil { + args = append(args, err) + panic(fmt.Errorf(msg+": %w", args...)) + } } diff --git a/scripts/build.sh b/scripts/build.sh index f6ca652..881e914 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -9,6 +9,14 @@ set -o pipefail script_dir="$(cd "$(dirname "${0}")" && pwd)" cd "${script_dir}/.." +pkg_content_root() { + local _pkg_name="${1}" + local _os_reg="${2}" + local _arch_reg="${3}" + + echo "${BUILD_DIR}/${_os_reg}-${_arch_reg}/${_pkg_name}" +} + build_binary() { local _pkg_name="${1}" local _go_name="${2}" @@ -19,15 +27,28 @@ build_binary() { local _arch_reg="${7}" local _ext="${8:-}" - local _platform="${_os_reg}-${_arch_reg}" + local _pkg_root="$(pkg_content_root "${_pkg_name}" "${_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}/${_pkg_name}/${_bin_name}${_ext}" \ + -o "${_pkg_root}/${_bin_name}${_ext}" \ "./${_go_name}" } +additional_file() { + local _pkg_name="${1}" + local _os_reg="${2}" + local _arch_reg="${3}" + local _src="${4}" + local _target_name="${5}" + + local _pkg_root="$(pkg_content_root "${_pkg_name}" "${_os_reg}" "${_arch_reg}")" + + cp -R "${_src}" "${_pkg_root}/${_target_name}" + +} + build_binaries() { local _pkg_name="${1}" local _go_name="${2}" @@ -51,9 +72,37 @@ build_binaries() { build_binary "${_pkg_name}" "${_go_name}" "${_bin_name}" "darwin" "arm64" "macosx" "aarch64" } +add_file() { + local _pkg_name="${1}" + local _src="${2}" + local _dst="${3}" + + local _pkg_root + + _pkg_root="$(pkg_content_root "${_pkg_name}" "windows" "x64")" + mkdir -p "${_pkg_root}" + cp -R "${_src}" "${_pkg_root}/${_dst}" + + _pkg_root="$(pkg_content_root "${_pkg_name}" "linux" "x64")" + mkdir -p "${_pkg_root}" + cp -R "${_src}" "${_pkg_root}/${_dst}" + + _pkg_root="$(pkg_content_root "${_pkg_name}" "linux" "aarch64")" + mkdir -p "${_pkg_root}" + cp -R "${_src}" "${_pkg_root}/${_dst}" + + _pkg_root="$(pkg_content_root "${_pkg_name}" "macosx" "x64")" + mkdir -p "${_pkg_root}" + cp -R "${_src}" "${_pkg_root}/${_dst}" + + _pkg_root="$(pkg_content_root "${_pkg_name}" "macosx" "aarch64")" + mkdir -p "${_pkg_root}" + cp -R "${_src}" "${_pkg_root}/${_dst}" +} + rm -rf "${script_dir}/${BUILD_DIR}" -build_binaries "runenv-java-stub" "runenv-java-stub.go" "bin/java" +build_binaries "runenv-java-stub" "dump-args.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"