Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import ast
- import os
- import pipes
- import re
- import sys
- first_arg_only_calls = {'read', 'write'}
- re_filename = re.compile(r'"((?:[^"]|\\")*)"|\d<(.*?)>[,)]')
- if len(sys.argv) <= 1 or sys.argv[1:] in (['--help'], ['-h']):
- sys.stderr.write('''\
- usage: %s COMMAND
- Shows all paths access by a command. Relative paths are resolved
- relative to the working directory of tracepath, not actual command,
- and are thus not guaranteed to be accurate if command changes directory.
- ''' % os.path.basename(sys.argv[0]))
- sys.exit(1)
- if sys.argv[1:] == ['--internal-parse']:
- results = set()
- for line in sys.stdin:
- pid, line = line.split(None, 1)
- call, _, args = line.partition('(')
- if call in first_arg_only_calls:
- matches = [re_filename.search(args)]
- else:
- matches = re_filename.finditer(args)
- for m in matches:
- escaped = '"%s"' % (m.group(1) or m.group(2))
- try:
- string = ast.literal_eval(escaped)
- path = os.path.realpath(string)
- except Exception:
- results.add('%s: %s?!' % (call, escaped))
- else:
- results.add(path)
- #results.add('%s: %s' % (call, path))
- for path in sorted(results):
- sys.stderr.write('%s\n' % path)
- sys.exit(0)
- cmd = 'strace -y -e trace=file,desc -e verbose=none -f'
- cmd += ' -o %s' % pipes.quote('|%s --internal-parse' % pipes.quote(sys.argv[0]))
- cmd += ' ' + ' '.join(map(pipes.quote, sys.argv[1:]))
- sys.exit(os.system(cmd))
Add Comment
Please, Sign In to add comment