Skip to content

Instantly share code, notes, and snippets.

@abhi1010
Created November 13, 2019 02:45
Show Gist options
  • Save abhi1010/811158aa77b6f98212c60595485cf64d to your computer and use it in GitHub Desktop.
Save abhi1010/811158aa77b6f98212c60595485cf64d to your computer and use it in GitHub Desktop.
run bash commands on python
import logging
import sys
import argparse
import subprocess
import os
import logging.handlers
from typing import List, Union, Dict, Tuple, Sequence
import utils
from datetime import datetime, timedelta
_GREP_TOOL = 'egrep'
DEFAULT_TIMEOUT = 300 # in seconds
logger = logging.getLogger()
def setup_logging():
"""
Set up handlers, formatters and loggers
"""
root = logging.getLogger()
root.level = logging.INFO
screen_format = (
"%(asctime)s - %(levelname)s, %(funcName)s,%(lineno)d:: %(message)s"
)
# Info logs to screen and to summary file
screen_fmt = logging.Formatter(screen_format)
stream_handler = logging.StreamHandler()
stream_handler.formatter = screen_fmt
stream_handler.level = logging.DEBUG
root.addHandler(stream_handler)
def get_cmd_results(cmd: Sequence[str]) -> str:
"""
Runs a command, provides the results as Pipes
"""
return run_cmd(cmd).stdout.decode("utf-8").strip()
def run_str_cmd(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
res = p.communicate()
return res[0].decode('utf-8').split('\n')
def run_cmd(
cmd: Sequence[str], timeout: int = DEFAULT_TIMEOUT
) -> subprocess.CompletedProcess:
""""
Run command in subprocess and return job output
:param cmd: The command to run
:param timeout: Command timeout, in seconds
"""
logger.debug("Running %s", cmd)
try:
job = subprocess.run(
args=cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=timeout,
)
except subprocess.CalledProcessError:
logger.exception("Hit error running cmd:")
raise
except subprocess.TimeoutExpired:
logger.exception("Timed out waiting for cmd:")
raise
logger.debug(
"RC: %s, output: %s", job.returncode, job.stdout.decode("utf-8")
)
return job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment