shlex 960 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
"""
	shlex - execve but apply shlex to argv
"""
import shlex
import subprocess
import logging
import os
import sys
import argparse

LOGGER = logging.getLogger(os.path.basename(__file__))

ARGP = argparse.ArgumentParser()
ARGP.add_argument('remainder', nargs=argparse.REMAINDER)

def main(argp=None, argv=None):
    if argp is None:
        argp = ARGP.parse_args(argv)
    logging.basicConfig(level=logging.INFO)

    LOGGER.info('execve_orig: %r', argp.remainder)
    execve = shlex.split(' '.join(argp.remainder))  #  + ['&', 'exit']  windows hack to get returncode?
    LOGGER.info('execve_shlex: %r', execve)
    execve = [
        (os.path.normpath(i) if os.path.isfile(i) else i)
        for i in execve
    ]
    LOGGER.info('execve_shlex_normpath: %r', execve)
    cproc = subprocess.run(execve, shell=True)
    LOGGER.info('execve_rc: %r', cproc.returncode)
    return cproc.returncode

if __name__ == '__main__':
	exit(main())