Following up on the hack of Matthieu - A one-liner to catch'em all! - and the recent features of Debsources I got the idea to modify a bit the one liner in order to retrieve the license of foo.bar.
The script will calculate the SHA256 hash of the file and then query the Debsources API in order to retrieve the license of that particular file.
Save the following in a file as license-of and add it in your $PATH
#!/bin/bash
function license-of {
readlink -f $1 | xargs dpkg-query --search | awk -F ": " '{print $1}' | xargs apt-cache showsrc | grep-dctrl -s 'Package' -n '' | awk -v sha="$(sha256sum $1 | awk '{ print $1 }')" -F " " '{print "https://sources.debian.net/copyright/api/sha256/?checksum="sha"&packagename="$1""}' | xargs curl -sS
}
CMD="$1"
license-of ${CMD}
Then you can try something like:
license-of /usr/lib/python2.7/dist-packages/pip/exceptions.py