Update file_setup Pipeline

This commit is contained in:
Patrick Jentsch 2020-04-03 18:35:39 +02:00
parent 933136bd08
commit fa3b3cc556
5 changed files with 88 additions and 102 deletions

View File

@ -18,8 +18,8 @@ RUN apt-get update \
# Install merge_images # Install merge_images
COPY setup_files /usr/local/bin COPY file_setup /usr/local/bin
ENTRYPOINT ["setup_files"] ENTRYPOINT ["file_setup"]
CMD ["--help"] CMD ["--help"]

57
file_setup Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env python3.5
# coding=utf-8
"""
merge_images
Usage: For usage instructions run with option --help
Author: Stephan Porada <sporada@uni-bielefeld.de>
"""
from argparse import ArgumentParser
import os
import subprocess
def parse_arguments():
parser = ArgumentParser(description='Merges images into one .pdf file.')
parser.add_argument('i', metavar='input directory')
parser.add_argument('o', metavar='output directory')
parser.add_argument('f', metavar='output file base')
parser.add_argument('--log-dir')
parser.add_argument('--zip')
return parser.parse_args()
def merge_images(input_dir, output_dir, output_file_base, zip):
try:
os.mkdir(output_dir)
except FileExistsError:
pass
cmd = 'ls -Q -v "{i}"/*.* > "{i}"/file_list.txt'.format(i=input_dir)
subprocess.run(cmd, shell=True)
cmd = 'convert @"{}"/file_list.txt "{}"/"{}".pdf'.format(input_dir, output_dir, output_file_base) # noqa
subprocess.run(cmd, shell=True)
cmd = 'rm "{}"/file_list.txt'.format(input_dir)
subprocess.run(cmd, shell=True)
if zip is not None:
cmd = 'cd "{}"'.format(output_dir)
cmd += ' && '
cmd += 'zip'
cmd += ' -m'
cmd += ' "{}"_-_pdf "{}".pdf'.format(zip, output_file_base)
cmd += ' && '
cmd += 'cd -'
subprocess.run(cmd, shell=True)
def main():
args = parse_arguments()
print(args)
merge_images(args.i, args.o, args.f, args.zip)
if __name__ == '__main__':
main()

View File

@ -1,61 +0,0 @@
#!/usr/bin/env python3.5
# coding=utf-8
"""
merge_images
Usage: For usage instructions run with option --help
Author: Stephan Porada <sporada@uni-bielefeld.de>
"""
import argparse
import os
import subprocess
def parse_arguments():
parser = argparse.ArgumentParser(description='Merges given input images '
'into one multipage tiff.')
parser.add_argument('-i',
dest='input_dir',
required=True,
type=os.path.abspath)
parser.add_argument('-o',
dest='output_dir',
required=True,
type=os.path.abspath)
parser.add_argument('--zip',
default='combines-pictures',
dest='zip',
type=str,
help='''package result files in zip bundles and set a
filename prefix as a string.''',
required=False)
return parser.parse_args()
def merge_images(input_dir, output_dir, zip):
try:
os.mkdir(output_dir)
except FileExistsError:
pass
cmd = 'ls -Q -v {i}/*.* > {i}/file_list.txt'.format(i=input_dir)
subprocess.run(cmd, shell=True)
cmd = 'convert @{i}/file_list.txt {o}/combined.tif'.format(i=input_dir, o=output_dir)
subprocess.run(cmd, shell=True)
cmd = 'rm {i}/file_list.txt'.format(i=input_dir)
subprocess.run(cmd, shell=True)
if zip:
cmd = 'cd {o} && zip -m {f}-combined.zip combined.tif && cd -'.format(o=output_dir, f=zip)
subprocess.run(cmd, shell=True)
def main():
args = parse_arguments()
merge_images(args.input_dir, args.output_dir, args.zip)
if __name__ == '__main__':
main()

29
wrapper/file_setup Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# coding=utf-8
from argparse import ArgumentParser
import os
import subprocess
CONTAINER_IMAGE = 'gitlab.ub.uni-bielefeld.de:4567/sfb1288inf/file_setup:latest'
CONTAINER_INPUT_DIR = '/input'
CONTAINER_OUTPUT_DIR = '/output'
UID = str(os.getuid())
GID = str(os.getgid())
parser = ArgumentParser(add_help=False)
parser.add_argument('-i')
parser.add_argument('-o')
args, remaining_args = parser.parse_known_args()
cmd = ['docker', 'run', '--rm', '-it', '-u', '{}:{}'.format(UID, GID)]
if args.o is not None:
cmd += ['-v', '{}:{}'.format(os.path.abspath(args.o), CONTAINER_OUTPUT_DIR)]
remaining_args.insert(0, CONTAINER_OUTPUT_DIR)
if args.i is not None:
cmd += ['-v', '{}:{}'.format(os.path.abspath(args.i), CONTAINER_INPUT_DIR)]
remaining_args.insert(0, CONTAINER_INPUT_DIR)
cmd.append(CONTAINER_IMAGE)
cmd += remaining_args
subprocess.run(cmd)

View File

@ -1,39 +0,0 @@
#!/usr/bin/env python3
# coding=utf-8
import argparse
import os
import subprocess
container_image = 'gitlab.ub.uni-bielefeld.de:4567/sfb1288inf/setup_files:latest'
container_input_dir = '/input'
container_output_dir = '/output'
uid = str(os.getuid())
gid = str(os.getgid())
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
'-i',
dest='input_dir',
required=False
)
parser.add_argument(
'-o',
dest='output_dir',
required=False
)
args, remaining_args = parser.parse_known_args()
cmd = ['docker', 'run', '--rm', '-it', '-u', uid + ':' + gid]
if args.input_dir is not None:
host_input_dir = os.path.abspath(args.input_dir)
cmd += ['-v', host_input_dir + ':' + container_input_dir]
remaining_args += ['-i', container_input_dir]
if args.output_dir is not None:
host_output_dir = os.path.abspath(args.output_dir)
cmd += ['-v', host_output_dir + ':' + container_output_dir]
remaining_args += ['-o', container_output_dir]
cmd.append(container_image)
cmd += remaining_args
subprocess.run(cmd)