-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunPythonFull.bat
More file actions
118 lines (101 loc) · 4.55 KB
/
Copy pathRunPythonFull.bat
File metadata and controls
118 lines (101 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@echo off
REM This script launches the given Python script from the given directory
REM Chris Nyland
REM 2015-12-09
REM Last Updated: 2017-10-19
REM This script launches the given Python script taking into account several
REM situations. There are a total of three arguments that can be given.
REM The first is the full path to the directory where the script in question
REM resides. The second argument is the name of the script it self. The
REM third argument is optional and effects the way the script is launched.
REM Specifically there are two different ways to start the script in PDB
REM debugger mode and starting it in and IPython interactive session.
REM This script also searches for the existence of a virtual environment and
REM if found activates it and runs the script using the virtual environment.
REM The virtual environment is found by searching for a specified folder name
REM defined by the "np_virtualfolder" variable in the script below.
REM The Python installation to start the script with is determined by using the
REM Py Launcher program and the shebang line in the script file. If no
REM shebang line is present then the default Python installation found by
REM PyLauncher.
REM This will fail badly if the line endings are not Windows
REM Give the window a unique title
title PyLauncher Running %1 %2
REM Setup most of the variables that we need for the script
set np_virtualfolder=venv
set np_virtualscripts=%1\%np_virtualfolder%\Scripts\
set np_virtualactivate=%np_virtualscripts%activate.bat
set np_virtualdeactivate=%np_virtualscripts%deactivate.bat
set np_getexe=%~dp0np_getexecutable.txt
REM The pushd allows the script to deal with UNC paths however
REM if the batch script terminates abnormally then the drive letter
REM that was created by the script will remain.
pushd %1
REM Grab the first line of the script file
REM This will fail if the line endings are not windows
set /p firstline=<%2
REM If the virtual environment exists then activate it
REM echo %np_virtualactivate%
IF EXIST %np_virtualactivate% (
call %np_virtualactivate%
echo Virtual
)
REM If the first line is a shebang line which is identified as the first
REM two characters being "#!". Then write that line to a temp file else
REM wipe the temp file out since it is reused every time
IF "%firstline:~0,2%"=="#!" (
echo %firstline% > %np_getexe%
) ELSE (
break > %np_getexe%
)
REM Write code to the file that will print the path to the Python
REM executable used to stdout
(
echo import sys
echo print^(sys.executable^)
) >> %np_getexe%
REM Run the temp file using the PyLauncher and then save the output to the
REM variable np_pythonexecutable. Then get the directory in which the Python
REM executable resides.
for /f "delims=" %%i in ('py %np_getexe%') do set np_pythonexecutable="%%i"
for %%i in (%np_pythonexecutable%) do set np_pythondirectory=%%~dpi
REM This is setup for the IPython executable. I am doing it here because of
REM some nonsense about expanding variables that I honestly don't understand
REM but if I do this setup below in the if statements the variables won't get
REM set correctly. Basically what is happening here though is if this is a
REM virtual environment then the python.exe is in the Scripts folder with all
REM the other executables. So we don't need to add Scripts to path to get
REM to the IPython executable.
IF "%np_pythondirectory:~-8%"=="Scripts\" (
set np_pythonscriptspath=%np_pythondirectory%
) ELSE (
set np_pythonscriptspath=%np_pythondirectory%Scripts\
)
REM Set the path to the IPython executable and the error message if needed
set inp_pythonexecutable=%np_pythonscriptspath%ipython.exe
set ipythonerrormsg=IPython executable does not exist at %inp_pythonexecutable% running Python interactive instead
REM Print the Python version out to the screen
echo Running Python
REM echo %np_pythonexecutable%
%np_pythonexecutable% -c "import sys; print(sys.version)"
REM If the third argument is "pdb" then run the script under PDB debugger
REM mode. If the third argument is "ipython" then run the script under
REM a IPython interactive session with PDB mode enabled. Else finally
REM just run the script using the PyLauncher
IF "%3"=="pdb" (
%np_pythonexecutable% -m pdb %2
) ELSE IF "%3"=="ipython" (
IF EXIST "%inp_pythonexecutable%" (
"%inp_pythonexecutable%" -i --pdb %2
) ELSE (
echo %ipythonerrormsg%
%np_pythonexecutable% -i %2
)
) ELSE (
py -W once %2
)
REM Deactivate the virtual environment if it exists and then popd the
REM directory. After which we pause so that any error messages can be viewed.
IF EXIST %np_virtualdeactivate% call %np_virtualdeactivate%
popd
pause