113 lines
3.3 KiB
Python
Executable File
113 lines
3.3 KiB
Python
Executable File
#!/bin/python
|
|
import os
|
|
import re
|
|
import sys
|
|
import getopt
|
|
import subprocess
|
|
import re
|
|
import logging
|
|
from subprocess import check_output
|
|
from subprocess import call
|
|
|
|
def is_available(command):
|
|
status, result = subprocess.getstatusoutput(command)
|
|
if status < 127:
|
|
return True
|
|
return False
|
|
|
|
def is_exe(fpath):
|
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
|
|
|
def run_command(command):
|
|
try:
|
|
return_value = call(command)
|
|
except (RuntimeError, TypeError, NameError, OSError) as error:
|
|
print(error)
|
|
raise
|
|
return return_value
|
|
|
|
def has_multiple_files(file):
|
|
file = re.escape(file)
|
|
check_7z_command = "7z l -slt " + file + " | grep -c 'Path = [^/]*$'"
|
|
check_tar_command = "tar --exclude=\"*/*\" -tf " + file + " | wc -l" # returns top level list of files
|
|
|
|
check_command = None
|
|
|
|
m = re.search("\.tar\.", file)
|
|
if m != None:
|
|
check_command = check_tar_command
|
|
else:
|
|
check_command = check_7z_command
|
|
|
|
return_command = check_output(check_command, shell=True)
|
|
if return_command is None:
|
|
print('Something went horribly wrong')
|
|
sys.exit(2)
|
|
|
|
number_of_files = int(return_command.decode("utf-8"))
|
|
|
|
if number_of_files > 1:
|
|
if check_command is check_7z_command and number_of_files > 2:
|
|
return True
|
|
else:
|
|
return False
|
|
return True
|
|
return False
|
|
|
|
def main(argv):
|
|
extractTools = [
|
|
{ "extension": "\.tar\.gz$", "command": "tar", "params": "-xf", "oparams": "-C" },
|
|
{ "extension": "\.tar\.xz$", "command": "tar", "params": "-xf", "oparams": "-C" },
|
|
{ "extension": "\.zip$", "command": "unzip", "params": "", "oparams": "-d" },
|
|
{ "extension": "\.rar$", "command": "unrar", "params": "x", "oparams": "" },
|
|
]
|
|
|
|
if os.path.isfile(argv[0]) == False:
|
|
print("No such file")
|
|
sys.exit(2)
|
|
|
|
print("Processing file " + argv[0])
|
|
|
|
for tool in extractTools:
|
|
m = re.search(tool["extension"], argv[0])
|
|
if m != None:
|
|
if is_available(tool["command"]):
|
|
command = [tool["command"]]
|
|
|
|
if tool["params"] is not "":
|
|
command.append(tool["params"])
|
|
|
|
command.append(argv[0])
|
|
|
|
output_path = "."
|
|
|
|
if has_multiple_files(argv[0]):
|
|
base = os.path.basename(argv[0])
|
|
basefile = os.path.splitext(base)[0]
|
|
|
|
output_path = os.path.dirname(os.path.abspath(argv[0]))
|
|
output_path = os.path.join(output_path, basefile)
|
|
print("Extracting to " + output_path)
|
|
|
|
if not os.path.exists(output_path):
|
|
os.makedirs(output_path)
|
|
|
|
if tool["oparams"] is not "":
|
|
command.append(tool['oparams'])
|
|
command.append(output_path)
|
|
|
|
try:
|
|
|
|
val = run_command(command)
|
|
if val > 0:
|
|
sys.exit(2)
|
|
except:
|
|
print('Something went horribly wrong')
|
|
sys.exit(2)
|
|
else:
|
|
print("Command " + tool["command"] + " not found")
|
|
sys.exit(2)
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|