maroph

system.py

Nov 8th, 2020 (edited)
1,787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import subprocess
  4.  
  5.  
  6. def system(argv):
  7.     if not argv:
  8.         # print("argv is None")
  9.         return 1
  10.     if isinstance(argv, str):
  11.         # print("convert string to list:", argv)
  12.         argv = argv.split()
  13.     if not isinstance(argv, list):
  14.         # print("argv is not a list")
  15.         return 1
  16.     if len(argv) == 0:
  17.         # print("len(argv) == 0")
  18.         return 1
  19.     if argv[0] == "":
  20.         # print("argv[0] == \"\"")
  21.         return 1
  22.  
  23.     # print("run command >>>>>", argv, "<<<<<")
  24.     # print("----------")
  25.     try:
  26.         cp = subprocess.run(argv)
  27.     except FileNotFoundError as e:
  28.         print("system:", e)
  29.         return 2
  30.     # print("----------")
  31.  
  32.     return_code = 0
  33.     try:
  34.         cp.check_returncode()
  35.     except subprocess.CalledProcessError as e:
  36.         return_code = e.returncode
  37.     # print("return_code:", return_code)
  38.  
  39.     if return_code < 0 or return_code > 255:
  40.         return_code = 1
  41.     return return_code
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     if len(sys.argv) < 2:
  46.         print('command missing')
  47.         sys.exit(1)
  48.  
  49.     if len(sys.argv) == 2:
  50.         exitcode = system(sys.argv[1])
  51.         sys.exit(exitcode)
  52.  
  53.     exitcode = system(sys.argv[1:])
  54.     sys.exit(exitcode)
  55.  
Advertisement
Add Comment
Please, Sign In to add comment