refactor(python): relocate py shims to module

This commit is contained in:
Marc 2024-01-28 16:54:23 -05:00
parent 3f776f972d
commit 9dc0d039d9
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 35 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

@ -14,7 +14,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,7 +22,7 @@ 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)
}

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,
}