Last active
December 21, 2015 17:08
-
-
Save code6/6338395 to your computer and use it in GitHub Desktop.
python recipe
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
import os | |
import imp | |
WORKDIR = os.path.dirname(os.path.abspath(__file__)) | |
path = lambda *a: os.path.join(WORKDIR, *a) | |
importer = lambda m, path: imp.load_module(m, *imp.find_module(m, [path])) | |
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
def parseMetrics(data, conf): | |
if type(data) == type(conf): | |
if isinstance(conf, dict): | |
for k, v in conf.iteritems(): | |
if isinstance(v, tuple): | |
yield v, data[k] | |
else: | |
for y in parseMetrics(data[k], conf[k]): yield y | |
elif isinstance(conf, list): | |
for i in conf: | |
for y in parseMetrics(_getFromList(data, i), i): yield y | |
else: | |
#raise Exception("parseMetrics conf and data type error") | |
pass |
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
import sys, os | |
import subprocess | |
def safe_popen(cmd, stdin=None, stdout=None): | |
""" | |
Safe way to execute subprocess.Popen: | |
subprocess.Popen(extractcmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable="/bin/bash") | |
Args: | |
cmd: cmd to execute | |
stdin: standard input, don't pass subprocess.PIPE | |
stdout: standard output, don't pass subprocess.PIPE | |
Returns: | |
tuple(returncode, stderr) | |
""" | |
p = subprocess.Popen(cmd, shell = True, stdin=stdin, stdout=stdout, stderr=subprocess.PIPE, executable="/bin/bash") | |
err_list = [] | |
while True: | |
next_line = p.stderr.readline() | |
err_list.append(next_line) | |
if next_line == '' and p.poll() != None: | |
break | |
return p.returncode, ''.join(err_list) | |
def bad_popen(cmd, stdin=None, stdout=None): | |
p = subprocess.Popen(cmd, shell = True, stdin=stdin, stdout=stdout, stderr=subprocess.PIPE, executable="/bin/bash") | |
p.wait() | |
return p.returncode, p.stderr.read() | |
if __name__ == "__main__": | |
#http://www.commandlinefu.com/commands/view/7949/generate-random-password-works-on-mac-os-x | |
returncode, err = safe_popen("ls -al a") | |
print "returncode = ", returncode, " err = ", err | |
returncode, err = safe_popen("openssl rand -base64 70000 >&2") | |
print "returncode = ", returncode, " len(err) = ", len(err) | |
returncode, err = bad_popen("ls -al a") | |
print "returncode = ", returncode, " err = ", err | |
returncode, err = bad_popen("openssl rand -base64 70000 >&2") | |
print "returncode = ", returncode, " len(err) = ", len(err) |
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
#coding=utf8 | |
from subprocess import Popen | |
popen = Popen(["ping", "google.com"]) | |
from time import sleep | |
sleep(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment