Guest User

Untitled

a guest
Jun 4th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import sys
  4. import getopt
  5. import os
  6. from pathlib import Path
  7. import subprocess
  8.  
  9. ARCHIVE_TYPES = [
  10.     ".zip",
  11.     ".rar",
  12.     ".7z"
  13. ]
  14. MAX_OUTPUT_LEN = 150
  15.  
  16. input_path = ""
  17. do_delete = False
  18.  
  19. def usage(name):
  20.     print('''{script} [options]
  21.     -i <path>  : specify input dir
  22.     -d         : do delete
  23.     -h         : print help and quit'''\
  24.     .format(script=name))
  25.  
  26. def opts(argv):
  27.     global input_path, do_delete
  28.     try:
  29.         opts, args = getopt.getopt(argv[1:], 'i:dh')
  30.     except getopt.GetoptError as e:
  31.             print("Unkown option. Quiting.")
  32.             exit(1)
  33.     for opt, arg in opts:
  34.         if opt == '-i':
  35.             input_path = arg
  36.         elif opt == '-d':
  37.             do_delete = True
  38.         elif opt == '-h':
  39.             usage(argv[0])
  40.             exit(0)
  41.  
  42. def is_password_protected(file):
  43.     command = ['7z', 't', '-p', file]
  44.     process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
  45.     try:
  46.         process.wait()
  47.     except:
  48.         pass
  49.     return 0 != process.returncode
  50.  
  51. def extract(file):
  52.     if is_password_protected(file):
  53.         #print('Ignoring ', file, '.', sep='')
  54.         return
  55.     global do_delete
  56.     output_dir = str(file.parent) + '/' + str(file.stem)
  57.     try:
  58.         os.mkdir(output_dir)
  59.     except:
  60.         pass
  61.     output_option = '-o' + str(output_dir)
  62.     command = ['7z', 'x', file, output_option]
  63.     try:
  64.         print('Extracting: ', file, '... ', sep='', end='')
  65.         process = subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.PIPE)
  66.         process.communicate(input='Y\n'.encode())
  67.         process.wait()
  68.         if process.returncode == 0:
  69.             print('Success.')
  70.             if do_delete:
  71.                 os.remove(file)
  72.         else:
  73.             print('Fail.')
  74.     except:
  75.         print('\nError while forking 7z. Is it installed correctly?')
  76.  
  77. def scandir(dir_):
  78.     #print('scandir', dir_)
  79.     global MAX_OUTPUT_LEN
  80.     global ARCHIVE_TYPES
  81.     if len(str(dir_)) >= MAX_OUTPUT_LEN:
  82.         return
  83.     for entry in Path(dir_).iterdir():
  84.         if entry.is_dir():
  85.             scandir(entry)
  86.         else:
  87.             for t in ARCHIVE_TYPES:
  88.                 if t == entry.suffix:
  89.                     extract(entry)
  90.  
  91. def main(argv):
  92.     global input_path, do_delete
  93.     opts(argv)
  94.     if input_path == '':
  95.         print("No input directory specified. Quiting.")
  96.         exit(1)
  97.     scandir(input_path)
  98.  
  99. if __name__ == "__main__":
  100.     main(sys.argv)
  101.  
Advertisement
Add Comment
Please, Sign In to add comment