superior extract.py created

master
lanxu 2017-03-19 19:56:44 +02:00
parent 44965bf762
commit 65cee8871e
3 changed files with 124 additions and 16 deletions

16
extract
View File

@ -1,16 +0,0 @@
#!/bin/sh
for archive in "$@"; do
# count files in root dir
rootfiles=$(7z l -slt "${archive}" | grep -c 'Path = [^/]*$')
# add -o switch, if more than one file in root
# checks for >2, because path of the zip file itself is listed too
if [ $rootfiles -gt 2 ]; then
filename=${archive##*/}
rootname=${filename%.*}
opt="-o${rootname}"
fi
#unpack
7z x "${opt}" "${archive}"
done

110
extract.py Executable file
View File

@ -0,0 +1,110 @@
#!/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" }
]
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)
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:])

14
sandbox Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# Run without any access
#% sudo groupadd sandbox
#% useradd -g sandbox sandbox
#
# Disable network
# iptables -A OUTPUT -m owner --gid-owner sandbox -j DROP
# Or
# iptables -A OUTPUT -m owner --gid-owner sandbox -d 192.168.1.0/24 -j ACCEPT
# iptables -A OUTPUT -m owner --gid-owner sandbox -d 127.0.0.0/8 -j ACCEPT
# iptables -A OUTPUT -m owner --gid-owner sandbox -j DROP
sg sandbox "$@"