Erotemic / ubelt

A Python utility library with a stdlib like feel and extra batteries. Paths, Progress, Dicts, Downloads, Caching, Hashing: ubelt makes it easy!
Apache License 2.0
725 stars 44 forks source link

Provide a subprocess-compatible interface in cmd #138

Closed jaraco closed 1 year ago

jaraco commented 1 year ago

I was exploring using subprocess_tee when I found it doesn't support Windows and has issues with Python 3.12.

When I tried dropping in ubelt.cmd as a replacement, I found it has a very different interface than subprocess, returning a dict instead of an object and having different keys.

It would be nice if the result from command was an object similar to the one returned by subprocess.Popen (or even identical), so that code like this can readily switch between the implementations.

Erotemic commented 1 year ago

The reason for this return API is because ubelt.cmd can be invoked in several different ways that make it difficult to support a unified result object. For instance it can be used to invoke a command via os.system instead of the normal Popen interface, or with detach=True where it doesn't block and lets the process run in the background. My thought was that an information dictionary that directly contains relevant information would be the most natural result type given its variable nature.

However, in all but the system=True case the return dictionary contains a field "proc", which is the relevant Popen object. So while it's not a drop-in, it does have a simple replacement.

I'm open to the idea of improving the return type, but at the very least it must be backwards compatible with the current return type as it's used in quite a few places.

Erotemic commented 1 year ago

@jaraco I've come up with a backwards compatible solution in https://github.com/Erotemic/ubelt/pull/147/commits/ac35aaae5d724f968110724de049b96539aee204

If all goes well in testing, version 1.3.0 ubelt.cmd will return a CmdOutput object which will ducktype subprocess.CompletedProcess. It also inherits from a dictionary and maintains the same keys so it will be backwards compatible. Hope that will be useful to you.

Erotemic commented 1 year ago

@jaraco 1.3.0 is released and contains the updated interface. I think this can be marked as done.