Merge pull request #21 from mcataford/refactor/move-py-shims-to-mod

refactor(python): relocate py shims to module
This commit is contained in:
Marc 2024-01-28 22:00:04 -05:00 committed by GitHub
commit d039bb5d4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 98 additions and 17 deletions

View file

@ -1,28 +1,25 @@
package main package main
import ( import (
"maps"
"os" "os"
cli "v/cli" cli "v/cli"
logger "v/logger" logger "v/logger"
python "v/python"
state "v/state" state "v/state"
) )
var DIRECTORIES = []string{ var stateDirectories = []string{
"cache", "cache",
"runtimes", "runtimes",
"shims", "shims",
} }
var SHIMS = []string{ const defaultFilePermissions = 0775
"python",
"python3",
}
const DEFAULT_PERMISSION = 0775 func writeShim(shimPath string, shimCall string) error {
shimContent := []byte("#!/bin/bash\n" + shimCall)
func writeShim(shimPath string) error { if err := os.WriteFile(shimPath, shimContent, defaultFilePermissions); err != nil {
shimContent := []byte("#!/bin/bash\n$(v python which --raw) $@")
if err := os.WriteFile(shimPath, shimContent, DEFAULT_PERMISSION); err != nil {
return err return err
} }
@ -37,13 +34,22 @@ func Initialize(args []string, flags cli.Flags, currentState state.State) error
return nil return nil
} }
os.Mkdir(state.GetStatePath(), DEFAULT_PERMISSION) os.Mkdir(state.GetStatePath(), defaultFilePermissions)
for _, dir := range DIRECTORIES { logger.InfoLogger.Printf("Created state directory: %s\n", state.GetStatePath())
os.Mkdir(state.GetStatePath(dir), DEFAULT_PERMISSION) for _, dir := range stateDirectories {
newPath := state.GetStatePath(dir)
os.Mkdir(newPath, defaultFilePermissions)
logger.InfoLogger.Printf("Created %s\n", newPath)
} }
for _, shim := range SHIMS { allShims := map[string]string{}
writeShim(state.GetStatePath("shims", shim))
maps.Copy(allShims, python.Shims)
for shimName, shimContent := range allShims {
newShim := state.GetStatePath("shims", shimName)
writeShim(newShim, shimContent)
logger.InfoLogger.Printf("Created shim: %s\n", newShim)
} }
return nil return nil

View file

@ -5,6 +5,8 @@ import (
"os" "os"
"strings" "strings"
"testing" "testing"
cli "v/cli"
python "v/python"
state "v/state" state "v/state"
testutils "v/testutils" testutils "v/testutils"
) )
@ -14,7 +16,7 @@ func TestWriteShim(t *testing.T) {
os.Mkdir(state.GetStatePath("shims"), 0775) os.Mkdir(state.GetStatePath("shims"), 0775)
testShimPath := state.GetStatePath("shims", "testshim") testShimPath := state.GetStatePath("shims", "testshim")
e := writeShim(testShimPath) e := writeShim(testShimPath, "testcommand")
shimContent, _ := ioutil.ReadFile(testShimPath) shimContent, _ := ioutil.ReadFile(testShimPath)
@ -22,8 +24,58 @@ func TestWriteShim(t *testing.T) {
t.Errorf("Errored while writing shim") t.Errorf("Errored while writing shim")
} }
if !strings.Contains(string(shimContent), "$(v python which --raw) $@") { if !strings.Contains(string(shimContent), "testcommand") {
t.Errorf("Expected shim to contain pass-through via 'which', got %s", shimContent) t.Errorf("Expected shim to contain pass-through via 'which', got %s", shimContent)
} }
} }
func TestInitializeCreatesStateDirectories(t *testing.T) {
defer testutils.SetupAndCleanupEnvironment(t)()
err := Initialize([]string{}, cli.Flags{}, state.State{})
if err != nil {
t.Errorf("Unexpected error initializing")
}
if _, err = os.Stat(state.GetStatePath()); os.IsNotExist(err) {
t.Errorf("Root state directory not found")
}
if _, err = os.Stat(state.GetStatePath("shims")); os.IsNotExist(err) {
t.Errorf("Shims directory not found")
}
if _, err = os.Stat(state.GetStatePath("cache")); os.IsNotExist(err) {
t.Errorf("Cache directory not found")
}
if _, err = os.Stat(state.GetStatePath("runtimes")); os.IsNotExist(err) {
t.Errorf("Runtimes directory not found")
}
}
func TestInitializeCreatesAllPythonShims(t *testing.T) {
defer testutils.SetupAndCleanupEnvironment(t)()
err := Initialize([]string{}, cli.Flags{}, state.State{})
if err != nil {
t.Errorf("Unexpected error initializing")
}
expectedShims := python.Shims
for shimLabel, shimCall := range expectedShims {
shimContent, err := os.ReadFile(state.GetStatePath("shims", shimLabel))
if os.IsNotExist(err) {
t.Errorf("%s shim not created", shimLabel)
}
if !strings.Contains(string(shimContent), shimCall) {
t.Errorf("%s shim does not contain expected call (%s not in %s)", shimLabel, shimCall, shimContent)
}
}
}

11
python/README.md Normal file
View file

@ -0,0 +1,11 @@
# Python support
Managing Python versions is supported via `v python <command>`, see `v help` for a breakdown of what's available.
## Shims
Shims are configured in `python/shims.go`.
## Available versions
Versions are pulled from [Python's ftp repository](https://www.python.org/ftp/python/) as gzipped tarballs. Any version available where is available for installation via `v python install`.

12
python/shims.go Normal file
View file

@ -0,0 +1,12 @@
package python
var pythonShimCall = "$(v python which --raw) $@"
var pipShimCall = "$(v python which --raw) -m pip $@"
var Shims = map[string]string{
"python": pythonShimCall,
"python3": pythonShimCall,
"pip": pipShimCall,
"pip3": pipShimCall,
}