def test_stdin_processing(): """ You’re also not limited to using just strings. You may use a file object, a Queue, or any iterable (list, set, dictionary, etc): :return: """ from sh import cat, tr # print "test" print cat(_in='test') print tr('[:lower:]', '[:upper:]', _in='sh is awesome')
def test_huge_piped_data(self): from sh import tr stdin = tempfile.NamedTemporaryFile() data = "herpderp" * 4000 + "\n" stdin.write(data.encode()) stdin.flush() stdin.seek(0) out = tr(tr("[:lower:]", "[:upper:]", _in=data), "[:upper:]", "[:lower:]") self.assertTrue(out == data)
def test_manual_stdin_iterable(self): from sh import tr test = ["testing\n", "herp\n", "derp\n"] out = tr("[:lower:]", "[:upper:]", _in=test) match = "".join([t.upper() for t in test]) self.assertEqual(out, match)
def get_last_tag(self): return repr( self.git.describe( '--tags', tr("-d", "'\n'", _in=self.git("rev-list", "--tags", "--max-count", 1)))).strip()
def evaluate_mem(_pid, logfile): """ Use the ps command for profiling memory for now. """ print "Start benchmarking the RSS memory usage, result will be in " + logfile with open(logfile, "w") as wf: while 1: info = sh.awk(sh.tr(sh.ps("aux"), "-s", "' '"), "-vpid={}".format(_pid), "{if ($2==pid) {print $6}}").strip() wf.write("{:<12s}: {:10s}\n".format(str(datetime.datetime.now()), info)) wf.flush() # FIXME: ugly solution here time.sleep(1)
def test_multiple_pipes(self): from sh import tr, python import time py = create_tmp_test( """ import sys import os import time for l in "andrew": print(l) time.sleep(.2) """ ) class Derp(object): def __init__(self): self.times = [] self.stdout = [] self.last_received = None def agg(self, line): self.stdout.append(line.strip()) now = time.time() if self.last_received: self.times.append(now - self.last_received) self.last_received = now derp = Derp() p = tr( tr(tr(python(py.name, _piped=True), "aw", "wa", _piped=True), "ne", "en", _piped=True), "dr", "rd", _out=derp.agg, ) p.wait() self.assertEqual("".join(derp.stdout), "werdna") self.assertTrue(all([t > 0.15 for t in derp.times]))
def test_multiple_pipes(self): from sh import tr, python import time py = create_tmp_test(""" import sys import os import time for l in "andrew": print(l) time.sleep(.2) """) class Derp(object): def __init__(self): self.times = [] self.stdout = [] self.last_received = None def agg(self, line): self.stdout.append(line.strip()) now = time.time() if self.last_received: self.times.append(now - self.last_received) self.last_received = now derp = Derp() p = tr(tr(tr(python(py.name, _piped=True), "aw", "wa", _piped=True), "ne", "en", _piped=True), "dr", "rd", _out=derp.agg) p.wait() self.assertEqual("".join(derp.stdout), "werdna") self.assertTrue(all([t > .15 for t in derp.times]))
def test_manual_stdin_file(self): from sh import tr import tempfile test_string = "testing\nherp\nderp\n" stdin = tempfile.NamedTemporaryFile() stdin.write(test_string.encode()) stdin.flush() stdin.seek(0) out = tr("[:lower:]", "[:upper:]", _in=stdin) self.assertEqual(out, test_string.upper())
def test_manual_stdin_queue(self): from sh import tr try: from Queue import Queue, Empty except ImportError: from queue import Queue, Empty test = ["testing\n", "herp\n", "derp\n"] q = Queue() for t in test: q.put(t) q.put(None) # EOF out = tr("[:lower:]", "[:upper:]", _in=q) match = "".join([t.upper() for t in test]) self.assertEqual(out, match)
def __init__(self, previous, new, workdir, flags=0): self.workdir = workdir self.previous = previous self.new = new self.flags = flags try: repo_list = sh.tr(sh.jq(sh.curl("-s", self.REPO_URL), "-M", ".[].name"),"-d", "\"").stdout.split("\n") except sh.ErrorReturnCode: sys.stderr.write("Failed retrieving OpenXT repository list from Github.") raise Error self.repos = [] for r in repo_list: try: self.repos.append(Repository(r,previous,new,workdir)) except: continue if not self.repos: sys.stderr.write("Unable to find any repo with both references, %s and %s." % (previous, new)) raise Error
print('-' * 50) from sh import ls, glob print(ls('-ld', glob('/etc/pr*'))) print('-' * 50) w = sh.who() print(w) print('-' * 50) disk_usage = sh.df('-h') print(disk_usage) print('-' * 50) from sh import uname print(uname()) print(uname('-a')) print(uname(a=True)) print('-' * 50) from sh import grep, wc # grep 'sh' /etc/passwd | wc -l print(grep('sh', '/etc/passwd')) print(wc(grep('sh', '/etc/passwd'), l=True)) print('-' * 50) from sh import tr fruits = 'apple banana mango orange'.split() print(tr("[:lower:]", "[:upper:]", _in=fruits))
#coding=utf8 from sh import tr, tail #Advanced piping for line in tr(tail("-f", "files.list", _piped=True), "[:lower:]", "[:upper:]", _iter=True): print(line)
def test_manual_stdin_string(self): from sh import tr out = tr("[:lower:]", "[:upper:]", _in="andrew").strip() self.assertEqual(out, "ANDREW")
cI=checkInternet() if theWord== "-clearhist": os.remove(savefile) # we also clear the open afile f.seek(0) f.truncate() print colored("History cleared ok","cyan") elif cI == True: if len(theWord) > 0: resgoog=doGoog(theWord) #we try to read and print the prev data using sh for line in sh.tr(sh.tail("-n 30", afile , _piped=True), "[:upper:]", "[:lower:]", _iter=True): print(line.strip('\n')) # print a line the length of the reply ll=len(resgoog)+len(theWord)+7 if ll > get_terminal_width(): ll = get_terminal_width() print colored("-"*ll,"green") rgs=resgoog.split(" ") # clean the word of prefixes if "-b" in theWord: theWord=theWord.replace("-b","") if "-v" in theWord: theWord=theWord.replace("-v","") if "-e" in theWord: theWord=theWord.replace("-e","")
#!/usr/bin/env python from sh import tr fruits = 'apple banana mango orange'.split() print(tr("[:lower:]", "[:upper:]", _in=fruits))
#coding=utf8 from sh import tr #Buffer sizes n = 0 for chunk in tr("[:lower:]", "[:upper:]", _in="testing", _iter=True): print(chunk) n = n + 1 print n n = 0 for chunk in tr("[:lower:]", "[:upper:]", _in="testing", _iter=True, _out_bufsize=0): print(chunk) n = n + 1 print n ''' There are 2 bufsize special keyword arguments: _in_bufsize and _out_bufsize. They may be set to the following values: 0 1 N '''
if theWord == "-clearhist": os.remove(savefile) # we also clear the open afile f.seek(0) f.truncate() print colored("History cleared ok", "cyan") elif cI == True: if len(theWord) > 0: resgoog = doGoog(theWord) #we try to read and print the prev data using sh for line in sh.tr(sh.tail("-n 30", afile, _piped=True), "[:upper:]", "[:lower:]", _iter=True): print(line.strip('\n')) # print a line the length of the reply ll = len(resgoog) + len(theWord) + 7 if ll > get_terminal_width(): ll = get_terminal_width() print colored("-" * ll, "green") rgs = resgoog.split(" ") # clean the word of prefixes if "-b" in theWord: theWord = theWord.replace("-b", "") if "-v" in theWord: theWord = theWord.replace("-v", "") if "-e" in theWord: theWord = theWord.replace("-e", "")