Debian Codegraph @

dh_qmldeps

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2023 Sandro Knauß <[email protected]>
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4
5 #INTROSPECTABLE: CONFIG-FILES pkgfile(qmlfiles) path(debian/qmldeps.overrides) path(debian/qmldeps.optional)
6
7 import copy
8 import itertools
9 import logging
10 import os
11 import pathlib
12 import subprocess
13 import sys
14 import typing
15
16 from debian.debian_support import DpkgArchTable
17
18 PKGKDE_GETQMLDEPENDS = "pkgkde-getqmldepends"
19
20 sys.path.insert(0,'/usr/share/pkg-kde-tools')
21 parent_path = pathlib.Path(__file__).parent.absolute()
22 if parent_path not in ("/usr/bin", "/bin"):
23 sys.path.insert(0, str(parent_path))
24 PKGKDE_GETQMLDEPENDS = parent_path/"pkgkde-getqmldepends"
25
26 from pythonlib import pkgkde
27 from pythonlib import qmldeps
28
29 QT_CONFIGS = qmldeps.get_config()
30 QT_VERSIONS = {i.qt_name: k for k,i in QT_CONFIGS.items()}
31
32 logging.basicConfig(format='%(levelname).1s: %(module)s:%(lineno)d: %(message)s')
33
34 log = logging.getLogger()
35 log.setLevel(logging.INFO)
36
37 def qmlfiles_args(pkg_name: str) -> list[str | pathlib.Path]:
38 qmlfiles_path = pathlib.Path(f"debian/{pkg_name}.qmlfiles")
39 ret = []
40 multiarch = qmldeps.deb_host_multiarch()
41
42 replace_args = {
43 "${DEB_HOST_MULTIARCH}" : lambda x: multiarch,
44 "${QMLDEPS_AUTODETECT_MODULES}": autodetect_modules,
45 "${QMLDEPS_AUTODETECT_FILES}": autodetect_files,
46 "${QMLDEPS_AUTODETECT_FILES_EXAMPLES}": lambda x: filter_examples(autodetect_files(x)),
47 "${QMLDEPS_AUTODETECT_FILES_LEFTOVER}": lambda x: filter_not_examples(autodetect_files(x)),
48 "${QMLDEPS_AUTODETECT_SOURCE_FILES}": autodetect_source_files,
49 }
50
51 if qmlfiles_path.exists():
52 content = qmlfiles_path.read_text()
53 args = content.split()
54 for arg in args:
55 for test_arg,func in replace_args.items():
56 if test_arg == arg:
57 arg = list(func(pkg_name))
58 elif test_arg in arg:
59 arg = arg.replace(test_arg, func(pkg_name))
60 if "*" in arg:
61 ret.extend(pathlib.Path().glob(arg))
62 elif isinstance(arg, list):
63 ret.extend(arg)
64 else:
65 ret.append(arg)
66 return ret
67
68 def qt_version_from_files(files: list[str|pathlib.Path]) -> typing.Optional[str]:
69 qt_version = None
70 for path in files:
71 if qt_version:
72 break
73 if not isinstance(path, pathlib.Path):
74 continue
75 if not qt_version:
76 for p, ver in QT_VERSIONS.items():
77 if p in path.parts:
78 qt_version = ver
79 break
80 return qt_version
81
82
83 def func_examples(file: pathlib.Path):
84 return "examples" in file.parts
85
86
87 def filter_examples(files):
88 return filter(func_examples, files)
89
90
91 def filter_not_examples(files):
92 return itertools.filterfalse(func_examples, files)
93
94
95 def autodetect_modules(pkg_name: str) -> list[pathlib.Path]:
96 """returns a list of all qml module paths shipped within a binary package."""
97 return (p.parent for p in pathlib.Path(f"debian/{pkg_name}").glob("**/qmldir"))
98
99
100 def autodetect_files(pkg_name: str) -> list[pathlib.Path]:
101 """returns a list of all qml files shipped within a binnry package."""
102 return pathlib.Path(f"debian/{pkg_name}").glob("**/*.qml")
103
104
105 def autodetect_source_files(pkg_name: str) -> list[pathlib.Path]:
106 """returns a list of all qml modules that are shipped within the source package and all binary packages.
107 This function is mostly usefull for a single binary package mode.
108 pkg_name is not used. It needs the same arguments than the other autodetect functions."""
109 return (i for i in pathlib.Path(".").glob("**/*.qml") if "tests/" not in str(i))
110
111
112 def main(command_line_arguments: typing.List[str]):
113 control = pkgkde.DebianControl(pathlib.Path("debian/control"))
114 single_pkg = control.isSinglePackage()
115
116 failed = False
117
118 getqmldepends_glob_args = command_line_arguments
119
120 arch_any = "-a" in getqmldepends_glob_args
121 arch_all = "-i" in getqmldepends_glob_args
122
123 try:
124 host_arch = os.environ["DEB_HOST_ARCH"]
125 except KeyError:
126 host_arch = subprocess.check_output(["dpkg-architecture", "-qDEB_HOST_ARCH"]).strip().decode()
127
128 archtable = DpkgArchTable.load_arch_table()
129
130 if not arch_any and not arch_all:
131 arch_any = True
132 arch_all = True
133
134 for pkg_name, pkg in control.packages.items():
135 archs = pkg.get("Architecture", "").split()
136
137 skip = True
138 if archs == []:
139 skip = False
140 elif arch_all and "all" in archs:
141 skip = False
142 elif arch_any and any(archtable.matches_architecture(host_arch, arch) for arch in archs):
143 skip = False
144
145 if skip:
146 continue
147
148 for qt_version, config_qt in QT_CONFIGS.items():
149 var_name = config_qt.substvar_name("Depends")
150 var = f"${{{var_name}}}"
151 for field in ("Depends", "Recommends", "Suggests"):
152 if var in pkg.get(field, ""):
153 break
154 else:
155 continue
156 break
157 else:
158 qt_version = None
159
160 extra_args = qmlfiles_args(pkg_name)
161
162 if not extra_args:
163 if root_path := list(autodetect_modules(pkg_name)):
164 extra_args = ["-d", "Depends", "-rootPath"] + root_path
165
166 if not extra_args:
167 files = list(autodetect_files(pkg_name))
168
169 if files:
170 examples_files = list(filter_examples(files))
171 if examples_files:
172 files = list(filter_not_examples(files))
173 extra_args += ["-d", "Recommends", "-qmlFiles"] + examples_files
174
175 elif single_pkg and not files:
176 files = list(autodetect_source_files(pkg_name))
177
178 if files:
179 extra_args += ["-d", "Depends", "-qmlFiles"] + files
180
181 if extra_args:
182 cmd = copy.copy(getqmldepends_glob_args)
183
184 cmd.extend(["-p", pkg_name])
185
186 if not qt_version:
187 qt_version = qt_version_from_files(extra_args)
188
189 if qt_version:
190 cmd.extend(["--qt", qt_version])
191
192 cont = True
193 start = 0
194
195 while cont:
196 try:
197 pos = extra_args.index("-d", start+1)
198 args_part = extra_args[start:pos]
199 start = pos
200 except ValueError:
201 args_part=extra_args[start:]
202 cont = False
203
204 if args_part[0] == "-d":
205 _cmd = cmd + args_part[:2] + ["--"] + args_part[2:]
206 else:
207 _cmd = cmd + ["--"] + args_part
208 log.info(f"Execute {' '.join(str(i) for i in _cmd)}")
209 try:
210 qmldeps.main([str(i) for i in _cmd])
211 except Exception:
212 failed = True
213 elif qt_version:
214 log.warning(f"{pkg_name}: No automatic qml files found for package you need to add the files by hand to debian/{pkg_name}.qmlfiles")
215 elif single_pkg:
216 log.error(f"{pkg_name}: No automatic qml files found for package you need to add the files by hand to debian/{pkg_name}.qmlfiles")
217 failed = True
218
219 if failed:
220 sys.exit(1)
221
222 if __name__ == '__main__':
223 main(sys.argv[1:])