| 1 |
|
| 2 |
|
| 3 |
"""check-symlinks: check for likely classes of errors in generated symlinks |
| 4 |
|
| 5 |
Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 6 |
SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
Author: Robie Basak <[email protected]> |
| 9 |
|
| 10 |
For every binary package in debian/control, this script inspects |
| 11 |
debian/<package>/. |
| 12 |
|
| 13 |
Two packages must not ship files with the same path, or one package a file and |
| 14 |
another a directory with the same path. |
| 15 |
|
| 16 |
Every symlink must be relative. If it resolves, it must resolve to somewhere |
| 17 |
inside debian/<package>. If it does not resolve, the target must nevertheless |
| 18 |
be found in debian/<other-package>/. In the latter case <package> must depend |
| 19 |
on <other-package>. |
| 20 |
|
| 21 |
If any must directive above is found to be violated then this script exits with |
| 22 |
a non-zero status. Otherwise it returns a zero status. |
| 23 |
|
| 24 |
To avoid this becoming too complex, I haven't covered the general case for all |
| 25 |
packaging, just what is done in this package that I think is more likely to |
| 26 |
break and that I would like to verify prior to sponsorship. Correct handling of |
| 27 |
absolute symlinks, Conflicts cases and finding upgrade path problems are not |
| 28 |
implemented. |
| 29 |
|
| 30 |
Run immediately after dh_install, for example via override_dh_install. |
| 31 |
""" |
| 32 |
|
| 33 |
import itertools |
| 34 |
import os |
| 35 |
import stat |
| 36 |
import sys |
| 37 |
|
| 38 |
import debian.deb822 |
| 39 |
|
| 40 |
|
| 41 |
class CheckError(RuntimeError): |
| 42 |
"""Exception raised when symlink validation checks fail. |
| 43 |
|
| 44 |
Raised to indicate errors in symlink configuration, such as missing |
| 45 |
targets, incorrect dependencies, or unsupported symlink types (eg. absolute |
| 46 |
symlinks). |
| 47 |
""" |
| 48 |
|
| 49 |
pass |
| 50 |
|
| 51 |
|
| 52 |
def check_symlink( |
| 53 |
paths: dict[str, str], |
| 54 |
dependencies: dict[str, set[str]], |
| 55 |
source_package: str, |
| 56 |
source_path: str, |
| 57 |
target_path: str, |
| 58 |
) -> None: |
| 59 |
"""Check that a symlink is valid and properly declared in dependencies. |
| 60 |
|
| 61 |
Validates that a symlink target exists and that the source package properly |
| 62 |
depends on the target package if they differ. |
| 63 |
|
| 64 |
:param paths: mapping of relative file/directory paths to the package |
| 65 |
names that provide them. Directory paths end with "/". |
| 66 |
:param dependencies: mapping of package names to sets of package names |
| 67 |
they depend on, as declared in debian/control. |
| 68 |
:param source_package: name of the binary package containing the symlink. |
| 69 |
:param source_path: full path to the symlink file being checked. |
| 70 |
:param target_path: the target path that the symlink points to (as read |
| 71 |
from the symlink itself). |
| 72 |
:raises CheckError: if the symlink is absolute (not implemented), if the |
| 73 |
target path cannot be resolved to any package, or if the source package |
| 74 |
does not declare a dependency on the target package. |
| 75 |
""" |
| 76 |
if target_path.startswith("/"): |
| 77 |
raise CheckError( |
| 78 |
f"Absolute symlink checking not implemented: {source_path}" |
| 79 |
) |
| 80 |
rel_target_path = os.path.relpath( |
| 81 |
os.path.normpath( |
| 82 |
os.path.join(os.path.dirname(source_path), target_path) |
| 83 |
), |
| 84 |
f"debian/{source_package}", |
| 85 |
) |
| 86 |
|
| 87 |
target_package = paths.get(rel_target_path) or paths.get( |
| 88 |
rel_target_path + "/" |
| 89 |
) |
| 90 |
if target_package is None: |
| 91 |
raise CheckError( |
| 92 |
f"{source_path} symlink target {target_path} not found" |
| 93 |
) |
| 94 |
if ( |
| 95 |
source_package != target_package |
| 96 |
and target_package not in dependencies[package] |
| 97 |
): |
| 98 |
raise CheckError( |
| 99 |
f"{source_path} symlink target {target_path} resolved through package {target_package} but no Depends found" |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
def stop_if_errored(errors): |
| 104 |
if errors: |
| 105 |
print(*errors, sep="\n") |
| 106 |
sys.exit(1) |
| 107 |
|
| 108 |
|
| 109 |
with open("debian/control") as control_file: |
| 110 |
binary_packages = list( |
| 111 |
list(debian.deb822.Packages.iter_paragraphs(control_file))[1:] |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
dependencies = { |
| 117 |
p["package"]: { |
| 118 |
|
| 119 |
d[0]["name"].split(" ")[0] |
| 120 |
for d in p.relations["depends"] |
| 121 |
|
| 122 |
if not d[0]["name"].startswith("$") |
| 123 |
} |
| 124 |
for p in binary_packages |
| 125 |
} |
| 126 |
|
| 127 |
binary_package_names = list(dependencies.keys()) |
| 128 |
errors = [] |
| 129 |
paths = {} |
| 130 |
|
| 131 |
for package in binary_package_names: |
| 132 |
package_root = f"debian/{package}" |
| 133 |
for dirpath, dirnames, filenames in os.walk(f"debian/{package}"): |
| 134 |
package_paths = set( |
| 135 |
itertools.chain( |
| 136 |
[ |
| 137 |
os.path.relpath(os.path.join(dirpath, path), package_root) |
| 138 |
for path in filenames |
| 139 |
], |
| 140 |
[ |
| 141 |
os.path.relpath(os.path.join(dirpath, path), package_root) |
| 142 |
+ "/" |
| 143 |
for path in dirnames |
| 144 |
], |
| 145 |
) |
| 146 |
) |
| 147 |
for path in package_paths.intersection(paths.keys()): |
| 148 |
if path.endswith("/"): |
| 149 |
|
| 150 |
continue |
| 151 |
errors.append( |
| 152 |
f"File provided by {paths[path]} but also {package}: {path}" |
| 153 |
) |
| 154 |
file_dir_collisions = [ |
| 155 |
path |
| 156 |
for path in package_paths |
| 157 |
if path.endswith("/") and path.rstrip("/") in paths |
| 158 |
] |
| 159 |
for path in file_dir_collisions: |
| 160 |
file_path = path.rstrip("/") |
| 161 |
errors.append( |
| 162 |
f"File provided by {paths[file_path]} but is also a directory shipped by {package}: {file_path}" |
| 163 |
) |
| 164 |
dir_file_collisions = [ |
| 165 |
path |
| 166 |
for path in package_paths |
| 167 |
if not path.endswith("/") and f"{path}/" in paths |
| 168 |
] |
| 169 |
for path in dir_file_collisions: |
| 170 |
errors.append( |
| 171 |
f"File provided by {package} but is also a directory shipped by {paths[f'{path}/']}: {path}" |
| 172 |
) |
| 173 |
paths.update({path: package for path in package_paths}) |
| 174 |
|
| 175 |
|
| 176 |
stop_if_errored(errors) |
| 177 |
|
| 178 |
for package in binary_package_names: |
| 179 |
for dirpath, dirnames, filenames in os.walk(f"debian/{package}"): |
| 180 |
for filename in filenames: |
| 181 |
source_path = os.path.join(dirpath, filename) |
| 182 |
if stat.S_ISLNK(os.lstat(source_path).st_mode): |
| 183 |
try: |
| 184 |
check_symlink( |
| 185 |
paths=paths, |
| 186 |
dependencies=dependencies, |
| 187 |
source_package=package, |
| 188 |
source_path=source_path, |
| 189 |
target_path=os.readlink(source_path), |
| 190 |
) |
| 191 |
except CheckError as e: |
| 192 |
errors.append(str(e)) |
| 193 |
|
| 194 |
stop_if_errored(errors) |