1
0

system.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import re
  3. import subprocess
  4. from .compat import decode
  5. from .log import debug
  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. raise RuntimeError(err)
  34. return str(decode(out).strip())
  35. _ldd_pattern = re.compile('=> (.+) [(]0x')
  36. def ldd(path):
  37. '''Get dependencies list of dynamic libraries
  38. '''
  39. out = system(('ldd', path))
  40. return _ldd_pattern.findall(out)