Created
December 16, 2023 06:48
-
-
Save kyeno/55be07ee40683144e7489c846cf48deb to your computer and use it in GitHub Desktop.
Get Debian-styled "depends" string for a self-compiled binary. A helper tool for `.deb` package builders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Get Debian-styled "depends" string for a self-compiled binary. | |
# A helper tool for `.deb` package builders. | |
# | |
# REQUIRES: | |
# - `apt-file` Debian package with recent database | |
# | |
# (c) 2023 Ratan M. Kyeno | |
# This code is licensed under MIT license, see https://opensource.org/license/mit/ | |
import sys | |
import subprocess | |
# Get arguments slice past 1 | |
args = sys.argv[1:] | |
if not args: | |
print('Usage: ' + sys.argv[0] + ' /path/to/binary') | |
sys.exit(1) | |
# Safe execution wrapper | |
def exec_or_die(cmd): | |
result = subprocess.run([cmd], shell=True, capture_output=True, text=True) | |
if result.returncode > 0 or not result.stdout or result.stdout == 'dynamic\n': | |
print(f'Error executing: {cmd}') | |
sys.exit(1) | |
return result.stdout.strip() | |
# Iterate over raw dependencies and list packages | |
libs = exec_or_die("ldd " + ' '.join(args) + "$1|awk '{print $3}'|sed '/^[[:space:]]*$/d'|uniq -u|sort").split('\n') | |
packages = [] | |
for lib in libs: | |
print(lib, '... ', end='') | |
# Support self-compiled libraries | |
# ** L8R: DRY it up with exec_or_die() one day ;) | |
cmd = f'apt-file search {lib}|head -1|cut -d " " -f 1|sed s/":"//g' | |
result = subprocess.run([cmd], shell=True, capture_output=True, text=True) | |
if result.returncode > 0: | |
print(f'Error executing: {cmd}') | |
sys.exit(1) | |
elif not result.stdout: | |
print('SELF COMPILED!') | |
else: | |
package = result.stdout.strip() | |
print(package) | |
packages.append(package) | |
# Uniquify packages | |
packages = list(dict.fromkeys(packages)) | |
if not packages: | |
print('There are no known dependencies to: ' + ''.join(args)) | |
sys.exit(0) | |
# Get package versions | |
depstring = '' | |
for index, package in enumerate(packages): | |
# Test for currently installed version | |
version = exec_or_die("dpkg-query -f '${Version}' -W '" + package + "'|cut -d '-' -f 1") | |
# Append to dependency string | |
depstring += package + ' (>=' + version + ')' | |
if(index < len(packages) - 1): | |
depstring += ', ' | |
# Spit it out | |
print(f'Depends: {depstring}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment