-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwho_depends_this_lib
executable file
·195 lines (157 loc) · 5.64 KB
/
who_depends_this_lib
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
#!/usr/bin/python3
from __future__ import annotations
import os
import subprocess
import logging
import tarfile
import re
from pathlib import Path
from typing import Optional, Tuple, Iterator
from cmdutils import so_depends
from pkgchecker import get_buildinfo, extract_package, iter_installed
logger = logging.getLogger(__name__)
def path_suspicious(path: str) -> bool:
basename = os.path.basename(path)
return '/bin/' in path or '.so' in basename
def walkdir(dir: str) -> Iterator[os.DirEntry]:
for entry in os.scandir(dir):
yield entry
if not entry.is_symlink() and entry.is_dir():
yield from walkdir(entry.path)
def check_dependency(dir: str, lib_re: re.Pattern) -> Optional[Tuple[str, str]]:
for entry in walkdir(dir):
if not path_suspicious(entry.path):
continue
if not entry.is_file() or entry.is_symlink():
continue
try:
libs = so_depends(entry.path)
logger.debug('so_depends %s: %s', entry.path, libs)
except subprocess.CalledProcessError:
continue
for l in libs:
if lib_re.search(l):
return entry.path, l
return None
def buildinfo_matches(pkg: Path, dep_pkgname: str, dep_pkgver: Optional[str]) -> Optional[bool]:
# If a package links to a library that matches `lib_re` but does not have
# `dep_pkgname` installed during the build, that package is already broken
# For example, packages with files linked to libprotobuf.so should have
# protobuf installed during the build.
buildinfo = get_buildinfo(pkg)
if buildinfo is None:
return None
has_dep = False
for parts in iter_installed(buildinfo):
if len(parts) != 4:
logger.warning('Old .BUILDINFO format - entry %s found in %s; checking anyway', '-'.join(parts), pkg)
has_dep = True
break
pkgname, pkgver, _, _ = parts
if pkgname == dep_pkgname and (dep_pkgver is None or dep_pkgver == pkgver):
has_dep = True
break
if not has_dep:
logger.info('%s does not depend on %s, skipping' % (pkg, dep_pkgname))
return has_dep
def check_package_so(pkg: Path, lib_re: re.Pattern) -> Optional[Tuple[str, str]]:
with extract_package(pkg) as d:
logger.info('checking...')
a = check_dependency(d, lib_re)
if a is not None:
r, lib = a
r = os.path.relpath(r, d)
logger.warning('%s depends on %s: %s', pkg, lib, r)
return r, lib
return None
def main(db: Path, checker: Checker) -> None:
ret = []
dir = db.parent
with tarfile.open(db) as tar:
for tarinfo in tar:
if tarinfo.isdir():
filename = files_match = None
name = tarinfo.name.split('/', 1)[0]
continue
if '-debug-' in name:
continue
if tarinfo.name.endswith('/depends'):
continue
if tarinfo.name.endswith('/desc'):
f = tar.extractfile(tarinfo)
assert f
data = f.read().decode()
it = iter(data.splitlines())
while True:
try:
l = next(it)
except StopIteration:
break
if l == '%FILENAME%':
filename = next(it)
break
if tarinfo.name.endswith('/files'):
f = tar.extractfile(tarinfo)
assert f
data = f.read().decode()
it = iter(data.splitlines())
next(it)
for path in it:
if path_suspicious(path):
files_match = True
break
if filename and files_match:
r = checker.check(dir / filename)
if r is not None:
ret.append((name, r))
for name, r in ret:
checker.output(name, r)
class Checker[R]:
def check(self, pkg: Path) -> Optional[R]:
raise NotImplementedError
def output(self, name: str, r: R) -> None:
raise NotImplementedError
class LibChecker(Checker):
def __init__(self, libname: str, dep_pkgname: Optional[str]) -> None:
self.lib_re = re.compile(libname)
self.dep_pkgname = dep_pkgname
def check(self, pkg: Path) -> Optional[tuple[str, str]]:
if self.dep_pkgname is not None:
if buildinfo_matches(pkg, self.dep_pkgname, None) is False:
return None
return check_package_so(pkg, self.lib_re)
def output(self, name: str, r: tuple[str, str]) -> None:
print('%s: %s (%s)' % (name, *r))
class PkgverChecker(Checker):
def __init__(self, dep_pkgname: str, dep_pkgver: str) -> None:
self.dep_pkgname = dep_pkgname
self.dep_pkgver = dep_pkgver
def check(self, pkg: Path) -> Optional[bool]:
if buildinfo_matches(pkg, self.dep_pkgname, self.dep_pkgver) is True:
return True
return None
def output(self, name: str, _r: bool) -> None:
print(name)
if __name__ == '__main__':
from nicelogger import enable_pretty_logging
enable_pretty_logging('INFO')
import argparse
parser = argparse.ArgumentParser(
description='find out what Arch packages need a particular library. Depends on cmdutils from winterpy.')
parser.add_argument('pkgdb',
help='the package files database, eg. /data/repo/x86_64/archlinuxcn.files.tar.gz')
parser.add_argument('--dep-pkgname', metavar='PKGNAME',
help='the package name that affected packages should depend on')
parser.add_argument('--libname',
help='the library filename regex to match')
parser.add_argument('--dep-pkgver', metavar='PKGVER',
help='the package version that affected packages should depend on')
args = parser.parse_args()
checker: Checker
if args.libname:
checker = LibChecker(args.libname, args.dep_pkgname)
elif args.dep_pkgver:
checker = PkgverChecker(args.dep_pkgname, args.dep_pkgver)
else:
raise ValueError('--libname or --dep-pkgver is needed')
main(Path(args.pkgdb), checker)