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

View file

@ -5,6 +5,8 @@ import (
"os"
"strings"
"testing"
cli "v/cli"
python "v/python"
state "v/state"
testutils "v/testutils"
)
@ -14,7 +16,7 @@ func TestWriteShim(t *testing.T) {
os.Mkdir(state.GetStatePath("shims"), 0775)
testShimPath := state.GetStatePath("shims", "testshim")
e := writeShim(testShimPath)
e := writeShim(testShimPath, "testcommand")
shimContent, _ := ioutil.ReadFile(testShimPath)
@ -22,8 +24,58 @@ func TestWriteShim(t *testing.T) {
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)
}
}
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,
}