1
0

system.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. for pattern in exclude:
  35. stripped = [line for line in stripped
  36. if not line.startswith(pattern)]
  37. if stripped:
  38. # Tolerate single line warning(s)
  39. for line in stripped:
  40. if (len(line) < 8) or (line[:8].lower() != "warning:"):
  41. raise RuntimeError(err)
  42. else:
  43. for line in stripped:
  44. log('WARNING', line[8:].strip())
  45. return str(decode(out).strip())
  46. _ldd_pattern = re.compile('=> (.+) [(]0x')
  47. def ldd(path):
  48. '''Get dependencies list of dynamic libraries
  49. '''
  50. out = system(('ldd', path))
  51. return _ldd_pattern.findall(out)