Skip to content

Instantly share code, notes, and snippets.

@mkg20001
Last active May 2, 2023 23:18
Show Gist options
  • Save mkg20001/b34be073e69253d18a72f88aedc5a01a to your computer and use it in GitHub Desktop.
Save mkg20001/b34be073e69253d18a72f88aedc5a01a to your computer and use it in GitHub Desktop.
OpenWRT package dependency debugging script
#!/usr/bin/env node
'use strict'
/*
Usage:
- first dump package dependencies with "opkg status > /tmp/status" and copy the file "scp -O root@your-router:/tmp/status ./dependencies"
- then use the script:
$ node opkg-deps.js ./dependencies why luci
$ node opkg-deps.js ./dependencies search iptables
$ node opkg-deps.js ./dependencies tree luci # same as why, but lists deps with a tree
*/
const fs = require('fs')
const data = String(fs.readFileSync(process.argv[2]))
let list = []
let cur = {}
let curkey = null
data.split('\n').forEach(l => {
if (!l.trim()) {
list.push(cur)
cur = {}
curkey = null
} else {
if (l.indexOf(':') !== -1) {
let [key, val] = l.split(':')
key = key.toLowerCase()
if (!val) {
cur[key] = []
curkey = key
} else {
cur[key] = val.trim()
}
}
}
})
list = list.filter(p => Boolean(p.package))
list.forEach(i => {
i.depends = i.depends ? i.depends.split(', ').map(v => v.trim()) : []
})
list.forEach(i => {
i.dependents = list.filter(i2 => i2.depends.indexOf(i.package) !== -1)
})
function showTree(pkg, indent = 0) {
const s = ' '.repeat(indent)
console.log('%s+ %s', s, pkg.package)
pkg.dependents.forEach(d => {
displayWhy(d, indent + 1)
})
}
function displayWhy(pkg, s = []) {
s = s.concat([pkg.package])
if (!pkg.dependents.length) console.log(s.reverse().join(' > '))
pkg.dependents.forEach(dep => {
displayWhy(dep, s)
})
}
const arg = process.argv[3]
switch(arg) {
case 'why': {
const pkgName = process.argv[4]
const pkg = list.filter(i => i.package === pkgName)[0]
if (!pkg) return console.error('%s not found - use search perhaps', pkgName)
displayWhy(pkg)
break
}
case 'tree': {
const pkgName = process.argv[4]
const pkg = list.filter(i => i.package === pkgName)[0]
if (!pkg) return console.error('%s not found - use search perhaps', pkgName)
showTree(pkg)
break
}
case 'search':
const str = process.argv[4]
console.log(list.filter(i => i.package.indexOf(str) !== -1).map(i => i.package).join(', '))
break
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment