-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
list_dependencies.js
285 lines (252 loc) · 10.5 KB
/
list_dependencies.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const path = require('path');
const {spawn} = require('child_process');
const {registryDirectory} = require('playwright-core/lib/server');
const readdirAsync = util.promisify(fs.readdir.bind(fs));
const readFileAsync = util.promisify(fs.readFile.bind(fs));
const readline = require('readline');
// These libraries are accessed dynamically by browsers using `dlopen` system call and
// thus have to be installed in the system.
//
// Tip: to assess which libraries are getting opened dynamically, one can use `strace`:
//
// strace -f -e trace=open,openat <program>
//
const DL_OPEN_LIBRARIES = {
chromium: [],
firefox: [],
webkit: [ 'libGLESv2.so.2' ],
};
(async () => {
console.log('Working on:', await getDistributionName());
console.log('Started at:', currentTime());
const browserDescriptors = (await readdirAsync(registryDirectory)).filter(dir => !dir.startsWith('.')).map(dir => ({
// Full browser name, e.g. `webkit-1144`
name: dir,
// Full patch to browser files
path: path.join(registryDirectory, dir),
// All files that we will try to inspect for missing dependencies.
filePaths: [],
// All libraries that are missing for the browser.
missingLibraries: new Set(),
// All packages required for the browser.
requiredPackages: new Set(),
// Libraries that we didn't find a package.
unresolvedLibraries: new Set(),
}));
// Collect all missing libraries for all browsers.
const allMissingLibraries = new Set();
for (const descriptor of browserDescriptors) {
// Browser vendor, can be `webkit`, `firefox` or `chromium`
const vendor = descriptor.name.split('-')[0];
for (const library of (DL_OPEN_LIBRARIES[vendor] || [])) {
descriptor.missingLibraries.add(library);
allMissingLibraries.add(library);
}
const {stdout} = await runCommand('find', [descriptor.path, '-type', 'f']);
descriptor.filePaths = stdout.trim().split('\n').map(f => f.trim()).filter(filePath => !filePath.toLowerCase().endsWith('.sh'));
await Promise.all(descriptor.filePaths.map(async filePath => {
const missingLibraries = await missingFileDependencies(filePath);
for (const library of missingLibraries) {
descriptor.missingLibraries.add(library);
allMissingLibraries.add(library);
}
}));
}
const libraryToPackage = new Map();
const ambiguityLibraries = new Map();
// Map missing libraries to packages that could be installed to fulfill the dependency.
console.log(`Finding packages for ${allMissingLibraries.size} missing libraries...`);
for (let i = 0, array = [...allMissingLibraries].sort(); i < allMissingLibraries.size; ++i) {
const library = array[i];
const packages = await findPackages(library);
const progress = `${i + 1}/${allMissingLibraries.size}`;
console.log(`${progress.padStart(7)}: ${library} => ${JSON.stringify(packages)}`);
if (!packages.length) {
const browsersWithMissingLibrary = browserDescriptors.filter(d => d.missingLibraries.has(library)).map(d => d.name).join(', ');
const PADDING = ''.padStart(7) + ' ';
console.log(PADDING + `ERROR: failed to resolve '${library}' required by ${browsersWithMissingLibrary}`);
} else if (packages.length === 1) {
libraryToPackage.set(library, packages[0]);
} else {
ambiguityLibraries.set(library, packages);
}
}
console.log('');
console.log(`Picking packages for ${ambiguityLibraries.size} libraries that have multiple package candidates`);
// Pick packages to install to fulfill missing libraries.
//
// This is a 2-step process:
// 1. Pick easy libraries by filtering out debug, test and dev packages.
// 2. After that, pick packages that we already picked before.
// Step 1: pick libraries that are easy to pick.
const totalAmbiguityLibraries = ambiguityLibraries.size;
for (const [library, packages] of ambiguityLibraries) {
const package = pickPackage(library, packages);
if (!package)
continue;
libraryToPackage.set(library, package);
ambiguityLibraries.delete(library);
const progress = `${totalAmbiguityLibraries - ambiguityLibraries.size}/${totalAmbiguityLibraries}`;
console.log(`${progress.padStart(7)}: ${library} => ${package}`);
console.log(''.padStart(9) + `(note) packages are ${JSON.stringify(packages)}`);
}
// 2nd pass - prefer packages that we already picked.
const allUsedPackages = new Set(libraryToPackage.values());
for (const [library, packages] of ambiguityLibraries) {
const package = packages.find(package => allUsedPackages.has(package));
if (!package)
continue;
libraryToPackage.set(library, package);
ambiguityLibraries.delete(library);
const progress = `${totalAmbiguityLibraries - ambiguityLibraries.size}/${totalAmbiguityLibraries}`;
console.log(`${progress.padStart(7)}: ${library} => ${package}`);
console.log(''.padStart(9) + `(note) packages are ${JSON.stringify(packages)}`);
}
// 3rd pass - prompt user to resolve ambiguity.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const promptAsync = (question) => new Promise(resolve => rl.question(question, resolve));
// Report all ambiguities that were failed to resolve.
for (const [library, packages] of ambiguityLibraries) {
const question = [
`Pick a package for '${library}':`,
...packages.map((package, index) => ` (${index + 1}) ${package}`),
'Enter number: ',
].join('\n');
const answer = await promptAsync(question);
const index = parseInt(answer, 10) - 1;
if (isNaN(index) || (index < 0) || (index >= packages.length)) {
console.error(`ERROR: unknown index "${answer}". Must be a number between 1 and ${packages.length}`);
process.exit(1);
}
const package = packages[index];
ambiguityLibraries.delete(library);
libraryToPackage.set(library, package);
console.log(answer);
console.log(`- ${library} => ${package}`);
}
rl.close();
// For each browser build a list of packages to install.
for (const descriptor of browserDescriptors) {
for (const library of descriptor.missingLibraries) {
const package = libraryToPackage.get(library);
if (package)
descriptor.requiredPackages.add(package);
else
descriptor.unresolvedLibraries.add(library);
}
}
// Formatting results.
console.log('');
console.log(`----- Library to package name mapping -----`);
console.log('{');
const sortedEntries = [...libraryToPackage.entries()].sort((a, b) => a[0].localeCompare(b[0]));
for (const [library, package] of sortedEntries)
console.log(` "${library}": "${package}",`);
console.log('}');
// Packages and unresolved libraries for every browser
for (const descriptor of browserDescriptors) {
console.log('');
console.log(`======= ${descriptor.name}: required packages =======`);
const requiredPackages = [...descriptor.requiredPackages].sort();
console.log(JSON.stringify(requiredPackages, null, 2));
console.log('');
console.log(`------- ${descriptor.name}: unresolved libraries -------`);
const unresolvedLibraries = [...descriptor.unresolvedLibraries].sort();
console.log(JSON.stringify(unresolvedLibraries, null, 2));
}
const status = browserDescriptors.some(d => d.unresolvedLibraries.size) ? 'FAILED' : 'SUCCESS';
console.log(`
====================
${status}
====================
`);
})();
function pickPackage(library, packages) {
// Step 1: try to filter out debug, test and dev packages.
packages = packages.filter(package => !package.endsWith('-dbg') && !package.endsWith('-test') && !package.endsWith('-dev') && !package.endsWith('-mesa'));
if (packages.length === 1)
return packages[0];
// Step 2: use library name to filter packages with the same name.
const prefix = library.split(/[-.]/).shift().toLowerCase();
packages = packages.filter(package => package.toLowerCase().startsWith(prefix));
if (packages.length === 1)
return packages[0];
return null;
}
async function findPackages(libraryName) {
const {stdout} = await runCommand('apt-file', ['search', libraryName]);
if (!stdout.trim())
return [];
const libs = stdout.trim().split('\n').map(line => line.split(':')[0]);
return [...new Set(libs)];
}
async function fileDependencies(filePath) {
const {stdout, code} = await lddAsync(filePath);
if (code !== 0)
return [];
const deps = stdout.split('\n').map(line => {
line = line.trim();
const missing = line.includes('not found');
const name = line.split('=>')[0].trim();
return {name, missing};
});
return deps;
}
async function missingFileDependencies(filePath) {
const deps = await fileDependencies(filePath);
return deps.filter(dep => dep.missing).map(dep => dep.name);
}
async function lddAsync(filePath) {
let LD_LIBRARY_PATH = [];
// Some shared objects inside browser sub-folders link against libraries that
// ship with the browser. We consider these to be included, so we want to account
// for them in the LD_LIBRARY_PATH.
for (let dirname = path.dirname(filePath); dirname !== '/'; dirname = path.dirname(dirname))
LD_LIBRARY_PATH.push(dirname);
return await runCommand('ldd', [filePath], {
cwd: path.dirname(filePath),
env: {
...process.env,
LD_LIBRARY_PATH: LD_LIBRARY_PATH.join(':'),
},
});
}
function runCommand(command, args, options = {}) {
const childProcess = spawn(command, args, options);
return new Promise((resolve) => {
let stdout = '';
let stderr = '';
childProcess.stdout.on('data', data => stdout += data);
childProcess.stderr.on('data', data => stderr += data);
childProcess.on('close', (code) => {
resolve({stdout, stderr, code});
});
});
}
async function getDistributionName() {
const osReleaseText = await readFileAsync('/etc/os-release', 'utf8');
const fields = new Map();
for (const line of osReleaseText.split('\n')) {
const tokens = line.split('=');
const name = tokens.shift();
let value = tokens.join('=').trim();
if (value.startsWith('"') && value.endsWith('"'))
value = value.substring(1, value.length - 1);
if (!name)
continue;
fields.set(name.toLowerCase(), value);
}
return fields.get('pretty_name') || '';
}
function currentTime() {
const date = new Date();
const dateTimeFormat = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' });
const [{ value: month },,{ value: day },,{ value: year }] = dateTimeFormat .formatToParts(date );
return `${month} ${day}, ${year}`;
}