https://pantsbuild.org/ logo
#general
Title
# general
f

flat-queen-95161

06/27/2022, 9:57 PM
Is it possible for pants to list all python modules that are not dependencies of any package targets?
h

happy-kitchen-89482

06/27/2022, 10:11 PM
Not straightforwardly, as far as I can think of, but I think it should be possible to get this info with some scripting on the output of
./pants filter
and
./pants dependees
, let me try something
./pants filter --target-type=pex_binary :: | xargs ./pants dependencies --transitive
will give you all the targets that all pex_binary targets transitively depend on, and
./pants list ::
will give you all the targets in the repo, so then you'd just need to subtract the former from the latter
f

flat-queen-95161

06/27/2022, 10:16 PM
Thanks. I will have a play.
h

happy-kitchen-89482

06/27/2022, 10:16 PM
Or something like that...
f

flat-queen-95161

06/27/2022, 10:17 PM
It will come in useful while migrating dozens of old repo’s to the new mono-repo.
08:29 $ cat dead.sh
all=$(./pants list ::|grep -v tests | grep py$)
used=$(./pants filter --target-type=pex_binary :: | xargs ./pants dependencies --transitive|grep py$)
diff  <(echo "$all" ) <(echo "$used") | grep "<"
h

happy-kitchen-89482

06/27/2022, 11:28 PM
Nice one!
f

fresh-cat-90827

07/01/2022, 2:35 PM
perhaps related and useful: 1. Get a list of all Python modules in the repository:
Copy code
$ find $(./pants roots --roots-sep=' ') -name \*.py
2. Get a list of all files that Pants is aware of (
BUILD
files, Python modules, test data and resources). The
filedeps
goal lists all source and
BUILD
files a target depends on.
Copy code
$ ./pants filedeps ::
3. Compare these two lists to find files that are in the repository, but are not listed as file dependencies of Pants projects. All Python modules from the first command’s output should be in the output of the second command. That’s what I do at my repo
classical example — a developer left a file
tes_database.py
— it’s not going to be picked up by
pytest
not particularly readable, but works for the 3) part:
Copy code
# find files that are in the repository, but are not listed as file dependencies of Pants projects
awk 'NR==FNR{a[$0]=1;next}!a[$0]' pants_files.txt repo_files.txt > orphan_python_modules.txt
2 Views