Debian Codegraph @

pyqtbuild/bundle/abstract_package.py

raw
1 # SPDX-License-Identifier: BSD-2-Clause
2
3 # Copyright (c) 2024 Phil Thompson <[email protected]>
4
5
6 from abc import ABC, abstractmethod
7 import os
8 import packaging
9
10 from sipbuild import UserException
11
12 from .qt_metadata import VersionedMetadata
13 from .verbose import verbose
14
15
16 class AbstractPackage(ABC):
17 """ This specifies the API of a package. """
18
19 def __init__(self, qt_dir, version_str=None):
20 """ Initialise the package. """
21
22 self._qt_dir = os.path.abspath(qt_dir)
23
24 # Get the Qt version.
25 self.qt_version = self._parse_version(
26 os.path.basename(os.path.dirname(self._qt_dir)))
27
28 # We don't support anything older that the current LTS release.
29 if self.qt_version < (5, 15, 0):
30 raise UserException(
31 "Version of Qt older than v5.15 are not supported")
32
33 # Parse any package version string.
34 if version_str:
35 self._version = self._parse_version(version_str)
36
37 # If we set the maintenance number to 0 then this will be the
38 # version of Qt the wheel was built against. (This is not a valid
39 # assumption on Windows because of the QAxContainer problem but
40 # this is handled elsewhere.)
41 min_qt_version = (self._version[0], self._version[1], 0)
42
43 # Check the versions are compatible.
44 if self.qt_version < min_qt_version:
45 raise UserException(
46 "The version of Qt being bundled is too old")
47 else:
48 self._version = None
49
50 def bundle_msvc_runtime(self, target_qt_dir, platform_tag):
51 """ Bundle the MSVC runtime. """
52
53 # This default implementation does nothing.
54
55 def bundle_openssl(self, target_qt_dir, openssl_dir, platform_tag):
56 """ Bundle the OpenSSL DLLs. """
57
58 # This default implementation does nothing.
59
60 def bundle_qt(self, target_qt_dir, platform_tag, exclude, ignore_missing,
61 bindings=True, subwheel=None):
62 """ Bundle the relevant parts of the Qt installation. Returns True if
63 the LGPL applies to all bundled parts.
64 """
65
66 # Architecture-specific values.
67 if platform_tag.startswith('manylinux'):
68 module_extensions = ['.abi3.so', '.so']
69 elif platform_tag.startswith('macosx'):
70 module_extensions = ['.abi3.so', '.so']
71 elif platform_tag.startswith('win'):
72 module_extensions = ['.pyd']
73 else:
74 raise UserException(
75 "Unsupported platform tag '{0}'".format(platform_tag))
76
77 package_dir = os.path.dirname(target_qt_dir)
78 lgpl = True
79
80 for name, metadata in self.get_qt_metadata().items():
81 # Ignore a module if it is explicitly excluded.
82 if name in exclude:
83 continue
84
85 # Get the metadata for the Qt version.
86 if isinstance(metadata, VersionedMetadata):
87 metadata = [metadata]
88
89 metadata = [md for md in metadata
90 if md.is_applicable(self.qt_version)]
91
92 if len(metadata) == 0:
93 continue
94
95 metadata = metadata[0]
96
97 # See if we need to check if the bindings are present to decide to
98 # bundle this part of Qt.
99 if bindings:
100 # Find the bindings.
101 for ext in module_extensions:
102 if os.path.isfile(os.path.join(package_dir, name + ext)):
103 break
104 else:
105 verbose(
106 "Skipping {0} as it is not in the wheel".format(
107 name))
108 continue
109 elif metadata.legacy:
110 # We don't bundle Qt for legacy bindings.
111 continue
112
113 lgpl = lgpl and metadata.lgpl
114
115 metadata.bundle(name, target_qt_dir, self._qt_dir, platform_tag,
116 self.qt_version, ignore_missing, subwheel)
117
118 return lgpl
119
120 @abstractmethod
121 def get_qt_metadata(self):
122 """ Return the package-specific meta-data describing the parts of Qt to
123 install.
124 """
125
126 def get_target_qt_dir(self):
127 """ Return the directory, relative to the wheel root, containing the
128 bundled Qt directory.
129 """
130
131 # Assume the current naming of the bundled Qt directory.
132 qt_major_version = self.qt_version[0]
133
134 # See if it is an older version.
135 if self._version is not None:
136 if (6, 0, 0) <= self._version <= (6, 0, 2):
137 qt_major_version = ''
138 elif self._version <= (5, 15, 3):
139 qt_major_version = ''
140
141 return os.path.join('PyQt{}'.format(qt_major_version),
142 'Qt{}'.format(qt_major_version))
143
144 @staticmethod
145 def missing_executable(exe):
146 """ Return True if an executable cannot be found on PATH. """
147
148 for p in os.environ.get('PATH', '').split(os.pathsep):
149 exe_path = os.path.join(p, exe)
150
151 if os.access(exe_path, os.X_OK):
152 return False
153
154 return True
155
156 @property
157 def qt_version_str(self):
158 """ The version number of the Qt installation as a string. """
159
160 return '.'.join([str(v) for v in self.qt_version])
161
162 @staticmethod
163 def _parse_version(version_str):
164 """ Parse a version string as a 3-tuple of major, minor and maintenance
165 versions.
166 """
167
168 base_version = packaging.version.parse(version_str).base_version
169 base_version = base_version.split('.')
170 del base_version[3:]
171
172 while len(base_version) < 3:
173 base_version.append('0')
174
175 version = []
176 for part in base_version:
177 try:
178 version.append(int(part))
179 except ValueError:
180 raise UserException(
181 "Unable to parse '{0}' as a version number".format(
182 version_str))
183
184 return tuple(version)