Debian Codegraph @

pyqtbuild/bundle/bundle.py

raw
1 # SPDX-License-Identifier: BSD-2-Clause
2
3 # Copyright (c) 2024 Phil Thompson <[email protected]>
4
5
6 import fnmatch
7 import os
8 import shutil
9
10 from sipbuild import UserException
11
12 from . import packages
13 from .abstract_package import AbstractPackage
14 from .verbose import verbose
15 from .wheel import create_wheel, unpack_wheel, write_record_file
16
17
18 def bundle(wheel_path, qt_dir, build_tag_suffix, msvc_runtime, openssl,
19 openssl_dir, exclude, ignore_missing, arch):
20 """ Bundle a Qt installation with a PyQt wheel. """
21
22 wheel_path = os.path.abspath(wheel_path)
23 qt_dir = os.path.abspath(qt_dir)
24
25 if openssl_dir:
26 openssl_dir = os.path.abspath(openssl_dir)
27
28 # Deconstruct the wheel name.
29 wheel_name = os.path.basename(wheel_path)
30 parts = wheel_name.split('-')
31
32 max_nr_parts = 6
33 if parts[-1] == 'unlicensed':
34 max_nr_parts += 1
35
36 if len(parts) == max_nr_parts:
37 # Remove the build tag.
38 del parts[2]
39 elif len(parts) != max_nr_parts - 1:
40 raise UserException(
41 "Unable to recognise '{0}' as a wheel name".format(wheel_name))
42
43 # If an architecture is specified then it must be supported by the wheel.
44 if arch:
45 if arch in parts[-1]:
46 pass
47 elif 'universal2' in parts[-1]:
48 parts[-1] = parts[-1].replace('universal2', arch)
49 else:
50 raise UserException(
51 "'{0}' is not supported by {1}".format(arch, wheel_name))
52
53 # Get the package object.
54 package_title = parts[0]
55 if package_title.endswith('_commercial'):
56 package_title = package_title[:-11]
57
58 package_name = package_title.lower()
59
60 # Look for the factory corresponding to the normalised package name.
61 for key, value in packages.__dict__.items():
62 # This is the name of an intermediate class so explicity exclude it.
63 # It can only happen if the user has been changing the wheel names.
64 if key == 'PyQt':
65 continue
66
67 if key.lower() == package_name and isinstance(value, type) and issubclass(value, AbstractPackage):
68 package_factory = value
69 break
70 else:
71 raise UserException(
72 "'{0}' is not a supported package".format(package_title))
73
74 package = package_factory(qt_dir, parts[1])
75
76 # Construct the name of the bundled wheel.
77 build_tag = package.qt_version_str
78
79 if build_tag_suffix:
80 build_tag += build_tag_suffix
81
82 parts.insert(2, build_tag)
83
84 bundled_wheel_name = '-'.join(parts)
85 bundled_wheel_path = os.path.abspath(bundled_wheel_name)
86
87 bundled_wheel_dir = bundled_wheel_name
88 for tail in ('-unlicensed', '.whl'):
89 if bundled_wheel_dir.endswith(tail):
90 bundled_wheel_dir = bundled_wheel_dir[:-len(tail)]
91
92 platform_tag = bundled_wheel_dir.split('-')[-1]
93
94 # Create the directory to contain the existing wheel contents.
95 shutil.rmtree(bundled_wheel_dir, ignore_errors=True)
96 os.mkdir(bundled_wheel_dir)
97
98 # Unpack the existing wheel.
99 saved_cwd = os.getcwd()
100 os.chdir(bundled_wheel_dir)
101
102 verbose("Unpacking {0}".format(wheel_name))
103 unpack_wheel(wheel_path)
104
105 # Remove any existing bundled Qt installation while protecting some
106 # specific directories.
107 verbose("Removing any existing Qt bundle")
108
109 target_qt_dir = package.get_target_qt_dir()
110 if os.path.isdir(target_qt_dir):
111 for fn in os.listdir(target_qt_dir):
112 if fn not in ('qsci', ):
113 shutil.rmtree(os.path.join(target_qt_dir, fn),
114 ignore_errors=True)
115
116 # Bundle the relevant parts of the Qt installation.
117 package.bundle_qt(target_qt_dir, platform_tag, exclude, ignore_missing)
118
119 if platform_tag in ('win32', 'win_amd64'):
120 # Bundle the MSVC runtime if required.
121 if msvc_runtime:
122 package.bundle_msvc_runtime(target_qt_dir, platform_tag)
123
124 # Bundle OpenSSL if required.
125 if openssl:
126 package.bundle_openssl(target_qt_dir, openssl_dir, platform_tag)
127
128 # Find the .dist-info directory.
129 for distinfo_dir in os.listdir('.'):
130 if fnmatch.fnmatch(distinfo_dir, '*.dist-info'):
131 break
132 else:
133 raise UserException(
134 "'{0}' doesn't contain a .dist-info directory".format(
135 wheel_path))
136
137 # Remove any dependency on an external Qt wheel from the METADATA file.
138 update_metadata = False
139 updated_metadata = ''
140 qt_wheel = package_title + '-Qt'
141 metadata_path = os.path.join(distinfo_dir, 'METADATA')
142
143 with open(metadata_path) as f:
144 for line in f:
145 if 'Requires-Dist:' in line and qt_wheel in line:
146 update_metadata = True
147 else:
148 updated_metadata += line
149
150 if update_metadata:
151 verbose("Updating the METADATA file")
152
153 with open(metadata_path, 'w') as f:
154 f.write(updated_metadata)
155
156 # Rewrite the wheel's RECORD file.
157 verbose("Writing the RECORD file")
158 names = write_record_file(distinfo_dir)
159
160 # Create the bundled wheel.
161 verbose("Writing {0}".format(bundled_wheel_name))
162 create_wheel(bundled_wheel_path, names)
163
164 # Tidy up.
165 os.chdir(saved_cwd)
166 shutil.rmtree(bundled_wheel_dir)
167
168 verbose("Bundling complete.")