Skip to content

Commit 51e5e9c

Browse files
committed
move pieces of initialization from env_for_icons.bat to winpythonIni.py
1 parent b1c23bc commit 51e5e9c

2 files changed

Lines changed: 30 additions & 35 deletions

File tree

winpython/portable/launchers_final/scripts/WinPythonIni.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,36 @@ def main():
7676
my_lines += ["QT_API=pyqt5"]
7777
if (PYPANDOC_PANDOC := WINPYDIRBASE / "t" / "pandoc.exe").is_file():
7878
my_lines += [f"PYPANDOC_PANDOC={PYPANDOC_PANDOC}"]
79-
79+
8080
# theorical option: a "winpython.ini" file as an initial parameter
8181
if len(args) >=2 and args[1].endswith("winpython.ini"):
8282
file_name = args[1]
8383
args = args[1:]
8484
else:
8585
file_name = "..\\settings\\winpython.ini"
86+
87+
# env_for_icons.bat default directory logic transfered to WinpythonIni.py
88+
89+
env['WINPYWORKDIR'] = WINPYWORKDIR = WINPYDIRBASE / "Notebooks"
90+
my_lines += [f"WINPYWORKDIR={WINPYWORKDIR}"]
91+
92+
# supposing winpython.ini is given the %* parameters to apply in python the [tricky] change of directory logic
93+
# if a file or directory is in %1 parameter, and current directory is not the icon nor scripts, we use that directory to define WINPYWORKDIR1
94+
WINPYWORKDIR1 = WINPYWORKDIR
95+
if len(args) >=2:
96+
if Path(args[1:1]).isfile():
97+
WINPYWORKDIR1 = Path(args[1:1]).parent
98+
if Path(args[1:1]).isdir():
99+
WINPYWORKDIR1 = Path(args[1:1])
100+
101+
# if WinPython launched from another directory than icon origin, it's not a Drag&Drop so keep current directory
102+
CD_directory = Path(os.getcwd())
103+
dp0_directory = Path(__file__).parent
104+
if CD_directory != dp0_directory and CD_directory / "scripts" != dp0_directory:
105+
WINPYWORKDIR1 = CD_directory
106+
env['WINPYWORKDIR'] = WINPYWORKDIR1
107+
my_lines += [f"WINPYWORKDIR1={WINPYWORKDIR1}"]
108+
86109
# classic WinpythonIni.py actions: digesting winpython.ini file
87110
my_lines += get_file(file_name).splitlines()
88111

@@ -119,12 +142,13 @@ def main():
119142
if segment == "debug" and data[0].strip() == "state":
120143
txt += f"{prefix}WINPYDEBUG={data[1].strip()}{postfix}"
121144

122-
print(txt)
123-
124-
# set potential directory
125-
for i in ('HOME', 'WINPYWORKDIR'):
145+
# create potential directory
146+
for i in ('HOME', 'WINPYWORKDIR', 'WINPYWORKDIR1'):
126147
if i in env:
127148
os.makedirs(Path(env[i]), exist_ok=True)
149+
# output to push change upward
150+
print(txt)
151+
128152
# later_version:
129153
# p = subprocess.Popen(["start", "cmd", "/k", "set"], shell = True)
130154
# p.wait() # I can wait until finished (although it too finishes after start finishes)

winpython/portable/launchers_final/scripts/env_for_icons.bat

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
@echo off
22
call "%~dp0env.bat"
33

4-
rem default is as before: Winpython ..\Notebooks
5-
set WINPYWORKDIR=%WINPYDIRBASE%\Notebooks
6-
set WINPYWORKDIR1=%WINPYWORKDIR%
7-
8-
rem if we have a file or directory in %1 parameter, we use that directory to define WINPYWORKDIR1
9-
if not "%~1"=="" (
10-
if exist "%~1" (
11-
if exist "%~1\" (
12-
rem echo it is a directory %~1
13-
set WINPYWORKDIR1=%~1
14-
) else (
15-
rem echo it is a file %~1, so we take the directory %~dp1
16-
set WINPYWORKDIR1=%~dp1
17-
)
18-
)
19-
) else (
20-
rem if it is launched from another directory than icon origin , we keep it that one echo %__CD__%
21-
if not "%__CD__%"=="%~dp0" if not "%__CD__%scripts\"=="%~dp0" set WINPYWORKDIR1="%__CD__%"
22-
)
23-
rem remove potential doublequote
24-
set WINPYWORKDIR1=%WINPYWORKDIR1:"=%
25-
rem remove some potential last \
26-
if "%WINPYWORKDIR1:~-1%"=="\" set WINPYWORKDIR1=%WINPYWORKDIR1:~0,-1%
27-
284
rem you can use winpython.ini to change defaults
29-
FOR /F "delims=" %%i IN ('""%WINPYDIR%\python.exe" "%~dp0WinpythonIni.py""') DO set winpythontoexec=%%i
5+
FOR /F "delims=" %%i IN ('""%WINPYDIR%\python.exe" "%~dp0WinpythonIni.py" %*"') DO set winpythontoexec=%%i
306
%winpythontoexec%set winpythontoexec=
317

32-
33-
rem Preventive Working Directories creation if needed
34-
if not "%WINPYWORKDIR%"=="" if not exist "%WINPYWORKDIR%" mkdir "%WINPYWORKDIR%"
35-
if not "%WINPYWORKDIR1%"=="" if not exist "%WINPYWORKDIR1%" mkdir "%WINPYWORKDIR1%"
36-
378
rem Change of directory only if we are in a launcher directory
389
if "%__CD__%scripts\"=="%~dp0" cd/D %WINPYWORKDIR1%
3910
if "%__CD__%"=="%~dp0" cd/D %WINPYWORKDIR1%

0 commit comments

Comments
 (0)