Created
September 11, 2023 18:46
-
-
Save asukaminato0721/e9ea3ec0832d618f12d3e5565dac58ec to your computer and use it in GitHub Desktop.
A simple script to do cargo tree -i, use that instead. I wrote this since I didn't find that.
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
import sys | |
try: | |
import tomllib | |
except ImportError as e: | |
print(e, "require python 3.11, exit", file=sys.stderr) | |
exit() | |
from pathlib import Path | |
from typing import Any, Dict, List | |
if len(sys.argv) == 1: | |
print( | |
f""" | |
usage: | |
python {Path(__file__).name} your_proj target_deps | |
""", | |
file=sys.stderr, | |
) | |
exit() | |
dic: Dict[str, List[str]] = {} | |
project = sys.argv[1] | |
to_be_find = sys.argv[2] | |
roots: List[str] = tomllib.load(Path(project, "Cargo.toml").open("rb"))[ | |
"dependencies" | |
].keys() | |
package: List[Dict[str, Any]] = tomllib.load(Path(project, "Cargo.lock").open("rb"))[ | |
"package" | |
] | |
for f in package: | |
dic[f["name"]] = f.get("dependencies", []) | |
def dfs(r: str, arr: List[str]): | |
if r.startswith(to_be_find): | |
print(*[*arr, r], sep=" -> ") | |
return | |
if " " in r: # something like "aa 1.0" | |
rr = r.split()[0] | |
else: | |
rr = r | |
for i in dic[rr]: | |
dfs(i, [*arr, r]) | |
[dfs(x, []) for x in roots] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment