-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
freezer.py
1258 lines (1138 loc) · 49.4 KB
/
freezer.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
"""The core class for freezing scripts into executables."""
from __future__ import annotations
import marshal
import os
import shutil
import stat
import struct
import sys
import sysconfig
import time
from abc import abstractmethod
from collections.abc import Sequence
from contextlib import suppress
from functools import cached_property
from importlib import import_module
from importlib.util import MAGIC_NUMBER
from pathlib import Path
from sysconfig import get_config_var
from typing import Any
from zipfile import ZIP_DEFLATED, ZIP_STORED, PyZipFile, ZipInfo
from cx_Freeze._compat import IS_MACOS, IS_MINGW, IS_WINDOWS
from cx_Freeze._typing import IncludesList, InternalIncludesList
from cx_Freeze.common import get_resource_file_path, process_path_specs
from cx_Freeze.exception import FileError, OptionError
from cx_Freeze.executable import Executable
from cx_Freeze.finder import ModuleFinder
from cx_Freeze.module import ConstantsModule, Module
from cx_Freeze.parser import ELFParser, Parser, PEParser
if IS_WINDOWS or IS_MINGW:
with suppress(ImportError):
from .util import AddIcon, GetSystemDir, GetWindowsDir, UpdateCheckSum
elif IS_MACOS:
from .darwintools import DarwinFile, DarwinFileTracker, MachOReference
__all__ = ["ConstantsModule", "Executable", "Freezer"]
class Freezer:
"""Freezer base class."""
def __new__(cls, *args, **kwargs):
# create instance of appropriate sub-class, depending on the platform.
if IS_WINDOWS or IS_MINGW:
return super().__new__(WinFreezer)
if IS_MACOS:
return super().__new__(DarwinFreezer)
# assume any other platform would be handled by LinuxFreezer
return super().__new__(LinuxFreezer)
def __init__(
self,
executables: list[Executable],
constants_module: ConstantsModule | None = None,
includes: list[str] | None = None,
excludes: list[str] | None = None,
packages: list[str] | None = None,
replace_paths: list[str] | None = None,
compress: bool = True,
optimize: int = 0,
path: list[str | Path] | None = None,
target_dir: str | Path | None = None,
bin_includes: list[str] | None = None,
bin_excludes: list[str] | None = None,
bin_path_includes: list[str] | None = None,
bin_path_excludes: list[str] | None = None,
include_files: IncludesList | None = None,
zip_includes: IncludesList | None = None,
silent: bool | int = 0,
metadata: Any = None,
include_msvcr: bool = False,
zip_include_packages: Sequence[str] | None = None,
zip_exclude_packages: Sequence[str] | None = None,
):
self.executables: list[Executable] = list(executables)
if constants_module is None:
constants_module = ConstantsModule()
self.constants_module: ConstantsModule = constants_module
self.includes: list[str] = list(includes or [])
self.excludes: list[str] = list(excludes or [])
self.packages: set[str] = set(packages or [])
self.replace_paths: list[str] = list(replace_paths or [])
self.compress = True if compress is None else compress
self.optimize: int = optimize
self.path: list[str] | None = self._validate_path(path)
self.include_msvcr: bool = include_msvcr
self.target_dir = target_dir
self.bin_includes: list[str] | None = bin_includes
self.bin_excludes: list[str] | None = bin_excludes
self.bin_path_includes: list[str] = self._validate_bin_path(
bin_path_includes
)
self.bin_path_excludes: list[str] = self._validate_bin_path(
bin_path_excludes
)
self.include_files: InternalIncludesList = process_path_specs(
include_files
)
self.zip_includes: InternalIncludesList = process_path_specs(
zip_includes
)
if isinstance(silent, bool):
if silent:
self.silent = 1
else:
self.silent = 0
else:
self.silent = silent
self.metadata: Any = metadata
self.zip_exclude_packages: set[str] = {"*"}
self.zip_include_packages: set[str] = set()
self.zip_include_all_packages: bool = False
self._populate_zip_options(zip_include_packages, zip_exclude_packages)
self._verify_configuration()
self.files_copied: set[Path] = set()
self.finder: ModuleFinder = self._get_module_finder()
@property
def target_dir(self) -> Path:
"""Directory for built executables and dependent files."""
return self._targetdir
@target_dir.setter
def target_dir(self, path: str | Path | None):
if path is None:
platform = sysconfig.get_platform()
python_version = sysconfig.get_python_version()
path = f"build/exe.{platform}-{python_version}"
path = Path(path).resolve()
if os.fspath(path) in self.path:
raise OptionError(
"the build_exe directory cannot be used as search path"
)
if path.is_dir():
# starts in a clean directory
try:
shutil.rmtree(path)
except OSError:
raise OptionError(
"the build_exe directory cannot be cleaned"
) from None
self._targetdir: Path = path
def _add_resources(self, exe: Executable) -> None:
"""Add resources for an executable, platform dependent."""
# Copy icon into application. (Overridden on Windows)
if exe.icon is None:
return
target_icon = self.target_dir / exe.icon.name
self._copy_file(exe.icon, target_icon, copy_dependent_files=False)
def _copy_file(
self,
source: Path,
target: Path,
copy_dependent_files: bool,
include_mode: bool = False,
):
if not self._should_copy_file(source):
return
# handle pre-copy tasks, normally on the target path
source, target = self._pre_copy_hook(source, target)
if target in self.files_copied:
return
if source == target:
return
self._create_directory(target.parent)
if self.silent < 1:
print(f"copying {source} -> {target}")
shutil.copyfile(source, target)
shutil.copystat(source, target)
if include_mode:
shutil.copymode(source, target)
self.files_copied.add(target)
# handle post-copy tasks, including copying dependencies
self._post_copy_hook(source, target, copy_dependent_files)
def _copy_package_data(self, module: Module, target_dir: Path):
"""Copy any non-Python files to the target directory."""
ignore_patterns = ("__pycache__", "*.py", "*.pyc", "*.pyo")
def copy_tree(source_dir: Path, target_dir: Path, excludes: set[str]):
self._create_directory(target_dir)
for source in source_dir.iterdir():
if any(filter(source.match, ignore_patterns)):
continue
source_name = source.name
if source_name in excludes:
continue
target = target_dir / source_name
if source.is_dir():
source_subdir = source_dir / source_name
excludes_subdir = {
".".join(m.split(".")[1:])
for m in excludes
if m.split(".")[0] == source_name
}
copy_tree(source_subdir, target, excludes_subdir)
else:
self._copy_file(source, target, copy_dependent_files=True)
source_dir = module.file.parent
module_name = module.name
if self.silent < 1:
print(f"copying data from package {module_name}...")
# do not copy the subfolders which belong to excluded modules
excludes = {
".".join(m.split(".")[1:])
for m in self.finder.excludes
if m.split(".")[0] == module_name
}
copy_tree(source_dir, target_dir, excludes)
@abstractmethod
def _pre_copy_hook(self, source: Path, target: Path) -> tuple[Path, Path]:
"""Prepare the source and target paths."""
@abstractmethod
def _post_copy_hook(
self,
source: Path,
target: Path,
copy_dependent_files: bool,
):
"""Post-copy task."""
def _create_directory(self, path: str | Path):
if isinstance(path, str):
path = Path(path)
if not path.is_dir():
if self.silent < 1:
print(f"creating directory {path}")
path.mkdir(parents=True, exist_ok=True)
def _freeze_executable(self, exe: Executable) -> None:
finder: ModuleFinder = self.finder
finder.include_file_as_module(exe.main_script, exe.main_module_name)
finder.include_file_as_module(exe.init_script, exe.init_module_name)
finder.include_file_as_module(
get_resource_file_path("initscripts", "__startup__", ".py")
)
# Ensure the copy of default python libraries
dependent_files: set[Path] = self.get_dependent_files(exe.base)
# Extra files like python3.dll need to be found
python_libs = self._default_bin_includes()
for bin_path in self.bin_path_includes:
for name in python_libs:
dependent_file = Path(bin_path, name)
if dependent_file.is_file():
dependent_files.add(dependent_file)
break
# Search the C runtimes, using the directory of the python libraries
# and the directories of the base executable
self._platform_add_extra_dependencies(dependent_files)
for source in dependent_files:
# Store dynamic libraries in appropriate location for platform.
self._copy_top_dependency(source)
# Once copied, it should be deleted from the list to ensure
# it will not be copied again.
name = Path(source.name)
if name in self.bin_includes:
self.bin_includes.remove(name)
self.bin_excludes.append(name)
target_path = self.target_dir / exe.target_name
self._copy_file(
exe.base,
target_path,
copy_dependent_files=False,
include_mode=True,
)
# copy a file with a the cx_freeze license into frozen application
respath = get_resource_file_path(
"initscripts", "frozen_application_license", ".txt"
)
if respath is None:
raise FileError("Unable to find license for frozen application.")
self._copy_file(
respath.absolute(),
self.target_dir / "frozen_application_license.txt",
copy_dependent_files=False,
include_mode=False,
)
if not os.access(target_path, os.W_OK):
mode = target_path.stat().st_mode
target_path.chmod(mode | stat.S_IWUSR)
# Add resources like version metadata and icon
self._add_resources(exe)
def _platform_add_extra_dependencies(
self, dependent_files: set[Path]
) -> None:
"""Override with platform specific files to add runtime libraries to
the list of dependent_files calculated in _freeze_executable.
"""
@abstractmethod
def _copy_top_dependency(self, source: Path) -> None:
"""Called for copying the top dependencies in _freeze_executable."""
def _default_bin_excludes(self) -> list[str]:
"""Return the file names of libraries that need not be included because
they would normally be expected to be found on the target system or
because they are part of a package which requires independent
installation anyway.
(overridden on Windows)
.
"""
return ["libclntsh.so", "libwtc9.so", "ldd"]
def _default_bin_includes(self) -> list[str]:
"""Return the file names of libraries which must be included for the
frozen executable to work.
(overriden on Windows)
.
"""
python_shared_libs: list[str] = []
name = sysconfig.get_config_var("INSTSONAME")
if name:
if name.endswith(".a"):
# Miniconda python Linux/macOS returns a static library.
if IS_MACOS:
name = name.replace(".a", ".dylib")
else:
name = name.replace(".a", ".so")
python_shared_libs.append(self._remove_version_numbers(name))
return python_shared_libs
@abstractmethod
def _default_bin_path_excludes(self) -> list[str]:
"""Return the paths of directories which contain files that should not
be included, generally because they contain standard system
libraries.
"""
@abstractmethod
def _default_bin_path_includes(self) -> list[str]:
"""Return the paths of directories which contain files that should
be included.
"""
def _get_module_finder(self) -> ModuleFinder:
finder = ModuleFinder(
self.constants_module,
self.excludes,
self.include_files,
self.path,
self.replace_paths,
self.zip_exclude_packages,
self.zip_include_packages,
self.zip_include_all_packages,
self.zip_includes,
)
finder.optimize = self.optimize
for name in self.includes:
finder.include_module(name)
for name in self.packages:
finder.include_package(name)
finder.add_base_modules()
return finder
def _post_freeze_hook(self) -> None:
"""Platform-specific post-Freeze work."""
def _print_report(self, filename: Path, modules: list[Module]) -> None:
print(f"writing zip file {filename}\n")
print(f" {'Name':<25} File")
print(f" {'----':<25} ----")
for module in modules:
if module.path:
print("P", end="")
else:
print("m", end="")
print(f" {module.name:<25} {module.file or ''}")
@staticmethod
def _remove_version_numbers(filename: str) -> str:
tweaked = False
parts = filename.split(".")
while parts:
if not parts[-1].isdigit():
break
parts.pop(-1)
tweaked = True
if tweaked:
return ".".join(parts)
return filename
def _should_copy_file(self, path: Path) -> bool: # noqa: PLR0911
"""Return true if the file should be copied to the target machine.
This is done by checking the bin_path_includes, bin_path_excludes,
bin_includes and bin_excludes configuration variables using first
the full file name, then just the base file name, then the file name
without any version numbers.
Files are included unless specifically excluded but inclusions take
precedence over exclusions.
"""
# check the full path
if path in self.bin_includes:
return True
if path in self.bin_excludes:
return False
# check the file name by itself (with any included version numbers)
filename = Path(path.name)
if filename in self.bin_includes:
return True
if filename in self.bin_excludes:
return False
# check the file name by itself (version numbers removed)
filename = Path(self._remove_version_numbers(path.name))
if filename in self.bin_includes:
return True
if filename in self.bin_excludes:
return False
# check the path for inclusion/exclusion
dirname = path.parent
for binpath in self.bin_path_includes:
try:
dirname.relative_to(binpath)
except ValueError:
pass
else:
return True
for binpath in self.bin_path_excludes:
try:
dirname.relative_to(binpath)
except ValueError:
pass
else:
return False
return True
@staticmethod
def _validate_path(path: list[str | Path] | None = None) -> list[str]:
"""Returns valid search path for modules, and fix the path for built-in
modules when it differs from the running python built-in modules.
"""
path = list(map(os.fspath, path or sys.path))
dynload = get_resource_file_path("bases", "lib-dynload", "")
if dynload and dynload.is_dir():
# add bases/lib-dynload to the finder path, if has modules
ext_suffix = get_config_var("EXT_SUFFIX")
if len(list(dynload.glob(f"*{ext_suffix}"))) > 0:
index = 0
dest_shared = sysconfig.get_config_var("DESTSHARED")
if dest_shared:
with suppress(ValueError, IndexError):
index = path.index(dest_shared)
path.pop(index)
path.insert(index, os.fspath(dynload))
return path
@staticmethod
def _validate_bin_path(bin_path: Sequence[str | Path] | None) -> list[str]:
"""Returns valid search path for bin_path_includes and
bin_path_excludes.
"""
if bin_path is None:
return []
valid = []
for path in map(Path, bin_path):
if path.is_dir():
valid.append(os.fspath(path))
return valid
def _verify_configuration(self) -> None:
"""Verify and normalize names and paths."""
filenames = list(self.bin_includes or [])
filenames += self._default_bin_includes()
self.bin_includes = [Path(name) for name in filenames]
filenames = list(self.bin_excludes or [])
filenames += self._default_bin_excludes()
self.bin_excludes = [Path(name) for name in filenames]
self.bin_path_includes += self._default_bin_path_includes()
self.bin_path_excludes += self._default_bin_path_excludes()
def _populate_zip_options(
self,
zip_include_packages: Sequence[str] | None,
zip_exclude_packages: Sequence[str] | None,
) -> None:
"""Verify, normalize and populate zip_*_packages options.
Raises OptionError on failure.
"""
if zip_include_packages is None and zip_exclude_packages is None:
zip_include_packages = []
zip_exclude_packages = ["*"]
else:
zip_include_packages = list(zip_include_packages or [])
zip_exclude_packages = list(zip_exclude_packages or [])
zip_include_all_packages = "*" in zip_include_packages
zip_exclude_all_packages = "*" in zip_exclude_packages
# check the '*' option
if zip_exclude_all_packages and zip_include_all_packages:
raise OptionError(
"all packages cannot be included and excluded "
"from the zip file at the same time"
)
# normalize namespace packages - syntax suggar
zip_include_packages = {
name.partition(".")[0] for name in zip_include_packages
}
zip_exclude_packages = {
name.partition(".")[0] for name in zip_exclude_packages
}
# check invalid usage
invalid = ", ".join(zip_include_packages & zip_exclude_packages)
if invalid:
raise OptionError(
f"package{'s' if len(invalid)>1 else ''} {invalid!r} "
"cannot be both included and excluded from zip file"
)
# populate
self.zip_include_packages = zip_include_packages
self.zip_exclude_packages = zip_exclude_packages
self.zip_include_all_packages = zip_include_all_packages
def _write_modules(self, filename: Path):
finder: ModuleFinder = self.finder
cache_path = finder.cache_path
modules = [m for m in finder.modules if m.name not in finder.excludes]
modules.append(
finder.include_file_as_module(
self.constants_module.create(cache_path, modules)
)
)
modules.sort(key=lambda m: m.name)
if self.silent < 1:
self._print_report(filename, modules)
if self.silent < 2:
finder.report_missing_modules()
target_lib_dir = filename.parent
self._create_directory(target_lib_dir)
# Prepare zip file
compress_type = ZIP_DEFLATED if self.compress else ZIP_STORED
with PyZipFile(filename, "w", compress_type) as outfile:
files_to_copy: list[tuple[Module, Path]] = []
for module in modules:
# determine if the module should be written to the file system;
# a number of packages make the assumption that files that they
# require will be found in a location relative to where they
# are located on disk; these packages will fail with strange
# errors when they are written to a zip file instead
include_in_file_system = module.in_file_system
mod_name = module.name
mod_name_parts = mod_name.split(".")
# if the module refers to a package, check to see if this
# package should be written to the file system
if (
include_in_file_system >= 1
and module.path is not None
and module.file is not None
):
parts = mod_name_parts
target_package_dir = target_lib_dir.joinpath(*parts)
if include_in_file_system == 2:
# a few packages are optimized on the hooks,
# so for now create the directory for this package
self._create_directory(target_package_dir)
elif not target_package_dir.exists():
# whether the package and its data will be written to
# the file system, any non-Python files are copied at
# this point if the target directory does not already
# exist
self._copy_package_data(module, target_package_dir)
# if an extension module is found in a package that is to be
# included in a zip file, copy the actual file to the build
# directory because shared libraries cannot be loaded from a
# zip file
if (
module.code is None
and module.file is not None
and include_in_file_system == 0
):
parts = mod_name_parts[:-1]
parts.append(module.file.name)
target = target_lib_dir / ".".join(parts)
files_to_copy.append((module, target))
# starting with Python 3.3 the pyc file format contains the
# source size; it is not actually used for anything except
# determining if the file is up to date so we can safely set
# this value to zero
if module.code is not None:
if module.file is not None and module.file.exists():
file_stat = module.file.stat()
mtime = int(file_stat.st_mtime) & 0xFFFF_FFFF
size = file_stat.st_size & 0xFFFFFFFF
else:
mtime = int(time.time()) & 0xFFFF_FFFF
size = 0
header = MAGIC_NUMBER + struct.pack("<iLL", 0, mtime, size)
data = header + marshal.dumps(module.code)
# if the module should be written to the file system, do so
if include_in_file_system >= 1 and module.file is not None:
parts = mod_name_parts.copy()
if module.code is None:
parts.pop()
parts.append(module.file.name)
target_name = target_lib_dir.joinpath(*parts)
self._copy_file(
module.file,
target_name,
copy_dependent_files=True,
)
else:
if module.path is not None:
parts.append("__init__")
target_name = target_lib_dir.joinpath(*parts)
target_name = target_name.with_suffix(".pyc")
self._create_directory(target_name.parent)
target_name.write_bytes(data)
# otherwise, write to the zip file
elif module.code is not None:
zip_time = time.localtime(mtime)[:6]
target_name = "/".join(mod_name_parts)
if module.path:
target_name += "/__init__"
zinfo = ZipInfo(target_name + ".pyc", zip_time)
zinfo.compress_type = compress_type
outfile.writestr(zinfo, data)
# put the distribution files metadata in the zip file
pos = len(cache_path.as_posix()) + 1
for name in cache_path.rglob("*.dist-info/*"):
if name.is_dir():
continue
outfile.write(name, name.as_posix()[pos:])
# write any files to the zip file that were requested specially
for source_path, target_path in finder.zip_includes:
if source_path.is_dir():
pos = len(source_path.as_posix()) + 1
for source_filename in source_path.rglob("*"):
if source_filename.is_dir():
continue
target = target_path / source_filename.as_posix()[pos:]
outfile.write(source_filename, target)
else:
outfile.write(source_path, target_path.as_posix())
# Copy Python extension modules from the list built above.
orig_path = os.environ["PATH"]
for module, target in files_to_copy:
try:
if module.parent is not None:
path = os.pathsep.join(
[orig_path, *list(map(os.fspath, module.parent.path))]
)
os.environ["PATH"] = path
self._copy_file(module.file, target, copy_dependent_files=True)
finally:
os.environ["PATH"] = orig_path
def freeze(self):
"""Do the freeze."""
finder: ModuleFinder = self.finder
# Add the executables to target
for executable in self.executables:
self._freeze_executable(executable)
# Write the modules
target_dir = self.target_dir
self._write_modules(target_dir / "lib" / "library.zip")
excluded_dependent_files = finder.excluded_dependent_files
for source_path, target_path in finder.included_files:
copy_dependent_files = source_path not in excluded_dependent_files
if source_path.is_dir():
# Copy directories by recursing into them.
# Can't use shutil.copytree because we may need dependencies
target_base = target_dir / target_path
for name in source_path.rglob("*"):
if name.is_dir():
continue
if any(
parent
for parent in name.parents
if parent.name in (".git", ".svn", "CVS")
):
continue
fulltarget = target_base / name.relative_to(source_path)
self._create_directory(fulltarget.parent)
self._copy_file(name, fulltarget, copy_dependent_files)
else:
# Copy regular files.
fulltarget = target_dir / target_path
self._copy_file(source_path, fulltarget, copy_dependent_files)
# do any platform-specific post-Freeze work
self._post_freeze_hook()
class WinFreezer(Freezer, PEParser):
"""Freezer base class for Windows OS."""
def __init__(self, *args, **kwargs):
Freezer.__init__(self, *args, **kwargs)
PEParser.__init__(self, self.path, self.bin_path_includes, self.silent)
# deal with C-runtime files
self.runtime_files: set[str] = set()
self._set_runtime_files()
def _add_resources(self, exe: Executable) -> None:
target_path: Path = self.target_dir / exe.target_name
# Change the manifest
manifest: str | None = exe.manifest
if manifest is not None or exe.uac_admin:
if self.silent < 1:
print(f"writing manifest -> {target_path}")
try:
if exe.uac_admin:
manifest = manifest or self.read_manifest(target_path)
manifest = manifest.replace(
"asInvoker", "requireAdministrator"
)
self.write_manifest(target_path, manifest)
except FileNotFoundError as exc:
if self.silent < 3:
print("WARNING:", exc)
except RuntimeError as exc:
if self.silent < 3:
print(f"WARNING: error parsing {target_path}:", exc)
# Add version resource
if self.metadata is not None:
warning_msg = "WARNING: unable to create version resource:"
if not self.metadata.version:
if self.silent < 3:
print(warning_msg, "version must be specified")
else:
winversioninfo = import_module("cx_Freeze.winversioninfo")
version = winversioninfo.VersionInfo(
self.metadata.version,
comments=self.metadata.long_description,
description=self.metadata.description,
company=self.metadata.author,
product=self.metadata.name,
copyright=exe.copyright,
trademarks=exe.trademarks,
verbose=bool(self.silent < 1),
)
try:
version.stamp(target_path)
except (FileNotFoundError, RuntimeError) as exc:
if self.silent < 3:
print(warning_msg, exc)
# Add icon
if exe.icon is not None:
try:
AddIcon(target_path, exe.icon)
except MemoryError:
if self.silent < 3:
print("WARNING: MemoryError")
except RuntimeError as exc:
if self.silent < 3:
print("WARNING:", exc)
except OSError as exc:
if "\\WindowsApps\\" in sys.base_prefix:
if self.silent < 3:
print("WARNING:", exc)
print(
"WARNING: Because of restrictions on Microsoft "
"Store apps, Python scripts may not have full "
"write access to built executable. "
"You will need to install the full installer."
)
else:
raise
# Update the PE checksum (or fix it in case it is zero)
try:
UpdateCheckSum(target_path)
except MemoryError:
if self.silent < 3:
print("WARNING: MemoryError")
except (RuntimeError, OSError) as exc:
if self.silent < 3:
print("WARNING:", exc)
def _copy_top_dependency(self, source: Path) -> None:
"""Called for copying certain top dependencies in _freeze_executable.
We need this as a separate method so that it can be overridden on
Darwin and Windows.
"""
# top dependencies go into build root directory on windows
# MS VC runtimes are handled in _copy_file/_pre_copy_hook
# msys2 libpython depends on libgcc_s_seh and libwinpthread dlls
# conda-forge python3x.dll depends on zlib.dll
target_dir = self.target_dir
target = target_dir / source.name
self._copy_file(
source, target, copy_dependent_files=False, include_mode=True
)
for dependent_source in self.get_dependent_files(source):
if IS_MINGW:
if self._should_copy_file(dependent_source):
self._copy_top_dependency(dependent_source)
else:
self._copy_file(
source=dependent_source,
target=target_dir / dependent_source.name,
copy_dependent_files=True,
)
def _pre_copy_hook(self, source: Path, target: Path) -> tuple[Path, Path]:
"""Prepare the source and target paths. Also, adjust the target of
C runtime libraries.
"""
# fix the target path for C runtime files
norm_target_name = target.name.lower()
if norm_target_name in self.runtime_files:
target = self.target_dir / norm_target_name
return source, target
def _post_copy_hook(
self,
source: Path,
target: Path,
copy_dependent_files: bool,
) -> None:
if (
copy_dependent_files
and source not in self.finder.excluded_dependent_files
):
library_dir = self.target_dir / "lib"
source_dir = source.parent
target_dir = target.parent
platform_bin_path = self._platform_bin_path
for dependent_source in self.get_dependent_files(source):
if not self._should_copy_file(dependent_source):
continue
# put the dependency in the target_dir (except C runtime)
dependent_srcdir = dependent_source.parent
dependent_name = dependent_source.name
try:
# dependency located with source or in a subdirectory
relative = dependent_source.relative_to(source_dir)
dependent_target = target_dir / relative
except ValueError:
# check if dependency is on default binaries path
if dependent_srcdir in platform_bin_path:
dependent_target = library_dir / dependent_name
else:
# check if dependency is located in a upper level
try:
relative = source_dir.relative_to(dependent_srcdir)
# fix the target_dir - go to the previous level
parts = target_dir.parts[: -len(relative.parts)]
dependent_target = Path(*parts) / dependent_name
except ValueError:
dependent_target = target_dir / dependent_name
# check to make sure the dependency is in the correct place
# because it can be outside of the python subtree
try:
dependent_target.relative_to(library_dir)
except ValueError:
dependent_target = library_dir / dependent_name
else:
_, dependent_target = self._pre_copy_hook(
dependent_source, dependent_target
)
if dependent_target not in self.files_copied:
for file in self.files_copied:
if file.match(dependent_name):
dependent_target = file
break
self._copy_file(
dependent_source, dependent_target, copy_dependent_files
)
def _default_bin_excludes(self) -> list[str]:
return ["comctl32.dll", "oci.dll"]
def _default_bin_includes(self) -> list[str]:
python_shared_libs: list[str] = []
if IS_MINGW:
# MSYS2 python returns a static library.
name = sysconfig.get_config_var("INSTSONAME")
if name:
python_shared_libs.append(name.replace(".dll.a", ".dll"))
else:
python_shared_libs += [
f"python{sys.version_info[0]}.dll",
f"python{sys.version_info[0]}{sys.version_info[1]}.dll",
]
return python_shared_libs
def _default_bin_path_excludes(self) -> list[str]:
system_dir = GetSystemDir()
windows_dir = GetWindowsDir()
return [windows_dir, system_dir, os.path.join(windows_dir, "WinSxS")]
def _default_bin_path_includes(self) -> list[str]:
paths = {Path(path) for path in sys.path if path}
paths.update(self._platform_bin_path)
return self._validate_bin_path(paths)
@cached_property
def _platform_bin_path(self) -> list[Path]:
# try to find the paths (windows, conda-forge, msys2/mingw)
paths = set()
dest_shared = sysconfig.get_config_var("DESTSHARED") # msys2
dest_relative = None
if dest_shared:
dest_shared = Path(dest_shared)
paths.add(dest_shared)
with suppress(ValueError):
dest_relative = dest_shared.relative_to(sys.prefix)
prefixes = [
sys.base_exec_prefix,
sys.base_prefix,
sys.exec_prefix,
sys.prefix,
]
for prefix in map(Path, prefixes):
paths.add(prefix / "bin")
paths.add(prefix / "DLLs")
paths.add(prefix / "Library/bin")
if dest_relative:
paths.add(prefix / dest_relative)
# return only valid paths
return [path for path in paths if path.is_dir()]
def _platform_add_extra_dependencies(
self, dependent_files: set[Path]
) -> None:
search_dirs: set[Path] = set()
for filename in dependent_files:
search_dirs.add(filename.parent)
for filename in self.runtime_files:
for search_dir in search_dirs:
filepath = search_dir / filename
if filepath.exists():
dependent_files.add(filepath)
def _post_freeze_hook(self) -> None:
target_lib = self.target_dir / "lib"
# Recursing into directories to search for load order files.
# Some libraries use delvewheel to patch them.
for loader_file in target_lib.rglob(".load-order-*"):
load_order = loader_file.read_text(encoding="utf-8").split()
load_dir = loader_file.parent
new_order = [
f for f in load_order if load_dir.joinpath(f).is_file()
]
if new_order != load_order:
loader_file.write_text("\n".join(new_order))
def _set_runtime_files(self) -> None:
winmsvcr = import_module("cx_Freeze.winmsvcr")
if self.include_msvcr:
self.runtime_files.update(winmsvcr.FILES)
else:
# just put on the exclusion list
self.bin_excludes.extend(list(map(Path, winmsvcr.FILES)))
class DarwinFreezer(Freezer, Parser):
"""Freezer base class for macOS."""
def __init__(self, *args, **kwargs):
Freezer.__init__(self, *args, **kwargs)
Parser.__init__(self, self.path, self.bin_path_includes, self.silent)
self.darwin_tracker: DarwinFileTracker | None = None
self.darwin_tracker = DarwinFileTracker()