0

Purpose

For each subdirectory in directory that contains a setup.py, run pip uninstall -y <directory name> and pip install .

Windows solution

> for /D %f in (.\*) do (cd "%cd%\%f" && set s=%f && set j=%s:~3% && pip uninstall %j% && pip install .)

EDIT: Looks like pip uninstall/reinstall can be done with:

(for %F in ("%cd%") do pip uninstall -y "%~nxF") & pip install .

Linux solution

#!/usr/bin/env bash

DIR="${DIR:-$PWD}
VENV="${VENV:-.venv}"
REQUIREMENTS="${REQUIREMENTS:-'requirements.txt'}";

if [ ! -d "$VENV/bin" ]; then
    echo Cannot find "$VENV/bin"
    exit 2;
fi

source "$VENV/bin/activate"

for f in "${DIR[@]}"; do
    if [ -f "$f/setup.py" ]; then
        builtin cd "$f";
        pip uninstall -y "${PWD##*/}";
        if [ -f "$REQUIREMENTS" ]; then
            pip install -r "$REQUIREMENTS"
        fi
        pip install .;
        builtin cd ..;
    fi;
done

As you can see, my Linux solution is much more versatile. Actually my Windows solution doesn't work.

The Windows solution is squashing the strings so things aren't deterministic between runs. Seems to be some weird parameter expansion going on. How am I meant to do it in CMD?

2
  • Are you open to Powershell? Have a play around with: get-childitem -ErrorAction SilentlyContinue -recurse "C:\folder\" -filter setup.py | foreach {write-host "pip uninstall -y " $_.directory.fullname; "pip install ."} as a starting point if you are? Commented Feb 26, 2017 at 13:10
  • "The Windows solution is squashing the strings so things aren't deterministic between runs" Please explain what you mean by this.
    – DavidPostill
    Commented Feb 26, 2017 at 13:35

1 Answer 1

0

That looks like an unfair comparison between a badly written batch one liner and a full fledged bash script (I agree that cmd.exe is much more limited).
Why do you strip off the first 3 chars from the folder name when uninstalling?

This batch might work:

@Echo off
for /D %%f in (*) do (
  If exist "%f\setup.py" (
    PushD "%%f"
    pip uninstall "%%f"
    pip install .
    PopD
  )
)
4
  • I strip because I get the ./; and it is an unfair advantage because the Windows one is very much a WiP. - Thanks, I'll give yours a shot.
    – A T
    Commented Feb 26, 2017 at 13:49
  • 1
    Since for /d works from the current path, there is no need to use .\* and with only * there is nothing prepended to the folder name. Converting from a one line I also missed to double the percent signs. Changed above.
    – LotPings
    Commented Feb 26, 2017 at 13:54
  • Thanks: I'll give that a shot. BTW: The PushD/PopD; is doing the cd right?
    – A T
    Commented Feb 28, 2017 at 7:08
  • 1
    Yes, pusd put's the current location on a stack and changes to the new folder. Popd returns to the previous folder.
    – LotPings
    Commented Feb 28, 2017 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .