Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #!/usr/bin/python
  2. import ast
  3. import os
  4. import pipes
  5. import re
  6. import sys
  7.  
  8. first_arg_only_calls = {'read', 'write'}
  9.  
  10. re_filename = re.compile(r'"((?:[^"]|\\")*)"|\d<(.*?)>[,)]')
  11.  
  12. if len(sys.argv) <= 1 or sys.argv[1:] in (['--help'], ['-h']):
  13. sys.stderr.write('''\
  14. usage: %s COMMAND
  15.  
  16. Shows all paths access by a command. Relative paths are resolved
  17. relative to the working directory of tracepath, not actual command,
  18. and are thus not guaranteed to be accurate if command changes directory.
  19. ''' % os.path.basename(sys.argv[0]))
  20. sys.exit(1)
  21.  
  22. if sys.argv[1:] == ['--internal-parse']:
  23. results = set()
  24. for line in sys.stdin:
  25. pid, line = line.split(None, 1)
  26. call, _, args = line.partition('(')
  27. if call in first_arg_only_calls:
  28. matches = [re_filename.search(args)]
  29. else:
  30. matches = re_filename.finditer(args)
  31. for m in matches:
  32. escaped = '"%s"' % (m.group(1) or m.group(2))
  33. try:
  34. string = ast.literal_eval(escaped)
  35. path = os.path.realpath(string)
  36. except Exception:
  37. results.add('%s: %s?!' % (call, escaped))
  38. else:
  39. results.add(path)
  40. #results.add('%s: %s' % (call, path))
  41. for path in sorted(results):
  42. sys.stderr.write('%s\n' % path)
  43. sys.exit(0)
  44.  
  45. cmd = 'strace -y -e trace=file,desc -e verbose=none -f'
  46. cmd += ' -o %s' % pipes.quote('|%s --internal-parse' % pipes.quote(sys.argv[0]))
  47. cmd += ' ' + ' '.join(map(pipes.quote, sys.argv[1:]))
  48. sys.exit(os.system(cmd))
Add Comment
Please, Sign In to add comment