forked from winpython/winpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
1251 lines (1062 loc) · 49.4 KB
/
make.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Copyright © 2012 Pierre Raybaut
# Copyright © 2014-2024+ The Winpython development team https://github.com/winpython/
# Licensed under the terms of the MIT License
# (see winpython/__init__.py for details)
"""
WinPython build script
Created on Sun Aug 12 11:17:50 2012
"""
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
from winpython import wppm, utils
# Local imports
import diff
CHANGELOGS_DIR = Path(__file__).parent / "changelogs"
PORTABLE_DIR = Path(__file__).parent / "portable"
assert CHANGELOGS_DIR.is_dir(), f"Changelogs directory not found: {CHANGELOGS_DIR}"
assert PORTABLE_DIR.is_dir(), f"Portable directory not found: {PORTABLE_DIR}"
def find_7zip_executable() -> str:
"""
Locates the 7-Zip executable (7z.exe) in common installation directories.
Raises:
RuntimeError: If 7-Zip executable is not found.
Returns:
str: Path to the 7-Zip executable.
"""
program_files_dirs = [
Path(r"C:\Program Files"),
Path(r"C:\Program Files (x86)"),
Path(sys.prefix).parent.parent / "7-Zip",
]
for base_dir in program_files_dirs:
for subdir in [".", "App"]:
exe_path = base_dir / subdir / "7-Zip" / "7z.exe"
if exe_path.is_file():
return str(exe_path)
raise RuntimeError("7ZIP is not installed on this computer.")
def replace_lines_in_file(filepath: Path, replacements: list[tuple[str, str]]):
"""
Replaces lines in a file that start with a given prefix.
Args:
filepath: Path to the file to modify.
replacements: A list of tuples, where each tuple contains:
- The prefix of the line to replace (str).
- The new text for the line (str).
"""
lines: list[str] = []
with open(filepath, "r") as f:
lines = f.readlines()
updated_lines = list(lines) # Create a mutable copy
for index, line in enumerate(lines):
for prefix, new_text in replacements:
start_prefix = prefix
if prefix not in ("Icon", "OutFile") and not prefix.startswith("!"):
start_prefix = "set " + prefix
if line.startswith(start_prefix + "="):
updated_lines[index] = f"{start_prefix}={new_text}\n"
with open(filepath, "w") as f:
f.writelines(updated_lines)
print(f"Updated 7-zip script: {filepath}")
def build_installer_7zip(
script_template_path: Path, output_script_path: Path, replacements: list[tuple[str, str]]
):
"""
Creates a 7-Zip installer script by copying a template and applying text replacements.
Args:
script_template_path: Path to the template 7-Zip script (.bat file).
output_script_path: Path to save the generated 7-Zip script.
replacements: A list of tuples for text replacements (prefix, new_text).
"""
sevenzip_exe = find_7zip_executable()
shutil.copy(script_template_path, output_script_path)
# Standard replacements for all 7zip scripts
data = [
("PORTABLE_DIR", str(PORTABLE_DIR)),
("SEVENZIP_EXE", sevenzip_exe),
] + replacements
replace_lines_in_file(output_script_path, data)
try:
# Execute the generated 7-Zip script
command = f'"{output_script_path}"'
print(f"Executing 7-Zip script: {command}")
subprocess.run(
command, shell=True, check=True, stderr=sys.stderr, stdout=sys.stderr
# with stdout=sys.stdout, we would not see 7zip compressing
) # Use subprocess.run for better error handling
except subprocess.CalledProcessError as e:
print(f"Error executing 7-Zip script: {e}", file=sys.stderr)
class WinPythonDistributionBuilder:
"""
Builds a WinPython distribution.
"""
JULIA_PATH_REL = r"\t\Julia\bin" # Relative path within WinPython dir
NODEJS_PATH_REL = r"\n" # Relative path within WinPython dir
def __init__(
self,
build_number: int,
release_level: str,
target_dir: Path,
wheels_dir: Path,
tools_dirs: list[Path] = None,
docs_dirs: list[Path] = None,
verbose: bool = False,
base_dir: Path = None,
install_options: list[str] = None,
flavor: str = "",
):
"""
Initializes the WinPythonDistributionBuilder.
Args:
build_number: The build number (integer).
release_level: The release level (e.g., "beta", "").
target_dir: The base directory where WinPython will be created.
wheels_dir: Directory containing wheel files for packages.
tools_dirs: List of directories containing development tools to include.
docs_dirs: List of directories containing documentation to include.
verbose: Enable verbose output.
base_dir: Base directory for building (optional, for relative paths).
install_options: Additional pip install options.
flavor: WinPython flavor (e.g., "Barebone").
"""
self.build_number = build_number
self.release_level = release_level
self.target_dir = Path(target_dir) # Ensure Path object
self.wheels_dir = Path(wheels_dir) # Ensure Path object
self.tools_dirs = tools_dirs or []
self.docs_dirs = docs_dirs or []
self.verbose = verbose
self.winpy_dir: Path | None = None # Will be set during build
self.distribution: wppm.Distribution | None = None # Will be set during build
self.base_dir = base_dir
self.install_options = install_options or []
self.flavor = flavor
self.python_zip_file: Path = self._get_python_zip_file()
self.python_name = self.python_zip_file.stem # Filename without extension
self.python_dir_name = "python" # Standardized Python directory name
def _get_python_zip_file(self) -> Path:
"""
Finds the Python zip file in the wheels directory.
Returns:
Path: Path to the Python zip file.
Raises:
RuntimeError: if no python zip file is found
"""
patterns = [
r"(pypy3|python-)([0-9]|[a-zA-Z]|.)*.zip", # PyPy pattern
r"python-([0-9\.rcba]*)((\.|\-)amd64)?\.(zip|zip)", # Standard Python pattern
]
for pattern in patterns:
for filename in os.listdir(self.wheels_dir):
if re.match(pattern, filename):
return self.wheels_dir / filename
raise RuntimeError(f"Could not find Python zip package in {self.wheels_dir}")
@property
def package_index_markdown(self) -> str:
"""
Generates a Markdown formatted package index page.
Returns:
str: Markdown content for the package index.
"""
installed_tools_md = self._get_installed_tools_markdown()
installed_packages_md = self._get_installed_packages_markdown()
python_description = "Python programming language with standard library"
return f"""## WinPython {self.winpyver2 + self.flavor}
The following packages are included in WinPython-{self.architecture_bits}bit v{self.winpyver2 + self.flavor} {self.release_level}.
<details>
### Tools
Name | Version | Description
-----|---------|------------
{installed_tools_md}
### Python packages
Name | Version | Description
-----|---------|------------
[Python](http://www.python.org/) | {self.python_full_version} | {python_description}
{installed_packages_md}
</details>
"""
def _get_installed_tools_markdown(self) -> str:
"""Generates Markdown for installed tools section in package index."""
installed_tools = []
def get_tool_path(rel_path):
path = self.winpy_dir / rel_path if self.winpy_dir else None
return path if path and (path.is_file() or path.is_dir()) else None
julia_path = get_tool_path(self.JULIA_PATH_REL)
if julia_path:
julia_version = utils.get_julia_version(str(julia_path))
installed_tools.append(("Julia", julia_version))
nodejs_path = get_tool_path(self.NODEJS_PATH_REL)
if nodejs_path:
node_version = utils.get_nodejs_version(str(nodejs_path))
installed_tools.append(("Nodejs", node_version))
npm_version = utils.get_npmjs_version(str(nodejs_path))
installed_tools.append(("npmjs", npm_version))
pandoc_exe = get_tool_path(r"\t\pandoc.exe")
if pandoc_exe:
pandoc_version = utils.get_pandoc_version(str(pandoc_exe.parent))
installed_tools.append(("Pandoc", pandoc_version))
vscode_exe = get_tool_path(r"\t\VSCode\Code.exe")
if vscode_exe:
vscode_version = utils.getFileProperties(str(vscode_exe))["FileVersion"]
installed_tools.append(("VSCode", vscode_version))
tool_lines = []
for name, version in installed_tools:
metadata = utils.get_package_metadata("tools.ini", name)
url, description = metadata["url"], metadata["description"]
tool_lines.append(f"[{name}]({url}) | {version} | {description}")
return "\n".join(tool_lines)
def _get_installed_packages_markdown(self) -> str:
"""Generates Markdown for installed packages section in package index."""
if self.distribution is None:
return "" # Distribution not initialized yet.
self.installed_packages = self.distribution.get_installed_packages(update=True)
package_lines = [
f"[{pkg.name}]({pkg.url}) | {pkg.version} | {pkg.description}"
for pkg in sorted(self.installed_packages, key=lambda p: p.name.lower())
]
return "\n".join(package_lines)
@property
def winpython_version_name(self) -> str:
"""Returns the full WinPython version string."""
return f"{self.python_full_version}.{self.build_number}{self.flavor}{self.release_level}"
@property
def python_full_version(self) -> str:
"""
Retrieves the Python full version string from the distribution.
Will be set after _extract_python is called and distribution is initialized.
"""
if self.distribution is None:
return "0.0.0" # Placeholder before initialization
return utils.get_python_long_version(self.distribution.target)
@property
def python_executable_dir(self) -> str:
"""Returns the directory containing the Python executable."""
python_path_dir = self.winpy_dir / self.python_dir_name if self.winpy_dir else None
if python_path_dir and python_path_dir.is_dir():
return str(python_path_dir)
else:
python_path_exe = self.winpy_dir / self.python_name if self.winpy_dir else None # Fallback for older structure
return str(python_path_exe) if python_path_exe else ""
@property
def architecture_bits(self) -> int:
"""Returns the architecture (32 or 64 bits) of the distribution."""
if self.distribution:
return self.distribution.architecture
return 64 # Default to 64 if distribution is not initialized yet
@property
def pre_path_entries(self) -> list[str]:
"""Returns a list of PATH entries to prepend to the environment."""
return [
r"Lib\site-packages\PyQt5",
"", # Python root directory
"DLLs",
"Scripts",
r"..\t",
r".." + self.JULIA_PATH_REL,
r".." + self.NODEJS_PATH_REL,
]
@property
def post_path_entries(self) -> list[str]:
"""Returns a list of PATH entries to append to the environment."""
return []
@property
def tools_directories(self) -> list[Path]:
"""Returns the list of tools directories to include."""
return self.tools_dirs
@property
def docs_directories(self) -> list[Path]:
"""Returns the list of documentation directories to include."""
default_docs_dir = Path(__file__).resolve().parent / "docs"
if default_docs_dir.is_dir():
return [default_docs_dir] + self.docs_dirs
return self.docs_dirs
def create_batch_script(self, name: str, contents: str, replacements: list[tuple[str, str]] = None):
"""
Creates a batch script in the WinPython scripts directory.
Args:
name: The name of the batch script file.
contents: The contents of the batch script.
replacements: A list of tuples for text replacements in the content.
"""
script_dir = self.winpy_dir / "scripts" if self.winpy_dir else None
if not script_dir:
print("Warning: WinPython directory not set, cannot create batch script.")
return
script_dir.mkdir(parents=True, exist_ok=True)
final_contents = contents
if replacements:
for old_text, new_text in replacements:
final_contents = final_contents.replace(old_text, new_text)
script_path = script_dir / name
with open(script_path, "w") as f:
f.write(final_contents)
print(f"Created batch script: {script_path}")
def create_python_launcher_batch(
self,
name: str,
script_name: str,
working_dir: str = None,
options: str = None,
command: str = None,
):
"""
Creates a batch file to launch a Python script within the WinPython environment.
Args:
name: The name of the batch file.
script_name: The name of the Python script to execute.
working_dir: Optional working directory for the script.
options: Optional command-line options for the script.
command: Optional command to execute python, defaults to python.exe or pythonw.exe
"""
options_str = f" {options}" if options else ""
if command is None:
command = '"%WINPYDIR%\\pythonw.exe"' if script_name.endswith(".pyw") else '"%WINPYDIR%\\python.exe"'
change_dir_cmd = f"cd /D {working_dir}\n" if working_dir else ""
script_name_str = f" {script_name}" if script_name else ""
batch_content = f"""@echo off
call "%~dp0env_for_icons.bat"
{change_dir_cmd}{command}{script_name_str}{options_str} %*"""
self.create_batch_script(name, batch_content)
def create_installer_7zip(self, installer_type: str = ".exe"):
"""
Creates a WinPython installer using 7-Zip.
Args:
installer_type: Type of installer to create (".exe", ".7z", ".zip").
"""
self._print_action(f"Creating WinPython installer ({installer_type})")
template_name = "installer_7zip.bat"
output_name = "installer_7zip-tmp.bat" # temp file to avoid overwriting template
if installer_type not in [".exe", ".7z", ".zip"]:
print(f"Warning: Unsupported installer type '{installer_type}'. Defaulting to .exe")
installer_type = ".exe"
replacements = [
("DISTDIR", str(self.winpy_dir)),
("ARCH", str(self.architecture_bits)),
("VERSION", f"{self.python_full_version}.{self.build_number}{self.flavor}"),
(
"VERSION_INSTALL",
f'{self.python_full_version.replace(".", "")}{self.build_number}',
),
("RELEASELEVEL", self.release_level),
("INSTALLER_OPTION", installer_type), # Pass installer type as option to bat script
]
build_installer_7zip(
PORTABLE_DIR / template_name,
PORTABLE_DIR / output_name,
replacements
)
self._print_action_done()
def _print_action(self, text: str):
"""Prints an action message with progress indicator."""
if self.verbose:
utils.print_box(text)
else:
print(f"{text}... ", end="", flush=True)
def _print_action_done(self):
"""Prints "OK" to indicate action completion."""
if not self.verbose:
print("OK")
def _extract_python_archive(self):
"""Extracts the Python zip archive to create the base Python environment."""
self._print_action("Extracting Python archive")
utils.extract_archive(
str(self.python_zip_file),
targetdir=str(self.winpy_dir), # Extract directly to winpy_dir
)
self._print_action_done()
# Relocate to /python subfolder if needed (for newer structure) #2024-12-22 to /python
python_target_dir = self.winpy_dir / self.python_dir_name
if self.python_dir_name != self.python_name and not python_target_dir.is_dir():
os.rename(self.winpy_dir / self.python_name, python_target_dir)
def _copy_tools(self):
"""Copies development tools to the WinPython 't' directory."""
tools_target_dir = self.winpy_dir / "t"
self._print_action(f"Copying tools to {tools_target_dir}")
tools_target_dir.mkdir(parents=True, exist_ok=True)
for source_dir in self.tools_directories:
if not source_dir.is_dir():
print(f"Warning: Tools directory not found: {source_dir}")
continue
for item_name in os.listdir(source_dir):
source_item = source_dir / item_name
target_item = tools_target_dir / item_name
copy_func = shutil.copytree if source_item.is_dir() else shutil.copy2
try:
copy_func(source_item, target_item)
if self.verbose:
print(f" Copied: {source_item} -> {target_item}")
except Exception as e:
print(f"Error copying {source_item} to {target_item}: {e}")
# Special handling for Node.js to move it up one level
nodejs_current_dir = tools_target_dir / "n"
nodejs_target_dir = self.winpy_dir / self.NODEJS_PATH_REL
if nodejs_current_dir != nodejs_target_dir and nodejs_current_dir.is_dir():
try:
shutil.move(nodejs_current_dir, nodejs_target_dir)
except Exception as e:
print(f"Error moving Node.js directory: {e}")
self._print_action_done()
def _copy_documentation(self):
"""Copies documentation files to the WinPython 'docs' directory."""
docs_target_dir = self.winpy_dir / "notebooks" / "docs"
self._print_action(f"Copying documentation to {docs_target_dir}")
docs_target_dir.mkdir(parents=True, exist_ok=True)
for source_dir in self.docs_directories:
if not source_dir.is_dir():
print(f"Warning: Documentation directory not found: {source_dir}")
continue
for item_name in os.listdir(source_dir):
source_item = source_dir / item_name
target_item = docs_target_dir / item_name
copy_func = shutil.copytree if source_item.is_dir() else shutil.copy2
try:
copy_func(source_item, target_item)
if self.verbose:
print(f" Copied: {source_item} -> {target_item}")
except Exception as e:
print(f"Error copying {source_item} to {target_item}: {e}")
self._print_action_done()
def _create_launchers(self):
"""Copies pre-made launchers to the WinPython directory."""
self._print_action("Creating launchers")
launchers_source_dir = PORTABLE_DIR / "launchers_final"
for item in launchers_source_dir.rglob('*.exe'):
shutil.copy2(item, self.winpy_dir)
if self.verbose:
print(f" Copied launcher: {item.name} -> {self.winpy_dir}")
for item in launchers_source_dir.rglob('licence*.*'):
shutil.copy2(item, self.winpy_dir)
self._print_action_done()
def _create_initial_batch_scripts(self):
"""Creates initial batch scripts, including environment setup."""
self._print_action("Creating initial batch scripts")
path_entries_str = ";".join([rf"%WINPYDIR%\{pth}" for pth in self.pre_path_entries])
full_path_env_var = f"{path_entries_str};%PATH%;" + ";".join([rf"%WINPYDIR%\{pth}" for pth in self.post_path_entries])
path_entries_ps_str = ";".join([rf"$env:WINPYDIR\\{pth}" for pth in self.pre_path_entries])
full_path_ps_env_var = f"{path_entries_ps_str};$env:path;" + ";".join([rf"$env:WINPYDIR\\{pth}" for pth in self.post_path_entries])
# Replacements for batch scripts (PyPy compatibility)
exe_name = self.distribution.short_exe if self.distribution else "python.exe" # default to python.exe if distribution is not yet set
batch_replacements = [
(r"DIR%\\python.exe", rf"DIR%\\{exe_name}"),
(r"DIR%\\PYTHON.EXE", rf"DIR%\\{exe_name}"),
]
if self.distribution and (Path(self.distribution.target) / r"lib-python\3\idlelib").is_dir():
batch_replacements.append((r"\Lib\idlelib", r"\lib-python\3\idlelib"))
env_bat_content = f"""@echo off
set WINPYDIRBASE=%~dp0..
rem get a normalized path
set WINPYDIRBASETMP=%~dp0..
pushd %WINPYDIRBASETMP%
set WINPYDIRBASE=%__CD__%
if "%WINPYDIRBASE:~-1%"=="\\" set WINPYDIRBASE=%WINPYDIRBASE:~0,-1%
set WINPYDIRBASETMP=
popd
set WINPYDIR=%WINPYDIRBASE%\\{self.python_dir_name}
rem 2019-08-25 pyjulia needs absolutely a variable PYTHON=%WINPYDIR%\\python.exe
set PYTHON=%WINPYDIR%\\python.exe
set PYTHONPATHz=%WINPYDIR%;%WINPYDIR%\\Lib;%WINPYDIR%\\DLLs
set WINPYVER={self.winpython_version_name}
rem 2023-02-12 utf-8 on console to avoid pip crash
rem see https://github.com/pypa/pip/issues/11798#issuecomment-1427069681
set PYTHONIOENCODING=utf-8
rem set PYTHONUTF8=1 creates issues in "movable" patching
set HOME=%WINPYDIRBASE%\\settings
rem see https://github.com/winpython/winpython/issues/839
rem set USERPROFILE=%HOME%
set JUPYTER_DATA_DIR=%HOME%
set JUPYTER_CONFIG_DIR=%WINPYDIR%\\etc\\jupyter
set JUPYTER_CONFIG_PATH=%WINPYDIR%\\etc\\jupyter
set FINDDIR=%WINDIR%\\system32
echo ";%PATH%;" | %FINDDIR%\\find.exe /C /I ";%WINPYDIR%\\;" >nul
if %ERRORLEVEL% NEQ 0 (
set "PATH={full_path_env_var}"
cd .
)
rem force default pyqt5 kit for Spyder if PyQt5 module is there
if exist "%WINPYDIR%\\Lib\\site-packages\\PyQt5\\__init__.py" set QT_API=pyqt5
"""
self.create_batch_script("env.bat", env_bat_content, replacements=batch_replacements)
ps1_content = r"""### WinPython_PS_Prompt.ps1 ###
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
# $env:PYTHONUTF8 = 1 would create issues in "movable" patching
$env:WINPYDIRBASE = "$dp0\.."
# get a normalize path
# http://stackoverflow.com/questions/1645843/resolve-absolute-path-from-relative-path-and-or-file-name
$env:WINPYDIRBASE = [System.IO.Path]::GetFullPath( $env:WINPYDIRBASE )
# avoid double_init (will only resize screen)
if (-not ($env:WINPYDIR -eq [System.IO.Path]::GetFullPath( $env:WINPYDIRBASE+""" + '"\\' + self.python_dir_name + '"' + r""")) ) {
$env:WINPYDIR = $env:WINPYDIRBASE+""" + '"\\' + self.python_dir_name + '"' + r"""
# 2019-08-25 pyjulia needs absolutely a variable PYTHON=%WINPYDIR%python.exe
$env:PYTHON = "%WINPYDIR%\python.exe"
$env:PYTHONPATHz = "%WINPYDIR%;%WINPYDIR%\Lib;%WINPYDIR%\DLLs"
$env:WINPYVER = '""" + self.winpython_version_name + r"""'
# rem 2023-02-12 try utf-8 on console
# rem see https://github.com/pypa/pip/issues/11798#issuecomment-1427069681
$env:PYTHONIOENCODING = "utf-8"
$env:HOME = "$env:WINPYDIRBASE\settings"
# rem read https://github.com/winpython/winpython/issues/839
# $env:USERPROFILE = "$env:HOME"
$env:WINPYDIRBASE = ""
$env:JUPYTER_DATA_DIR = "$env:HOME"
if (-not $env:PATH.ToLower().Contains(";"+ $env:WINPYDIR.ToLower()+ ";")) {
$env:PATH = """ + '"' + full_path_ps_env_var + '"' + r""" }
#rem force default pyqt5 kit for Spyder if PyQt5 module is there
if (Test-Path "$env:WINPYDIR\Lib\site-packages\PyQt5\__init__.py") { $env:QT_API = "pyqt5" }
# PyQt5 qt.conf creation and winpython.ini creation done via Winpythonini.py (called per env_for_icons.bat for now)
# Start-Process -FilePath $env:PYTHON -ArgumentList ($env:WINPYDIRBASE + '\scripts\WinPythonIni.py')
### Set-WindowSize
Function Set-WindowSize {
Param([int]$x=$host.ui.rawui.windowsize.width,
[int]$y=$host.ui.rawui.windowsize.heigth,
[int]$buffer=$host.UI.RawUI.BufferSize.heigth)
$buffersize = new-object System.Management.Automation.Host.Size($x,$buffer)
$host.UI.RawUI.BufferSize = $buffersize
$size = New-Object System.Management.Automation.Host.Size($x,$y)
$host.ui.rawui.WindowSize = $size
}
# Windows10 yelling at us with 150 40 6000
# Set-WindowSize 195 40 6000
### Colorize to distinguish
$host.ui.RawUI.BackgroundColor = "Black"
$host.ui.RawUI.ForegroundColor = "White"
}
"""
self.create_batch_script("WinPython_PS_Prompt.ps1", ps1_content, replacements=batch_replacements)
cmd_ps_bat_content = r"""@echo off
call "%~dp0env_for_icons.bat"
Powershell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy RemoteSigned -noexit -File ""%~dp0WinPython_PS_Prompt.ps1""'}"
"""
self.create_batch_script("cmd_ps.bat", cmd_ps_bat_content, replacements=batch_replacements)
env_for_icons_bat_content = r"""@echo off
call "%~dp0env.bat"
rem default is as before: Winpython ..\Notebooks
set WINPYWORKDIR=%WINPYDIRBASE%\Notebooks
set WINPYWORKDIR1=%WINPYWORKDIR%
rem if we have a file or directory in %1 parameter, we use that directory to define WINPYWORKDIR1
if not "%~1"=="" (
if exist "%~1" (
if exist "%~1\" (
rem echo it is a directory %~1
set WINPYWORKDIR1=%~1
) else (
rem echo it is a file %~1, so we take the directory %~dp1
set WINPYWORKDIR1=%~dp1
)
)
) else (
rem if it is launched from another directory than icon origin , we keep it that one echo %__CD__%
if not "%__CD__%"=="%~dp0" if not "%__CD__%scripts\"=="%~dp0" set WINPYWORKDIR1="%__CD__%"
)
rem remove potential doublequote
set WINPYWORKDIR1=%WINPYWORKDIR1:"=%
rem remove some potential last \
if "%WINPYWORKDIR1:~-1%"=="\" set WINPYWORKDIR1=%WINPYWORKDIR1:~0,-1%
rem you can use winpython.ini to change defaults
FOR /F "delims=" %%i IN ('""%WINPYDIR%\python.exe" "%~dp0WinpythonIni.py""') DO set winpythontoexec=%%i
%winpythontoexec%set winpythontoexec=
rem Preventive Working Directories creation if needed
if not "%WINPYWORKDIR%"=="" if not exist "%WINPYWORKDIR%" mkdir "%WINPYWORKDIR%"
if not "%WINPYWORKDIR1%"=="" if not exist "%WINPYWORKDIR1%" mkdir "%WINPYWORKDIR1%"
rem Change of directory only if we are in a launcher directory
if "%__CD__%scripts\"=="%~dp0" cd/D %WINPYWORKDIR1%
if "%__CD__%"=="%~dp0" cd/D %WINPYWORKDIR1%
if not exist "%HOME%\.spyder-py%WINPYVER:~0,1%" mkdir "%HOME%\.spyder-py%WINPYVER:~0,1%"
if not exist "%HOME%\.spyder-py%WINPYVER:~0,1%\workingdir" echo %HOME%\Notebooks>"%HOME%\.spyder-py%WINPYVER:~0,1%\workingdir"
"""
self.create_batch_script("env_for_icons.bat", env_for_icons_bat_content, replacements=batch_replacements)
winpython_ini_py_content = r"""
# Prepares a dynamic list of variables settings from a .ini file
import os
import subprocess
from pathlib import Path
winpython_inidefault=r'''
[debug]
state = disabled
[inactive_environment_per_user]
## <?> changing this segment to [active_environment_per_user] makes this segment of lines active or not
HOME = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\settings
USERPROFILE = %HOME%
JUPYTER_DATA_DIR = %HOME%
WINPYWORKDIR = %HOMEDRIVE%%HOMEPATH%\Documents\WinPython%WINPYVER%\Notebooks
[inactive_environment_common]
USERPROFILE = %HOME%
[environment]
## <?> Uncomment lines to override environment variables
#JUPYTERLAB_SETTINGS_DIR = %HOME%\.jupyter\lab
#JUPYTERLAB_WORKSPACES_DIR = %HOME%\.jupyter\lab\workspaces
#R_HOME=%WINPYDIRBASE%\t\R
#R_HOMEbin=%R_HOME%\bin\x64
#JULIA_HOME=%WINPYDIRBASE%\t\Julia\bin\
#JULIA_EXE=julia.exe
#JULIA=%JULIA_HOME%%JULIA_EXE%
#JULIA_PKGDIR=%WINPYDIRBASE%\settings\.julia
#QT_PLUGIN_PATH=%WINPYDIR%\Lib\site-packages\pyqt5_tools\Qt\plugins
'''
def get_file(file_name):
if file_name.startswith("..\\"):
file_name = os.path.join(os.path.dirname(os.path.dirname(__file__)), file_name[3:])
elif file_name.startswith(".\\"):
file_name = os.path.join(os.path.dirname(__file__), file_name[2:])
try:
with open(file_name, 'r') as file:
return file.read()
except FileNotFoundError:
if file_name[-3:] == 'ini':
os.makedirs(Path(file_name).parent, exist_ok=True)
with open(file_name, 'w') as file:
file.write(winpython_inidefault)
return winpython_inidefault
def translate(line, env):
parts = line.split('%')
for i in range(1, len(parts), 2):
if parts[i] in env:
parts[i] = env[parts[i]]
return ''.join(parts)
def main():
import sys
args = sys.argv[1:]
file_name = args[0] if args else "..\\settings\\winpython.ini"
my_lines = get_file(file_name).splitlines()
segment = "environment"
txt = ""
env = os.environ.copy() # later_version: env = os.environ
# default directories (from .bat)
os.makedirs(Path(env['WINPYDIRBASE']) / 'settings' / 'Appdata' / 'Roaming', exist_ok=True)
# default qt.conf for Qt directories
qt_conf='''echo [Paths]
echo Prefix = .
echo Binaries = .
'''
pathlist = [Path(env['WINPYDIR']) / 'Lib' / 'site-packages' / i for i in ('PyQt5', 'PyQt6', 'Pyside6')]
for p in pathlist:
if p.is_dir():
if not (p / 'qt.conf').is_file():
with open(p / 'qt.conf', 'w') as file:
file.write(qt_conf)
for l in my_lines:
if l.startswith("["):
segment = l[1:].split("]")[0]
elif not l.startswith("#") and "=" in l:
data = l.split("=", 1)
if segment == "debug" and data[0].strip() == "state":
data[0] = "WINPYDEBUG"
if segment in ["environment", "debug", "active_environment_per_user", "active_environment_common"]:
txt += f"set {data[0].strip()}={translate(data[1].strip(), env)}&& "
env[data[0].strip()] = translate(data[1].strip(), env)
if segment == "debug" and data[0].strip() == "state":
txt += f"set WINPYDEBUG={data[1].strip()}&&"
print(txt)
# set potential directory
for i in ('HOME', 'WINPYWORKDIR'):
if i in env:
os.makedirs(Path(env[i]), exist_ok=True)
# later_version:
# p = subprocess.Popen(["start", "cmd", "/k", "set"], shell = True)
# p.wait() # I can wait until finished (although it too finishes after start finishes)
if __name__ == "__main__":
main()
"""
self.create_batch_script("WinPythonIni.py", winpython_ini_py_content)
self._print_action_done()
def _create_standard_batch_scripts(self):
"""Creates standard WinPython batch scripts for various actions."""
self._print_action("Creating standard batch scripts")
exe_name = self.distribution.short_exe if self.distribution else "python.exe"
batch_replacements = [
(r"DIR%\\python.exe", rf"DIR%\\{exe_name}"),
(r"DIR%\\PYTHON.EXE", rf"DIR%\\{exe_name}"),
]
if self.distribution and (Path(self.distribution.target) / r"lib-python\3\idlelib").is_dir():
batch_replacements.append((r"\Lib\idlelib", r"\lib-python\3\idlelib"))
self.create_batch_script("readme.txt", """These batch files are required to run WinPython icons.
These files should help the user writing his/her own
The environment variables are set-up in 'env.bat' and 'env_for_icons.bat'.""",
)
self.create_batch_script(
"make_winpython_movable.bat",
r"""@echo off
call "%~dp0env.bat"
echo patch pip and current launchers for move
"%WINPYDIR%\python.exe" -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip', to_movable=True)"
pause""",
replacements=batch_replacements
)
self.create_batch_script(
"make_winpython_fix.bat",
r"""@echo off
call "%~dp0env.bat"
echo patch pip and current launchers for non-move
"%WINPYDIR%\python.exe" -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip', to_movable=False)"
pause""",
replacements=batch_replacements
)
for ini_patch_script in [
("make_working_directory_be_not_winpython.bat", "[active_environment", "[inactive_environment", "[inactive_environment_per_user]", "[active_environment_per_user]"),
("make_working_directory_be_winpython.bat", "[active_environment", "[inactive_environment"),
("make_working_directory_and_userprofile_be_winpython.bat", "[active_environment", "[inactive_environment", "[inactive_environment_common]", "[active_environment_common]")
]:
name, patch1_start, patch1_end, *patch2 = ini_patch_script
content = f"""call "%~dp0env_for_icons.bat"
"%PYTHON%" -c "from winpython.utils import patch_sourcefile;patch_sourcefile(r'%~dp0..\\settings\winpython.ini', '{patch1_start}', '{patch1_end}' )"
"""
if patch2:
content += f""""%PYTHON%" -c "from winpython.utils import patch_sourcefile;patch_sourcefile(r'%~dp0..\\settings\winpython.ini', '{patch2[0]}', '{patch2[1]}' )" """
self.create_batch_script(name, content)
self.create_batch_script("cmd.bat", r"""@echo off
call "%~dp0env_for_icons.bat" %*
cmd.exe /k""", replacements=batch_replacements)
self.create_batch_script("WinPython_Terminal.bat", r"""@echo off
Powershell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy RemoteSigned -noexit -File ""%~dp0WinPython_PS_Prompt.ps1""'}"
exit""", replacements=batch_replacements)
self.create_batch_script("python.bat", r"""@echo off
call "%~dp0env_for_icons.bat" %*
"%WINPYDIR%\python.exe" %*""", replacements=batch_replacements)
self.create_batch_script(
"winpython.bat",
r"""@echo off
call "%~dp0env_for_icons.bat" %*
rem backward compatibility for non-ptpython users
if exist "%WINPYDIR%\scripts\ptpython.exe" (
"%WINPYDIR%\scripts\ptpython.exe" %*
) else (
"%WINPYDIR%\python.exe" %*
)""",
replacements=batch_replacements
)
self.create_batch_script(
"winidle.bat",
r"""@echo off
call "%~dp0env_for_icons.bat" %*
"%WINPYDIR%\python.exe" "%WINPYDIR%\Lib\idlelib\idle.pyw" %*""",
replacements=batch_replacements
)
self.create_batch_script(
"winspyder.bat",
r"""@echo off
call "%~dp0env_for_icons.bat" %*
"%WINPYDIR%\scripts\spyder.exe" %* -w "%WINPYWORKDIR1%" """,
)
self.create_batch_script(
"spyder_reset.bat",
r"""@echo off
call "%~dp0env_for_icons.bat"
"%WINPYDIR%\scripts\spyder.exe" --reset %*""",
)
for jupyter_script in [
("winipython_notebook.bat", "jupyter-notebook.exe"),
("winjupyter_lab.bat", "jupyter-lab.exe"),
("winqtconsole.bat", "jupyter-qtconsole.exe"),
]:
name, exe = jupyter_script
self.create_batch_script(name, f"""@echo off
call "%~dp0env_for_icons.bat" %*
"%WINPYDIR%\\scripts\\{exe}" %*""")
self.create_python_launcher_batch(
"register_python.bat",
r'"%WINPYDIR%\Lib\site-packages\winpython\register_python.py"',
working_dir=r'"%WINPYDIR%\Scripts"',
)
self.create_python_launcher_batch(
"unregister_python.bat",
r'"%WINPYDIR%\Lib\site-packages\winpython\unregister_python.py"',
working_dir=r'"%WINPYDIR%\Scripts"',
)
for register_all_script in [
("register_python_for_all.bat", "register_python.bat"),
("unregister_python_for_all.bat", "unregister_python.bat"),
]:
name, base_script = register_all_script
self.create_batch_script(name, f"""@echo off
call "%~dp0env.bat"
call "%~dp0{base_script}" --all""")
self.create_batch_script("wpcp.bat", r"""@echo off
call "%~dp0env_for_icons.bat" %*
cmd.exe /k "echo wppm & wppm" """, replacements=batch_replacements)
self.create_batch_script(
"upgrade_pip.bat",
r"""@echo off
call "%~dp0env.bat"
echo this will upgrade pip with latest version, then patch it for WinPython portability ok ?
pause
"%WINPYDIR%\python.exe" -m pip install --upgrade pip
"%WINPYDIR%\python.exe" -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip', to_movable=True)
pause""",
replacements=batch_replacements
)
self.create_batch_script("activate.bat", r"""@echo off
call "%~dp0env.bat" %*""", replacements=batch_replacements)
vscode_bat_content = r"""@echo off
call "%~dp0env_for_icons.bat"
if exist "%WINPYDIR%\..\t\vscode\code.exe" (
"%WINPYDIR%\..\t\vscode\code.exe" %*
) else (
if exist "%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" (
"%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" %*
) else (
"code.exe" %*
))"""
self.create_batch_script("winvscode.bat", vscode_bat_content)
self._print_action_done()
def _run_complementary_batch_scripts(self, script_name="run_complement.bat"):
"""Runs complementary batch scripts from tools directories."""
print(f"Running {script_name} from tools directories...")
unique_tools_parent_dirs = set(str(Path(s).parent) for s in self.tools_directories)
for tools_parent_dir in unique_tools_parent_dirs:
script_path = Path(tools_parent_dir) / script_name
if script_path.is_file():
print(f' Executing "{script_path}" for "{self.winpy_dir}"')
self._print_action(f'Executing "{script_path}" for "{self.winpy_dir}" !')
try:
subprocess.run(
[str(script_path), str(self.winpy_dir)],
shell=True,
check=True,
stderr=sys.stderr,
stdout=sys.stdout
)
except subprocess.CalledProcessError as e:
print(f" Execution failed: {e}", file=sys.stderr)
self._print_action(f"Execution failed: {e}!")
self._print_action_done()
def build(self, remove_existing: bool = True, requirements=None, winpy_dirname: str = None):
"""Make WinPython distribution in target directory from the installers
located in wheels_dir
remove_existing=True: (default) install all from scratch
remove_existing=False: for complementary purposes (create installers)
requirements=file(s) of requirements (separated by space if several)"""
python_zip_filename = self.python_zip_file.name
print(f"Building WinPython with Python archive: {python_zip_filename}")
if winpy_dirname is None:
raise RuntimeError("WinPython base directory to create is undefined")
else:
self.winpy_dir = self.target_dir / winpy_dirname # Create/re-create the WinPython base directory