Commit afa1f4ce authored by bseeger's avatar bseeger

Add ability to unpack a zip/tar file. Not much else yet as

structure of file is unspecified.
parent 767a7e85
#!/usr/bin/python
# This is the main script - it does the majority of the work.
from __future__ import absolute_import, division, print_function
from __future__ import absolute_import, print_function
import sys
import requests
import os
import subprocess
print("Hello there, I received a request to do an import. Args: " + str(sys.argv))
print("That's all. Goodbye!");
##############################################################
# Helpers
##############################################################
# opens a zip/tgz file and returns the directory location
def unpack_file(filename):
print("Opening zip file " + filename)
if filename.endswith('tgz') :
retval, dirname = unpack_tar(filename)
elif filename.endswith('.zip') :
retval, dirname = unpack_zip(filename)
elif filename.endswith('.gzip') :
print('Not implemented yet')
retval = -1
else:
print('Unknown filetype passed in')
if retval != 0 :
print('Unable to unpack file \'{0}\''.format(filename))
return ''
else :
return dirname
def unpack_zip(filename) :
# first test and get info from the file
args = ['zipinfo', '-1', filename]
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
dirname, unused_err = process.communicate()
retval = process.poll()
print('retval after zipinfo call is {0}'.format(retval))
# now actually unpack it.
if retval == 0:
ind = dirname.find('/')
dirname = dirname.strip('/\n')
args = ['unzip', filename]
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_, unused_err = process.communicate()
retval = process.poll()
if retval == 0:
print('Successfully extracted files from zip file');
else:
print('Unable to extract files from zip \'{0}\': {1}'.format(filename, retval))
print('dirname from {0} is: {1}'.format(args[0], dirname))
if retval != 0 :
print('\'unzip\' failed for file {0} with error code {1}'.format(filename, retval))
return (retval, dirname)
def unpack_tar(filename):
#check the file first and get dirname
args = ['tar', '-tzf', filename]
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
dirname, unused_err = process.communicate()
retval = process.poll()
print('retval after tar call is {0}'.format(retval))
# now actually unpack it
if retval == 0:
print('dir is {0}'.format(dirname))
dirname = (dirname.split('\n'))[0].strip('/')
args = ['tar', '-xvf', filename]
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_, unused_err = process.communicate()
retval = process.poll()
if retval == 0:
print('Successfully extracted files from tgz file');
else:
print('Unable to extract files from tgz \'{0}\': {1}'.format(filename, retval))
print('dir is {0}'.format(dirname))
return (retval, dirname)
##############################################################
# End helpers
##############################################################
def ingest_data(location) :
print("ingest_data: noop")
if __name__ == '__main__':
if sys.argv[1] == None:
exit -1
theFile = sys.argv[1]
# unpack file into directory
directory = unpack_file(theFile)
print("directory that houses unpackaged files: " + directory)
# start to pick up files ...
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment