Skip to content

Instantly share code, notes, and snippets.

@nvlled
Last active July 4, 2025 03:35
Show Gist options
  • Save nvlled/64a1c358181acc4d68647f3dfb29c2b2 to your computer and use it in GitHub Desktop.
Save nvlled/64a1c358181acc4d68647f3dfb29c2b2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env -S dotnet run
#:package [email protected]
#:package [email protected]
// This is a tool that recursively list the dependencies of an apt (debian) package.
// Requires at least dotnet v10.0.0.
using Shell.NET;
using System.Text.RegularExpressions;
using static System.Console;
using System.CommandLine;
var cmd = new RootCommand();
var nameArg = new Argument<string>(name: "package-name");
var depthOption = new Option<int>(name: "--depth", description: "maximum recursion depth");
depthOption.IsRequired = false;
depthOption.SetDefaultValue(1);
cmd.AddArgument(nameArg);
cmd.AddOption(depthOption);
nameArg.Arity = ArgumentArity.ExactlyOne;
cmd.SetHandler((name, depth) =>
{
var bash = new Bash();
var root = name;
var maxDepth = depth;
var packages = new List<string>();
var done = new HashSet<string>();
var queue = new Queue<QueueItem> { };
queue.Enqueue(new QueueItem(root, 0));
while (queue.TryDequeue(out QueueItem? entry))
{
if (done.Contains(entry.Name)) continue;
if (entry.Depth > maxDepth) continue;
packages.Add(entry.Name);
done.Add(entry.Name);
foreach (var line in bash.Command($"apt depends {entry.Name}").Lines)
{
var m = Regex.Match(line, @"Depends: ([^\s]+).*");
if (!m.Success)
{
continue;
}
var dep = m.Groups[1].Value;
queue.Enqueue(new QueueItem(dep, entry.Depth + 1));
}
}
foreach (var pkg in packages)
{
WriteLine($"# packages files for {pkg}:");
foreach (var line in bash.Command($"dpkg -L {pkg}").Lines)
{
if (line == "/.") continue;
var attrs = File.GetAttributes(line);
if (attrs.HasFlag(FileAttributes.Directory)) continue;
WriteLine(line);
}
}
}, nameArg, depthOption);
cmd.Invoke(Environment.GetCommandLineArgs()[1..]);
record QueueItem(string Name, int Depth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment