Created
February 8, 2017 06:22
-
-
Save djnugent/4b46595dd0c0a643572dbb95af631b9f to your computer and use it in GitHub Desktop.
Outline for a bash scripting module in python
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
#todo | |
# Implement | |
# how to handle sudo | |
# how to handle readline/buffer with errors | |
class process(): | |
#cmd - bash cmd + args | |
#bg - run in bacground | |
#output_stream - an array of io stream destination for stdout and stderr. If only one output stream is provided both will be directed to that stream | |
#verbose - how much of the output to print. 0 = silent, 1 = stdout&stderr, 2=stdout, 3=stderr | |
#buffer_size - how much of the output to hold onto | |
#callbacks - list of progress callbacks while process is executing | |
def __init__(cmd,bg=False,output_stream = [], verbose=0, buffer_size=500, callbacks = []): | |
pass | |
#runs a process | |
def run(): | |
pass | |
# What was the exit code when the process exitted | |
def exit_code(self): | |
return self.exit_code | |
# Running/sleeping/stopped | |
def status(self): | |
return self.status | |
# read the next line from the output buffer | |
def read_line(self, ignore_errors=False): | |
return self.out.next_line | |
#read the entire buffer | |
def read_buffer(self): | |
return | |
def terminate(self): | |
pass | |
# when a new line appears in buffer | |
class NewLineCallback(): | |
pass | |
# when a newline contains a certain string(maybe support regrex) | |
class ContainsCallback(): | |
pass | |
# when a process finishes | |
class FinishCallback(): | |
pass | |
#Examples | |
#blocking process that prints | |
p = process("apt-get install gdm", verbose=1) | |
p.run() | |
#background server that only prints errors | |
p = process("./server", bg=True, verbose=3) | |
p.run() | |
while(p.status()=="running"): | |
#ping server | |
#Background process that prints stdout and stderr while saving both to a file, and call a class | |
f1 = open("out.txt") | |
def endtask(): | |
print("im done") | |
cb = [FinishCallback(endtask)] | |
p = process("./server", bg=True, out_stream=[f1], verbose=1, callback = cb) | |
#blocking process that logs stderr | |
f2 = open("error.txt") | |
p = process("service stop docker", output_stream=[None,f2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment