Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d9bfb9

Browse files
committed
extracting Winpythonini.py out of make.py
1 parent ba94d4d commit 0d9bfb9

File tree

3 files changed

+116
-108
lines changed

3 files changed

+116
-108
lines changed

make.py

+11-107
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,15 @@ def _create_launchers(self):
501501
shutil.copy2(item, self.winpy_dir)
502502
self._print_action_done()
503503

504+
def _copy_default_scripts(self):
505+
"""Copies launchers and defeult scripts."""
506+
self._print_action("copying pre-made scripts")
507+
origin = PORTABLE_DIR / "scripts"
508+
destination = self.winpy_dir / "scripts"
509+
for item in origin.rglob('*.*'):
510+
shutil.copy2(item, destination)
511+
if self.verbose:
512+
print(f" Copied : {item.name} -> {destination}")
504513

505514
def _create_initial_batch_scripts(self):
506515
"""Creates initial batch scripts, including environment setup."""
@@ -679,113 +688,6 @@ def _create_initial_batch_scripts(self):
679688
"""
680689
self.create_batch_script("env_for_icons.bat", env_for_icons_bat_content, replacements=batch_replacements)
681690

682-
683-
winpython_ini_py_content = r"""
684-
# Prepares a dynamic list of variables settings from a .ini file
685-
import os
686-
import subprocess
687-
from pathlib import Path
688-
689-
winpython_inidefault=r'''
690-
[debug]
691-
state = disabled
692-
[inactive_environment_per_user]
693-
## <?> changing this segment to [active_environment_per_user] makes this segment of lines active or not
694-
HOME = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\settings
695-
USERPROFILE = %HOME%
696-
JUPYTER_DATA_DIR = %HOME%
697-
WINPYWORKDIR = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\Notebooks
698-
[inactive_environment_common]
699-
USERPROFILE = %HOME%
700-
[environment]
701-
## <?> Uncomment lines to override environment variables
702-
#JUPYTERLAB_SETTINGS_DIR = %HOME%\.jupyter\lab
703-
#JUPYTERLAB_WORKSPACES_DIR = %HOME%\.jupyter\lab\workspaces
704-
#R_HOME=%WINPYDIRBASE%\t\R
705-
#R_HOMEbin=%R_HOME%\bin\x64
706-
#JULIA_HOME=%WINPYDIRBASE%\t\Julia\bin\
707-
#JULIA_EXE=julia.exe
708-
#JULIA=%JULIA_HOME%%JULIA_EXE%
709-
#JULIA_PKGDIR=%WINPYDIRBASE%\settings\.julia
710-
#QT_PLUGIN_PATH=%WINPYDIR%\Lib\site-packages\pyqt5_tools\Qt\plugins
711-
'''
712-
713-
def get_file(file_name):
714-
if file_name.startswith("..\\"):
715-
file_name = os.path.join(os.path.dirname(os.path.dirname(__file__)), file_name[3:])
716-
elif file_name.startswith(".\\"):
717-
file_name = os.path.join(os.path.dirname(__file__), file_name[2:])
718-
try:
719-
with open(file_name, 'r') as file:
720-
return file.read()
721-
except FileNotFoundError:
722-
if file_name[-3:] == 'ini':
723-
os.makedirs(Path(file_name).parent, exist_ok=True)
724-
with open(file_name, 'w') as file:
725-
file.write(winpython_inidefault)
726-
return winpython_inidefault
727-
728-
def translate(line, env):
729-
parts = line.split('%')
730-
for i in range(1, len(parts), 2):
731-
if parts[i] in env:
732-
parts[i] = env[parts[i]]
733-
return ''.join(parts)
734-
735-
def main():
736-
import sys
737-
args = sys.argv[1:]
738-
file_name = args[0] if args else "..\\settings\\winpython.ini"
739-
740-
my_lines = get_file(file_name).splitlines()
741-
segment = "environment"
742-
txt = ""
743-
env = os.environ.copy() # later_version: env = os.environ
744-
745-
# default directories (from .bat)
746-
os.makedirs(Path(env['WINPYDIRBASE']) / 'settings' / 'Appdata' / 'Roaming', exist_ok=True)
747-
748-
# default qt.conf for Qt directories
749-
qt_conf='''echo [Paths]
750-
echo Prefix = .
751-
echo Binaries = .
752-
'''
753-
754-
pathlist = [Path(env['WINPYDIR']) / 'Lib' / 'site-packages' / i for i in ('PyQt5', 'PyQt6', 'Pyside6')]
755-
for p in pathlist:
756-
if p.is_dir():
757-
if not (p / 'qt.conf').is_file():
758-
with open(p / 'qt.conf', 'w') as file:
759-
file.write(qt_conf)
760-
761-
for l in my_lines:
762-
if l.startswith("["):
763-
segment = l[1:].split("]")[0]
764-
elif not l.startswith("#") and "=" in l:
765-
data = l.split("=", 1)
766-
if segment == "debug" and data[0].strip() == "state":
767-
data[0] = "WINPYDEBUG"
768-
if segment in ["environment", "debug", "active_environment_per_user", "active_environment_common"]:
769-
txt += f"set {data[0].strip()}={translate(data[1].strip(), env)}&& "
770-
env[data[0].strip()] = translate(data[1].strip(), env)
771-
if segment == "debug" and data[0].strip() == "state":
772-
txt += f"set WINPYDEBUG={data[1].strip()}&&"
773-
774-
print(txt)
775-
776-
# set potential directory
777-
for i in ('HOME', 'WINPYWORKDIR'):
778-
if i in env:
779-
os.makedirs(Path(env[i]), exist_ok=True)
780-
# later_version:
781-
# p = subprocess.Popen(["start", "cmd", "/k", "set"], shell = True)
782-
# p.wait() # I can wait until finished (although it too finishes after start finishes)
783-
784-
if __name__ == "__main__":
785-
main()
786-
"""
787-
self.create_batch_script("WinPythonIni.py", winpython_ini_py_content)
788-
789691
self._print_action_done()
790692

791693

@@ -994,8 +896,10 @@ def build(self, remove_existing: bool = True, requirements=None, winpy_dirname:
994896

995897
if remove_existing:
996898
self._create_initial_batch_scripts()
899+
self._copy_default_scripts()
997900
self._create_standard_batch_scripts()
998901
self._create_launchers()
902+
999903
utils.python_execmodule("ensurepip", self.distribution.target) # Ensure pip is installed for PyPy
1000904
self.distribution.patch_standard_packages("pip")
1001905

portable/scripts/WinPythonIni.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
# Prepares a dynamic list of variables settings from a .ini file
3+
import os
4+
import subprocess
5+
from pathlib import Path
6+
7+
winpython_inidefault=r'''
8+
[debug]
9+
state = disabled
10+
[inactive_environment_per_user]
11+
## <?> changing this segment to [active_environment_per_user] makes this segment of lines active or not
12+
HOME = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\settings
13+
USERPROFILE = %HOME%
14+
JUPYTER_DATA_DIR = %HOME%
15+
WINPYWORKDIR = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\Notebooks
16+
[inactive_environment_common]
17+
USERPROFILE = %HOME%
18+
[environment]
19+
## <?> Uncomment lines to override environment variables
20+
#JUPYTERLAB_SETTINGS_DIR = %HOME%\.jupyter\lab
21+
#JUPYTERLAB_WORKSPACES_DIR = %HOME%\.jupyter\lab\workspaces
22+
#R_HOME=%WINPYDIRBASE%\t\R
23+
#R_HOMEbin=%R_HOME%\bin\x64
24+
#JULIA_HOME=%WINPYDIRBASE%\t\Julia\bin\
25+
#JULIA_EXE=julia.exe
26+
#JULIA=%JULIA_HOME%%JULIA_EXE%
27+
#JULIA_PKGDIR=%WINPYDIRBASE%\settings\.julia
28+
#QT_PLUGIN_PATH=%WINPYDIR%\Lib\site-packages\pyqt5_tools\Qt\plugins
29+
'''
30+
31+
def get_file(file_name):
32+
if file_name.startswith("..\\"):
33+
file_name = os.path.join(os.path.dirname(os.path.dirname(__file__)), file_name[3:])
34+
elif file_name.startswith(".\\"):
35+
file_name = os.path.join(os.path.dirname(__file__), file_name[2:])
36+
try:
37+
with open(file_name, 'r') as file:
38+
return file.read()
39+
except FileNotFoundError:
40+
if file_name[-3:] == 'ini':
41+
os.makedirs(Path(file_name).parent, exist_ok=True)
42+
with open(file_name, 'w') as file:
43+
file.write(winpython_inidefault)
44+
return winpython_inidefault
45+
46+
def translate(line, env):
47+
parts = line.split('%')
48+
for i in range(1, len(parts), 2):
49+
if parts[i] in env:
50+
parts[i] = env[parts[i]]
51+
return ''.join(parts)
52+
53+
def main():
54+
import sys
55+
args = sys.argv[1:]
56+
file_name = args[0] if args else "..\\settings\\winpython.ini"
57+
58+
my_lines = get_file(file_name).splitlines()
59+
segment = "environment"
60+
txt = ""
61+
env = os.environ.copy() # later_version: env = os.environ
62+
63+
# default directories (from .bat)
64+
os.makedirs(Path(env['WINPYDIRBASE']) / 'settings' / 'Appdata' / 'Roaming', exist_ok=True)
65+
66+
# default qt.conf for Qt directories
67+
qt_conf='''echo [Paths]
68+
echo Prefix = .
69+
echo Binaries = .
70+
'''
71+
72+
pathlist = [Path(env['WINPYDIR']) / 'Lib' / 'site-packages' / i for i in ('PyQt5', 'PyQt6', 'Pyside6')]
73+
for p in pathlist:
74+
if p.is_dir():
75+
if not (p / 'qt.conf').is_file():
76+
with open(p / 'qt.conf', 'w') as file:
77+
file.write(qt_conf)
78+
79+
for l in my_lines:
80+
if l.startswith("["):
81+
segment = l[1:].split("]")[0]
82+
elif not l.startswith("#") and "=" in l:
83+
data = l.split("=", 1)
84+
if segment == "debug" and data[0].strip() == "state":
85+
data[0] = "WINPYDEBUG"
86+
if segment in ["environment", "debug", "active_environment_per_user", "active_environment_common"]:
87+
txt += f"set {data[0].strip()}={translate(data[1].strip(), env)}&& "
88+
env[data[0].strip()] = translate(data[1].strip(), env)
89+
if segment == "debug" and data[0].strip() == "state":
90+
txt += f"set WINPYDEBUG={data[1].strip()}&&"
91+
92+
print(txt)
93+
94+
# set potential directory
95+
for i in ('HOME', 'WINPYWORKDIR'):
96+
if i in env:
97+
os.makedirs(Path(env[i]), exist_ok=True)
98+
# later_version:
99+
# p = subprocess.Popen(["start", "cmd", "/k", "set"], shell = True)
100+
# p.wait() # I can wait until finished (although it too finishes after start finishes)
101+
102+
if __name__ == "__main__":
103+
main()
104+

winpython/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
OTHER DEALINGS IN THE SOFTWARE.
2929
"""
3030

31-
__version__ = '13.1.20250222'
31+
__version__ = '13.2.20250309'
3232
__license__ = __doc__
3333
__project_url__ = 'http://winpython.github.io/'

0 commit comments

Comments
 (0)