system.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. import re
  3. import subprocess
  4. from .compat import decode
  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):
  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. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
  24. stderr=subprocess.PIPE)
  25. out, err = p.communicate()
  26. if err:
  27. err = decode(err)
  28. stripped = [line for line in err.split(os.linesep) if line]
  29. for pattern in exclude:
  30. stripped = [line for line in stripped
  31. if not line.startswith(pattern)]
  32. if stripped:
  33. # Tolerate single line warning(s)
  34. for line in stripped:
  35. if (len(line) < 8) or (line[:8].lower() != "warning:"):
  36. raise RuntimeError(err)
  37. else:
  38. for line in stripped:
  39. log('WARNING', line[8:].strip())
  40. return str(decode(out).strip())
  41. _ldd_pattern = re.compile('=> (.+) [(]0x')
  42. def ldd(path):
  43. '''Get dependencies list of dynamic libraries
  44. '''
  45. out = system(('ldd', path))
  46. return _ldd_pattern.findall(out)