Last active
October 19, 2017 13:40
-
-
Save vstoykov/a9f7c3d57bb19d6587ca26e85caa4b21 to your computer and use it in GitHub Desktop.
Run pdftk in Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import subprocess | |
PDFTK_DOCKER_IMAGE = 'agileek/pdftk' | |
def main(*argv): | |
file_args = {'stamp', 'output'} | |
files = {} | |
last_arg = None | |
pdftk_args = argv[1:] | |
if (pdftk_args): | |
files['input'] = pdftk_args[0] | |
for arg in pdftk_args[1:]: | |
if last_arg: | |
if last_arg in file_args: | |
files[last_arg] = arg | |
last_arg = None | |
else: | |
last_arg = arg | |
volumes = set() | |
for val in files.values(): | |
if val not in {'-', 'PROMPT'}: | |
volumes.add(os.path.dirname(os.path.abspath(os.path.normpath(val)))) | |
docker_args = [ | |
'docker', | |
'run', | |
'-i', | |
'--rm', | |
'--user', '{uid}:{gid}'.format(uid=os.getuid(), gid=os.getgid()), | |
] | |
for volume in sorted(volumes): | |
docker_args.extend(['-v', '{volume}:{volume}:z'.format(volume=volume)]) | |
docker_args.append(PDFTK_DOCKER_IMAGE) | |
docker_args.extend(pdftk_args) | |
return subprocess.call(docker_args) | |
if __name__ == '__main__': | |
exit(main(*sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment