Reputation: 170498
The usual rule for apps that support console coloring is to activate it when the current console is a TTY.
This method breaks on continuous integration servers (like Jenkins, Travis,...) because they do not open a TTY for their pseudo-console.
I am wondering if it is possible to distinguish between these two cases:
mycommand
run under Jenkins - we want to enable coloringmycommand >> output.log
-- we don't want to end up with ANSI escapes in the log file.My old ANSI detection code:
import sys
import os
if (hasattr(sys.stderr, "isatty") and sys.stderr.isatty()) or \
('TERM' in os.environ.keys() and os.environ['TERM'] in ['linux']) or \
('PYCHARM_HOSTED' in os.environ.keys()):
coloring = True
Can I improve this in order to address this issue?
Upvotes: 4
Views: 277
Reputation: 49804
Since you are already using the Environment Variable PYCHARM_HOSTED
, I suggest you use something similar for you CI environment. You mentioned Jenkins and Travis specifically. Both of these systems set environment variables that can be used. Some possibilities are...
Jenkins Environment Variables:
JENKINS_URL Set to the URL of the Jenkins master that's running the build.
BUILD_URL The URL where the results of this build can be found.
BUILD_TAG String of jenkins-${JOB_NAME}-${BUILD_NUMBER}.
(Source)
Travis Environment Variables:
CI=true
TRAVIS=true
CONTINUOUS_INTEGRATION=true
(Source)
Upvotes: 2