Skip to content

Instantly share code, notes, and snippets.

@hradec
Last active February 24, 2025 01:14
Show Gist options
  • Save hradec/370dc46326256ed64ff58c7f59446765 to your computer and use it in GitHub Desktop.
Save hradec/370dc46326256ed64ff58c7f59446765 to your computer and use it in GitHub Desktop.
A windows batch file to quickly run python.sh in windows. It will download MSYS2 with all dependencies in the current folder, download python.sh and run it.
:: ================================================================================================
:: a wrapper script to run python.sh script in MSYS2 environment using bash
:: it will download and install MSYS2 in the current folder if it is not already installed
:: and then download python.sh script from the following URL and run it:
:: https://gist.github.com/hradec/32840097fe4cada3119468d8027155f9#file-python-sh
:: running python.sh in the MSYS2 environment allows to run the same linux python.sh script in
:: Windows environment without any changes!!!
:: ================================================================================================
@echo off
:: ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
:: LOCAL INSTALLED THINGS - VISUAL STUDIO components (modify these paths to match your installation)
:: ================================================================================================
:: VC_HOME => find -name cl.exe
set "VC_HOME=s:/VisualStudio/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/"
:: NINJA_HOME => find -name ninja.exe
set "NINJA_HOME=s:/VisualStudio/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/"
:: WSDK_HOME => find -name rc.exe
set "WSDK_HOME=s:/Windows Kits/10/bin/10.0.22621.0/x64"
:: ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
set "__PWD=%CD%"
:: folder where this script is located
set "_CMD_PATH=%~dp0"
:: _CD is the folder where we are installing everything (MSYS2, python.sh, etc.) - %CD%==current folder
set "_CD=%_CMD_PATH%"
:: make sure we are in the correct folder to download an install python.sh and msys2
cd /d "%_CD%"
:: check if we need to download and install MSYS2 (avoid downloading if it's already installed)
:: pass FORCE as the first argument to force the download and install of MSYS2
:: or set FORCE=1 in the environment variables before running this script
if not exist "%_CD%\msys64\usr\bin\bash.exe" (
set "execute_commands=true"
) else if "%FORCE%"=="1" (
set "execute_commands=true"
) else if "%@"=="FORCE" (
shift
set "execute_commands=true"
) else (
set "execute_commands=false"
)
:: setup MSYS2 environment to run linux python.sh script
if "%execute_commands%"=="true" (
curl -k -L https://repo.msys2.org/distrib/msys2-x86_64-latest.exe -o "%_CD%/msys2-x86_64-latest.exe"
"%_CD%/msys2-x86_64-latest.exe" in --confirm-command --accept-messages --root "%_CD%/msys64"
del "%_CD%/msys2-x86_64-latest.exe"
"%_CD%\msys64\usr\bin\bash.exe" -lc "pacman -S --noconfirm p7zip curl"
)
:: download python.sh script from github gist if not already downloaded
if not exist "%_CD%\python.sh" (
curl -k -L "https://gist.githubusercontent.com/hradec/32840097fe4cada3119468d8027155f9/raw/python.sh" -o "%_CD%\python.sh"
)
:: mangle the _CD path to be in the correct format to use inside MSYS2 bash when calling python.sh
set "__CD=%_CD: =@%"
for /f "tokens=*" %%n in ('"%_CD%msys64\usr\bin\cygpath.exe" -t mixed -a -m %__CD%') do @(set "CCD=%%n")
set "CCD=%CCD:\=/%"
set "CCD=%CCD:@=\ %"
setlocal enabledelayedexpansion
:: make sure paths in the arguments are in the correct format for MSYS2
set "ARGS=%*"
if not "%FORCE%"=="" (
set "ARGS=%ARGS:\=/%"
)
:: set the path to the MSYS2 environment
set "PATH=%_CD%\msys64\usr\bin;%_CD%\msys64\mingw64\usr\bin;%_CD%\msys64\usr\local\bin;%PATH%"
:: set visual studio if we have it
if exist "%VC_HOME%" (
call %VC_HOME%/../../../../../../../VC/Auxiliary/Build/vcvars64.bat
set DISTUTILS_USE_SDK=1
set "PATH=%NINJA_HOME%;%VC_HOME%;%WSDK_HOME%;%PATH%"
)
:: set the path to the ffmpeg static binaries (hack)
set "PATH=%CCD%\ffmpeg\6.0.0\win32\;%PATH%"
:: make sure we are in the correct folder
cd %__PWD%
set "__PWD=%__PWD:\=/%"
::"%_CD%\msys64\usr\bin\bash.exe" -c 'cd %CCD%; ./python.sh %ARGS%'
"%_CD%\msys64\usr\bin\bash.exe" -c 'cd %__PWD% ; %CCD%/python.sh %ARGS%'
if %ERRORLEVEL% NEQ 0 (
echo.
echo ERROR: Python script "%ARGS%" failed with error code %ERRORLEVEL%
echo.
)
:: pause
::)
@hradec
Copy link
Author

hradec commented Oct 19, 2024

In windows, you don't need to download python.sh anymore. This script will download it for you automatically and run it.

for reference, python.sh is here:
https://gist.github.com/hradec/32840097fe4cada3119468d8027155f9#file-python-sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment