system.py 951 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. import re
  3. import subprocess
  4. from .log import debug
  5. __all__ = ['ldd', 'system']
  6. def _decode(s):
  7. '''Decode Python 3 bytes as str
  8. '''
  9. try:
  10. return s.decode()
  11. except AttributeError:
  12. return s
  13. def system(*args):
  14. '''System call with capturing output
  15. '''
  16. cmd = ' '.join(args)
  17. debug('SYSTEM', cmd)
  18. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
  19. stderr=subprocess.PIPE)
  20. out, err = p.communicate()
  21. if err:
  22. err = _decode(err)
  23. stripped = [line for line in err.split(os.linesep)
  24. if line and not line.startswith('fuse: warning:')]
  25. if stripped:
  26. raise RuntimeError(err)
  27. return str(_decode(out).strip())
  28. _ldd_pattern = re.compile('=> (.+) [(]0x')
  29. def ldd(path):
  30. '''Get dependencies list of dynamic libraries
  31. '''
  32. out = system('ldd', path)
  33. return _ldd_pattern.findall(out)