Dear #python and #unix hackers,
I’m trying to find out, in Python, if "grep" has the option "--color=auto" or not.
(to make #offpunk compatible with #openbsd grep )
Any suggestion? (the one on the mailing-list doesn’t work for me)
I could simply scan "grep --help" for the option but, intuitively, I don’t like it.
https://lists.sr.ht/~lioploum/offpunk-devel/%3CD5XQC1ECYEID.3KHOCET4CTSM0@dsilva.email%3E
@ploum run the command with the option and check whether the exit code is not 0?
@ploum the first line of grep -V reads "grep (GNU grep) 3.11" for me (on Termux) so probably you could just parse the first line of the version and make your own inferences?
@fdavies93 @ploum that's not going to work if the grep supports color auto, but isn't gnu's
@SRAZKVT @ploum presumably, you just bake some logic about that into your application, which would let you be more intelligent than just running with the flag and checking the exit code, but could require annoying version checking logic (there might be a versioned index of capabilities of UNIX utilities or something out there tho, idk)
@ploum i would either look through --help, or leave it to package maintainers to set some value at build time that disables color, though im not sure how to do that in python, that's more if a c thing
@ploum I have another idea, but I think it's much worse: uncompress /usr/share/man/man1/grep.1.gz and kind of search / parse the relevant bit.
Advantage: does not start the grep process.
Inconvenients: probably lots, at least it looks like more work. And the man page location looks the same in Linux and FreeBSD, but I have no idea if that can be generalized.
@ploum maybe you can set grep options based on sys.platform? https://docs.python.org/3/library/sys.html#sys.platform
@ploum
Utiliser une implémentation en python comme « greplica » ?
b'color' in open('/bin/grep','rb').read() ?
@tnt : that’s quite error prone (need to check the path).
I managed to do it by checking for "x" in the binary stream b'x' with the color option. If it fails, fallback to grep without color. (the challenge was to ensure that the grep test would succeed)
https://git.sr.ht/~lioploum/offpunk/commit/6d897bf891d80948f0fae9c37e7f3cb50e4fb7fa