Skip to content

Commit

Permalink
feat: make python runenv stub be more like python
Browse files Browse the repository at this point in the history
  • Loading branch information
DenKoren committed Sep 4, 2024
1 parent d4fdc09 commit 297e28a
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 23 deletions.
2 changes: 1 addition & 1 deletion runenv-java-stub.go → dump-args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

38 changes: 32 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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/",
Expand All @@ -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",

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions python-stub/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello from python!")
1 change: 1 addition & 0 deletions python-stub/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
click >= 8.0.0
77 changes: 66 additions & 11 deletions runenv-python-stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -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)
Expand All @@ -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...))
}
}
55 changes: 52 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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}"
Expand All @@ -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"
Expand Down

0 comments on commit 297e28a

Please sign in to comment.