Is it possible for pants to list all python module...
# general
f
Is it possible for pants to list all python modules that are not dependencies of any package targets?
h
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
Thanks. I will have a play.
h
Or something like that...
f
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
Nice one!
f
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