Advertisement
funcelot

utils.py

Feb 6th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.47 KB | None | 0 0
  1. class Command:
  2.     def __init__(self):
  3.         self.load_params()
  4.         self.script_path = os.path.dirname(os.path.abspath(__file__))
  5.         self.current_path=os.path.abspath(os.curdir)
  6.  
  7.  
  8.     def sh(self, args, print_output=True, shell=True, cwd=None):
  9.         try:
  10.             try:
  11.                 print(args)
  12.                 if cwd != None:
  13.                     os.chdir(cwd)
  14.                 else:
  15.                     if self.current_path == os.getcwd():
  16.                         os.chdir(os.path.abspath(os.path.curdir))
  17.                     else:
  18.                         os.chdir(os.path.abspath(self.current_path))
  19.                 p = subprocess.Popen(args=args.split(), encoding='utf-8', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, cwd=cwd)
  20.                 out, err = p.communicate()
  21.                 p.stdout.close()
  22.                 if out:
  23.                     print(out)
  24.                 if err:
  25.                     print(err)
  26.                 return out
  27.             except Exception as e:
  28.                 print(e)
  29.                 return subprocess.check_output(args, shell=shell).decode('utf-8')
  30.         except OSError as ex:
  31.             print(ex)
  32.             sys.exit(32)
  33.    
  34.  
  35.     def sh_inline(self, args, print_output=True, shell=True):
  36.         try:
  37.             return self.sh(args, print_output=print_output, shell=shell).replace('\n','')
  38.         except OSError as ex:
  39.             print(ex)
  40.             sys.exit(32)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement