Howto remove empty dirs in post-deactivate?

Ryan Schmidt ryandesign at
Wed Apr 16 14:04:26 PDT 2014

On Apr 16, 2014, at 15:52, Peter Danecek wrote:

> On 16 Apr 2014, at 22:41, Ryan Schmidt wrote:
>> On Apr 16, 2014, at 15:34, Peter Danecek wrote:
>>> I am looking for an elegant way to remove some directory (directories) if they are empty in a Portfile.
>>> I would like to use this in the post-deactivate hook. There I remove some unregistered files, after related registered files are deactivated. However, afterwords I'd like to check if the dir now is empty and conditionally remove it. 
>>> I am not very fluent in TCL, so any tip how to do this in an elegant way is welcome.
>> MacPorts automatically removes empty directories from the destroot before installation. This is easy for it to do. It might be nice if there were a way for it to do the same post-
> The directories were registered, but are unregistered in the deactivate step, even if they are not removed because not (yet) empty, right?
> It would be most consistent to postpone this unregister step only after retrying to remove them after post-deactivate. Unfortunately, I do not know base too well. Would this be difficult to implement?

Sorry, I was writing on my phone and managed to hit send before I was done.

I was just musing that it’s easy for base to remove empty directories from the destroot directory before it creates the package tarball. But this isn’t so easy for it to do in the activate or deactivate phases when we no longer have a destroot.

Also, MacPorts only registers files to active ports; it doesn’t register directories. 

My point was going to be that it would be nice if base handled this somehow for you; I wouldn’t want you to have to write too much code specific to this in every portfile where you wanted this behavior. 

On the other hand, this is apparently not a common need, or else it would have come up before now. And it may be as simple as adding this line after you’ve removed the file(s):

file delete ${prefix}/path/to/directory

Note that “file delete” will not delete non-empty directories, unless you use the “-force” flag.

This is in contrast to the MacPorts function “delete” which is an alias for “file delete -force” and would remove non-empty directories.

If “file delete” throws an error if the directory is non-empty, then you may have to wrap it in a catch.

More information about the macports-dev mailing list