https://github.com/lunixbochs/meta/tree/master/utils/brew_al...
I have been doing this for years. The only thing it breaks for me is casks that try to install applications, and I don't mind.
MacPorts has excellent housekeeping, which is controlled through port command arguments. One can choose to leave everything from the entire build, or have everything cleaned up as it builds, or clean it up after the build. Showing and eliminating leaves is also pretty simple.
Uninstalling MacPorts with these 3 commands leaves absolutely nothing behind:
>$ sudo port -dfp uninstall --follow-dependencies installed
>$ sudo port -dfp uninstall all
>$ sudo rm -rf /opt/local /Library/Tcl/macports*
Good luck completely uninstalling Homebrew without having to look everywhere to make sure it is all gone. It requires downloading and trusting the Homebrew uninstall script.
When I first got a Mac about 4 years ago moving from Linux, the first thing I did was compare homebrew and macports.
homebrew was such a cluster-fuck in how it screwed around in /usr/local and its requirements for root etc, especially with Apple's move to basically own most of /usr under SPI.
Macports follows the BSD ports model and since Apple is a BSD based unix underneath, it makes sense. I haven't found anything I've needed that hasn't been ported.
It installs by default in /opt, which is where it should. When needed it will create the appropriate startup configurations for services as part of launchd and in the correct locations.
Capable of installing both source and binaries. Works well in my experience.
(No experience with MacPorts so I can't compare; but coming from Linux/apt I didn't feel much was missing, just a little different)
I haven't been involved with macports recently enough to compare, but one advantage of Homebrew's popularity is the depth of package specifications and how quickly they're updated