Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
acdc
repository-extension-services
Commits
afa1f4ce
Commit
afa1f4ce
authored
Apr 22, 2016
by
bseeger
Browse files
Add ability to unpack a zip/tar file. Not much else yet as
structure of file is unspecified.
parent
767a7e85
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
2 deletions
+100
-2
acrepo-import-service/src/main/resources/fedora-ingest.py
acrepo-import-service/src/main/resources/fedora-ingest.py
+100
-2
No files found.
acrepo-import-service/src/main/resources/fedora-ingest.py
View file @
afa1f4ce
#!/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 ...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment