Created
February 18, 2019 03:03
-
-
Save JavaCS3/dabd7e8316e29a600eb8afb18cb72aab to your computer and use it in GitHub Desktop.
Python color print and shell utils scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Style Guide: https://github.com/google/styleguide/blob/gh-pages/pyguide.md | |
# Pythonic: https://docs.python-guide.org/writing/style/ | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import os | |
import sys | |
import string | |
import subprocess | |
__dir__ = os.path.dirname(os.path.realpath(__file__)) | |
COLORS = { | |
'black': u'0;30', 'bright gray': u'0;37', | |
'blue': u'0;34', 'white': u'1;37', | |
'green': u'0;32', 'bright blue': u'1;34', | |
'cyan': u'0;36', 'bright green': u'1;32', | |
'red': u'0;31', 'bright cyan': u'1;36', | |
'purple': u'0;35', 'bright red': u'1;31', | |
'yellow': u'0;33', 'bright purple': u'1;35', | |
'dark gray': u'1;30', 'bright yellow': u'1;33', | |
'magenta': u'0;35', 'bright magenta': u'1;35', | |
'normal': u'0', 'bold': u'1', | |
} | |
def echo(*objects, **kwargs): | |
sep = kwargs.get('sep', ' ') | |
end = kwargs.get('end', '\n') | |
out = kwargs.get('file', sys.stdout) | |
msg = sep.join(map(str, objects)) + end | |
color = kwargs.get('color') | |
isatty = hasattr(out, 'isatty') and out.isatty() | |
if color and isatty: | |
out.write(u'\033[%sm%s\033[0m' % (COLORS[color], msg)) | |
else: | |
out.write(msg) | |
out.flush() | |
def bold(msg): | |
echo(msg, color='bold') | |
def ok(msg): | |
echo(msg, color='green') | |
def warn(msg): | |
echo(msg, color='yellow') | |
def error(msg): | |
echo(msg, color='red') | |
def banner(msg, width=30, color='blue'): | |
echo('%s\n%s\n%s' % ('=' * width, msg, '=' * width), color=color) | |
def sh(script, debug=False, check_output=False, **kwargs): | |
"""Run shell script and wait for it complete | |
Example: | |
sh('echo hello world && ls') | |
""" | |
header = 'set -e;' | |
if debug: | |
header += 'set -x;' | |
func = subprocess.check_output if check_output else subprocess.check_call | |
return func(header + script, shell=True, **kwargs) | |
def sh_out(script, **kwargs): | |
return sh(script, check_output=True, **kwargs).rstrip() | |
def subst(tpl, **env): | |
return string.Template(tpl).safe_substitute(**env) | |
def cmd(cmds, debug=False, check_output=False, **kwargs): | |
"""Execute a command and wait for it complete | |
Example: | |
cmd(['ls', '-a', '-l']) | |
""" | |
if debug: | |
echo('cmd: %s' % cmds) | |
func = subprocess.check_output if check_output else subprocess.check_call | |
return func(cmds, **kwargs) | |
def cmd_out(cmds, **kwargs): | |
return cmd(cmds, check_output=True, **kwargs).rstrip() | |
def check_in_docker(): | |
try: | |
with open('/proc/self/cgroup', 'r') as fp: | |
if 'docker' not in fp.read(): | |
raise IOError('No docker in /proc/self/cgroup') | |
except IOError: | |
warn('WARNING: You are NOT running in docker!!!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment