Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Fix Windows uninstall and repair broken bundled Python runtime (#35)

Teethat Kamsaif19fcc79ef1d
diff --git a/cli/internal/provision/provision.go b/cli/internal/provision/provision.go
index 8cc3fb8..999b2af 100644
--- a/cli/internal/provision/provision.go
+++ b/cli/internal/provision/provision.go
@@ -610,6 +610,25 @@ func PythonBin() string {
return filepath.Join(paths.RuntimeDir(), "python", "bin", "python3")
}
+func pythonHealthy(bin string) bool {
+ if !have(bin) {
+ return false
+ }
+ root := pythonRoot(bin)
+ if runtime.GOOS == "windows" {
+ return have(filepath.Join(root, "Lib", "encodings", "__init__.py"))
+ }
+ matches, err := filepath.Glob(filepath.Join(root, "lib", "python*", "encodings", "__init__.py"))
+ return err == nil && len(matches) > 0
+}
+
+func pythonRoot(bin string) string {
+ if runtime.GOOS == "windows" {
+ return filepath.Dir(bin)
+ }
+ return filepath.Dir(filepath.Dir(bin))
+}
+
// pythonAssetURL resolves a python-build-standalone install_only tarball for
// this platform via the GitHub latest-release API, so it tracks upstream
// without a hardcoded version that rots.
@@ -663,7 +682,11 @@ func pythonAssetURL() (url, name, sumsURL string, err error) {
func EnsurePython(requirements string) (string, error) {
bin := PythonBin()
- if !have(bin) {
+ if !pythonHealthy(bin) {
+ root := pythonRoot(bin)
+ if err := os.RemoveAll(root); err != nil {
+ return "", fmt.Errorf("remove corrupted Python runtime %s: %w (close any running podcli or Python subprocesses)", root, err)
+ }
url, name, sumsURL, err := pythonAssetURL()
if err != nil {
return "", err
@@ -683,8 +706,8 @@ func EnsurePython(requirements string) (string, error) {
if err != nil {
return "", err
}
- if !have(bin) {
- return "", fmt.Errorf("python missing after extraction")
+ if !pythonHealthy(bin) {
+ return "", fmt.Errorf("python runtime missing stdlib encodings after extraction")
}
}
if requirements != "" {
diff --git a/cli/internal/provision/python_test.go b/cli/internal/provision/python_test.go
new file mode 100644
index 0000000..b2e107f
--- /dev/null
+++ b/cli/internal/provision/python_test.go
@@ -0,0 +1,37 @@
+package provision
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+func TestPythonHealthyRequiresStdlibEncodings(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("PODCLI_HOME", home)
+ bin := PythonBin()
+ if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(bin, []byte("x"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if pythonHealthy(bin) {
+ t.Fatal("python without encodings should be unhealthy")
+ }
+
+ encodings := filepath.Join(pythonRoot(bin), "lib", "python3.12", "encodings", "__init__.py")
+ if runtime.GOOS == "windows" {
+ encodings = filepath.Join(pythonRoot(bin), "Lib", "encodings", "__init__.py")
+ }
+ if err := os.MkdirAll(filepath.Dir(encodings), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(encodings, []byte("x"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if !pythonHealthy(bin) {
+ t.Fatal("python with encodings should be healthy")
+ }
+}
diff --git a/cli/main.go b/cli/main.go
index 96bd90b..7b7140a 100644
--- a/cli/main.go
+++ b/cli/main.go
@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "runtime"
"strings"
"podcli/internal/backend"
@@ -373,10 +374,14 @@ func uninstall(args []string) int {
}
runningInUse := false
for _, p := range targets {
- if err := os.RemoveAll(p); err != nil {
- if pathContains(p, self) {
- runningInUse = true
+ if runtime.GOOS == "windows" && pathContains(p, self) {
+ runningInUse = true
+ if err := removeAllExcept(p, self); err != nil {
+ fmt.Fprintf(os.Stderr, " warning: could not remove %s: %v\n", p, err)
}
+ continue
+ }
+ if err := os.RemoveAll(p); err != nil {
fmt.Fprintf(os.Stderr, " warning: could not remove %s: %v\n", p, err)
}
}
@@ -425,6 +430,29 @@ func pathContains(dir, file string) bool {
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
+func removeAllExcept(root, keep string) error {
+ var paths []string
+ if err := filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+ paths = append(paths, p)
+ return nil
+ }); err != nil {
+ return err
+ }
+ for i := len(paths) - 1; i >= 0; i-- {
+ p := paths[i]
+ if pathContains(p, keep) {
+ continue
+ }
+ if err := os.RemoveAll(p); err != nil && !os.IsNotExist(err) {
+ return err
+ }
+ }
+ return nil
+}
+
func linkPointsTo(link, target string) bool {
dest, err := os.Readlink(link)
if err != nil {
diff --git a/cli/uninstall_test.go b/cli/uninstall_test.go
index cf887d0..b989a21 100644
--- a/cli/uninstall_test.go
+++ b/cli/uninstall_test.go
@@ -92,3 +92,34 @@ func TestPodcliLinksPreservesSelfOutsideManagedBin(t *testing.T) {
t.Fatalf("podcliLinks should preserve self outside managed bin: %v", got)
}
}
+
+func TestRemoveAllExceptKeepsRunningBinaryOnly(t *testing.T) {
+ root := t.TempDir()
+ keep := filepath.Join(root, "bin", "podcli"+paths.ExeSuffix())
+ sibling := filepath.Join(root, "bin", "helper.exe")
+ otherDir := filepath.Join(root, "runtime")
+ if err := os.MkdirAll(filepath.Dir(keep), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.MkdirAll(otherDir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ for _, p := range []string{keep, sibling, filepath.Join(otherDir, "x")} {
+ if err := os.WriteFile(p, []byte("x"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ if err := removeAllExcept(root, keep); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(keep); err != nil {
+ t.Fatalf("kept binary missing: %v", err)
+ }
+ if _, err := os.Stat(sibling); !os.IsNotExist(err) {
+ t.Fatalf("sibling should be removed, got %v", err)
+ }
+ if _, err := os.Stat(otherDir); !os.IsNotExist(err) {
+ t.Fatalf("other dir should be removed, got %v", err)
+ }
+}