For thousands of files, I don't think you need to optimize; you might consider it for hundreds of thousands. I think the approach you outline is going to be best. You'll probably want to print exceptions to handle manually (or improve your tool) if you can't find "package" or it has a leading path component that doesn't match. I think you want
. Watch out for apostrophes :)