1
0

system.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import re
  3. import subprocess
  4. from .compat import decode, encode
  5. from .log import debug, log
  6. __all__ = ['ldd', 'system']
  7. try:
  8. basestring
  9. except NameError:
  10. basestring = (str, bytes)
  11. def system(args, exclude=None, stdin=None):
  12. '''System call with capturing output
  13. '''
  14. cmd = ' '.join(args)
  15. debug('SYSTEM', cmd)
  16. if exclude is None:
  17. exclude = []
  18. elif isinstance(exclude, basestring):
  19. exclude = [exclude]
  20. else:
  21. exclude = list(exclude)
  22. exclude.append('fuse: warning:')
  23. if stdin:
  24. in_arg = subprocess.PIPE
  25. stdin = encode(stdin)
  26. else:
  27. in_arg = None
  28. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
  29. stderr=subprocess.PIPE, stdin=in_arg)
  30. out, err = p.communicate(input=stdin)
  31. if err:
  32. err = decode(err)
  33. stripped = [line for line in err.split(os.linesep) if line]
  34. def matches_pattern(line, pattern):
  35. if isinstance(pattern, re.Pattern):
  36. return bool(pattern.match(line))
  37. return line.startswith(pattern)
  38. for pattern in exclude:
  39. stripped = [line for line in stripped
  40. if not matches_pattern(line, pattern)]
  41. if stripped:
  42. # Tolerate single line warning(s)
  43. for line in stripped:
  44. if (len(line) < 8) or (line[:8].lower() != "warning:"):
  45. raise RuntimeError(err)
  46. else:
  47. for line in stripped:
  48. log('WARNING', line[8:].strip())
  49. return str(decode(out).strip())
  50. _ldd_pattern = re.compile('=> (.+) [(]0x')
  51. def ldd(path):
  52. '''Get dependencies list of dynamic libraries
  53. '''
  54. out = system(('ldd', path))
  55. return _ldd_pattern.findall(out)