Created
February 28, 2016 03:37
-
-
Save kyhau/e640814fe4c9b8350549 to your computer and use it in GitHub Desktop.
Random fortune cow
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
""" | |
Fortune and cowsay | |
""" | |
import subprocess | |
def fortune_cow(logger): | |
""" | |
Run: fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf -n1) | |
""" | |
try: | |
logger.debug('Running fortune_cow ...') | |
ps = subprocess.Popen(('ls', '/usr/share/cowsay/cows/'), stdout=subprocess.PIPE) | |
ret1 = subprocess.check_output(('/usr/bin/shuf', '-n1'), stdin=ps.stdout).strip() | |
ps2 = subprocess.Popen(('/usr/games/fortune'), stdout=subprocess.PIPE) | |
ret2 = subprocess.check_output(('/usr/games/cowsay', '-f', ret1), stdin=ps2.stdout) | |
ret = '\n'.join([' {}'.format(line) for line in ret2.split('\n')]) | |
return ret | |
except Exception as e: | |
logger.error(e) | |
return None | |
if __name__ == '__main__': | |
# debug-only | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
logger = logging.getLogger(__name__) | |
fortune_cow(logger) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment